PayJs.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Utils\Payments;
  3. use App\Services\PaymentService;
  4. use App\Utils\Library\Templates\Gateway;
  5. use Auth;
  6. use Illuminate\Http\JsonResponse;
  7. use Illuminate\Http\Request;
  8. use Log;
  9. use Response;
  10. use Xhat\Payjs\Payjs as Pay;
  11. class PayJs extends PaymentService implements Gateway
  12. {
  13. private static array $config;
  14. public function __construct()
  15. {
  16. self::$config = [
  17. 'mchid' => sysConfig('payjs_mch_id'), // 配置商户号
  18. 'key' => sysConfig('payjs_key'), // 配置通信密钥
  19. ];
  20. }
  21. public function purchase(Request $request): JsonResponse
  22. {
  23. $payment = $this->createPayment(Auth::id(), $request->input('id'), $request->input('amount'));
  24. $result = (new Pay($this::$config))->cashier([
  25. 'body' => sysConfig('subject_name') ?: sysConfig('website_name'),
  26. 'total_fee' => $payment->amount * 100,
  27. 'out_trade_no' => $payment->trade_no,
  28. 'notify_url' => route('payment.notify', ['method' => 'payjs']),
  29. ]);
  30. // 获取收款二维码内容
  31. $payment->update(['qr_code' => 1, 'url' => $result]);
  32. //$this->addPamentCallback($payment->trade_no, null, $payment->amount * 100);
  33. return Response::json(['status' => 'success', 'data' => $payment->trade_no, 'message' => trans('user.payment.order_creation.success')]);
  34. }
  35. public function notify(Request $request): void
  36. {
  37. $data = (new Pay($this::$config))->notify();
  38. if ($data['return_code'] == 1) {
  39. if ($this->paymentReceived($data['out_trade_no'])) {
  40. exit('success');
  41. }
  42. } else {
  43. Log::error('【PayJs】交易失败:'.var_export($data, true));
  44. }
  45. exit('fail');
  46. }
  47. }