使用 transactionReceipt 生成 JSON 对象
过去几天我一直在尝试测试我的第一个应用内购买 iPhone 应用程序。不幸的是我找不到与 iTunes 服务器对话来验证交易收据的方法。
因为这是我第一次尝试使用这项技术,所以我选择直接从 iPhone 验证收据,而不是使用服务器支持。但是在尝试使用使用 google 代码中的 JSON api 创建的 JSON onbject 发送 POST 请求后,itunes 总是返回一个奇怪的响应(而不是我等待的“status = 0”字符串)。
这是我用来验证收据的代码:
- (void)recordTransaction:(SKPaymentTransaction *)transaction {
NSString *receiptStr = [[NSString alloc] initWithData:transaction.transactionReceipt encoding:NSUTF8StringEncoding];
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"algo mas",@"receipt-data",nil];
NSString *jsonString = [jsonDictionary JSONRepresentation];
NSLog(@"string to send: %@",jsonString);
NSLog(@"JSON Created");
urlData = [[NSMutableData data] retain];
//NSURL *sandboxStoreURL = [[NSURL alloc] initWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"will create connection");
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
也许我忘记了请求标头中的某些内容,但我认为问题出在我用来创建 JSON 对象的方法中。
以下是将 JSON 对象添加到 HTTPBody 之前的样子:
string to send: {"receipt-data":"{\n\t\"signature\" = \"AUYMbhY
...........
D0gIjEuMCI7Cn0=\";\n\t\"pod\" = \"100\";\n\t\"signing-status\" = \"0\";\n}"}
我得到的响应:
完整回复{ 异常=“java.lang.IllegalArgumentException:尝试读取不带引号的字符串时属性列表解析失败。未找到允许的字符。位于行号:1,列:0。”; 状态=21002; }
感谢您的指导。
I've been the past days trying to test my first in-app purchse iphone application. Unfortunately I can't find the way to talk to iTunes server to verify the transactionReceipt.
Because it's my first try with this technology I chose to verify the receipt directly from the iPhone instead using server support. But after trying to send the POST request with a JSON onbject created using the JSON api from google code, itunes always returns a strange response (instead the "status = 0" string I wait for).
Here's the code that I use to verify the receipt:
- (void)recordTransaction:(SKPaymentTransaction *)transaction {
NSString *receiptStr = [[NSString alloc] initWithData:transaction.transactionReceipt encoding:NSUTF8StringEncoding];
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"algo mas",@"receipt-data",nil];
NSString *jsonString = [jsonDictionary JSONRepresentation];
NSLog(@"string to send: %@",jsonString);
NSLog(@"JSON Created");
urlData = [[NSMutableData data] retain];
//NSURL *sandboxStoreURL = [[NSURL alloc] initWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"will create connection");
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
maybe I'm forgetting something in the request's headers but I think that the problem is in the method I use to create the JSON object.
HEre's how the JSON object looks like before I add it to the HTTPBody :
string to send: {"receipt-data":"{\n\t\"signature\" = \"AUYMbhY
...........
D0gIjEuMCI7Cn0=\";\n\t\"pod\" = \"100\";\n\t\"signing-status\" = \"0\";\n}"}
The responses I've got:
complete response {
exception = "java.lang.IllegalArgumentException: Property list parsing failed while attempting to read unquoted string. No allowable characters were found. At line number: 1, column: 0.";
status = 21002;
}
Thanks a lot for your guidance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过两天的努力,我刚刚解决了这个问题。在插入 json 对象之前,您必须使用 Base64 对收据进行编码。就像那样(Ruby):
官方文档中没有提到 Base64(至少对于 SDK 3.0),只在几个博客中提到过。
例如,此处该人在将收据传递给PHP 服务器,但不会在 PHP 中对其进行解码,从而将 Base64 编码的字符串发送到 iTunes。
I have just fixed that after 2 days of struggling. You have to encode receipt using Base64 before inserting into json object. Like that (Ruby):
Base64 is not mentioned anywhere in the official docs (at least for SDK 3.0), only on a couple of blogs.
For instance, here the guy encodes the receipt in Base64 before passing it to the PHP server, but does not decode it back in PHP, thus sending Base64-encoded string to iTunes.
回复:“21002:java.lang.IllegalArgumentException:propertyListFromString解析了一个对象,但字符串中还有更多文本。:”
我通过在编码之前将收据数据包装在{}中解决了代码中的类似问题。
生成的收据如下所示:
这是我使用的代码:
Apple 的回应:
祝你好运!
Re: "21002: java.lang.IllegalArgumentException: propertyListFromString parsed an object, but there's still more text in the string.:"
I fixed a similar issue in my code by wrapping the receipt data in {} before encoding.
The resulting receipt looks like:
Here's the code I use:
Apple's Response:
Good luck!