iOS 7:应用内购买收据验证和验证

问题描述 投票:0回答:1

我是 iOS 收据验证新手。我已经成功实现了应用内购买。现在我希望在此应用内购买中包含收据验证部分。

我的应用内购买了 3 种产品。我希望在购买应用程序中的每个产品之前,应执行收据验证。

为此,我点击了以下链接:官方苹果开发者教程

我成功拿到了收据数据,但是我很困惑拿到收据数据后该怎么办。如何将其发送回苹果服务器进行验证,然后开始应用内购买流程?

以下是我的代码:

-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {

NSLog(@"transactions count : %d",transactions.count);

BOOL flgIsProductRestorable=NO;

for (SKPaymentTransaction *transaction in transactions) {
    switch (transaction.transactionState) {
        case SKPaymentTransactionStatePurchasing:
        {
            // show wait view here
            //statusLabel.text = @"Processing...";

            NSLog(@"Processing...");
        }
            break;

        case SKPaymentTransactionStatePurchased:
        {
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            [SVProgressHUD dismiss];
            // remove wait view and unlock feature 2
            //statusLabel.text = @"Done!";
            NSLog(@"Success : %@ = %@",transaction.payment.productIdentifier,transaction.transactionIdentifier);
            NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
            NSData *receipt = [NSData dataWithContentsOfURL:receiptURL];
            NSLog(@"%@",receipt);

            NSError *error;
            NSDictionary *requestContents = @{
                                              @"receipt-data": [receipt base64EncodedStringWithOptions:0]
                                              };
            NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents
                                                                  options:0
                                                                    error:&error];

            if (!requestData) { /* ... Handle error ... */ }

            // Create a POST request with the receipt data.
            NSURL *storeURL = [NSURL URLWithString:@"https://sand.itunes.apple.com/verifyReceipt"];
            NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:storeURL];
            [storeRequest setHTTPMethod:@"POST"];
            [storeRequest setHTTPBody:requestData];

            // Make a connection to the iTunes Store on a background queue.
            NSOperationQueue *queue = [[NSOperationQueue alloc] init];
            [NSURLConnection sendAsynchronousRequest:storeRequest queue:queue
                                   completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                                       if (connectionError) {
                                           /* ... Handle error ... */
                                       } else {
                                           NSError *error;
                                           NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
                                           NSLog(@"response : %@",jsonResponse);
                                           if (!jsonResponse) { /* ... Handle error ...*/ }
                                           /* ... Send a response back to the device ... */
                                       }
                                   }];


            [self WS_EbookDetail:transaction.transactionIdentifier];

        }...

我刚刚从我在这个问题中给出的苹果链接复制粘贴了收据检索代码。

接下来我该怎么办?

ios objective-c ios7 in-app-purchase receipt-validation
1个回答
0
投票

请记住,Apple 建议您将收据数据安全地发送到您的服务器,然后调用他们的服务器。调用 Apple 服务器并不安全。

您还可以在应用程序中进行本地收据验证。有关如何执行此操作的信息,请参阅本地验证收据。

还有一个来自 2014 年的精彩 WWDC 视频“通过收据防止未经授权的购买”,其中详细介绍了如何在设备收据验证上实施。

© www.soinside.com 2019 - 2024. All rights reserved.