2013-05-30 30 views
2

我在應用程序內購買與iTune的實現代碼連接和我的服務器下載文件。如何在應用程序內購買交易發送包含產品ID和identifierForVendor在JSON到我的服務器

我想通過使用「POST」發送的產品ID和identifierForVendor和transaction.bytes在JSON。 這是顯示層次結構的圖像。 enter image description here

我在谷歌搜索,我發現這部分,但它在的NSString,它通過GET方法

- (BOOL)verifyReceipt:(SKPaymentTransaction *)transaction { 
NSString *jsonObjectString = [self encode:(uint8_t *)transaction.transactionReceipt.bytes length:transaction.transactionReceipt.length]; 
NSString *completeString = [NSString stringWithFormat:@"http://localhost:8888/amm/php_testing_json.php?receipt=%@", jsonObjectString]; 
NSURL *urlForValidation = [NSURL URLWithString:completeString]; 
NSMutableURLRequest *validationRequest = [[NSMutableURLRequest alloc] initWithURL:urlForValidation]; 
[validationRequest setHTTPMethod:@"GET"]; 
NSData *responseData = [NSURLConnection sendSynchronousRequest:validationRequest returningResponse:nil error:nil]; 

NSString *responseString = [[NSString alloc] initWithData:responseData encoding: NSUTF8StringEncoding]; 
NSInteger response = [responseString integerValue]; 

return (response == 0); 
} 

- (NSString *)encode:(const uint8_t *)input length:(NSInteger)length { 
static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/="; 

NSMutableData *data = [NSMutableData dataWithLength:((length + 2)/3) * 4]; 
uint8_t *output = (uint8_t *)data.mutableBytes; 

for (NSInteger i = 0; i < length; i += 3) { 
    NSInteger value = 0; 
    for (NSInteger j = i; j < (i + 3); j++) { 
     value <<= 8; 

     if (j < length) { 
      value |= (0xFF & input[j]); 
     } 
    } 

    NSInteger index = (i/3) * 4; 
    output[index + 0] =     table[(value >> 18) & 0x3F]; 
    output[index + 1] =     table[(value >> 12) & 0x3F]; 
    output[index + 2] = (i + 1) < length ? table[(value >> 6) & 0x3F] : '='; 
    output[index + 3] = (i + 2) < length ? table[(value >> 0) & 0x3F] : '='; 
} 

return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 

}

任何幫助表示讚賞發送 預先感謝您

回答

0

首先確保你想爲你的任務提供同步請求。 如果你這樣做,那麼你幾乎就在那裏。但請注意,爲了將POST請求發送到您的服務器,您的服務器必須支持它們。你不應該把正確的參數到URL,所以在下面的一塊,這是考慮到:

- (BOOL)verifyReceipt:(SKPaymentTransaction *)transaction 
{ 
    NSString *jsonObjectString = [self encode:(uint8_t*)transaction.transactionReceipt.bytes length:transaction.transactionReceipt.length]; 
    NSString *completeString = @"http://localhost:8888/amm/php_testing_json"; // just an URL your server developer will give you 
    NSURL *urlForValidation = [NSURL URLWithString:completeString]; 
    NSMutableURLRequest *validationRequest = [NSMutableURLRequest requestWithURL:urlForValidation cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:5.0f]; 
    [validationRequest setHTTPMethod:@"POST"]; 
    [validationRequest setHTTPBody:[jsonObjectString dataUsingEncoding:NSUTF8StringEncoding]]; // the string with receipt we set into the http body, not straight into parameters in url 
    // Now, when the request is ready, send it and wait for a response 
    NSData *responseData = [NSURLConnection sendSynchronousRequest:validationRequest returningResponse:nil error:nil]; 

    NSString *responseString = [NSString stringWithUTF8String:[responseData bytes]]; 
    if (responseString && [responseString length]) 
     return YES; 
    return NO; 
} 

,當然還有,你到底應該正確檢查出響應字符串(取決於你預計在不同情況下,這種反應看),不只是它的存在或不存在:-)希望這有助於

+0

謝謝你的幫助,但我怎樣才能把identifireforvendor號,iphone,操作系統和設備,產品ID的名稱? ?我如何使用此值創建JSON?你可以幫我嗎? –

+0

現在,我正在爲應用內購買編寫服務器和客戶端,我可以告訴你(如果我正確理解你的問題),它是*服務器*的作者,誰可以幫助你。對於蘋果公司的收據驗證它足以只發送回執,其餘全部服務器的作者最有可能的定製需求的用途:服務器(本地)收到驗證所以對於,記錄,交易數據庫等 – makaron

+0

(初步驗證了蘋果的驗證發送回執之前)現在,僅僅爲了測試,你可以用虛擬硬編碼數據填充字段(*收據*除外,它必須是真實的。也許productId(部分服務器也可以在本地驗證收據) - 這是你購買的產品的ID並因此得到您的收據)。因此,用假數據發送請求,看看你得到的迴應你期望 – makaron

相關問題