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

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