F2Fpay.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. parent::__construct();
  16. self::$aliConfig = [
  17. 'use_sandbox' => false,
  18. 'app_id' => self::$systemConfig['f2fpay_app_id'],
  19. 'sign_type' => 'RSA2',
  20. 'ali_public_key' => self::$systemConfig['f2fpay_public_key'],
  21. 'rsa_private_key' => self::$systemConfig['f2fpay_private_key'],
  22. 'limit_pay' => [],
  23. 'notify_url' => (self::$systemConfig['website_callback_url']?: self::$systemConfig['website_url']).'/callback/notify?method=f2fpay',
  24. 'return_url' => self::$systemConfig['website_url'].'/invoices',
  25. 'fee_type' => 'CNY',
  26. ];
  27. }
  28. public function purchase($request): JsonResponse {
  29. $payment = $this->creatNewPayment(Auth::id(), $request->input('oid'), $request->input('amount'));
  30. $data = [
  31. 'body' => '',
  32. 'subject' => self::$systemConfig['subject_name']?: self::$systemConfig['website_name'],
  33. 'trade_no' => $payment->trade_no,
  34. 'time_expire' => time() + 900, // 必须 15分钟 内付款
  35. 'amount' => $payment->amount,
  36. ];
  37. try{
  38. $client = new Client(Client::ALIPAY, self::$aliConfig);
  39. $result = $client->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::whereId($payment->id)->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. $data = [
  55. 'trade_no' => $request->input('out_trade_no'),
  56. 'transaction_id' => $request->input('trade_no'),
  57. ];
  58. try{
  59. $client = new Client(Client::ALIPAY, self::$aliConfig);
  60. $result = $client->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. $ret = "fail";
  73. if($result['code'] == 10000 && $result['msg'] === "Success"){
  74. $ret = "success";
  75. if($_POST['trade_status'] === 'TRADE_FINISHED' || $_POST['trade_status'] === 'TRADE_SUCCESS'){
  76. $this->postPayment($request->input('out_trade_no'), '支付宝当面付');
  77. }else{
  78. Log::info('支付宝当面付-POST:交易失败['.getClientIp().']');
  79. }
  80. }else{
  81. Log::info('支付宝当面付-POST:验证失败['.getClientIp().']');
  82. }
  83. // 返回验证结果
  84. exit($ret);
  85. }
  86. }