123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- namespace App\Modules\Trade\Pay;
- use App\Modules\Book\Services\BookConfigService;
- use App\Modules\Subscribe\Models\Order;
- use DB;
- use App\Jobs\QappOrder;
- /**
- *
- */
- class OrderPaySuccess
- {
- /**
- * 支付成功回调处理订单
- * @param string $trade_no 订单号
- * @param string $transaction_id 微信商户号
- * @return bool
- */
- public static function handle(string $trade_no, string $transaction_id)
- {
- $order = Order::where('trade_no', $trade_no)->first();
- \Log::info('OrderPaySuccess_handle:trade_no:'.$trade_no.' transaction_id:'.$transaction_id);
- if(is_empty($order)){
- \Log::info('OrderPaySuccess_handle_error:not find by read node:trade_no:'.$trade_no.' transaction_id:'.$transaction_id);
- $appEnv = env('APP_ENV', 'production');
- $date = date("Y-m-d H:i:s");
- $msg = <<<EOF
- 项目:quick_app [$appEnv]
- 报错时间:$date
- 报错信息:- 支付回调在读库查找失败-trade_no: $trade_no;transaction_id:$transaction_id
- EOF;
- sendNotice($msg);
- $order= Order::onWriteConnection()->where('trade_no', $trade_no)->first();
- }
- if ($order) {
- $is_change = self::orderAcrossDay($order);
- //订单跨天
- $order->transaction_id = $transaction_id;
- try {
- //添加异步任务队列
- $job = new QappOrder($trade_no);
- dispatch($job->onConnection('redis_queue')->delay(5)->onQueue('{qapp_order_pay_success}'));
- } catch (Exception $e) {
- sendNotice($e->getMessage());
- }
- if ($order->status == 'PAID') {
- myLog('pay_notify')->info('has_pay:' . $trade_no);
- return true;
- }
- if ($order->order_type == 'YEAR') {
- $app = new YearOrderPaySuccess($order);
- } elseif ($order->order_type == 'BOOK') {
- $app = new BookOrderPaySuccess($order);
- } elseif ($order->order_type == 'QUARTER') {
- $app = new QuarterOrderPaySuccess($order);
- } elseif ($order->order_type == 'RECHARGE') {
- $app = new RechargeOrderPaySuccess($order);
- }
- $status = $app->success();
- if ($is_change) {
- DB::table('orders')->where('id', $order->id)->update([
- 'pay_end_at' => date('Y-m-d H:i:s', time() + 5),
- 'updated_at' => date('Y-m-d H:i:s', time() + 5),
- 'created_at' => date('Y-m-d H:i:s')
- ]);
- }
- self::freeBookStats($order->from_bid,$order->price,$order->uid);
- return $status;
- } else {
- return false;
- }
- }
- private static function freeBookStats($bid,$price,$uid){
- if(!$bid) return ;
- $free_book = BookConfigService::getByBidNoFilter($bid);
- if(!$free_book) return ;
- if(strtotime($free_book->end_time)+7*86400 < time()) return ;
- BookConfigService::chargeStats($free_book->id,$price,$uid);
- }
- private static function orderAcrossDay($order_info)
- {
- if (date('Y-m-d', strtotime($order_info->created_at)) == date('Y-m-d')) return false;
- $created_at = date('Y-m-d H:i:s', strtotime($order_info->created_at));
- $trade_no = $order_info->trade_no;
- $init_order = [
- 'distribution_channel_id' => $order_info->distribution_channel_id,
- 'uid' => $order_info->uid,
- 'product_id' => $order_info->product_id,
- 'price' => $order_info->price,
- 'pay_type' => $order_info->pay_type,
- 'trade_no' => 'cd-' . $trade_no,
- 'pay_merchant_source' => $order_info->pay_merchant_source,
- 'pay_merchant_id' => $order_info->pay_merchant_id,
- 'create_ip' => $order_info->create_ip,
- 'send_order_id' => $order_info->send_order_id,
- 'send_order_name' => $order_info->send_order_name,
- 'order_type' => $order_info->order_type,
- 'from_bid' => $order_info->from_bid,
- 'from_type' => $order_info->from_type,
- 'activity_id' => $order_info->activity_id,
- 'inner_send_order_id' => $order_info->inner_send_order_id,
- 'status' => 'UNPAID',
- 'transaction_id' => '',
- 'pay_end_at' => '0000-00-00 00:00:00',
- 'created_at' => $created_at,
- 'updated_at' => $created_at
- ];
- try {
- DB::table('orders')->insert($init_order);
- } catch (\Exception $e) {
- return false;
- }
- return true;
- }
- }
|