アップルの購入-バックエンドの注意事項

6787 ワード

ボスは保護費をもらうから、弟を待っても仕方がないよ...はい、私が言ったのはりんごの内買いです.
1.php検証関数を先に上げる:
/**
 *   AppStore  
 * @param  string $receipt_data      
 * @return array       
 */
function validate_apple_pay($receipt_data)
{
    /**
     *  0    receipt provided is valid.
     * 21000 App Store        JSON  
     * 21002 receipt-data       
     * 21003 receipt      
     * 21004    shared secret        shared secret
     * 21005 receipt        
     * 21006 receipt  ,       。            ,receipt            
     * 21007 receipt Sandbox receipt,              
     * 21008 receipt   receipt,     Sandbox       
     */
    function acurl($receipt_data, $sandbox = 0)
    {
        //    
        $POSTFIELDS = array("receipt-data" => $receipt_data);
        $POSTFIELDS = json_encode($POSTFIELDS);

        //             
        $url_buy = "https://buy.itunes.apple.com/verifyReceipt";
        $url_sandbox = "https://sandbox.itunes.apple.com/verifyReceipt";
        $url = $sandbox ? $url_sandbox : $url_buy;

        //   curl
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    //     
    if (strlen($receipt_data) < 20) {
        $result = array(
            'status' => false,
            'message' => '    '
        );
        return $result;
    }
    //     
    $html = acurl($receipt_data);
    $data = json_decode($html, true);
    //                
    if ($data['status'] == '21007') {
        //     
        $html = acurl($receipt_data, 1);
        $data = json_decode($html, true);
        $data['sandbox'] = '1';
    }

    if (isset($_GET['debug'])) {
        exit(json_encode($data));
    }

    //         
    if (intval($data['status']) === 0) {
        $order = $data['receipt']['in_app'];//        
        $k = count($order) - 1;
        $need = $order[$k];//       
        $result = array(
            'status' => true,
            'message' => '    ',
            'product_id' => substr($need['product_id'], 19),  //    
            'transaction_id' => $need['transaction_id']  //     

        );
    } else {
        $result = array(
            'status' => false,
            'message' => '     status:' . $data['status']
        );
    }
    return $result;
}
2.IOS              validate_apple_pay()    ;     json    :
 
  
   {
        “status”:0,
“environment”: “Sandbox”,
“receipt”: {
        “receipt_type”:“ProductionSandbox”,
“adam_id”: 0,
“app_item_id”: 0,
“bundle_id”: “com . christ . diandeng”,
“application_version”: “1”,
“download_id”: 0,
“version_external_identifier”: 0,
“receipt_creation_date”: “2018 - 05 - 07 10:54:48 Etc / GMT”,
“receipt_creation_date_ms”: “1525690488000”,
“receipt_creation_date_pst”: “2018 - 05 - 07 03:54:48 America / Los_Angeles”,
“request_date”: “2018 - 05 - 07 13:22:40 Etc / GMT”,
“request_date_ms”: “1525699360048”,
“request_date_pst”: “2018 - 05 - 07 06:22:40 America / Los_Angeles”,
“original_purchase_date”: “2013 - 08 - 01 07:00:00 Etc / GMT”,
“original_purchase_date_ms”: “1375340400000”,
“original_purchase_date_pst”: “2013 - 08 - 01 00:00:00 America / Los_Angeles”,
“original_application_version”: “1.0”,
“in_app”: [
{
“quantity”:“1”,
“product_id”: “com . christ . diandeng6”,
“transaction_id”: “1000000396341988”,
“original_transaction_id”: “1000000396341988”,
“purchase_date”: “2018 - 05 - 07 10:53:05 Etc / GMT”,
“purchase_date_ms”: “1525690385000”,
“purchase_date_pst”: “2018 - 05 - 07 03:53:05 America / Los_Angeles”,
“original_purchase_date”: “2018 - 05 - 07 10:53:05 Etc / GMT”,
“original_purchase_date_ms”: “1525690385000”,
“original_purchase_date_pst”: “2018 - 05 - 07 03:53:05 America / Los_Angeles”,
“is_trial_period”: “false”
},
{
“quantity”:“1”,
“product_id”: “com . christ . diandeng208”,
“transaction_id”: “1000000396342867”,
“original_transaction_id”: “1000000396342867”,
“purchase_date”: “2018 - 05 - 07 10:54:29 Etc / GMT”,
“purchase_date_ms”: “1525690469000”,
“purchase_date_pst”: “2018 - 05 - 07 03:54:29 America / Los_Angeles”,
“original_purchase_date”: “2018 - 05 - 07 10:54:29 Etc / GMT”,
“original_purchase_date_ms”: “1525690469000”,
“original_purchase_date_pst”: “2018 - 05 - 07 03:54:29 America / Los_Angeles”,
“is_trial_period”: “false”
}
]
}
}

3.次に、次のように自分のインタフェースを書きます.
 
   
 // paypal         
    public function paypal_app(){
        try {
            //         
            $receipt_data = I('post.apple_receipt');
            $user_id = I('post.user_id');
            // 1.      
            $result = validate_apple_pay($receipt_data);
            if ($result['status']) {
            // 2.                      
                $PayLogic = new PayLogic();
                $this->data = $recharge= $PayLogic->getSaveRecharge($user_id,$result['product_id'],$result['transaction_id']);
                $this->data['status'] = 1;
                $this->data['message'] = $result['message'];
            } else {
              // 3.     
                $this->data['status'] = 0;
                $this->data['message'] = $result['message'];
            }
        } catch (\Exception $e) {
            $this->error_code = $e->getCode();
            $this->error_msg = $e->getMessage();
        }
        return $this->getReturnJson($this->error_code, $this->error_msg, $this->data);
    }
  ,  php         ,      (         ):
1.ios        ,        ( base64  ),     ,          ; 2.     ios   ,             ; 3.  in_app,              ,             ?               :transaction_id,                   ,    ,    ;       。 4.  ios    ,     in_app      ?            ,         。         : 1:       (IAP),           2:php         3.    :iOS In-App Purchase(IAP)              4. tp    :thinkphp       AppStore         php