diff --git a/app/Endpoint/Callback/Http/Controllers/WeChatPayController.php b/app/Endpoint/Callback/Http/Controllers/WeChatPayController.php index 4af543a5..5e3c4e71 100644 --- a/app/Endpoint/Callback/Http/Controllers/WeChatPayController.php +++ b/app/Endpoint/Callback/Http/Controllers/WeChatPayController.php @@ -2,7 +2,14 @@ namespace App\Endpoint\Callback\Http\Controllers; +use App\Exceptions\BizException; +use App\Models\Order; +use App\Services\OrderService; use App\Services\WeChatPayService; +use Illuminate\Database\Eloquent\ModelNotFoundException; +use Illuminate\Support\Carbon; +use Illuminate\Support\Facades\DB; +use Throwable; class WeChatPayController extends Controller { @@ -10,12 +17,50 @@ class WeChatPayController extends Controller * 直接结果通知 * * @param \App\Services\WeChatPayService $request + * @param \App\Services\WeChatPayService $request * @return \Illuminate\Http\Response */ public function paidNotify(WeChatPayService $weChatPayService) { - $weChatPayService->handlePaidNotify(function ($message, $fail) { - logger()->debug('wechat notify', $message); + return $weChatPayService->handlePaidNotify(function ($message, $fail) { + // 通信失败 + if (data_get($message, 'return_code') !== 'SUCCESS') { + return $fail('通信失败'); + } + + // 支付失败 + if (data_get($message, 'result_code') !== 'SUCCESS') { + return true; + } + + $attach = json_decode(data_get($message, 'attach'), true); + + if (! isset($attach['pay_target'])) { + return true; + } + + switch ($attach['pay_target']) { + // 支付订单 + case 'order': + try { + DB::beginTransaction(); + + (new OrderService())->paySuccess($message['out_trade_no'], [ + 'pay_sn' => $message['transaction_id'], + 'pay_way' => Order::PAY_WAY_WXPAY, + 'pay_at' => Carbon::parse($message['time_end']), + ]); + + DB::commit(); + } catch (BizException|ModelNotFoundException $e) { + DB::rollBack(); + } catch (Throwable $e) { + DB::rollBack(); + + throw $e; + } + break; + } return true; }); diff --git a/app/Models/Order.php b/app/Models/Order.php index c30f792f..019456de 100644 --- a/app/Models/Order.php +++ b/app/Models/Order.php @@ -13,15 +13,26 @@ class Order extends Model use Filterable; use HasDateTimeFormatter; + /** + * 订单状态 + */ public const STATUS_PENDING = 0; // 待付款 public const STATUS_PAID = 1; // 已付款 public const STATUS_COMPLETED = 9; // 已完成 public const STATUS_CANCELLED = 10; // 已取消 + /** + * 发货状态 + */ public const SHIPPING_STATE_PENDING = 0; // 待发货 public const SHIPPING_STATE_PROCESSING = 1; // 发货中 public const SHIPPING_STATE_PROCESSED = 2; // 已完成 + /** + * 支付方式 + */ + public const PAY_WAY_WXPAY = 'wxpay'; // 微信支付 + /** * @var array */ diff --git a/app/Services/OrderService.php b/app/Services/OrderService.php index 73a8b369..014a6778 100644 --- a/app/Services/OrderService.php +++ b/app/Services/OrderService.php @@ -696,4 +696,29 @@ class OrderService } } } + + /* + * 支付成功 + * + * @param string $sn + * @param array $params + * @return void + */ + public function paySuccess(string $sn, array $params = []): void + { + $order = Order::where('sn', $sn)->findOrFail(); + + if (! $order->isPending()) { + throw new BizException('订单状态不是待支付'); + } + + $order->update([ + 'pay_sn' => $params['pay_sn'], + 'pay_way' => $params['pay_way'], + 'pay_at' => $params['pay_at'], + 'status' => Order::STATUS_PAID, + ]); + + // todo 处理预收益 + } }