Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4917281
  • 博文数量: 763
  • 博客积分: 12108
  • 博客等级: 上将
  • 技术积分: 15717
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-28 21:21
个人简介

业精于勤,荒于嬉

文章分类

全部博文(763)

文章存档

2018年(6)

2017年(15)

2016年(2)

2015年(31)

2014年(14)

2013年(87)

2012年(75)

2011年(94)

2010年(190)

2009年(38)

2008年(183)

2007年(28)

分类: iOS平台

2013-05-07 10:11:11



客户端代码逻辑实现:
  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. }


如果验证逻辑放在服务器端。实现代码如下,
需要上传客户端得到的  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 = '';
  31.         }
  32.         else
  33.         {
  34.             $url = '';
  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. ?>



阅读(8216) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~