http://blog.chinaunix.net/uid-20622737-id-3658524.html
客户端代码逻辑实现:
- // iapData 用户购成功的transactionReceipt
- -(BOOL)putStringToItunes:(NSData*)iapData
- {
- NSString*encodingStr = [iapData base64EncodedString];
-
- NSString *URL=@URL_VerifyReceipt;
-
- // https://sandbox.itunes.apple.com/verifyReceipt 测试地址
- // https://buy.itunes.apple.com/verifyReceipt 正式发布验证地址
-
- NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];// autorelease];
- [request setURL:[NSURL URLWithString:URL]];
- [request setHTTPMethod:@"POST"];
- //设置contentType
- [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
- //设置Content-Length
- [request setValue:[NSString stringWithFormat:@"%d", [encodingStr length]] forHTTPHeaderField:@"Content-Length"];
-
- NSDictionary* body = [NSDictionary dictionaryWithObjectsAndKeys:encodingStr, @"receipt-data", nil];
- SBJsonWriter *writer = [SBJsonWriter new];
- [request setHTTPBody:[[writer stringWithObject:body] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
- NSHTTPURLResponse *urlResponse=nil;
- NSError *errorr=nil;
- NSData *receivedData = [NSURLConnection sendSynchronousRequest:request
- returningResponse:&urlResponse
- error:&errorr];
-
- //解析
- NSString *results=[[NSString alloc]initWithBytes:[receivedData bytes] length:[receivedData length] encoding:NSUTF8StringEncoding];
-
- printf(" \n MKStoreManager::putStringToItunes: %s \n", [results UTF8String]);
-
- NSDictionary*dic = [results JSONValue];
- if([[dic objectForKey:@"status"] intValue]==0) //注意,status=@"0" 是验证收据成功
- {
- return true;
- }
- return false;
- }
- // 交易成功
- -(void) provideContent: (SKPaymentTransaction *)transaction
- {
- if(delegate && [delegate respondsToSelector:@selector(productPurchasedSuccess:)])
- {
- if (!isNetworkOK()) {
- [delegate productPurchasedFailed:transaction.payment.productIdentifier];
- return;
- }
- if([self putStringToItunes:transaction.transactionReceipt])
- {
- printf("putStringToItunes check success!!! \n");
- [delegate productPurchasedSuccess:transaction.payment.productIdentifier];
- }else{
- [delegate productPurchasedFailed:transaction.payment.productIdentifier];
-
- }
- }
- }
如果验证逻辑放在服务器端。实现代码如下,
需要上传客户端得到的 NSString * encodingStr = [ iapData base64EncodedString ] ; 数据
- <?php
- /**
- * @说明: iap 购买服务器验证逻辑
- * @作者: linux_wuliqiang@163.com
- *
- * @data: 2013-05-06
- *
- * @备注: 客户端进行 iap 购买后,需要服务器再次进行验证。确定玩家是否购买成功
- *
- *
- */
-
- class BaseIapCheck
- {
- // 是否为沙盒测试环境
- const IapCheck_IsSandBox = true;
-
-
- /**
- * 得到 iap 购买的单据数据,如果成功购买了,返回正常的购买数据,否则返回 null
- * string $receipt, 客户端 iap 购买时,返回的单据数据, 此数据是在客户端经过 NSString*encodingStr = [iapData base64EncodedString]; 处理后的数据
- *
- * return ,验证成功,返回正常的购买数据,验证失败,返回 null
- *
- * 备注:可以通过 product_id 来判定具体购买的是哪一个收费道具
- */
- public static function GetReceiptData($receipt)
- {
- if (self::IapCheck_IsSandBox)
- {
- $url = 'https://sandbox.itunes.apple.com/verifyReceipt';
- }
- else
- {
- $url = 'https://buy.itunes.apple.com/verifyReceipt';
- }
-
- $postDataJson = json_encode(array('receipt-data' => $receipt));
- $opts = array
- (
- 'http' => array
- (
- 'method' => 'POST',
- 'header'=> "Content-type: application/json" . // 必须设置为 application/json 格式
- "Content-Length: " . strlen($postDataJson) . "\r\n",
- 'content' => $postDataJson
- )
- );
-
- //生成请求的句柄文件
- $context = stream_context_create($opts);
- $html = file_get_contents($url, false, $context);
- $data = json_decode($html);
-
- // echo '<br>';
- // echo '$html <br>';
- // var_dump($html);
- // echo '<br>';
- // echo 'data <br>';
- // var_dump($data);
- // echo '<br>';
-
- //判断返回的数据是否是对象
- if (!is_object($data))
- {
- return null;
- }
-
- //判断是否购买成功
- if (!isset($data->status) || $data->status != 0)
- {
- return null;
- }
-
- //返回产品的信息
- return array(
- 'quantity' => $data->receipt->quantity,
- 'product_id' => $data->receipt->product_id,
- 'transaction_id' => $data->receipt->transaction_id,
- 'purchase_date' => $data->receipt->purchase_date,
- 'item_id' => $data->receipt->item_id,
- 'bid' => $data->receipt->bid,
- 'bvrs' => $data->receipt->bvrs
- );
- }
- }
-
-
- ?>