diff --git a/app/Http/Controllers/Api/OrderController.php b/app/Http/Controllers/Api/OrderController.php index 17c6e5f..2bc976f 100644 --- a/app/Http/Controllers/Api/OrderController.php +++ b/app/Http/Controllers/Api/OrderController.php @@ -157,7 +157,7 @@ class OrderController extends Controller if ($formData['pay_type'] == 3) { //线下支付 return $this->success('操作成功,请及时联系客服线下支付'); } else { //在线支付或定金支付 - $config = $this->payConfig($order->title, $order->order_no, $order->pay_type, $price); + $config = $this->payConfig($order->title, $order->order_no, $order->status, $price); if (!empty($config['paySign'])) { return $this->success($config); } else { @@ -188,7 +188,7 @@ class OrderController extends Controller //TODO 需要后台处理 $order = Order::find($formData['id']); - if (!in_array($order->status, [Status::PAY_EARNEST, Status::PAID, Status::PAY_RETAINAGE])) { + if (!in_array($order->status, [Status::PAY_EARNEST, Status::PAID, Status::PAID_RETAINAGE])) { return $this->error('当前订单状态不允许退款'); } $order->status = Status::REFUNDING; @@ -253,7 +253,7 @@ class OrderController extends Controller $price = $this->calc($order['price'], $coupon, $order['num'], $order['pay_type']); } - $config = $this->payConfig($order['title'], $order['order_no'], $order['pay_type'], $price); + $config = $this->payConfig($order['title'], $order['order_no'], $order['status'], $price); if (!empty($config['paySign'])) { return $this->success($config); } else { @@ -262,7 +262,7 @@ class OrderController extends Controller } //获取支付配置信息 - private function payConfig($title, $order_no, $pay_type, $price) + private function payConfig($title, $order_no, $status, $price) { //用户openid $openid = User::query()->where('id', $this->user_id)->value('openid'); //此处要用where,value()用find有BUG @@ -280,9 +280,9 @@ class OrderController extends Controller $app = Factory::payment($config); $result = $app->order->unify([ 'body' => $title, - 'out_trade_no' => $order_no . '-' . $pay_type, //后面加pay_type,主要是为了方便微信支付回调时区分定金(首付款)和尾款支付 + 'out_trade_no' => $order_no . '-' . $status, //后面加status,主要是为了方便微信支付回调时区分定金(首付款)和尾款支付 'total_fee' => round($price * 100), //支付金额单位为分 - 'notify_url' => route('wxpay_notify', ['aid' => $this->agent_id]), // 支付结果通知网址,如果不设置则会使用配置里的默认地址 + 'notify_url' => route('wxpay_notify', ['agent_id' => $this->agent_id]), // 支付结果通知网址,如果不设置则会使用配置里的默认地址 'trade_type' => 'JSAPI', 'openid' => $openid, ]); diff --git a/app/Http/Controllers/Api/VerificationController.php b/app/Http/Controllers/Api/VerificationController.php index 00d0354..60b4628 100644 --- a/app/Http/Controllers/Api/VerificationController.php +++ b/app/Http/Controllers/Api/VerificationController.php @@ -22,7 +22,7 @@ class VerificationController extends Controller if (!$order) { return $this->error('订单不存在或无权限'); } - if (!in_array($order->status, [OrderStatus::PAID, OrderStatus::PAY_RETAINAGE, OrderStatus::OFFLINE_UNPAID])) { + if (!in_array($order->status, [OrderStatus::PAID, OrderStatus::PAID_RETAINAGE, OrderStatus::OFFLINE_UNPAID])) { return $this->error('当前订单状态不允许核销'); } $order->status = 16; diff --git a/app/Http/Controllers/Api/WxpayController.php b/app/Http/Controllers/Api/WxpayController.php index d668884..9fbe692 100644 --- a/app/Http/Controllers/Api/WxpayController.php +++ b/app/Http/Controllers/Api/WxpayController.php @@ -1,7 +1,6 @@ route('aid'); + $agent_id = request()->route('agent_id'); $agent = Agent::find($agent_id); $config = config('wechat.payment.default'); @@ -41,7 +40,7 @@ class WxpayController ->first(); //已经处理过的订单直接返回true - if ($order && in_array($order->status, [OrderStatus::PAID, OrderStatus::PAY_RETAINAGE])) { + if ($order && in_array($order->status, [OrderStatus::PAID, OrderStatus::PAID_RETAINAGE])) { return true; } @@ -56,7 +55,7 @@ class WxpayController if ($pay_type == OrderStatus::UNPAID) { $order->status = OrderStatus::PAY_EARNEST; } else if ($status == OrderStatus::PAY_EARNEST) { - $order->status = OrderStatus::PAY_RETAINAGE; + $order->status = OrderStatus::PAID_RETAINAGE; } } else if ($pay_type == 0) { $order->status = OrderStatus::PAID; @@ -88,7 +87,7 @@ class WxpayController DB::commit(); return true; - } catch (\Exception $e) { + } catch (Exception $e) { DB::rollBack(); $fail('Unknown error'); } @@ -101,7 +100,7 @@ class WxpayController // 希望微信重试 $fail('Order not exists.'); }); - } catch (InvalidSignException | \Exception $e) { + } catch (InvalidSignException | Exception $e) { $time = time(); $filename = storage_path('logs/wxpay_notify_') . date('Y-m-d-H', $time) . '.log'; $data = '[' . date('Y-m-d H:i:s', $time) . ']' . PHP_EOL; @@ -119,59 +118,71 @@ class WxpayController // {"sign": "3F99E6044C503B0E0131F95A1410B630", "appid": "wxb35ef055a4dd8ad4", "mch_id": "1606181693", "openid": "oBYj55W0gLv5MYUnsYUuJfzYzmsg", "cash_fee": "1", "fee_type": "CNY", "time_end": "20210623222330", "bank_type": "OTHERS", "nonce_str": "60d343d820e94", "total_fee": "1", "trade_type": "JSAPI", "result_code": "SUCCESS", "return_code": "SUCCESS", "is_subscribe": "N", "out_trade_no": "2842908479209865216", "transaction_id": "4200001210202106237487333085"} + $agent_id = request()->route('agent_id'); + $agent = Agent::find($agent_id); + $config = config('wechat.payment.default'); + $config = array_merge($config, [ + 'app_id' => $agent->appid, + 'mch_id' => $agent->mchid, + 'key' => $agent->mchkey, + ]); $app = Factory::payment($config); - $response = $app->handleRefundedNotify(function ($message, $reqInfo, $fail) { - // 记录一下本地调试 - file_put_contents(storage_path('/wxpay/refund'.date('Y-m-d-H').'.log'), date('Y-m-d H:i:s').PHP_EOL.json_encode($message).PHP_EOL.json_encode($reqInfo), FILE_APPEND); - // 请求成功 - if ($message['return_code'] === 'SUCCESS') { - //订单号带有pay_type后缀,主要是为了区分定金支付和尾款支付,前24位才是真正的订单号 - $order_no = substr($message['out_trade_no'], 0, 24); - $order = Order::query() - ->where(['order_sn' => $order_no]) - ->first(); - - // 退款成功 - if ($reqInfo['refund_status'] === 'SUCCESS') { - DB::beginTransaction(); - try { - - $apply = RefundApply::query() - ->where('refund_sn', $reqInfo['out_refund_no']) - ->first(); - - if (!$apply) { - $fail('退款单没找到'); - } + try { + $response = $app->handleRefundedNotify(function ($message, $reqInfo, $fail) { + // 记录一下本地调试 + file_put_contents(storage_path('/wxpay/refund' . date('Y-m-d-H') . '.log'), date('Y-m-d H:i:s') . PHP_EOL . json_encode($message) . PHP_EOL . json_encode($reqInfo), FILE_APPEND); + // 请求成功 + if ($message['return_code'] === 'SUCCESS') { + //订单号带有pay_type后缀,主要是为了区分定金支付和尾款支付,前24位才是真正的订单号 + $order_no = substr($message['out_trade_no'], 0, 24); + $order = Order::query() + ->where(['order_sn' => $order_no]) + ->first(); + + // 退款成功 + if ($reqInfo['refund_status'] === 'SUCCESS') { + DB::beginTransaction(); + try { - $apply->state = 2; - $apply->save(); + $apply = RefundApply::query() + ->where('refund_sn', $reqInfo['out_refund_no']) + ->first(); - $order->state = UserGoodsOrder::REFUNDED; - $order->save(); + if (!$apply) { + $fail('退款单没找到'); + } - // 退库存 - GoodsSpecs::query() - ->where([ - 'id' => $order->goods_specs_id, - 'goods_id' => $order->goods_id, - ]) - ->update([ - 'stock' => DB::raw('stock+'.$order->number.''), - ]); + $apply->state = 2; + $apply->save(); - DB::commit(); - return true; - } catch (\Exception $e) { - DB::rollBack(); - $fail('Unknown error'); + $order->state = UserGoodsOrder::REFUNDED; + $order->save(); + + // 退库存 + GoodsSpecs::query() + ->where([ + 'id' => $order->goods_specs_id, + 'goods_id' => $order->goods_id, + ]) + ->update([ + 'stock' => DB::raw('stock+' . $order->number . ''), + ]); + + DB::commit(); + return true; + } catch (\Exception $e) { + DB::rollBack(); + $fail('Unknown error'); + } } } - } - $fail('Unknown error'); - }); + $fail('Unknown error'); + }); + } catch (Exception $e) { + return 'error'; + } return $response; } diff --git a/app/Models/Order.php b/app/Models/Order.php index 3cc2184..0ae3edb 100644 --- a/app/Models/Order.php +++ b/app/Models/Order.php @@ -29,7 +29,7 @@ class Order extends BaseModel Status::UNPAID => '待付款', Status::PAY_EARNEST => '已付定金', Status::PAID => '已付款', - Status::PAY_RETAINAGE => '已付尾款', + Status::PAID_RETAINAGE => '已付尾款', Status::REFUNDING => '退款中', Status::REFUNDED => '已退款', Status::SUCCESS => '已完成', diff --git a/app/Service/OrderStatus.php b/app/Service/OrderStatus.php index 8cdd1c7..f666111 100644 --- a/app/Service/OrderStatus.php +++ b/app/Service/OrderStatus.php @@ -13,7 +13,7 @@ class OrderStatus const UNPAID = 0; //待付款 const PAY_EARNEST = 1; //已付定金 const PAID = 2; //已付全款 - const PAY_RETAINAGE = 3; //已付尾款 + const PAID_RETAINAGE = 3; //已付尾款 const OFFLINE_UNPAID= 4; //线下支付未付款 //const OFFLINE_PAID = 5; //线下支付已收款 const REFUNDING = 6; //退款中 diff --git a/routes/api.php b/routes/api.php index a888156..6210d69 100644 --- a/routes/api.php +++ b/routes/api.php @@ -67,8 +67,8 @@ Route::namespace('App\Http\Controllers\Api')->group(function () { # 微信支付 Route::prefix('wxpay')->group(function () { - Route::post('notify/{aid}', 'WxpayController@notify')->name('wxpay_notify'); //异步通知,aid为代理商ID - Route::post('refund/{aid}', 'WxpayController@refund')->name('wxpay_refund'); //退款通知,aid为代理商ID + Route::post('notify/{agent_id}', 'WxpayController@notify')->name('wxpay_notify'); //异步通知,aid为代理商ID + Route::post('refund/{agent_id}', 'WxpayController@refund')->name('wxpay_refund'); //退款通知,aid为代理商ID }); # 仅用于测试