diff --git a/app/Http/Controllers/IndustryProductWxpay.php b/app/Http/Controllers/IndustryProductWxpay.php new file mode 100644 index 0000000..88f77e3 --- /dev/null +++ b/app/Http/Controllers/IndustryProductWxpay.php @@ -0,0 +1,131 @@ + $setting['payee_appid'], + 'mch_id' => $setting['payee_mchid'], + 'key' => $setting['payee_mchkey'], + ]; + + $app = Factory::payment($config); + try { + $response = $app->handlePaidNotify(function ($message, $fail) { + //仅测试用,回调记录 + DB::table('pay_debugs')->insert(['agent_id' => 0, 'type' => 1, 'content' => json_encode($message)]); + + // 请求成功 + if ($message['return_code'] === 'SUCCESS') { + //主要是为了区分定金支付和尾款支付,订单号带有-status后缀,分割后前面才是真正的订单号 + $order_no = explode('-', $message['out_trade_no'])[0]; + $order = IndustryOrder::query() + ->where(['order_no' => $order_no]) + ->first(); + + //已经处理过的订单直接返回true + if ($order && in_array($order->status, [OrderStatus::PAID, OrderStatus::PAID_RETAINAGE, OrderStatus::SUCCESS])) { + return true; + } + + //判断该微信支付订单号有没有处理过 + $exist_log = IndustryPayLog::where([ + 'agent_id' => $order->agent_id, + 'supplier_id' => $order->supplier_id, + 'industry_order_id' => $order->id, + 'type' => 1, + 'transaction_id' => $message['transaction_id'], + ])->first(); + if ($exist_log) { + return true; + } + + // 支付成功 + if ($message['result_code'] === 'SUCCESS') { + DB::beginTransaction(); + try { + //增加销量,库存在拍下时已经减了 + IndustryProduct::query() + ->where('id', $order->product_id) + ->increment('sale', $order->num); + + $old_status = $order->status; + $pay_type = $order->pay_type; + $money = $message['total_fee'] / 100; + //定金支付和首付款支付 + if (in_array($pay_type, [PayType::DEPOSIT_PAY, PayType::EARNEST_PAY, PayType::DOWN_PAYMENT])) { + if ($old_status == OrderStatus::UNPAID) { + $order->status = OrderStatus::PAY_EARNEST; + } else if ($old_status == OrderStatus::PAY_EARNEST) { + $order->status = OrderStatus::PAID_RETAINAGE; + $order->verify_code = uniqid(); //生成核销码 + } + } else if ($pay_type == PayType::ONLINE) { + $order->status = OrderStatus::PAID; + $order->verify_code = uniqid(); //生成核销码 + } + + $order->paid_at = now(); + $order->paid_money = DB::raw('`paid_money` + ' . $money); + $order->timeout = null; + $order->save(); + + //资金流水 + IndustryPayLog::create([ + 'user_id' => $order->user_id, + 'agent_id' => $order->agent_id, + 'money' => $money, + 'industry_order_id' => $order->id, + 'type' => 1, + 'desc' => DB::raw("LEFT('购买产品:{$order->title}', 250)"), + 'transaction_id' => $message['transaction_id'], //微信支付订单号 + 'created_at' => now(), //模型没有updated_at,无法自动写入时间 + 'out_trade_no' => $message['out_trade_no'] ?? '', + ]); + + DB::commit(); + return true; + } catch (Exception $e) { + DB::rollBack(); + $fail('Unknown error'); + } + } // 支付失败 + else if ($message['result_code'] === 'FAIL') { + return true; + } + } + + // 希望微信重试 + $fail('Unknown error 2'); + }); + } catch (InvalidSignException | Exception $e) { + return 'error' . $e->getFile() . $e->getMessage() . $e->getLine(); + } + + return $response; + } +} diff --git a/app/Models/IndustryPayLog.php b/app/Models/IndustryPayLog.php new file mode 100644 index 0000000..4bd6897 --- /dev/null +++ b/app/Models/IndustryPayLog.php @@ -0,0 +1,32 @@ +timestamps = false; + } + + public function agent() + { + return $this->belongsTo(Agent::class)->withTrashed(); + } + + public function supplier() + { + return $this->belongsTo(Supplier::class)->withTrashed(); + } + + public function industryOrder() + { + return $this->belongsTo(IndustryOrder::class); + } +}