F2Fpay.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Http\Controllers\Gateway;
  3. use App\Models\Payment;
  4. use Auth;
  5. use Exception;
  6. use InvalidArgumentException;
  7. use Log;
  8. use Payment\Client;
  9. use Payment\Exceptions\ClassNotFoundException;
  10. use Response;
  11. class F2Fpay extends AbstractPayment {
  12. private static $aliConfig;
  13. function __construct() {
  14. parent::__construct();
  15. self::$aliConfig = [
  16. 'use_sandbox' => false,
  17. 'app_id' => self::$systemConfig['f2fpay_app_id'],
  18. 'sign_type' => 'RSA2',
  19. 'ali_public_key' => self::$systemConfig['f2fpay_public_key'],
  20. 'rsa_private_key' => self::$systemConfig['f2fpay_private_key'],
  21. 'limit_pay' => [],
  22. 'notify_url' => (self::$systemConfig['website_callback_url']?: self::$systemConfig['website_url']).'/callback/notify?method=f2fpay',
  23. 'return_url' => self::$systemConfig['website_url'].'/invoices',
  24. 'fee_type' => 'CNY',
  25. ];
  26. }
  27. public function purchase($request) {
  28. $payment = $this->creatNewPayment(Auth::id(),$request->input('oid'),$request->input('amount'));
  29. $data = [
  30. 'body' => '',
  31. 'subject' => self::$systemConfig['subject_name']?: self::$systemConfig['website_name'],
  32. 'trade_no' => $payment->trade_no,
  33. 'time_expire' => time() + 900, // 必须 15分钟 内付款
  34. 'amount' => $payment->amount,
  35. ];
  36. try{
  37. $client = new Client(Client::ALIPAY, self::$aliConfig);
  38. $result = $client->pay(Client::ALI_CHANNEL_QR, $data);
  39. }catch(InvalidArgumentException $e){
  40. Log::error("【支付宝当面付】输入信息错误: ".$e->getMessage());
  41. exit;
  42. }catch(ClassNotFoundException $e){
  43. Log::error("【支付宝当面付】未知类型: ".$e->getMessage());
  44. exit;
  45. }catch(Exception $e){
  46. Log::error("【支付宝当面付】错误: ".$e->getMessage());
  47. exit;
  48. }
  49. Payment::whereId($payment->id)
  50. ->update(['qr_code' => 'http://qr.topscan.com/api.php?text='.urlencode($result['qr_code']).'&el=1&w=400&m=10&logo=https://t.alipayobjects.com/tfscom/T1Z5XfXdxmXXXXXXXX.png']);//后备:https://cli.im/api/qrcode/code?text=".$result['qr_code']."&mhid=5EfGCwztyckhMHcmI9ZcOKs
  51. return Response::json(['status' => 'success', 'data' => $payment->trade_no, 'message' => '创建订单成功!']);
  52. }
  53. public function notify($request) {
  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. self::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. }