验证函数:
function appleVerify($receipt_data,$orderId = 0) { /* * 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环境的验证服务 * $receipt_data 苹果返回的支付凭证 * 正式 : https://buy.itunes.apple.com/verifyReceipt * 沙箱 : https://sandbox.itunes.apple.com/verifyReceipt */ $env = App::environment(); if($env == 'online'){
//线上环境 $url = 'https://buy.itunes.apple.com/verifyReceipt'; }else{
//沙盒环境 $url = 'https://sandbox.itunes.apple.com/verifyReceipt'; } $num = 0; while (1){ $num ++; $response = Curl::to($url) ->withData(array('receipt-data' => $receipt_data)) ->asJson(true) ->post(); if($response['status'] != 21005 || $num >= 3){ break; } } // $response = json_decode($response,true); // $data['status']==0 成功 // $data['receipt']['in_app'][0]['transaction_id'] 苹果订单号 // $data['receipt']['in_app'][0]['product_id']; 商品价格 // $data['receipt']['in_app'][0]['purchase_date_ms'] $status = 0; $purchaseTime = 0; if ($response['status'] == 0) { $status = 1; try{ $inapps = $response['receipt']['in_app']; $purchaseTime = $inapps[0]['purchase_date_ms']; if(!empty($orderId)){ foreach ($inapps as $item){ if($orderId == $item['transaction_id']){ $purchaseTime = $item['purchase_date_ms']; break; } } } }catch (Exception $e){ return ['status'=>0,'purcaseTime' => 0]; } } return ['status'=>$status,'purcaseTime' => $purchaseTime]; }
参数说明:
//$receipt_data : ios购买时苹果服务器返回的订单信息 //$orderId : 订单号,验证订单是返回的内购订单数据可能会有多条($response['receipt']['in_app']有多条数据),所以需要遍历每条记录,找到跟订单ID一样的那一条数据的购买时间。
$receipt_data="MIIbWQYJKoZIhvcNAQcCoIIbSjCCG0YCAQExCzAJBgUrDgMCGgUA......"; $orderId="1000000461350339";
返回数据:验证订单获取的数据 $response:(其中in_app有多个)。
{ "receipt": { "receipt_type": "Production", "adam_id": 127345, "app_item_id": 12732445, "bundle_id": "com.frd.cros", "application_version": "62", "download_id": 6703453, "version_external_identifier": 829054, "receipt_creation_date": "2019-03-29 02:03:00 Etc/GMT", "receipt_creation_date_ms": "1553820000", "receipt_creation_date_pst": "2019-03-28 19:03:00 America/Los_Angeles", "request_date": "2019-04-03 07:04:10 Etc/GMT", "request_date_ms": "1554275050780", "request_date_pst": "2019-04-03 00:04:10 America/Los_Angeles", "original_purchase_date": "2017-10-27 19:28:27 Etc/GMT", "original_purchase_date_ms": "1509132507000", "original_purchase_date_pst": "2017-10-27 12:28:27 America/Los_Angeles", "original_application_version": "3", "in_app": [ { "quantity": "1", "product_id": "com.wordguess.go.in2", "transaction_id": "270004425012", "original_transaction_id": "270000425012", "purchase_date": "2019-03-29 02:02:59 Etc/GMT", "purchase_date_ms": "1553879000", "purchase_date_pst": "2019-03-28 19:02:59 America/Los_Angeles", "original_purchase_date": "2019-03-29 02:02:59 Etc/GMT", "original_purchase_date_ms": "1553824979000", "original_purchase_date_pst": "2019-03-28 19:02:59 America/Los_Angeles", "is_trial_period": "false" }, { "quantity": "1", "product_id": "com.fil.removeads10", "transaction_id": "2700005471", "original_transaction_id": "27005471", "purchase_date": "2017-11-16 16:44:08 Etc/GMT", "purchase_date_ms": "1510850648000", "purchase_date_pst": "2017-11-16 08:44:08 America/Los_Angeles", "original_purchase_date": "2017-11-16 16:44:08 Etc/GMT", "original_purchase_date_ms": "1510850648000", "original_purchase_date_pst": "2017-11-16 08:44:08 America/Los_Angeles", "is_trial_period": "false" } ] }, "status": 0, "environment": "Production" }