From ae895f7d33e53a3cba0b19549229c3b150292f7d Mon Sep 17 00:00:00 2001 From: liapples Date: Thu, 29 Jul 2021 00:11:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0WxpayController=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E6=94=AF=E4=BB=98=E5=BC=82=E6=AD=A5=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=E5=99=A8=EF=BC=88=E6=9C=AA=E5=AE=8C=E6=88=90?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Api/WxpayController.php | 149 +++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 app/Http/Controllers/Api/WxpayController.php diff --git a/app/Http/Controllers/Api/WxpayController.php b/app/Http/Controllers/Api/WxpayController.php new file mode 100644 index 0000000..3491171 --- /dev/null +++ b/app/Http/Controllers/Api/WxpayController.php @@ -0,0 +1,149 @@ +handlePaidNotify(function ($message, $fail) { + // 请求成功 + if ($message['return_code'] === 'SUCCESS') { + $order = Order::query() + ->where(['order_no' => $message['out_trade_no']]) + ->first(); + + //已经处理过的订单直接返回true + if ($order && $order->paid_at) { + return true; + } + + // 支付成功 + if ($message['result_code'] === 'SUCCESS') { + DB::beginTransaction(); + try { + //TODO 需要判断尾款和全款 + $order->status = OrderStatus::PAID; + $order->paid_at = time(); + + $snowflake = resolve('snowflake'); + $confirmCode = $snowflake->id(); // TODO 这个码太长后续可以找方案搞短些 + $order->confirm_code = isset($params['platform:']) && $params['platform'] + ? $params['platform:'] . '|' . $confirmCode + : $confirmCode; + + $order->save(); + + // 计算销量 + GoodsSpecs::query() + ->where([ + 'id' => $order->goods_specs_id, + 'goods_id' => $order->goods_id, + ]) + ->update([ + 'sold_stock' => DB::raw('sold_stock+' . $order->number . ''), + ]); + + DB::commit(); + return true; + } catch (\Exception $e) { + DB::rollBack(); + $fail('Unknown error'); + } + + + } // 支付失败 + elseif ($message['result_code'] === 'FAIL') { + $order->state = UserGoodsOrder::PAID_FAIL; + $order->save(); + return true; + } + } + + // 希望微信重试 + $fail('Order not exists.'); + }); + } catch (Exception $e) { + $time = time(); + $filename = storage_path('/wxpay/notify/') . date('Y-m-d-H', $time) . '.log'; + $data = '[' . date('Y-m-d H:i:s', $time) . ']' . PHP_EOL; + $data .= '[message]:' . $e->getMessage() . PHP_EOL . PHP_EOL; + file_put_contents($filename, $data, FILE_APPEND); + } + + return $response; + } + + //退款通知 + public function refund() + { + + // {"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"} + + $config = config('wechat.payment.default'); + $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') { + $order = UserGoodsOrder::query() + ->where(['order_sn' => $reqInfo['out_trade_no']]) + ->first(); + + // 退款成功 + if ($reqInfo['refund_status'] === 'SUCCESS') { + DB::beginTransaction(); + try { + + $apply = RefundApply::query() + ->where('refund_sn', $reqInfo['out_refund_no']) + ->first(); + + if (!$apply) { + $fail('退款单没找到'); + } + + $apply->state = 2; + $apply->save(); + + $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'); + }); + + return $response; + } +}