苹果IAP内付费二次验证代码

3 篇文章 0 订阅
1. 验证逻辑在客户端实现:
  1. // iapData 用户购成功的transactionReceipt
  2. -(BOOL)putStringToItunes:(NSData*)iapData
  3. {
  4.     NSString*encodingStr = [iapData base64EncodedString];
  5.     
  6.     NSString *URL=@URL_VerifyReceipt;
  7.     
  8.     // https://sandbox.itunes.apple.com/verifyReceipt 测试地址
  9.     // https://buy.itunes.apple.com/verifyReceipt 正式发布验证地址
  10.     
  11.     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];// autorelease];
  12.     [request setURL:[NSURL URLWithString:URL]];
  13.     [request setHTTPMethod:@"POST"];
  14.     //设置contentType
  15.     [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  16.     //设置Content-Length
  17.     [request setValue:[NSString stringWithFormat:@"%d", [encodingStr length]] forHTTPHeaderField:@"Content-Length"];
  18.     
  19.     NSDictionary* body = [NSDictionary dictionaryWithObjectsAndKeys:encodingStr, @"receipt-data", nil];
  20.     SBJsonWriter *writer = [SBJsonWriter new];
  21.     [request setHTTPBody:[[writer stringWithObject:body] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
  22.     NSHTTPURLResponse *urlResponse=nil;
  23.     NSError *errorr=nil;
  24.     NSData *receivedData = [NSURLConnection sendSynchronousRequest:request
  25.                                                  returningResponse:&urlResponse
  26.                                                              error:&errorr];
  27.     
  28.     //解析
  29.     NSString *results=[[NSString alloc]initWithBytes:[receivedData bytes] length:[receivedData length] encoding:NSUTF8StringEncoding];
  30.     
  31.     printf(" \n MKStoreManager::putStringToItunes: %s \n", [results UTF8String]);
  32.     
  33.     NSDictionary*dic = [results JSONValue];
  34.     if([[dic objectForKey:@"status"] intValue]==0) //注意,status=@"0" 是验证收据成功
  35.     {
  36.         return true;
  37.     }
  38.     return false;
  39. }

  1. // 交易成功
  2. -(void) provideContent: (SKPaymentTransaction *)transaction
  3. {
  4.     if(delegate && [delegate respondsToSelector:@selector(productPurchasedSuccess:)])
  5.     {
  6.         if (!isNetworkOK()) {
  7.             [delegate productPurchasedFailed:transaction.payment.productIdentifier];
  8.             return;
  9.         }
  10.         if([self putStringToItunes:transaction.transactionReceipt])
  11.         {
  12.             printf("putStringToItunes check success!!! \n");
  13.             [delegate productPurchasedSuccess:transaction.payment.productIdentifier];
  14.         }else{
  15.             [delegate productPurchasedFailed:transaction.payment.productIdentifier];
  16.             
  17.         }
  18.     }
  19. }


2. 验证逻辑放在服务器端。

实现代码如下,

需要上传客户端得到的   NSString * encodingStr  =   [ iapData base64EncodedString ] ; 数据


  1. <?php
  2. /**
  3.  * @说明:        iap 购买服务器验证逻辑
  4.  * @作者:        linux_wuliqiang@163.com
  5.  *
  6.  * @data:         2013-05-06
  7.  *
  8.  * @备注:        客户端进行 iap 购买后,需要服务器再次进行验证。确定玩家是否购买成功
  9.  *
  10.  *
  11.  */

  12. class BaseIapCheck
  13. {
  14.     // 是否为沙盒测试环境
  15.     const IapCheck_IsSandBox = true;
  16.     
  17.     
  18.     /**
  19.      * 得到 iap 购买的单据数据,如果成功购买了,返回正常的购买数据,否则返回 null
  20.      * string $receipt, 客户端 iap 购买时,返回的单据数据, 此数据是在客户端经过 NSString*encodingStr = [iapData base64EncodedString]; 处理后的数据
  21.      *
  22.      * return ,验证成功,返回正常的购买数据,验证失败,返回 null
  23.      * 
  24.      * 备注:可以通过 product_id 来判定具体购买的是哪一个收费道具
  25.      */
  26.     public static function GetReceiptData($receipt)
  27.     {
  28.         if (self::IapCheck_IsSandBox) 
  29.         {
  30.             $url = 'https://sandbox.itunes.apple.com/verifyReceipt';
  31.         }
  32.         else 
  33.         {
  34.             $url = 'https://buy.itunes.apple.com/verifyReceipt';
  35.         }
  36.     
  37.         $postDataJson = json_encode(array('receipt-data' => $receipt));
  38.         $opts = array
  39.         (
  40.                 'http' => array
  41.                 (
  42.                         'method' => 'POST',
  43.                         'header'=> "Content-type: application/json" .                        // 必须设置为 application/json 格式
  44.                         "Content-Length: " . strlen($postDataJson) . "\r\n",
  45.                         'content' => $postDataJson
  46.                 )
  47.         );
  48.         
  49.         //生成请求的句柄文件
  50.         $context = stream_context_create($opts);
  51.         $html = file_get_contents($url, false, $context);
  52.         $data = json_decode($html);

  53. //         echo '
    ';

  54. //         echo '$html 
    ';

  55. //         var_dump($html);
  56. //         echo '
    ';

  57. //         echo 'data 
    ';

  58. //         var_dump($data);
  59. //         echo '
    ';

  60.         
  61.         //判断返回的数据是否是对象
  62.         if (!is_object($data)) 
  63.         {
  64.             return null;
  65.         }
  66.         
  67.         //判断是否购买成功
  68.         if (!isset($data->status) || $data->status != 0) 
  69.         {
  70.             return null;
  71.         }
  72.     
  73.         //返回产品的信息
  74.         return array(
  75.                 'quantity' => $data->receipt->quantity,
  76.                 'product_id' => $data->receipt->product_id,
  77.                 'transaction_id' => $data->receipt->transaction_id,
  78.                 'purchase_date' => $data->receipt->purchase_date,
  79.                 'item_id' => $data->receipt->item_id,
  80.                 'bid' => $data->receipt->bid,
  81.                 'bvrs' => $data->receipt->bvrs
  82.         );
  83.     }
  84. }


  85. ?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值