使用 transactionReceipt 生成 JSON 对象

发布于 2024-08-02 18:37:51 字数 1719 浏览 7 评论 0原文

过去几天我一直在尝试测试我的第一个应用内购买 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫罗兰の梦幻 2024-08-09 18:37:51

经过两天的努力,我刚刚解决了这个问题。在插入 json 对象之前,您必须使用 Base64 对收据进行编码。就像那样(Ruby):

dataForVerification = {"receipt-data" => Base64.encode64(receipt)}.to_json

官方文档中没有提到 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):

dataForVerification = {"receipt-data" => Base64.encode64(receipt)}.to_json

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.

恍梦境° 2024-08-09 18:37:51

回复:“21002:java.lang.IllegalArgumentException:propertyListFromString解析了一个对象,但字符串中还有更多文本。:”

我通过在编码之前将收据数据包装在{}中解决了代码中的类似问题。

生成的收据如下所示:

{
    "signature" = "A[...]OSzQ==";
    "purchase-info" = "ew[...]fQ==";
    "pod" = "100";
    "signing-status" = "0";
}

这是我使用的代码:

receipt = "{%s}" % receipt    // This step was not specified - trial and error
encoded = base64.b64encode(receipt)
fullpost = '{ "receipt-data" : "%s" }' % encoded
req = urllib2.Request(url, fullpost)
response = urllib2.urlopen(req)

Apple 的回应:

{"receipt":{"item_id":"371235", "original_transaction_id":"1012307", "bvrs":"1.0", "product_id":"com.foo.cup", "purchase_date":"2010-05-25 21:05:36 Etc/GMT", "quantity":"1", "bid":"com.foo.messenger", "original_purchase_date":"2010-05-25 21:05:36 Etc/GMT", "transaction_id":"11237"}, "status":0}

祝你好运!

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:

{
    "signature" = "A[...]OSzQ==";
    "purchase-info" = "ew[...]fQ==";
    "pod" = "100";
    "signing-status" = "0";
}

Here's the code I use:

receipt = "{%s}" % receipt    // This step was not specified - trial and error
encoded = base64.b64encode(receipt)
fullpost = '{ "receipt-data" : "%s" }' % encoded
req = urllib2.Request(url, fullpost)
response = urllib2.urlopen(req)

Apple's Response:

{"receipt":{"item_id":"371235", "original_transaction_id":"1012307", "bvrs":"1.0", "product_id":"com.foo.cup", "purchase_date":"2010-05-25 21:05:36 Etc/GMT", "quantity":"1", "bid":"com.foo.messenger", "original_purchase_date":"2010-05-25 21:05:36 Etc/GMT", "transaction_id":"11237"}, "status":0}

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文