海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.1 KiB

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Common\OrderStatus;
  4. use App\Common\PayType;
  5. use App\Models\AdminSetting;
  6. use App\Models\IndustryOrder;
  7. use App\Models\IndustryPayLog;
  8. use App\Models\IndustryProduct;
  9. use EasyWeChat\Factory;
  10. use EasyWeChat\Kernel\Exceptions\Exception;
  11. use EasyWeChat\Payment\Kernel\Exceptions\InvalidSignException;
  12. use Illuminate\Support\Facades\DB;
  13. /**
  14. * 行业产品微信支付回调
  15. * Class IndustryProductWxpay
  16. * @package App\Http\Controllers
  17. */
  18. class IndustryProductWxpay
  19. {
  20. public function notify()
  21. {
  22. $setting = AdminSetting::val(['payee_appid', 'payee_mchid', 'payee_mchkey']);
  23. if (!isset($setting['payee_appid'], $setting['payee_mchid'], $setting['payee_mchkey'])) {
  24. return '获取系统配置失败';
  25. }
  26. $config = [
  27. 'app_id' => $setting['payee_appid'],
  28. 'mch_id' => $setting['payee_mchid'],
  29. 'key' => $setting['payee_mchkey'],
  30. ];
  31. $app = Factory::payment($config);
  32. try {
  33. $response = $app->handlePaidNotify(function ($message, $fail) {
  34. //仅测试用,回调记录
  35. DB::table('pay_debugs')->insert(['agent_id' => 0, 'type' => 1, 'content' => json_encode($message)]);
  36. // 请求成功
  37. if ($message['return_code'] === 'SUCCESS') {
  38. //主要是为了区分定金支付和尾款支付,订单号带有-status后缀,分割后前面才是真正的订单号
  39. $order_no = explode('-', $message['out_trade_no'])[0];
  40. $order = IndustryOrder::query()
  41. ->where(['order_no' => $order_no])
  42. ->first();
  43. //已经处理过的订单直接返回true
  44. if ($order && in_array($order->status, [OrderStatus::PAID, OrderStatus::PAID_RETAINAGE, OrderStatus::SUCCESS])) {
  45. return true;
  46. }
  47. //判断该微信支付订单号有没有处理过
  48. $exist_log = IndustryPayLog::where([
  49. 'agent_id' => $order->agent_id,
  50. 'supplier_id' => $order->supplier_id,
  51. 'industry_order_id' => $order->id,
  52. 'type' => 1,
  53. 'transaction_id' => $message['transaction_id'],
  54. ])->first();
  55. if ($exist_log) {
  56. return true;
  57. }
  58. // 支付成功
  59. if ($message['result_code'] === 'SUCCESS') {
  60. DB::beginTransaction();
  61. try {
  62. //增加销量,库存在拍下时已经减了
  63. IndustryProduct::query()
  64. ->where('id', $order->product_id)
  65. ->increment('sale', $order->num);
  66. $old_status = $order->status;
  67. $pay_type = $order->pay_type;
  68. $money = $message['total_fee'] / 100;
  69. //定金支付和首付款支付
  70. if (in_array($pay_type, [PayType::DEPOSIT_PAY, PayType::EARNEST_PAY, PayType::DOWN_PAYMENT])) {
  71. if ($old_status == OrderStatus::UNPAID) {
  72. $order->status = OrderStatus::PAY_EARNEST;
  73. } else if ($old_status == OrderStatus::PAY_EARNEST) {
  74. $order->status = OrderStatus::PAID_RETAINAGE;
  75. $order->verify_code = uniqid(); //生成核销码
  76. }
  77. } else if ($pay_type == PayType::ONLINE) {
  78. $order->status = OrderStatus::PAID;
  79. $order->verify_code = uniqid(); //生成核销码
  80. }
  81. $order->paid_at = now();
  82. $order->paid_money = DB::raw('`paid_money` + ' . $money);
  83. $order->timeout = null;
  84. $order->save();
  85. //资金流水
  86. IndustryPayLog::create([
  87. 'user_id' => $order->user_id,
  88. 'agent_id' => $order->agent_id,
  89. 'money' => $money,
  90. 'industry_order_id' => $order->id,
  91. 'type' => 1,
  92. 'desc' => DB::raw("LEFT('购买产品:{$order->title}', 250)"),
  93. 'transaction_id' => $message['transaction_id'], //微信支付订单号
  94. 'created_at' => now(), //模型没有updated_at,无法自动写入时间
  95. 'out_trade_no' => $message['out_trade_no'] ?? '',
  96. ]);
  97. DB::commit();
  98. return true;
  99. } catch (Exception $e) {
  100. DB::rollBack();
  101. $fail('Unknown error');
  102. }
  103. } // 支付失败
  104. else if ($message['result_code'] === 'FAIL') {
  105. return true;
  106. }
  107. }
  108. // 希望微信重试
  109. $fail('Unknown error 2');
  110. });
  111. } catch (InvalidSignException | Exception $e) {
  112. return 'error' . $e->getFile() . $e->getMessage() . $e->getLine();
  113. }
  114. return $response;
  115. }
  116. }