IOS developers use RMStore to simplify in-house purchase code + in-house purchase order verification, iosrmstore
Now many apps have added in-app purchases. There are many tutorials on how to generate and set Apple certificates on the Internet. I will not repeat them here, I recommend a few URLs that I think are more detailed:
Http://www.jianshu.com/p/86ac7d3b593a
Http://blog.csdn.net/shenjie12345678/article/details/40978977
In the project, I used a third party of RMStore to implement the internal purchase code. Because this framework encapsulates the internal purchase code, the code after relying on the framework is very simple:
Request Product from appStore:
NSString * productIds = @ [@ "ID_1", @ "ID_2", @ "ID_3"], [[RMStore defaultStore] requestProducts: [NSSet setWithArray: productIds] success: ^ (NSArray * products, NSArray * invalidProductIdentifiers) {if ([products count] = 0) {NSLog (@ "---------------- no product ----------------"); return;} failure: ^ (NSError * error) {NSLog (@ "-------------- failed to request the product ----------------");}];
Purchased items:
NSString * appleProductId = productId; [[RMStore defaultStore] addPayment: appleProductId success: ^ (SKPaymentTransaction * transaction) {NSLog (@ "------ transaction successful ------");} failure: ^ (SKPaymentTransaction * transaction, NSError * error) {NSLog (@ "------ transaction Failed ------"); NSLog (@ "error: % @", error);}];
The original tedious code can be done with the above two methods. Detailed information during the purchase process can be obtained from parameters such as transaction and error in the block.
After the purchase is successful, order verification is required to prevent unlimited currency scalpers from being cracked. For more information, see the api receipvalidation Programming Guide.
In order to ensure the accuracy and stability of the verification, it is generally submitted to the backend for verification. On the iOS end, we need to obtain the order data and then send the data to the backend:
NSURL * receiptURL = [[NSBundle mainBundle] Response]; NSData * receiptData = [NSData dataWithContentsOfURL: receiptURL]; NSString * receiptStr = [receiptData response: Response]; // send receiptStr to the server;
Originally in the SKPaymentTransaction class, there was the receipparameter, directly transaction. you can obtain the verification data, but the verification data has expired after iOS7. Therefore, you can select the method to obtain the receipt based on the system version that the app adapts.
After receiving this string, the background partner sends it to the Apple Server through the following url. Note that the verification address used for testing and publishing is different.
Sandbox test: https://sandbox.itunes.apple.com/verifyReceipt
After the app is published:https://buy.itunes.apple.com/verifyReceipt
After verification, the Apple Server Returns the verification information about 3 seconds. The server's partner confirms whether the order is valid Based on the returned information. The parameters returned by the specific app can be found in the api.