ios 应用内购买收据验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5927258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In App Purchase Receipt verification within app
提问by SST
I want to verify the transaction receipt within my app,
我想在我的应用程序中验证交易收据,
Here is my code,
这是我的代码,
- (void)recordTransaction:(SKPaymentTransaction *)transaction {
NSData *receiptData = [NSData dataWithData:transaction.transactionReceipt];
NSString *encodedString = [Base64 encode:receiptData];
NSURL *url = [NSURL URLWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"];
ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
[request setPostValue:encodedString forKey:@"receipt-data"];
[request setRequestMethod:@"POST"];
[request setDelegate:self];
[request startAsynchronous];
}
I am getting output:
我得到输出:
{"status":21002, "exception":"java.lang.NullPointerException"}
{“状态”:21002,“异常”:“java.lang.NullPointerException”}
Can someone help me to get proper receipt verification?
有人可以帮助我获得正确的收据验证吗?
采纳答案by SST
After number of tries, I decided to do the receipt verification from server side. Actually this is the recommended way.
尝试次数后,我决定做从服务器端收到验证。其实这是推荐的方式。
Here is my code,
这是我的代码,
-(void)recordTransaction:(SKPaymentTransaction *)transaction {
NSString* receiptString = [[[NSString alloc] initWithData:transaction.transactionReceipt encoding:NSUTF8StringEncoding] autorelease];
// POST this string to your server
// I used ASIFormDataRequest
}
// server side
$url = 'https://sandbox.itunes.apple.com/verifyReceipt';
// encode the receipt data received from application
$purchase_encoded = base64_encode( $purchase_receipt );
//Create JSON
$encodedData = json_encode( Array(
'receipt-data' => $purchase_encoded
) );
// POST data
//Open a Connection using POST method, as it is required to use POST method.
$ch = curl_init();
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.
$response = json_decode( $encodedResponse );
// check response
if ($response->{'status'} != 0)
// Invalid receipt
else
// valid reciept
I found help form,
我找到了帮助表格,
回答by Chase
Just for those who may find it helpful. I noticed that apple has updated the In App Purchasing Guide with some status code that are for the auto renewable subscription purchases but seem to apply here as well.
只为那些可能会发现它有帮助的人。我注意到苹果已经更新了应用内购买指南,其中包含一些用于自动更新订阅购买的状态代码,但似乎也适用于此处。
- 21000 The App Store could not read the JSON object you provided.
- 21002 The data in the receipt-data property was malformed.
- 21003 The receipt could not be authenticated.
- 21004 The shared secret you provided does not match the shared secret on file for your account.
- 21005 The receipt server is not currently available.
- 21006 This receipt is valid but the subscription has expired. When this status code is returned to your server, the receipt data is also decoded and returned as part of the response.
- 21007 This receipt is a sandbox receipt, but it was sent to the production service for verification.
- 21008 This receipt is a production receipt, but it was sent to the sandbox service for verification.
- 21000 App Store 无法读取您提供的 JSON 对象。
- 21002 收据数据属性中的数据格式错误。
- 21003 无法验证收据。
- 21004 您提供的共享机密与您帐户中存档的共享机密不匹配。
- 21005 接收服务器当前不可用。
- 21006 此收据有效,但订阅已过期。当此状态代码返回到您的服务器时,接收数据也会被解码并作为响应的一部分返回。
- 21007 此收据是沙箱收据,但已发送到生产服务进行验证。
- 21008 此收据是生产收据,但已发送到沙盒服务进行验证。
Important:The non-zero status codes here apply only when recovering information about a auto-renewable subscription. Do not use these status codes when testing responses for other kinds of products. (Really?)
重要提示:此处的非零状态代码仅适用于恢复有关自动续订订阅的信息。在测试其他类型产品的响应时不要使用这些状态代码。(真的?)
I hope this helps as a reference. I got nailed with 21007.
我希望这有助于作为参考。我被 21007 搞砸了。
List of status codes on Apple's website: https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html
Apple 网站上的状态码列表:https: //developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html
回答by lxt
...you're not firing your request. So your response is null, because you haven't made the request yet!
......你没有提出你的要求。所以你的响应是空的,因为你还没有提出请求!
Either add a [request startSynchronous]
call (which is generally a bad idea, you should always run your network calls asynchronously), or better yet rewrite your code to support an asynchronous network call, and use [request startAsynchronous]
instead.
添加一个[request startSynchronous]
调用(这通常是一个坏主意,您应该始终异步运行网络调用),或者更好地重写您的代码以支持异步网络调用,并[request startAsynchronous]
改为使用。
I would suggest reviewing the ASI documentation if you need more information: http://allseeing-i.com/ASIHTTPRequest/How-to-use
如果您需要更多信息,我建议您查看 ASI 文档:http: //allseeing-i.com/ASIHTTPRequest/How-to-use