F2Fpay.php 3.5 KB

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