PayJs.php 1.6 KB

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