PayJs.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Http\Controllers\Gateway;
  3. use App\Models\Payment;
  4. use Auth;
  5. use Illuminate\Http\JsonResponse;
  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. $payment = Payment::whereTradeNo($data['out_trade_no'])->first();
  37. if ($payment) {
  38. $ret = $payment->order->update(['status' => 2]);
  39. if ($ret) {
  40. exit('success');
  41. }
  42. }
  43. }
  44. exit('fail');
  45. }
  46. }