123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <?php
- namespace app\common\library;
- use think\Db;
- /**
- * 苹果server to server
- */
- class Transaction
- {
- /**
- * 续费or升级
- *
- * @param $transaction_info
- * @param bool $is_upgrade
- * @return bool
- */
- public static function autoRenew($transaction_info, $is_upgrade = false)
- {
- $original_transaction_id = isset($transaction_info['originalTransactionId']) ? $transaction_info['originalTransactionId'] : ''; // 订阅原始ID
- $transaction_id = isset($transaction_info['transactionId']) ? $transaction_info['transactionId'] : ''; // 苹果订单号
- $product_id = isset($transaction_info['productId']) ? $transaction_info['productId'] : ''; // 产品id
- if (!$original_transaction_id || !$transaction_id || !$product_id) {
- return '参数有误';
- }
- filePut("\r\n\r\n".'新的S2S');
- $prefix = '[S2S]originalTransactionId:'.$original_transaction_id.',transactionId:'.$transaction_id.',productId:'.$product_id.'。';
- //检查重复订单
- $check_map = [
- 'original_transaction_id' => $original_transaction_id,
- 'transaction_id' => $transaction_id,
- ];
- $check_order = Db::name('user_vipxufei_task')->where($check_map)->field('id')->find();
- if($check_order){
- filePut($prefix.'续费早已完成');
- return '续费早已完成';
- }
- Db::startTrans();
- //查找订单,可能找到以前的。只看同一个用户的,其他用户的这里不处理
- $order_map = [
- 'original_transaction_id' => $original_transaction_id,
- ];
- $order_info = Db::name('user_vipxufei_task')->where($order_map)->order('expires_date_ms desc')->find();
- if (!$order_info) {
- Db::rollback();
- filePut($prefix.'不存在的订单');
- return '不存在的订单';
- }
- //续订,但是换了产品了,重新定义order_info
- //原始id换给别人用了
- $order_info_bundle_id = $order_info['bundle_id'];
- $order_info_user_id = $order_info['user_id'];
- if($product_id != $order_info['bundle_id']){
- $pay_order_map = [
- 'user_id' => $order_info['user_id'],
- 'bundle_id' => $product_id,
- 'order_status' => 0,
- 'table_name' => 'vip_recharge',
- ];
- $pay_order = Db::name('pay_order')->where($pay_order_map)->order('id desc')->lock(true)->find();
- if(!$pay_order){
- Db::rollback();
- filePut($prefix.'未找到匹配的交易,新订单找不到');
- return '未找到匹配的交易,新订单找不到';
- }
- // 修改订单状态
- $update_order = [
- 'notifytime'=>time(),
- 'order_status'=>1,
- 'original_transaction_id' => $original_transaction_id,
- 'transaction_info' => json_encode($transaction_info),
- ];
- $ros = Db::name('pay_order')->where(['id' => $pay_order['id']])->update($update_order);
- if($ros === false) {
- filePut($prefix.'修改订单状态失败');
- Db::rollback();
- return '修改订单状态失败';
- }
- $args = json_decode($pay_order['args'],true);
- //修改order_info
- $order_info['order_id'] = $pay_order['id'];
- $order_info['user_id'] = $pay_order['user_id'];
- $order_info['bundle_id'] = $pay_order['bundle_id'];
- $order_info['days'] = $args['days'];
- $order_info['original_transaction_id'] = $original_transaction_id;//多余
- }
- //验证时间,不得小于最新的一条预定信息
- if($transaction_info['purchaseDate'] <= $order_info['purchase_date_ms'] || $transaction_info['expiresDate'] <= $order_info['expires_date_ms']){
- Db::rollback();
- filePut($prefix.'时间对不上,返回成功,finish掉');
- return '时间对不上,返回成功,finish掉';
- }
- //逻辑开始
- //先充值
- $user_info = Db::name('user_wallet')->where('user_id',$order_info['user_id'])->lock(true)->find();
- if($user_info['vip_endtime'] < time()){
- //过期了
- $vip_endtime = time() + (intval($order_info['days']) * 86400);
- }else{
- //追加vip
- $vip_endtime = $user_info['vip_endtime'] + (intval($order_info['days']) * 86400);
- }
- $update_data = [
- 'vip_endtime'=>$vip_endtime,
- ];
- $result = Db::name('user_wallet')->where('user_id',$order_info['user_id'])->update($update_data);
- if($result === false)
- {
- filePut($prefix.'逻辑续费vip时间失败');
- Db::rollback();
- return '逻辑续费vip时间失败';
- }
- //逻辑结束
- //添加新的一个task
- $task_data = $order_info;
- unset($task_data['id']);
- $task_data['createtime'] = time();
- $task_data['apple_receipt'] = '';
- $task_data['in_app_one'] = '';
- $task_data['transaction_id'] = $transaction_id;
- $task_data['times'] = $order_info['times'] + 1;
- $task_data['original_purchase_date_ms'] = $transaction_info['originalPurchaseDate'];
- $task_data['purchase_date_ms'] = $transaction_info['purchaseDate'];
- $task_data['expires_date_ms'] = $transaction_info['expiresDate'];
- $task_data['transaction_info'] = json_encode($transaction_info);
- if($product_id != $order_info_bundle_id){
- $task_data['times'] = 1;//回归1
- }
- $task_id = Db::name('user_vipxufei_task')->insertGetId($task_data);
- if(!$task_id)
- {
- filePut($prefix.'用户添加vipxufei_task失败');
- Db::rollback();
- return '用户添加vipxufei_task失败';
- }
- Db::commit();
- filePut($prefix.'充值成功'.$task_id);
- return '充值成功'.$task_id;
- //逻辑结束
- }
- /**
- * 退款
- *
- * @param $transaction_info
- * @return bool
- */
- public static function refund($transaction_info)
- {
- return true;
- /*$original_transaction_id = $transaction_info['originalTransactionId']; // 订阅ID
- $web_order_line_item_id = $transaction_info['webOrderLineItemId']; // 交易ID
- $base_msg = self::_getBaseMsg($original_transaction_id, $web_order_line_item_id) . '- 退款:';
- $transaction_info_data = Transaction::query()
- ->where('web_order_line_item_id', '=', $transaction_info['webOrderLineItemId'])
- ->where('original_transaction_id', '=', $transaction_info['originalTransactionId'])
- ->first();
- if (!$transaction_info_data) {
- Log::channel('transaction')->warning($base_msg . ' not find transaction info');
- return false;
- }
- // 修改退款时间以及状态
- Transaction::query()
- ->where('web_order_line_item_id', '=', $transaction_info['webOrderLineItemId'])
- ->where('original_transaction_id', '=', $transaction_info['originalTransactionId'])
- ->update([
- 'cancellation_date_ms' => date('Y-m-d H:i:s', $transaction_info['revocationDate'] / 1000),
- 'is_cancellation' => 1,
- ]);
- // 修改用户会员为到期
- User::saveExpireVip($transaction_info_data->user_id);
- return true;*/
- }
- /**
- * 订阅
- *
- * @param $transaction_info
- * @param $subtype
- * @return bool
- */
- public static function updateSubscribed($transaction_info, $subtype)
- {
- return true;
- }
- /**
- * 续订失败
- *
- * @param $transaction_info
- * @return bool
- */
- public static function updateExpiredTrans($transaction_info)
- {
- return true;
- }
- /**
- * 续订失败
- *
- * @param $transaction_info
- * @return bool
- */
- public static function updateRenewFailTrans($transaction_info)
- {
- return true;
- }
- /**
- * 自动续费状态修改
- *
- * @param $transaction_info
- * @param $subtype
- * @return bool
- */
- public static function changeRenewStatus($transaction_info, $subtype)
- {
- return true;
- }
- }
|