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

80 lines
2.7 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
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Common\OrderStatus as Status;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\Agent;
  6. use App\Models\Order;
  7. use App\Models\User;
  8. use EasyWeChat\Factory;
  9. use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
  10. use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
  11. use GuzzleHttp\Exception\GuzzleException;
  12. use Illuminate\Support\Facades\Cache;
  13. /**
  14. * 跳转支付
  15. * Class SharePay
  16. * @package App\Http\Controllers\Api
  17. */
  18. class SharePayController extends Controller
  19. {
  20. public function pay()
  21. {
  22. $order_id = request()->input('id');
  23. if (!$order_id) {
  24. return $this->error('无效的ID');
  25. }
  26. $this->user_id = Cache::get(request()->header('Authentication'));
  27. if (!$this->user_id) {
  28. return $this->error('获取用户信息失败');
  29. }
  30. //用户openid
  31. $user_info = User::query()->where('id', $this->user_id)->first(['agent_id', 'openid']); //此处要用where,value()用find有BUG
  32. $openid = $user_info['openid'];
  33. $this->agent_id = $user_info['agent_id'];
  34. $order = Order::query()->whereIn('status', [Status::UNPAID, Status::PAY_EARNEST])->find($order_id);
  35. if (!$order) {
  36. return $this->error('订单不存在或已支付');
  37. }
  38. $order->pay_user_id = $this->user_id;
  39. $order->save();
  40. $config = config('wechat.payment.default');
  41. $config = array_merge($config, [
  42. 'app_id' => 'wxb35ef055a4dd8ad4',
  43. 'mch_id' => '1606181693',
  44. 'key' => 'lfyyhyz8888888888888888888888888',
  45. ]);
  46. $app = Factory::payment($config);
  47. try {
  48. $result = $app->order->unify([
  49. 'body' => $order->title,
  50. 'out_trade_no' => $order->order_no . '-' . $order->status, //后面加status,主要是为了方便微信支付回调时区分定金(首付款)和尾款支付
  51. 'total_fee' => 1, //TODO 测试暂时注释 round($price * 100), //支付金额单位为分
  52. 'notify_url' => route('wxpay_notify', ['agent_id' => $this->agent_id]), // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  53. 'trade_type' => 'JSAPI',
  54. 'openid' => $openid,
  55. 'attach' => $this->user_id, //保存支付用户的user_id
  56. // 'profit_sharing' => 'Y', //Y分账,N不分账,默认不分账,Y大写
  57. ]);
  58. } catch (InvalidArgumentException | InvalidConfigException | GuzzleException $e) {
  59. return ['error' => $e->getMessage(), 'line' => $e->getLine()];
  60. }
  61. if (empty($result['prepay_id'])) {
  62. return $result;
  63. }
  64. $jssdk = $app->jssdk;
  65. $goback_appid = Agent::where('id', $order->agent_id)->value('appid');
  66. $payConfig = $jssdk->bridgeConfig($result['prepay_id'], false) +
  67. ['id' => $order->id, 'order_no' => $order->order_no, 'jump_appid' => $goback_appid]; // 返回数组
  68. return $this->success($payConfig);
  69. }
  70. }