Module: ProMotion::IAP

Defined in:
lib/ProMotion/iap.rb

Defined Under Namespace

Classes: Product

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#completion_handlersObject

Returns the value of attribute completion_handlers.



3
4
5
# File 'lib/ProMotion/iap.rb', line 3

def completion_handlers
  @completion_handlers
end

Instance Method Details

#paymentQueue(_, restoreCompletedTransactionsFailedWithError: error) ⇒ Object

SKPaymentTransactionObserver methods



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/ProMotion/iap.rb', line 154

def paymentQueue(_, updatedTransactions:transactions)
  transactions.each do |transaction|
    case transaction.transactionState
    when SKPaymentTransactionStatePurchasing  then iap_callback(:in_progress, transaction)
    when SKPaymentTransactionStateDeferred    then iap_callback(:deferred,    transaction)
    when SKPaymentTransactionStatePurchased   then iap_callback(:purchased,   transaction, true)
    when SKPaymentTransactionStateRestored    then iap_callback(:restored,    transaction, true)
    when SKPaymentTransactionStateFailed
      if transaction.error.code == SKErrorPaymentCancelled
        iap_callback(:canceled, transaction, true)
      else
        iap_callback(:error, transaction, true)
      end
    end
  end
  iap_shutdown if transaction_complete?(transactions)
end

#productsRequest(_, didReceiveResponse: response) ⇒ Object

SKProductsRequestDelegate methods



135
136
137
138
139
140
141
142
143
# File 'lib/ProMotion/iap.rb', line 135

def productsRequest(_, didReceiveResponse:response)
  unless response.invalidProductIdentifiers.empty?
    red = "\e[0;31m"
    color_off = "\e[0m"
  end
  retrieved_iaps_handler(response.products, &self.completion_handlers["retrieve_iaps"]) if self.completion_handlers["retrieve_iaps"]
  @products_request = nil
  self.completion_handlers["retrieve_iaps"] = nil
end

#purchase_iaps(product_ids, options = {}, &callback) ⇒ Object Also known as: purchase_iap



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/ProMotion/iap.rb', line 5

def purchase_iaps(product_ids, options={}, &callback)
  iap_setup
  retrieve_iaps product_ids do |products|
    products.each do |product|
      self.completion_handlers["purchase-#{product[:product_id]}"] = callback

      payment = SKMutablePayment.paymentWithProduct(product[:product])
      payment.applicationUsername = options[:username] if options[:username]

      SKPaymentQueue.defaultQueue.addPayment(payment)
    end
  end
end

#request(_, didFailWithError: error) ⇒ Object



145
146
147
148
149
150
# File 'lib/ProMotion/iap.rb', line 145

def request(_, didFailWithError:error)
  self.completion_handlers["retrieve_iaps"].call([], error) if self.completion_handlers["retrieve_iaps"].arity == 2
  self.completion_handlers["retrieve_iaps"].call([]) if self.completion_handlers["retrieve_iaps"].arity < 2
  @products_request = nil
  self.completion_handlers["retrieve_iaps"] = nil
end

#restore_iaps(product_ids, options = {}, &callback) ⇒ Object Also known as: restore_iap



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ProMotion/iap.rb', line 20

def restore_iaps(product_ids, options={}, &callback)
  iap_setup
  retrieve_iaps Array(product_ids) do |products|
    products.each do |product|
      self.completion_handlers["restore-#{product[:product_id]}"] = callback
    end
    self.completion_handlers["restore-all"] = callback # In case of error

    if options[:username]
      SKPaymentQueue.defaultQueue.restoreCompletedTransactionsWithApplicationUsername(options[:username])
    else
      SKPaymentQueue.defaultQueue.restoreCompletedTransactions
    end
  end
end

#retrieve_iaps(*product_ids, &callback) ⇒ Object Also known as: retrieve_iap



37
38
39
40
41
42
43
# File 'lib/ProMotion/iap.rb', line 37

def retrieve_iaps(*product_ids, &callback)
  iap_setup
  self.completion_handlers["retrieve_iaps"] = callback
  @products_request = SKProductsRequest.alloc.initWithProductIdentifiers(NSSet.setWithArray(product_ids.flatten))
  @products_request.delegate = self
  @products_request.start
end