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

118 lines
3.9 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Common\OrderStatus as Status;
  4. use App\Common\PayType;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\Agent;
  7. use App\Models\AgentProduct;
  8. use App\Models\Order;
  9. use App\Models\User;
  10. use EasyWeChat\Factory;
  11. use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
  12. use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
  13. use GuzzleHttp\Exception\GuzzleException;
  14. use Illuminate\Support\Facades\Cache;
  15. /**
  16. * 跳转支付
  17. * Class SharePay
  18. * @package App\Http\Controllers\Api
  19. */
  20. class SharePayController extends Controller
  21. {
  22. public function pay()
  23. {
  24. $order_id = request()->input('id');
  25. if (!$order_id) {
  26. return $this->error('无效的ID');
  27. }
  28. $this->user_id = Cache::get(request()->header('Authentication'));
  29. if (!$this->user_id) {
  30. return $this->error('获取用户信息失败');
  31. }
  32. //用户openid
  33. $user_info = User::query()->where('id', $this->user_id)->first(['agent_id', 'openid']); //此处要用where,value()用find有BUG
  34. $openid = $user_info['openid'];
  35. $this->agent_id = $user_info['agent_id'];
  36. $order = Order::with(['agentProduct'])
  37. ->whereRaw('`timeout` >= NOW()')
  38. ->whereIn('status', [Status::UNPAID, Status::PAY_EARNEST])
  39. ->find($order_id);
  40. if (!$order) {
  41. return $this->error('订单已支付或已超时');
  42. }
  43. $order->pay_user_id = $this->user_id;
  44. $order->save();
  45. //如果已经付定金或首付款,则仅支付尾款
  46. if ($order->status == Status::PAY_EARNEST) {
  47. $price = $order->price - $order->paid_money;
  48. } else {
  49. $price = $this->calc($order->price, $order->num, $order->pay_type, $order->agentProduct);
  50. }
  51. $config = config('wechat.payment.default');
  52. $config = array_merge($config, [
  53. 'app_id' => 'wxb35ef055a4dd8ad4',
  54. 'mch_id' => '1606181693',
  55. 'key' => 'lfyyhyz8888888888888888888888888',
  56. ]);
  57. $app = Factory::payment($config);
  58. try {
  59. $result = $app->order->unify([
  60. 'body' => $order->title,
  61. 'out_trade_no' => $order->order_no . '-' . $order->status, //后面加status,主要是为了方便微信支付回调时区分定金(首付款)和尾款支付
  62. 'total_fee' => round($price * 100), //支付金额单位为分
  63. 'notify_url' => route('wxpay_notify', ['agent_id' => $this->agent_id]), // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  64. 'trade_type' => 'JSAPI',
  65. 'openid' => $openid,
  66. 'attach' => $this->user_id, //保存支付用户的user_id
  67. // 'profit_sharing' => 'Y', //Y分账,N不分账,默认不分账,Y大写
  68. ]);
  69. } catch (InvalidArgumentException | InvalidConfigException | GuzzleException $e) {
  70. return ['error' => $e->getMessage(), 'line' => $e->getLine()];
  71. }
  72. if (empty($result['prepay_id'])) {
  73. return $result;
  74. }
  75. $jump_appid = Agent::where('id', $order->agent_id)->value('appid');
  76. $jssdk = $app->jssdk;
  77. $payConfig = $jssdk->bridgeConfig($result['prepay_id'], false) +
  78. ['id' => $order->id, 'order_no' => $order->order_no, 'jump_appid' => $jump_appid]; // 返回数组
  79. return $this->success($payConfig);
  80. }
  81. /**
  82. * 计算最终价格
  83. * @param float $price 订单价格
  84. * @param float $num 购买数量
  85. * @param int $pay_type 支付方式
  86. * @param AgentProduct $agent_product 代理商产品
  87. * @return float
  88. */
  89. private function calc(float $price, float $num, int $pay_type, AgentProduct $agent_product): float
  90. {
  91. /** 修改需要同步修改Order里面的 */
  92. //根据支付方式计算价格
  93. if (in_array($pay_type, [PayType::DEPOSIT_PAY, PayType::EARNEST_PAY, PayType::DOWN_PAYMENT])) {
  94. if ($pay_type == PayType::DEPOSIT_PAY && $agent_product->deposit && $agent_product->deposit_timeout) {
  95. return $agent_product->deposit;
  96. } else if ($pay_type == PayType::EARNEST_PAY && $agent_product->earnest && $agent_product->earnest_timeout) {
  97. return $agent_product->earnest;
  98. }
  99. }
  100. $total_price = $price * $num;
  101. return round($total_price, 2);
  102. }
  103. }