PayJs.php 1.6 KB

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