IOS】IAP (内置购买)PHP 服务器端代码

PHP 服务器端代码,

首先要确  php的 curl 和  SSL (open_ssl)这两个模块开启,可以在  php.ini  中去掉  这两个dll前面的分号。

 

  1. <?php
  2.     //服务器二次验证代码
  3.     function getReceiptData($receipt, $isSandbox = false)
  4.     {
  5.         if ($isSandbox) {
  6.             $endpoint = 'https://sandbox.itunes.apple.com/verifyReceipt';
  7.         }
  8.         else {
  9.             $endpoint = 'https://buy.itunes.apple.com/verifyReceipt';
  10.         }
  11.         $postData = json_encode(
  12.             array('receipt-data' => $receipt)
  13.         );
  14.         $ch = curl_init($endpoint);
  15.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  16.         curl_setopt($ch, CURLOPT_POST, true);
  17.         curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  18.        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);  //这两行一定要加,不加会报SSL 错误
  19.         curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
  20.         $response = curl_exec($ch);
  21.         $errno    = curl_errno($ch);
  22.         $errmsg   = curl_error($ch);
  23.         curl_close($ch);
  24.     //判断时候出错,抛出异常
  25.         if ($errno != 0) {
  26.             throw new Exception($errmsg, $errno);
  27.         }
  28.         $data = json_decode($response);
  29.     //判断返回的数据是否是对象
  30.         if (!is_object($data)) {
  31.             throw new Exception('Invalid response data');
  32.         }
  33.     //判断购买时候成功
  34.         if (!isset($data->status) || $data->status != 0) {
  35.             throw new Exception('Invalid receipt');
  36.         }
  37.     //返回产品的信息
  38.         return array(
  39.             'quantity'       =>  $data->receipt->quantity,
  40.             'product_id'     =>  $data->receipt->product_id,
  41.             'transaction_id' =>  $data->receipt->transaction_id,
  42.             'purchase_date'  =>  $data->receipt->purchase_date,
  43.             'app_item_id'    =>  $data->receipt->app_item_id,
  44.             'bid'            =>  $data->receipt->bid,
  45.             'bvrs'           =>  $data->receipt->bvrs
  46.         );
  47.     }
  48.     //获取 App 发送过来的数据,设置时候是沙盒状态
  49.         $receipt   = $_GET['data'];
  50.         $isSandbox = true;
  51.     //开始执行验证
  52.     try
  53.      {
  54.          $info = getReceiptData($receipt, $isSandbox);
  55.          // 通过product_id 来判断是下载哪个资源
  56.          switch($info['product_id']){
  57.             case 'com.application.xxxxx.xxxx':
  58.                 Header("Location:xxxx.zip");
  59.             break;
  60.         }
  61.      }
  62.     //捕获异常
  63.     catch(Exception $e)
  64.     {
  65.         echo 'Message: ' .$e->getMessage();
  66.     }
  67. ?>
0

这篇文章还没有评论

发表评论