您好,登錄后才能下訂單哦!
這篇文章主要介紹了php怎么處理IOS自動續(xù)費的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇php怎么處理IOS自動續(xù)費文章都會有所收獲,下面我們一起來看看吧。
首先講一下業(yè)務(wù)邏輯:
先上圖
下面詳細(xì)講一下,作為服務(wù)端具體要做些什么,并貼上對應(yīng)的代碼:
第一步:
通過客戶端傳過來的recept(票據(jù))進(jìn)行生成訂單的操作【注意這里需要驗證訂單是否已存在】,訂單生成返回客戶端相關(guān)信息;
public function pay()
{
$uid = $this->request->header('uid');
$receipt_data = $this->request->post('receipt');
if (!$uid || !$receipt_data) return $this->rep(400);
$info = $this->getReceiptData($receipt_data, $this->isSandbox);//去蘋果進(jìn)行驗證,防止收到的是偽造的數(shù)據(jù)
Log::info(['uid'=>$uid,'receipt'=>$receipt_data,'iap_info'=>$info]);
if (is_array($info) && $info['status'] == 0) {//沒有錯誤就進(jìn)行生成訂單的業(yè)務(wù)邏輯的處理
} elseif (is_array($info) && $info['status'] == 21007) {
$new_info = $this->getReceiptData($receipt_data, true);//接著去蘋果官網(wǎng)進(jìn)行二次驗證(沙盒)
// 進(jìn)行生成訂單的業(yè)務(wù)邏輯的處理
}
}
private function getReceiptData($receipt, $isSandbox = false)
{
if ($isSandbox) {
$endpoint = 'https://sandbox.itunes.apple.com/verifyReceipt';//沙箱地址
} else {
$endpoint = 'https://buy.itunes.apple.com/verifyReceipt';//真實運營地址
}
$postData = json_encode(['receipt-data' => $receipt, 'password'=>'abde7d535c']);
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$response = curl_exec($ch);
$errno = curl_errno($ch);
curl_close($ch);
if ($errno != 0) {//curl請求有錯誤
$order['status'] = 408;
} else {
$data = json_decode($response, true);
if (isset($data['status'])) {
//返回產(chǎn)品的信息
$order = isset($data['receipt']) ? $data['receipt'] : [];
$order['status'] = $data['status'];
} else {
$order['status'] = 30000;
}
}
return $order;
}
第二步:
自動訂閱續(xù)費功能(在支付的基礎(chǔ)上,才能進(jìn)行自動續(xù)費的功能,這里的接口不需要客戶端調(diào)起,這個是蘋果端自動回調(diào)的,需要在蘋果官網(wǎng)上進(jìn)行填寫回調(diào)鏈接,如下圖:)
接下來配上自動回調(diào)的代碼:
/*
* 自動續(xù)費訂閱回調(diào)
* Doc: https://developer.apple.com/documentation/appstoreservernotifications/notification_type
*/
public function renew(){
$resp_str = $this->request->post();
Log::info(['resp_str'=>$resp_str]);
if(!empty($resp_str)) {
// $data = json_decode($resp_str,true);
//
$data = $resp_str['unified_receipt'];
//有時候蘋果那邊會傳空數(shù)據(jù)調(diào)用
// notification_type 幾種狀態(tài)描述
// INITIAL_BUY 初次購買訂閱。latest_receipt通過在App Store中驗證,可以隨時將您的服務(wù)器存儲在服務(wù)器上以驗證用戶的訂閱狀態(tài)。
// CANCEL Apple客戶支持取消了訂閱。檢查Cancellation Date以了解訂閱取消的日期和時間。
// RENEWAL 已過期訂閱的自動續(xù)訂成功。檢查Subscription Expiration Date以確定下一個續(xù)訂日期和時間。
// INTERACTIVE_RENEWAL 客戶通過使用用App Store中的App Store以交互方式續(xù)訂訂閱。服務(wù)立即可用。
// DID_CHANGE_RENEWAL_PREF 客戶更改了在下次續(xù)訂時生效的計劃。當(dāng)前的有效計劃不受影響。
$notification_type = $resp_str['notification_type'];//通知類型
$password = $resp_str['password']; // 共享秘鑰
if ($password == "abde7d5353") {
$receipt = isset($data['latest_receipt_info']) ? $data['latest_receipt_info'] : $data['latest_expired_receipt_info']; //latest_expired_receipt_info 好像只有更改續(xù)訂狀態(tài)才有
//找出來最近的那一組信息
$receipt = self::arraySort($receipt, 'purchase_date', 'desc');
$original_transaction_id = $receipt['original_transaction_id']; // //原始交易ID
$transaction_id = $receipt['transaction_id']; // //交易的標(biāo)識
$purchaseDate = str_replace(' America/Los_Angeles','',$receipt['purchase_date_pst']);
$orderinfo = Order::field('uid,original_transaction_id,money,order_no,pay_time')->where(['original_transaction_id' => $original_transaction_id])->find();
$user_info = User::field('app_uid,device_id,unionid')->get($orderinfo['uid']);
if ($notification_type == 'CANCEL') { //取消訂閱,做個記錄
IpaLog::addLog($orderinfo['uid'], $orderinfo['order_no'], $receipt, $resp_str);
} else {
if ($notification_type == "INTERACTIVE_RENEWAL" || $notification_type == "RENEWAL" || $notification_type == 'INITIAL_BUY') {
// $transTime = $this->toTimeZone($receipt['purchase_date']);
IapRenew::addRenew($orderinfo['uid'], $receipt, $data['latest_receipt'],1,$notification_type,$user_info['app_uid'],$purchaseDate);
IpaLog::addLog($orderinfo['uid'], $orderinfo['order_no'], $receipt, $resp_str);
} else {
IapRenew::addRenew($orderinfo['uid'], $receipt, $data['latest_receipt'],0,$notification_type,$user_info['app_uid'],$purchaseDate);
IpaLog::addLog($orderinfo['uid'], $orderinfo['order_no'], $receipt, $resp_str);
}
}
} else {
Log::info('通知傳遞的密碼不正確--password:'.$password);
}
}
}
private function toTimeZone($src, $from_tz = 'Etc/GMT', $to_tz = 'Asia/Shanghai', $fm = 'Y-m-d H:i:s')
{
$datetime = new \DateTime($src, new \DateTimeZone($from_tz));
$datetime->setTimezone(new \DateTimeZone($to_tz));
return $datetime->format($fm);
}
private static function arraySort($arr, $key, $type='asc')
{
$keyArr = []; // 初始化存放數(shù)組將要排序的字段值
foreach ($arr as $k=>$v){
$keyArr[$k] = $v[$key]; // 循環(huán)獲取到將要排序的字段值
}
if($type == 'asc'){
asort($keyArr); // 排序方式,將一維數(shù)組進(jìn)行相應(yīng)排序
}else{
arsort($keyArr);
}
foreach ($keyArr as $k=>$v){
$newArray[$k] = $arr[$k]; // 循環(huán)將配置的值放入響應(yīng)的下標(biāo)下
}
$newArray = array_merge($newArray); // 重置下標(biāo)
return $newArray[0]; // 數(shù)據(jù)返回
}
關(guān)于“php怎么處理IOS自動續(xù)費”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“php怎么處理IOS自動續(xù)費”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。