F2Fpay.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace App\Http\Controllers\Gateway;
  3. use App\Models\Payment;
  4. use Auth;
  5. use Exception;
  6. use Illuminate\Http\JsonResponse;
  7. use InvalidArgumentException;
  8. use Log;
  9. use Payment\Client;
  10. use Payment\Exceptions\ClassNotFoundException;
  11. use Response;
  12. class F2Fpay extends AbstractPayment
  13. {
  14. private static $aliConfig;
  15. public function __construct()
  16. {
  17. self::$aliConfig = [
  18. 'use_sandbox' => false,
  19. 'app_id' => sysConfig('f2fpay_app_id'),
  20. 'sign_type' => 'RSA2',
  21. 'ali_public_key' => sysConfig('f2fpay_public_key'),
  22. 'rsa_private_key' => sysConfig('f2fpay_private_key'),
  23. 'limit_pay' => [],
  24. 'notify_url' => route('payment.notify', ['method' => 'f2fpay']),
  25. 'return_url' => route('invoice'),
  26. 'fee_type' => 'CNY',
  27. ];
  28. }
  29. public function purchase($request): JsonResponse
  30. {
  31. $payment = $this->creatNewPayment(Auth::id(), $request->input('id'), $request->input('amount'));
  32. $data = [
  33. 'body' => '',
  34. 'subject' => sysConfig('subject_name') ?: sysConfig('website_name'),
  35. 'trade_no' => $payment->trade_no,
  36. 'time_expire' => time() + 900, // 必须 15分钟 内付款
  37. 'amount' => $payment->amount,
  38. ];
  39. try {
  40. $result = (new Client(Client::ALIPAY, self::$aliConfig))->pay(Client::ALI_CHANNEL_QR, $data);
  41. } catch (InvalidArgumentException $e) {
  42. Log::error('【支付宝当面付】输入信息错误: '.$e->getMessage());
  43. exit;
  44. } catch (ClassNotFoundException $e) {
  45. Log::error('【支付宝当面付】未知类型: '.$e->getMessage());
  46. exit;
  47. } catch (Exception $e) {
  48. Log::error('【支付宝当面付】错误: '.$e->getMessage());
  49. exit;
  50. }
  51. $payment->update(['qr_code' => 1, 'url' => $result['qr_code']]);
  52. return Response::json(['status' => 'success', 'data' => $payment->trade_no, 'message' => '创建订单成功!']);
  53. }
  54. public function notify($request): void
  55. {
  56. $data = [
  57. 'trade_no' => $request->input('out_trade_no'),
  58. 'transaction_id' => $request->input('trade_no'),
  59. ];
  60. try {
  61. $result = (new Client(Client::ALIPAY, self::$aliConfig))->tradeQuery($data);
  62. Log::info('【支付宝当面付】回调验证查询:'.var_export($result, true));
  63. } catch (InvalidArgumentException $e) {
  64. Log::error('【支付宝当面付】回调信息错误: '.$e->getMessage());
  65. exit;
  66. } catch (ClassNotFoundException $e) {
  67. Log::error('【支付宝当面付】未知类型: '.$e->getMessage());
  68. exit;
  69. } catch (Exception $e) {
  70. Log::error('【支付宝当面付】错误: '.$e->getMessage());
  71. exit;
  72. }
  73. if ($result['code'] == 10000 && $result['msg'] === 'Success') {
  74. if ($_POST['trade_status'] === 'TRADE_FINISHED' || $_POST['trade_status'] === 'TRADE_SUCCESS') {
  75. $payment = Payment::whereTradeNo($request->input('out_trade_no'))->first();
  76. if ($payment) {
  77. $ret = $payment->order->update(['status' => 2]);
  78. if ($ret) {
  79. exit('success');
  80. }
  81. }
  82. } else {
  83. Log::info('支付宝当面付-POST:交易失败');
  84. }
  85. } else {
  86. Log::info('支付宝当面付-POST:验证失败');
  87. }
  88. // 返回验证结果
  89. exit('fail');
  90. }
  91. }