$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; } }