Noun abbreviation

  • S–> our server
  • C – > client
  • Ipa –> Apple End, Apple Pay

Front reserves

  • Iap goods

An item can only generate one order, and a second purchase of the same (incomplete) item will return the last order. In other words, the client can only have one transaction in progress for each item.

  • Iap “orders”

ReceiptData, a string of data that, after iap parsing, is an array of product information.

  • Complete the transaction with IAP

Apple is only responsible for collecting the money, after receiving the money, it will be returned to the client, the product information of the paid goods. The server parses (successfully) and delivers the commodity. The client, in turn, completes the transaction.

  • C Interaction process with IAP
Graph TD A[click to buy]--> B[open IAP payment]-->C[successful payment]-->D[IPA returns receiptData]-->E[C returns receiptData to S]-->F[S delivers goods and tells C]-->G[C tells IAP to complete transaction]
  • ReceiptData parsing

ReceiptData is an array that contains unfinished items after S is parsed. S will deliver goods to the user for the goods in it.

Failure to deliver goods. Iap informs S that receiptData is illegal, or S failed to request IAP.

  • Orders for goods

For S, each time the user clicks buy, a new order is generated (a new orderId)

  • S interaction process with IAP, product delivery process
Graph TD A[Receive orderId and receiptData]-->B[request IAP]-->C[IAP parse receiptData]-->D[return to S ReceiptData parsing result]-->E[S check orderId and receiptData goods]-->F[issue goods]
  • C–S– IAP interaction logic
Graph TD A[S generates orderId to C]-->C B[IAP generates receiptData to C]-->C C[C sends orderId,receiptData to S]-->E[S sends receiptData to IAP]-->F[S receives IAP resolution receiptData]-->G[S sends goods]-->H[C receives notification of delivery]-->I[C completes IAP order]
  • Merchandise compensation logic

When the delivery fails (S fails to request iap),S will request again after some time

Core code parsing

SwiftyStoreKit(Open Source Library)

StoreKit’s official Apple API has been pre-packaged to make the code simpler.

Core functions: query goods, purchase goods, receive order result queue, verify the order (local verification), complete the order

AppleRechargeLocalModel

var orderNo : String = ""// Server order
var productId : String = ""Id / / commodities
var receiptData : String = ""/ / ipa receiptData
var identf: String = ""// Unique identifier generated
Copy the code

MLDAppleReceiptValidator(inherit ReceiptValidator)

//1. Enable the stub AppleRechargeLocalModel locally
//2. Send authentication information to the server
validate(receiptData:completion:)
Copy the code

MLDSwiftyStore(encap SwiftyStoreKit)

// initialize SwiftyStoreKit, which actually opens the screen to receive unfinished orders
/// SwiftyStoreKit.completeTransactions(atomically:completion:)
/// It will only be called once in the life cycle when the screen is on
initSwiftStore()

/// get the information about the in-app purchase
/// -parameter idArray: indicates the id set
/// -parameter success
/// -parameter fail: invalid cause
/// Query in iap with the ID of the item selected by the user. If the information about the item does not exist, the item is invalid/not available
/// retrieveProductsInfo(productIds:completion:)
getStoreList(idArray:success:)

/ / / purchase
/// -parameter productID: indicates the ID of a commodity
/// -parameter success: indicates that the purchase is successful
/// -parameter fail: indicates that the purchase fails, and the failure reason is displayed
/// purchaseProduct(productId:quantity:atomically:applicationUsername: simulatesAskToBuyInSandbox:completion:)
purchase(productID:applicationUsername:success:fail:)

// local validation (SwiftyStoreKit already written) AppleReceiptValidator
/// -parameter service:. Production Apple authentication. Sandbox Local authentication
/// -parameter success: Parameter success
/// -parameter fail: Parameter fails
/ / / internal method call MLDAppleReceiptValidator. Validate (receiptData: completion:)
verifyReceipt(service:success:fail:)

/// Complete the order
/// SwiftyStoreKit.finishTransaction(transaction)
finish(transaction:)
Copy the code

AppleRechargeDataBase(extension MLDSwiftyStore)

var orderNo : String = ""
var productId : String = ""
var receiptData : String = ""
var identf: String = ""// Unique identifier generated
Copy the code

The flow chart of the total

Graph TD A[click to buy] -->B[check Apple Stone goods] B-->C[create Server order] -->D[save order goods and server order __ goods are used for IAP] D-->E{initiate payment request to Apple} E--> A[success] E - > b [failure] -- > P > F [cancel] -- - > G/delete local orders a -- > H pay apple} {users H - > I pay [success] -- > J {apple return ReciptData} - > L (local preservation ReciptData) -->K[sends ReciptData to server] -->M[polls order status] -->N[sends order successfully] -->G O[locally checks if there is an unsent order] -->P[checks order status with server] --> T[not sent] -->U[Show restore Tips] U - - > Q [ReciptData] -- -- > K U [no ReciptData] -- > > R E P - > S [goods already issued] -- > G

Pay attention to

SwiftyStoreKit.completeTransactions(atomically: completion:)
Copy the code

Accept the order result queue to view the outstanding orders in this appleID

The order result queue will only be returned once when the app is open and cannot be called actively. Only when the payment is made again will the result be returned again.

When users do not bind alipay, Apple will directly return failure.

App –> Not bind Alipay –> Guide bind Alipay –> Bind successfully –> deduct money

↓ Apple directly returns the payment failureCopy the code