海南旅游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.

143 lines
4.2 KiB

  1. <?php
  2. namespace App\AdminSettled\Common;
  3. use App\Models\AdminSetting;
  4. use App\Models\Agent;
  5. use App\Models\Guide;
  6. use App\Models\SettledOrder;
  7. use App\Models\Supplier;
  8. use EasyWeChat\Factory;
  9. /**
  10. * 入驻支付
  11. * Class SettledPay
  12. * @package App\AdminSettled\Common
  13. */
  14. class SettledPay
  15. {
  16. /**
  17. * 获取支付回调JS
  18. * @param $model
  19. * @return string
  20. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  21. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  22. * @throws \GuzzleHttp\Exception\GuzzleException
  23. */
  24. public function PayJs($model): string
  25. {
  26. if ($model instanceof Supplier) {
  27. $type = 'supplier';
  28. } else if ($model instanceof Agent) {
  29. $type = 'agent';
  30. } else if ($model instanceof Guide) {
  31. $type = 'guide';
  32. } else {
  33. return "Dcat.swal.error('无效的商家类型', null);";
  34. }
  35. $cost = AdminSetting::val("settled_{$type}_cost"); //入驻费
  36. $pay = $this->payConfig($model, $cost, ['supplier' => 1, 'agent' => 2, 'guide' => 3][$type]);
  37. if (empty($pay['code_url'])) {
  38. if (isset($pay['result_code'], $pay['err_code_des']) && $pay['result_code'] != 'SUCCESS') {
  39. $msg = $pay['err_code_des'];
  40. } else {
  41. $msg = $pay['return_msg'] ?? '获取支付信息失败';
  42. }
  43. $js = "Dcat.swal.info('支付:$msg', null);";
  44. } else {
  45. $ajax_url = admin_url('is_pay');
  46. $js = <<<JS
  47. Dcat.swal.info(
  48. '<div style="margin-top:1rem;"><div id="qrcode"></div><p style="text-align:center;">入驻费:¥{$cost}元</p></div>',
  49. '<b style="color:red">请微信扫码支付,请勿关闭页面</b>',
  50. {
  51. type: null,
  52. imageWidth: 240,
  53. imageHeight: 240,
  54. animation: false,
  55. // confirmButtonText: '我已完成支付,刷新',
  56. showConfirmButton: false,
  57. allowOutsideClick: false,
  58. allowEscapeKey: false,
  59. onOpen: function () {
  60. $('#qrcode').qrcode({text:'{$pay["code_url"]}', width:240, height:240});
  61. if (window.timer) {
  62. clearInterval(window.timer);
  63. }
  64. window.timer = setInterval(function () {
  65. $.ajax({
  66. url: '$ajax_url',
  67. data: {
  68. username: '{$model->username}',
  69. type: '{$type}',
  70. },
  71. success: function (res) {
  72. if (res == 1) {
  73. clearInterval(window.timer);
  74. Dcat.swal.success('支付成功,请联系平台审核!', null, {
  75. onClose: function () {
  76. window.location.reload();
  77. }
  78. }).then(() => {
  79. window.location.reload();
  80. });
  81. }
  82. }
  83. });
  84. }, 1000);
  85. }
  86. });
  87. JS;
  88. }
  89. return $js;
  90. }
  91. /**
  92. * 获取支付配置
  93. * @param $model
  94. * @param $cost
  95. * @param $user_type
  96. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  97. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  98. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  99. * @throws \GuzzleHttp\Exception\GuzzleException
  100. */
  101. private function PayConfig($model, $cost, $user_type)
  102. {
  103. $setting = AdminSetting::val(['payee_appid', 'payee_mchid', 'payee_mchkey']);
  104. $config = [
  105. 'app_id' => $setting['payee_appid'],
  106. 'mch_id' => $setting['payee_mchid'],
  107. 'key' => $setting['payee_mchkey'],
  108. 'notify_url' => route('wxpay_settled_notify'),
  109. ];
  110. $app = Factory::payment($config);
  111. //生成订单号
  112. list($micro, $sec) = explode(' ', microtime());
  113. $micro = str_pad(floor($micro * 1000000), 6, 0, STR_PAD_LEFT);
  114. $order_no = date('ymdHis', $sec) . $micro . mt_rand(1000, 9999);
  115. //保存订单记录
  116. SettledOrder::insertOrIgnore([
  117. 'order_no' => $order_no,
  118. 'user_type' => $user_type,
  119. 'username' => $model->username,
  120. 'money' => $cost,
  121. 'status' => 0,
  122. 'created_at' => now(),
  123. 'updated_at' => now(),
  124. ]);
  125. return $app->order->unify([
  126. 'product_id' => $model->id,
  127. 'attach' => $model->username,
  128. 'body' => mb_strcut($model->company_name . ' 入驻易游平台', 0, 127),
  129. 'out_trade_no' => $order_no,
  130. 'total_fee' => round($cost * 100), //支付金额单位为分
  131. 'trade_type' => 'NATIVE', // 请对应换成你的支付方式对应的值类型
  132. ]);
  133. }
  134. }