2009-08-19 108 views
8

我过去几天试图测试我的第一个应用内购买iPhone应用程序。不幸的是,我找不到与iTunes服务器交谈的方式来验证transactionReceipt。使用transactionReceipt生成JSON对象

因为这是我第一次尝试这种技术,我选择使用服务器支持直接从iPhone验证收据。但在尝试发送带有使用JSON API从Google代码创建的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。 status = 21002; }

非常感谢您的指导。

+0

没有在文档中说'transactionReceipt'可以解释为一个UTF-8编码的字符串 – user102008 2013-04-30 22:22:38

回答

20

我刚刚修好了2天的挣扎之后。在插入到json对象之前,您必须使用Base64编码收据。这样的(红宝石):

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

Base64是不是在官方文档提及任何地方(至少对于SDK 3.0),只有一对夫妇的博客。

例如,here这个家伙在将Base64中的收据传递给PHP服务器之前先对收据进行编码,但不会将其解码回PHP,从而向Base64发送Base64编码的字符串。

+0

非常感谢您的回答! – Carlos 2009-09-09 13:56:54

+0

非常感谢您指出这一点! – 2009-11-09 19:19:22

+0

目前的In App Purchase Programming Guide在第1步中提到了base 64编码。http://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/StoreKitGuide/VerifyingStoreReceipts/VerifyingStoreReceipts.html#//apple_ref/doc/ UID/TP40008267-CH104-SW1 – 2011-08-24 12:01:20

2

回复:“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) 

苹果回应:

{"receipt":{"item_id":"371235", "original_transaction_id":"1", "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} 

祝你好运!