ios - 收据验证不正确

标签 ios objective-c receipt-validation

我已经在 Sandbox 环境下对 Consumable 类型的 IAP 进行了测试。我想确定我购买的商品是否有效。但结果总是以状态“21004”返回。我对共享 secret 不做任何事情。所以大家可以看看我关注apple购买成功时的示例代码:

- (void)verifyStatus:(SKPaymentTransaction *)transaction {

     NSData *receiptData = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]];
     NSError *receiptError;
     BOOL isPresent = [[[NSBundle mainBundle] appStoreReceiptURL] checkResourceIsReachableAndReturnError:&receiptError];
     if (!isPresent) {
         NSLog(@"Validation failed");
     } 
     NSString *receiptStr = [receiptData base64EncodedStringWithOptions:0];
     NSDictionary *requestContents = @{@"receipt-data":receiptStr};
     NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents
                                                      options:0
                                                        error:nil];

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

     NSURL *storeURL = [NSURL URLWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"];
     NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:storeURL];
     [storeRequest setHTTPMethod:@"POST"];
     [storeRequest setHTTPBody:requestData];

     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(@"Respond : %@",jsonResponse);
                               if (!jsonResponse) { /* ... Handle error ...*/ }
                               /* ... Send a response back to the device ... */
                           }
}];

}

我遇到了什么问题?请分享您的经验。谢谢

最佳答案

请为 SECRECT KEY 尝试以下代码和步骤。

 #define SHARED_SECRET @"SECRECT KEY"

-(void)checkReceipt {
    // verifies receipt with Apple
    NSError *jsonError = nil;

    NSString *receiptBase64 = [receiptData base64EncodedStringWithOptions:0];//receiptData NSData for validate Receipt
    NSLog(@"Receipt Base64: %@",receiptBase64);

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                receiptBase64,@"receipt-data",
                                                                SHARED_SECRET,@"password",
                                                                nil]
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&jsonError
                        ];
    NSLog(@"%@",jsonData);
    NSError * error=nil;
    NSDictionary * parsedData = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
    NSLog(@"%@",parsedData);
    NSLog(@"JSON: %@",[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);
    // URL for sandbox receipt validation; replace "sandbox" with "buy" in production or you will receive
    // error codes 21006 or 21007
    NSURL *requestURL = [NSURL URLWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"];

    NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:requestURL];
    [req setHTTPMethod:@"POST"];
    [req setHTTPBody:jsonData];


    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [NSURLConnection sendAsynchronousRequest:req 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(@"Respond : %@",jsonResponse);
                                   if (!jsonResponse) { /* ... Handle error ...*/ }
                                   /* ... Send a response back to the device ... */
                               }
                           }];

}

您需要使用从 iTunes Connect 获得的 key 更改“SECRECT KEY”。示例看起来像 39fkjc38jd02mg72k9cn29dfkm39fk00

以下是创建/查看Secreate Key的步骤。

Login into iTunes Connect -> My Apps > Select your app > In-App Purchases > View or generate a shared secret.

关于ios - 收据验证不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34152814/

相关文章:

ios - 如何在 iOS 应用程序中使用 PostScript Type 1 或 TrueType 字体文件?

ios - 我如何知道我的 XCode 将部署到哪些 iOS 版本?

ios - 无法从 UIWebView 获取 HTML 字符串

iOS 收据验证作为后端服务

objective-c - 使用未解析的标识符 'PKCS7_type_is_signed'

ios - 如何检测scrollToItem是否会滚动

ios - 将相机拍摄的图像上传到服务器 AFNetworking 3.0 时出错

objective-c - 为什么 self.navigationItem.backBarButtonItem 总是 nil?

iphone - iOS 中的 LinkedIn 集成 : Invitation API not working

ios - iOS 应用内购买取消是否反射(reflect)在 NSBundle.mainBundle().appStoreReceiptURL 上