PayJs.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. private static $config;
  10. public function __construct() {
  11. parent::__construct();
  12. self::$config = [
  13. 'mchid' => self::$sysConfig['payjs_mch_id'], // 配置商户号
  14. 'key' => self::$sysConfig['payjs_key'], // 配置通信密钥
  15. ];
  16. }
  17. public function purchase($request): JsonResponse {
  18. $payment = $this->creatNewPayment(Auth::id(), $request->input('id'), $request->input('amount'));
  19. $result = (new Pay($this::$config))->cashier([
  20. 'body' => self::$sysConfig['subject_name']?: self::$sysConfig['website_name'],
  21. 'total_fee' => $payment->amount * 100,
  22. 'out_trade_no' => $payment->trade_no,
  23. 'notify_url' => (self::$sysConfig['website_callback_url']?: self::$sysConfig['website_url']).'/callback/notify?method=payjs',
  24. ]);
  25. // 获取收款二维码内容
  26. $payment->update(['qr_code' => 1, 'url' => $result]);
  27. //$this->addPamentCallback($payment->trade_no, null, $payment->amount * 100);
  28. return Response::json(['status' => 'success', 'data' => $payment->trade_no, 'message' => '创建订单成功!']);
  29. }
  30. public function notify($request): void {
  31. $data = (new Pay($this::$config))->notify();
  32. if($data['return_code'] == 1){
  33. $payment = Payment::whereTradeNo($data['out_trade_no'])->first();
  34. if($payment){
  35. $ret = $payment->order->update(['status' => 2]);
  36. if($ret){
  37. exit('success');
  38. }
  39. }
  40. }
  41. exit('fail');
  42. }
  43. }