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

74 lines
2.5 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
  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. $openid = User::query()->where('id', $this->user_id)->value('openid'); //此处要用where,value()用find有BUG
  32. $order = Order::query()->whereIn('status', [Status::UNPAID, Status::PAY_EARNEST])->find($order_id);
  33. if (!$order) {
  34. return $this->error('订单不存在或已支付');
  35. }
  36. $config = config('wechat.payment.default');
  37. $config = array_merge($config, [
  38. 'app_id' => 'wxb35ef055a4dd8ad4',
  39. 'mch_id' => '1606181693',
  40. 'key' => 'lfyyhyz8888888888888888888888888',
  41. ]);
  42. $app = Factory::payment($config);
  43. try {
  44. $result = $app->order->unify([
  45. 'body' => $order->title,
  46. 'out_trade_no' => $order->order_no . '-' . $order->status, //后面加status,主要是为了方便微信支付回调时区分定金(首付款)和尾款支付
  47. 'total_fee' => 1, //TODO 测试暂时注释 round($price * 100), //支付金额单位为分
  48. 'notify_url' => route('wxpay_notify', ['agent_id' => $this->agent_id]), // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  49. 'trade_type' => 'JSAPI',
  50. 'openid' => $openid,
  51. // 'profit_sharing' => 'Y', //Y分账,N不分账,默认不分账,Y大写
  52. ]);
  53. } catch (InvalidArgumentException | InvalidConfigException | GuzzleException $e) {
  54. return ['error' => $e->getMessage(), 'line' => $e->getLine()];
  55. }
  56. if (empty($result['prepay_id'])) {
  57. return $result;
  58. }
  59. $jssdk = $app->jssdk;
  60. $goback_appid = Agent::where('id', $order->agent_id)->value('appid');
  61. $payConfig = $jssdk->bridgeConfig($result['prepay_id'], false) +
  62. ['id' => $order->id, 'order_no' => $order->order_no, 'appid' => $goback_appid]; // 返回数组
  63. return $this->success($payConfig);
  64. }
  65. }