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

64 lines
2.1 KiB

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