F2Fpay.php 3.1 KB

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