PHP服务器上的iOS 7收据验证问题

时间:2014-03-21 19:50:21

标签: php ios7 in-app-purchase

2天的不间断搜索,我在IAP的一个重要问题上空白。我们需要服务器验证收据,响应始终是21002。

在我发送的客户端上

NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
NSData *receipt = [NSData dataWithContentsOfURL:receiptURL];
NSDictionary* post = @{@"receipt":[receipt base64EncodedStringWithOptions:0]};
[ServerRequest requestWithUrl:url Callback:nil SendToIM:NO PostData:post];//does standard _POST to our php server

这是一个沙箱收据,作为base64字符串,然后才能到达服务器

MIIU2gYJKoZIhvcNAQcCoIIUyzCCFMcCAQExCzAJBgUrDgMCGgUAMIIEiwYJKoZIhvcNAQcBoIIEfASCBHgxggR0MAoCAQgCAQEEAhYAMAoCARICAQEEAhYAMAoCARMCAQEEAgwAMAoCARQCAQEEAgwAMAsCAQECAQEEAwIBADALAgELAgEBBAMCAQAwCwIBDgIBAQQDAgFTMAsCAQ8CAQEEAwIBADALAgEQAgEBBAMCAQAwCwIBGQIBAQQDAgEDMAwCAQoCAQEEBBYCNCswDQIBAwIBAQQFDAMwLjYwDQIBDQIBAQQFAgMBEdQwDgIBCQIBAQQGAgRQMjI4MBgCAQQCAQIEEM7oReI9bPs0X/hqFDngWuUwGwIBAAIBAQQTDBFQcm9kdWN0aW9uU2FuZGJveDAcAgEFAgEBBBQ8LF7D7deEfXmQd9NITmWuDFXXkDAeAgEMAgEBBBYWFDIwMTQtMDMtMjFUMTk6NDU6MDhaMCwCAQICAQEEJAwiY29tLmNsaXB3aXJlZ2FtZXMuQ3JpdHRlci1Db25xdWVzdDA4AgEHAgEBBDBrGHw8H08L2bC6tSu08pjZKzOeXY8iNjZxZFrJSPr1QnpwMtHhPd44Lky0BciZY0wwOwIBBgIBAQQz4eLimps057PHKFJAyTiNF4fTO5uWOQQkM1dk2ivH6xnWf3/e8qD+9GzS2HH2qzIzZvonMIIBTAIBEQIBAQSCAUIxggE+MAsCAgasAgEBBAIWADALAgIGrQIB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//标准cURL发布并从Apple Sandbox Server获取

我已经阅读了关于此的所有示例和论坛帖子,但似乎无法使其正常运行。

我最好的猜测是我的收据编码错误,但我已尝试过每种格式选项,而且我总是得到21002(在我的服务器上)。

我有什么东西在这里失踪吗?我基本上试图: 1.将NSData收据作为字符串发送到服务器 2.让我的服务器ping Apple服务器 3.处理有效或无效的收据问题

看来我的第1步是错的,但我不确定。我使用base64encode发送收据数据是错误的吗?

我的服务器上是否需要保留格式?

非常感谢任何和所有建议。

更新

以下是我用来将其发送到服务器的代码(一旦它到达服务器,它当前正在剪切哈希码中的+)

for (NSString* k in _post)       
{            
    postDataStr = [NSString stringWithFormat:@"%@&%@=%@", postDataStr,k,_post[k]];      
}

_req = [NSMutableURLRequest requestWithURL:_url cachePolicy:nil timeoutInterval:15.0f];    
[_req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];    
[_req setHTTPMethod:@"POST"];    
[_req setHTTPBody:[postDataStr dataUsingEncoding:NSUTF8StringEncoding]];

解答:

当发送到服务器时,+被从我的字符串中删除。我没有做正确的URL编码。我用%2B取代了+它现在可以使用了

3 个答案:

答案 0 :(得分:0)

很明显,服务器端出现了问题。我将您的收据数据插入我的服务器API并获得以下经过验证的收据。试试这个例子PHP服务器端代码iOS7 - receipts not validating at sandbox - error 21002 (java.lang.IllegalArgumentException)

{
   "status":0,
   "environment":"Sandbox",
   "receipt":{
      "receipt_type":"ProductionSandbox",
      "adam_id":0,
      "bundle_id":"<<snip>>",
      "application_version":"0.6",
      "download_id":0,
      "request_date":"2014-03-23 17:38:03 Etc\/GMT",
      "request_date_ms":"1395596283823",
      "request_date_pst":"2014-03-23 10:38:03 America\/Los_Angeles",
      "in_app":[
         {
            "quantity":"1",
            "product_id":"Gems500",
            "transaction_id":"1000000105271988",
            "original_transaction_id":"1000000105271988",
            "purchase_date":"2014-03-21 19:45:08 Etc\/GMT",
            "purchase_date_ms":"1395431108000",
            "purchase_date_pst":"2014-03-21 12:45:08 America\/Los_Angeles",
            "original_purchase_date":"2014-03-21 15:13:47 Etc\/GMT",
            "original_purchase_date_ms":"1395414827000",
            "original_purchase_date_pst":"2014-03-21 08:13:47 America\/Los_Angeles",
            "is_trial_period":"false"
         },
         {
            "quantity":"1",
            "product_id":"Gems500",
            "transaction_id":"1000000105295974",
            "original_transaction_id":"1000000105295974",
            "purchase_date":"2014-03-21 19:45:08 Etc\/GMT",
            "purchase_date_ms":"1395431108000",
            "purchase_date_pst":"2014-03-21 12:45:08 America\/Los_Angeles",
            "original_purchase_date":"2014-03-21 19:45:08 Etc\/GMT",
            "original_purchase_date_ms":"1395431108000",
            "original_purchase_date_pst":"2014-03-21 12:45:08 America\/Los_Angeles",
            "is_trial_period":"false"
         }
      ]
   }
}

答案 1 :(得分:0)

以下是我用于将收据直接发送到Apple服务器的代码。

NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
NSData *receiptData = [NSData dataWithContentsOfURL:receiptURL];
[self checkTransactionWithAppleUseProduction:productionWebsite andData:receiptData];
NSString *string64=[receiptData base64EncodedStringWithOptions:kNilOptions];
NSString *payload = [NSString stringWithFormat:@"{\"receipt-data\" : \"%@\"}",string64];
NSData *payloadData = [payload dataUsingEncoding:NSUTF8StringEncoding];
NSString *serverURL;
if(productionWebsite)serverURL= @"https://buy.itunes.apple.com/verifyReceipt"; 
if(!productionWebsite)serverURL=@"https://sandbox.itunes.apple.com/verifyReceipt";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:serverURL]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:payloadData];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[conn start];

答案 2 :(得分:-3)

我对iOS代码了解不多。

但我知道很多关于PHP的知识。 我假设您的服务器端代码超过此处提供的代码段?我不确定json_encode的用法?什么是$ receipt和什么使用$ postData?