這篇文章主要介紹了php怎么處理IOS自動(dòng)續(xù)費(fèi)的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇php怎么處理IOS自動(dòng)續(xù)費(fèi)文章都會(huì)有所收獲,下面我們一起來看看吧。
創(chuàng)新互聯(lián)建站服務(wù)電話:18980820575,為您提供成都網(wǎng)站建設(shè)網(wǎng)頁設(shè)計(jì)及定制高端網(wǎng)站建設(shè)服務(wù),創(chuàng)新互聯(lián)建站網(wǎng)頁制作領(lǐng)域10多年,包括成都木屋等多個(gè)行業(yè)擁有多年的網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn),選擇創(chuàng)新互聯(lián)建站,為企業(yè)錦上添花!
首先講一下業(yè)務(wù)邏輯:
先上圖
下面詳細(xì)講一下,作為服務(wù)端具體要做些什么,并貼上對應(yīng)的代碼:
第一步:
通過客戶端傳過來的recept(票據(jù))進(jìn)行生成訂單的操作【注意這里需要驗(yà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)行驗(yàn)證,防止收到的是偽造的數(shù)據(jù)
Log::info(['uid'=>$uid,'receipt'=>$receipt_data,'iap_info'=>$info]);
if (is_array($info) && $info['status'] == 0) {//沒有錯(cuò)誤就進(jìn)行生成訂單的業(yè)務(wù)邏輯的處理
} elseif (is_array($info) && $info['status'] == 21007) {
$new_info = $this->getReceiptData($receipt_data, true);//接著去蘋果官網(wǎng)進(jìn)行二次驗(yà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';//真實(shí)運(yùn)營地址
}
$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請求有錯(cuò)誤
$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;
}
第二步:
自動(dòng)訂閱續(xù)費(fèi)功能(在支付的基礎(chǔ)上,才能進(jìn)行自動(dòng)續(xù)費(fèi)的功能,這里的接口不需要客戶端調(diào)起,這個(gè)是蘋果端自動(dòng)回調(diào)的,需要在蘋果官網(wǎng)上進(jìn)行填寫回調(diào)鏈接,如下圖:)
接下來配上自動(dòng)回調(diào)的代碼:
/*
* 自動(dòng)續(xù)費(fèi)訂閱回調(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í)候蘋果那邊會(huì)傳空數(shù)據(jù)調(diào)用
// notification_type 幾種狀態(tài)描述
// INITIAL_BUY 初次購買訂閱。latest_receipt通過在App Store中驗(yàn)證,可以隨時(shí)將您的服務(wù)器存儲(chǔ)在服務(wù)器上以驗(yàn)證用戶的訂閱狀態(tài)。
// CANCEL Apple客戶支持取消了訂閱。檢查Cancellation Date以了解訂閱取消的日期和時(shí)間。
// RENEWAL 已過期訂閱的自動(dòng)續(xù)訂成功。檢查Subscription Expiration Date以確定下一個(gè)續(xù)訂日期和時(shí)間。
// INTERACTIVE_RENEWAL 客戶通過使用用App Store中的App Store以交互方式續(xù)訂訂閱。服務(wù)立即可用。
// DID_CHANGE_RENEWAL_PREF 客戶更改了在下次續(xù)訂時(shí)生效的計(jì)劃。當(dāng)前的有效計(jì)劃不受影響。
$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') { //取消訂閱,做個(gè)記錄
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自動(dòng)續(xù)費(fèi)”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“php怎么處理IOS自動(dòng)續(xù)費(fèi)”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
標(biāo)題名稱:php怎么處理IOS自動(dòng)續(xù)費(fèi)
本文地址:http://bm7419.com/article44/gociee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、軟件開發(fā)、企業(yè)網(wǎng)站制作、做網(wǎng)站、網(wǎng)站建設(shè)、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)