收据验证不正确

时间:2015-12-08 09:54:46

标签: ios objective-c receipt-validation

我已经在使用Sandbox环境的消费类型的IAP上进行了测试。我想确保我购买的商品有效或无效。但结果总是返回状态" 21004"。分享秘密我什么都不做。因此,当我成功购买项目时,您可以查看我关注苹果的示例代码:

- (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 ... */
                           }
}];

}

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

1 个答案:

答案 0 :(得分:1)

请尝试使用以下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 ... */
                               }
                           }];

}

您需要更改" SECRECT KEY"使用从iTunes Connect.sample获得的密钥看起来像39fkjc38jd02mg72k9cn29dfkm39fk00

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

  

登录iTunes Connect - >我的应用>选择你的应用>应用内广告   购买>查看或生成共享密钥。