PayJs.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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(
  21. Auth::id(),
  22. $request->input('id'),
  23. $request->input('amount')
  24. );
  25. $result = (new Pay($this::$config))->cashier(
  26. [
  27. 'body' => sysConfig('subject_name') ?: sysConfig(
  28. 'website_name'
  29. ),
  30. 'total_fee' => $payment->amount * 100,
  31. 'out_trade_no' => $payment->trade_no,
  32. 'notify_url' => (sysConfig(
  33. 'website_callback_url'
  34. ) ?: sysConfig(
  35. 'website_url'
  36. )) . '/callback/notify?method=payjs',
  37. ]
  38. );
  39. // 获取收款二维码内容
  40. $payment->update(['qr_code' => 1, 'url' => $result]);
  41. //$this->addPamentCallback($payment->trade_no, null, $payment->amount * 100);
  42. return Response::json(
  43. [
  44. 'status' => 'success',
  45. 'data' => $payment->trade_no,
  46. 'message' => '创建订单成功!',
  47. ]
  48. );
  49. }
  50. public function notify($request): void
  51. {
  52. $data = (new Pay($this::$config))->notify();
  53. if ($data['return_code'] == 1) {
  54. $payment = Payment::whereTradeNo($data['out_trade_no'])->first();
  55. if ($payment) {
  56. $ret = $payment->order->update(['status' => 2]);
  57. if ($ret) {
  58. exit('success');
  59. }
  60. }
  61. }
  62. exit('fail');
  63. }
  64. }