状态21002验证交易收据时 [英] Status 21002 While Verifying Transaction Receipt

查看:104
本文介绍了状态21002验证交易收据时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着查看所有其他问题,似乎没有解决这个问题。我已将其缩小,以便 https://sandbox.itunes.apple.com/verifyReceipt只是 {status:21002} 。任何帮助将不胜感激,我已复制下面的相关代码。

I tried looking through all of the other questions and nothing seemed to fix this issue. I have narrowed it down so the response from https://sandbox.itunes.apple.com/verifyReceipt is simply {"status":21002}. Any help would be greatly appreciated, I've copied the related code below.

NSString *completeString = @"http://www.mysite.com/verify.php";

NSURL *urlForValidation = [NSURL URLWithString:completeString];

NSMutableURLRequest *validationRequest = [[NSMutableURLRequest alloc] initWithURL:urlForValidation];

[validationRequest setHTTPMethod:@"POST"];

NSString *strTest = [NSString stringWithFormat:@"receipt=%@", [self createEncodedString:transaction.transactionReceipt]];

[validationRequest setHTTPBody:[NSData dataFromBase64String:strTest]];

NSData *responseData = [NSURLConnection sendSynchronousRequest:validationRequest returningResponse:nil error:nil];

NSString *response = [[NSString alloc] initWithData: responseData encoding: NSUTF8StringEncoding];

NSLog(@"%@", response);

- (NSString*) createEncodedString:(NSData*)data {
    static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    const int size = ((data.length + 2)/3)*4;
    uint8_t output[size];

    const uint8_t* input = (const uint8_t*)[data bytes];
    for (int i = 0; i < data.length; i += 3)
    {
        int value = 0;
        for (int j = i; j < (i + 3); j++)
        {
            value <<= 8;
            if (j < data.length)
                value |= (0xFF & input[j]);
        }

        const int index = (i / 3) * 4;
        output[index + 0] =  table[(value >> 18) & 0x3F];
        output[index + 1] =  table[(value >> 12) & 0x3F];
        output[index + 2] = (i + 1) < data.length ? table[(value >> 6)  & 0x3F] : '=';
        output[index + 3] = (i + 2) < data.length ? table[(value >> 0)  & 0x3F] : '=';
    }

    return  [[NSString alloc] initWithBytes:output length:size encoding:NSASCIIStringEncoding];
}

最后这是使用的PHP代码:

Finally this is the PHP code used:

$url = 'https://sandbox.itunes.apple.com/verifyReceipt';

$receipt = "{%s}" % $_POST["receipt"];

$purchase_encoded = base64_encode( $receipt );

$encodedData = json_encode( Array( 
    'receipt-data' => $purchase_encoded 
) );

//Open a Connection using POST method, as it is required to use POST method.
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encodedData);
$encodedResponse = curl_exec($ch);
curl_close($ch);


//Decode response data using json_decode method to get an object.

echo $encodedResponse;

$response = json_decode( $encodedResponse );

echo $response;


推荐答案

经过大量的摆弄,我能够算出来出。我认为最大的错误是我如何设置HTTP正文。最终的代码如下,希望这可以帮助其他人!

After a lot of fiddling I was able to figure it out. I believe the biggest error was with how I was setting the HTTP body. Finalized code is below, hopefully this can help someone else out!

Objective-C:

Objective-C:

NSString *completeString = @"http://www.mysite.com/verify.php";

NSURL *urlForValidation = [NSURL URLWithString:completeString];

NSMutableURLRequest *validationRequest = [[NSMutableURLRequest alloc] initWithURL:urlForValidation];

[validationRequest setHTTPMethod:@"POST"];

NSString *strTest = [NSString stringWithFormat:@"receipt=%@", [self base64forData:transaction.transactionReceipt]];

[validationRequest setHTTPBody:[strTest dataUsingEncoding:NSUTF8StringEncoding]];

NSData *responseData = [NSURLConnection sendSynchronousRequest:validationRequest returningResponse:nil error:nil];

NSString *response = [[NSString alloc] initWithData: responseData encoding: NSUTF8StringEncoding];

NSLog(@"%@", response);

PHP:

$url = 'https://sandbox.itunes.apple.com/verifyReceipt';

$encodedData = json_encode( Array( 
    'receipt-data' => $_POST["receipt"] 
) );


//Open a Connection using POST method, as it is required to use POST method.
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encodedData);
$encodedResponse = curl_exec($ch);
curl_close($ch);

$response = json_decode( $encodedResponse );

/*
echo "Status Code:";

echo $response->{'status'};
*/

if ($response->{'status'} != 0) {
    echo "FAIL";
} else {
    echo "SUCCESS";
}

这篇关于状态21002验证交易收据时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆