4

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

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

为此,我点击了以下链接:official apple developer tutorial

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

以下是我的代码:

-(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];

        }...

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

接下来我该怎么做?

4

1 回答 1

0

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

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

还有一个很棒的 WWDC 视频,来自 2014 年“Preventing Unauthorized Purchases with Receipts”,其中详细介绍了在设备上实施收据验证。

于 2014-07-25T22:54:12.623 回答