2009-08-19 34 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} 

祝你好運!