# wx.miniapp.IAP

This capability is encapsulated using some of Apple Pay's native interfaces wx.miniapp Interface, the Apple interface parameters, the return value through the transmission. So that developers can use JS code to achieve Apple Pay capabilities.Developers need to understand the process and rules of Apple Pay, prepare yourself for the development and debugging environment.

# interface

  1. requestSKProducts Get Product Information
  2. cancelRequestSKProducts Cancel request for product information
  3. addPaymentByProductIdentifiers Initiate a product purchase
  4. addTransactionObserver Listening Transaction Event Callback
  5. removeTransactionObserver Remove Listening
  6. getTransactions Get a list of current transactions
  7. finishTransaction Closing a transaction
  8. restoreCompletedTransactions Resume transactions that have been closed
  9. canMakePayments Is it possible to initiate a payment
  10. getAppStoreReceiptURL Get the transaction receipt at the local URL
  11. getAppStoreReceiptData Get the transaction receipt in the local data to base64Encoding back
  12. requestSKReceiptRefreshRequest Initiate a request to refresh the receipt
  13. getStorefront Get App Information for Store

# requestSKProducts/cancelRequestSKProducts

Get Product Information and Cancel Get product information. adopt SKProductsRequest Achieved. At the bottom, a request is made and the request is cached, returning an object with a requestId. use cancelRequestSKProducts This object as a parameter, it uses the Request, proceed [request cancel]Cancel the request (after testing, cancellation may still callback success)。 It will also help developers set up Delegate And listen. Didreceiveresponse, callback the product information to success Callback, including the invalidProductIdentifiers as well as products。

Apple Documentation:SKProductsRequest

# parameter

attribute type Default value Required Introductions
productIdentifiers string[] - yes Need to obtain goods productIdentifiers
success function no Successful callback after getting
fail function no Get a failed callback

# case

const requestObj = wx.miniapp.IAP.requestSKProducts({
  productIdentifiers: [
    'testProductIdentifier',
    'testProductIdentifier',
    'testProductIdentifier',
    'testProductIdentifier',
  ],
  success(ret) {
    console.log(ret.invalidProductIdentifiers )
    console.log(ret.products)
  },
  fail(error) {
    console.error(`requestSKProducts failed. ${error}`)
  }
})

wx.miniapp.IAP.cancelRequestSKProducts(requestObj)

# addPaymentByProductIdentifiers

Initiate the purchase of goods. Create a new SKMutablePayment, and then through the[[SKPaymentQueue defaultQueue] addPayment]To initiate a payment. The developer can customize some payment information through parameters. such as productIdentifier product id,quantity Quantity, etc. Developers need to use addTransactionObserver To listen in on relevant incidents.

Note: Before using this interface, be sure to use the QuestSKProducts, this operation will transfer Apple's SKProduct OC Object cache. At the time of payment will be based on the productIdentifier To get SKProduct, in order to call the [SKMutablePayment paymentWithProduct], or it will fail.

Discounts can also be set up using the [[SKPaymentDiscount alloc] initWithIdentifier ] Create a discount object assignment to SKMutablePayment.paymentDiscount in. Parameters can be added discount object {identifier: String, keyIdentifier: String, nonceString: String, signature: String,timestamp: String}, parameters are required. among nonceString will [[NSUUID alloc] initWithUUIDString:nonceString]Turn to nonce。

Note: This interface simply initiates addPayment, but does not listen for transaction events. When the success callback does not mean that the transaction is completed, it is only successfully initiated.

Apple Documentation:addPaymentSKMutablePaymentSKPaymentDiscount

# parameter

attribute type Default value Required Introductions
productIdentifier string - yes "A string that identifies a product that can be purchased from within your app."
applicationUsername string - no "A string that associates the transaction with a user account on your service "
quantity number 1 no "The number of items the user wants to purchase."
simulatesAskToBuyInSandbox boolean - no "A Boolean value that produces an “ask to buy” flow for this payment in the sandbox."
discount object - no "The details of the discount offer to apply to the payment."
success function - no Successful launch, but does not mean that the transaction is completed
fail function - no Failed callback

# case

wx.miniapp.IAP.addPaymentByProductIdentifiers({
  productIdentifier:  'testidentifier', 
  applicationUsername: 'testidentifierUserName',
  quantity: 1,
  simulatesAskToBuyInSandbox: false,
  discount: {
    identifier: 'xxx', 
    keyIdentifier:  'xxx', 
    nonce: 'xxx', 
    signature: 'xxx', 
    timestamp: 'xxx', 
  },
  success: (args) => {
    // The addPayment call was successful, but does not mean the transaction is complete.
    console.log(`addPaymentByProductIdentifiers success`, args)
  },
  fail: (args) => {
    // The addPayment call succeeded
    console.error(`addPaymentByProductIdentifiers fail`, args)
  }
})

# addTransactionObserver/removeTransactionObserver

Listen for changes in the transaction queue. The underlying use[[SKPaymentQueue defaultQueue] addTransactionObserver:self]Achieved. Notify the callback to the JS side. Monitor transactions from this location. Using the removeTransactionObserver on the js side removes this listening.

Apple Documentation:addTransactionObserver

# parameter

attribute type Default value Required Introductions
updatedTransactions function - no PaymentQueue for Apple Pay: updatedTransactions:
restoreCompletedTransactionsFailedWithError function - no Corresponding to Apple Pay paymentQueue:restoreCompletedTransactionsFailedWithError:
paymentQueueRestoreCompletedTransactionsFinished function - no PaymentQueueRestoreCompletedTransactionsFinished for Apple Pay:
shouldAddStorePayment function - no PaymentQueue: shouldAddStorePayment: forProduct:
paymentQueueDidChangeStorefront function - no Method for Apple Pay paymentQueueDidChangeStorefront:
didRevokeEntitlementsForProductIdentifiers function - no Corresponding to Apple Pay paymentQueue:didRevokeEntitlementsForProductIdentifiers:

# case

const ob = {
  updatedTransactions: (args) => {
    console.log(`updatedTransactions:`, args)
  },
  restoreCompletedTransactionsFailedWithError: (args) => {
    console.log(`restoreCompletedTransactionsFailedWithError:`, args)
  },
  paymentQueueRestoreCompletedTransactionsFinished: (args) => {
    console.log(`paymentQueueRestoreCompletedTransactionsFinished:`, args)
  },
  shouldAddStorePayment: (args) => {
    console.log(`shouldAddStorePayment:`, args)
  },
  paymentQueueDidChangeStorefront: (args) => {
    console.log(`paymentQueueDidChangeStorefront:`, args)
  },
  didRevokeEntitlementsForProductIdentifiers: (args) => {
    console.log(`didRevokeEntitlementsForProductIdentifiers:`, args)
  },
}

wx.miniapp.IAP .addTransactionObserver (ob)

wx.miniapp.IAP.removeTransactionObserver(ob)

# getTransactions

Get the current list of transactions. adopt[SKPaymentQueue defaultQueue]. Transactions are realized.

Apple Documentation:transactions

# parameter

attribute type Default value Required Introductions
success function no Get success
fail function no Failed callback

# case

wx.miniapp.IAP.getTransactions({
  success(ts) {
    console.log(ts)
  },
  fail(error) {
    console.log(error)
  }
})

# finishTransaction

Close the deal. use[[SKPaymentQueue defaultQueue] finishTransaction:paymentTransaction]Achieved. There must be a cache of transactions. For example, when updatedTransactions, the Native layer caches the transaction.

Note: transactionIdentifier is only available in the transaction state SKPaymentTransactionStatePurchased or SKPaymentTransactionStateRestored Only when it's time. So you can use a temporary id created by multiple ends Transaction.temTransactionIdentifier is used to finishTransaction.

Apple Documentation:finishTransaction

# parameter

attribute type Default value Required Introductions
transactionIdentifier string - yes Transaction id
success function no Successful launch, but does not mean that the transaction is completed
fail function no Failed callback

# case

wx.miniapp.IAP.finishTransaction({
  transactionIdentifier:  'xxx', 
  success(args) {
    console.log('wx.miniapp.IAP.finishTransaction', args)
  },
  fail(args) {
    console.error('failed wx.miniapp.IAP.finishTransaction', args)
  }
})

# restoreCompletedTransactions

Let paymentQueue resume the transaction queue that ended earlier.[[SKPaymentQueue defaultQueue] restoreCompletedTransactions]Achieved.

Apple Documentation:restoreCompletedTransactions

# parameter

attribute type Default value Required Introductions
success function no Initiates successfully, but does not mean restore is complete
fail function no Failed callback

Note: This interface simply initiates restoreCompletedTransactions, and success does not mean that the restore succeeds.

# case

wx.miniapp.IAP.restoreCompletedTransactions({
  success(ret) {
    console.log('restoreCompletedTransactions', ret)
  },
  fail(error) {
    console.log('failed restoreCompletedTransactions', error)
  }
})

# canMakePayments

Whether it can be paid. use[SKPaymentQueue canMakePayments]Achieved.

Apple Documentation:canMakePayments

# case

const CanMake = wx.miniapp.IAP.canMakePayments()

# getAppStoreReceiptURL

Get a receipt Local Receipt URL。通过getAppStoreReceiptURL实现。 But this address is not available through fileSystem to the contents of the file, needs to be obtained using getAppStoreReceiptData.

Apple Documentation:getAppStoreReceiptURL

# parameter

attribute type Default value Required Introductions
success function no Get success
fail function no Failed callback

# case

wx.miniapp.IAP .getAppStoreReceiptURL ({
  success(args) {
    console.log('getAppStoreReceiptURL', args.url)
  },
  fail(error) {
    console.error('getAppStoreReceiptURL fail', error)
  }
})

# getAppStoreReceiptData

Get the NSData of the receipt local Receipt, transfer base64Encode back.

# parameter

attribute type Default value Required Introductions
success function no Get success
fail function no Failed callback

# case

wx.miniapp.IAP .getAppStoreReceiptData ({
  success(args) {
    console.log('getAppStoreReceiptData', args.receipt )
  },
  fail(error) {
    console.error('getAppStoreReceiptData fail', error)
  }
})

# requestSKReceiptRefreshRequest

Local receipts may not be available and can be refreshed using this interface.[[SKReceiptRefreshRequest alloc] initWithReceiptProperties:nil ]Achieved. And use delegate to listen for requestDidFinish to see if the request succeeds. Listening whether didFailWithError failed.

Apple Documentation:SKReceiptRefreshRequest

# parameter

attribute type Default value Required Introductions
success function no Get success
fail function no Failed callback

# case

wx.miniapp.IAP.requestSKReceiptRefreshRequest({
  success(args) {
    console.log('requestSKReceiptRefreshRequest', args)
  },
  fail(error) {
    console.error('requestSKReceiptRefreshRequest fail', error)
  }
})

# getStorefront

Get App The state of the store. use[SKPaymentQueue defaultQueue]. Storefront implementation.

Apple Documentation:Storefront

# parameter

attribute type Default value Required Introductions
success function no Get success
fail function no Failed callback

# case

wx.miniapp.IAP.getStorefront ({
  success(args) {
    console.log('getStorefront',  args)
  },
  fail(error) {
    console.error(GetStorefront  fail', error)
  }
})