PayJs.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace App\Http\Controllers\Gateway;
  3. use App\Http\Models\Payment;
  4. use Auth;
  5. use Log;
  6. use Response;
  7. use Xhat\Payjs\Payjs as Pay;
  8. class PayJs extends AbstractPayment
  9. {
  10. private static $config;
  11. function __construct()
  12. {
  13. parent::__construct();
  14. self::$config = [
  15. 'mchid' => self::$systemConfig['payjs_mch_id'], // 配置商户号
  16. 'key' => self::$systemConfig['payjs_key'], // 配置通信密钥
  17. ];
  18. }
  19. public function purchase($request)
  20. {
  21. $payment = new Payment();
  22. $payment->sn = self::generateGuid();
  23. $payment->user_id = Auth::user()->id;
  24. $payment->oid = $request->input('oid');
  25. $payment->amount = $request->input('amount');
  26. $payment->save();
  27. $result = (new Pay($this::$config))->native([
  28. 'body' => parent::$systemConfig['subject_name']? : parent::$systemConfig['website_name'],
  29. 'total_fee' => $payment->amount*100,
  30. 'out_trade_no' => $payment->sn,
  31. 'attach' => '',
  32. 'notify_url' => (parent::$systemConfig['website_callback_url']? : parent::$systemConfig['website_url']).'/callback/notify?method=payjs',
  33. ]);
  34. if(!$result->return_code){
  35. Log::error('PayJs '.$result->return_msg);
  36. }
  37. // 获取收款二维码内容
  38. Payment::whereId($payment->id)->update(['qr_code' => $result->qrcode]);
  39. return Response::json(['status' => 'success', 'data' => $payment->sn, 'message' => '创建订单成功!']);
  40. }
  41. public function notify($request)
  42. {
  43. $data = (new Pay($this::$config))->notify();
  44. if($data['return_code'] == 1){
  45. $this::postPayment($data['out_trade_no'], 'PayJs');
  46. exit("success");
  47. }
  48. exit("fail");
  49. }
  50. public function getReturnHTML($request)
  51. {
  52. // TODO: Implement getReturnHTML() method.
  53. }
  54. public function getPurchaseHTML()
  55. {
  56. // TODO: Implement getReturnHTML() method.
  57. }
  58. }