• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

php - 验证交易收据时的状态 21002

[复制链接]
菜鸟教程小白 发表于 2022-12-13 17:09:48 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我尝试查看所有其他问题,但似乎没有任何解决此问题的方法。我已经缩小范围,所以来自 https://sandbox.itunes.apple.com/verifyReceipt 的响应只是 {"status":21002}。任何帮助将不胜感激,我已经复制了下面的相关代码。

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

NSURL *urlForValidation = [NSURL URLWithString:completeString];

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

[validationRequest setHTTPMethod"OST"];

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*) createEncodedStringNSData*)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] initWithBytesutput length:size encoding:NSASCIIStringEncoding];
}

最后这是使用的PHP代码:

$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;



Best Answer-推荐答案


经过一番折腾,我终于弄明白了。我相信最大的错误在于我如何设置 HTTP 正文。最终代码如下,希望这可以帮助其他人!

Objective-C :

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

NSURL *urlForValidation = [NSURL URLWithString:completeString];

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

[validationRequest setHTTPMethod"OST"];

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";
}

关于php - 验证交易收据时的状态 21002,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12964728/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap