F2Fpay.php 4.1 KB

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