F2Fpay.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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' => (sysConfig(
  25. 'website_callback_url'
  26. ) ?: sysConfig(
  27. 'website_url'
  28. )) . '/callback/notify?method=f2fpay',
  29. 'return_url' => sysConfig('website_url') . '/invoices',
  30. 'fee_type' => 'CNY',
  31. ];
  32. }
  33. public function purchase($request): JsonResponse
  34. {
  35. $payment = $this->creatNewPayment(
  36. Auth::id(),
  37. $request->input('id'),
  38. $request->input('amount')
  39. );
  40. $data = [
  41. 'body' => '',
  42. 'subject' => sysConfig('subject_name') ?: sysConfig(
  43. 'website_name'
  44. ),
  45. 'trade_no' => $payment->trade_no,
  46. 'time_expire' => time() + 900, // 必须 15分钟 内付款
  47. 'amount' => $payment->amount,
  48. ];
  49. try {
  50. $result = (new Client(Client::ALIPAY, self::$aliConfig))->pay(
  51. Client::ALI_CHANNEL_QR,
  52. $data
  53. );
  54. } catch (InvalidArgumentException $e) {
  55. Log::error("【支付宝当面付】输入信息错误: " . $e->getMessage());
  56. exit;
  57. } catch (ClassNotFoundException $e) {
  58. Log::error("【支付宝当面付】未知类型: " . $e->getMessage());
  59. exit;
  60. } catch (Exception $e) {
  61. Log::error("【支付宝当面付】错误: " . $e->getMessage());
  62. exit;
  63. }
  64. $payment->update(['qr_code' => 1, 'url' => $result['qr_code']]);
  65. return Response::json(
  66. [
  67. 'status' => 'success',
  68. 'data' => $payment->trade_no,
  69. 'message' => '创建订单成功!',
  70. ]
  71. );
  72. }
  73. public function notify($request): void
  74. {
  75. $data = [
  76. 'trade_no' => $request->input('out_trade_no'),
  77. 'transaction_id' => $request->input('trade_no'),
  78. ];
  79. try {
  80. $result = (new Client(
  81. Client::ALIPAY, self::$aliConfig
  82. ))->tradeQuery($data);
  83. Log::info("【支付宝当面付】回调验证查询:" . var_export($result, true));
  84. } catch (InvalidArgumentException $e) {
  85. Log::error("【支付宝当面付】回调信息错误: " . $e->getMessage());
  86. exit;
  87. } catch (ClassNotFoundException $e) {
  88. Log::error("【支付宝当面付】未知类型: " . $e->getMessage());
  89. exit;
  90. } catch (Exception $e) {
  91. Log::error("【支付宝当面付】错误: " . $e->getMessage());
  92. exit;
  93. }
  94. if ($result['code'] == 10000 && $result['msg'] === "Success") {
  95. if ($_POST['trade_status'] === 'TRADE_FINISHED' || $_POST['trade_status'] === 'TRADE_SUCCESS') {
  96. $payment = Payment::whereTradeNo(
  97. $request->input('out_trade_no')
  98. )->first();
  99. if ($payment) {
  100. $ret = $payment->order->update(['status' => 2]);
  101. if ($ret) {
  102. exit('success');
  103. }
  104. }
  105. } else {
  106. Log::info('支付宝当面付-POST:交易失败[' . getClientIp() . ']');
  107. }
  108. } else {
  109. Log::info('支付宝当面付-POST:验证失败[' . getClientIp() . ']');
  110. }
  111. // 返回验证结果
  112. exit('fail');
  113. }
  114. }