F2Fpay.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace App\Payments;
  3. use App\Payments\Library\AlipayF2F;
  4. use App\Payments\Library\Gateway;
  5. use Auth;
  6. use Exception;
  7. use Illuminate\Http\JsonResponse;
  8. use Log;
  9. use Response;
  10. class F2Fpay extends Gateway
  11. {
  12. private static $aliConfig;
  13. public function __construct()
  14. {
  15. self::$aliConfig = [
  16. 'app_id' => sysConfig('f2fpay_app_id'),
  17. 'ali_public_key' => sysConfig('f2fpay_public_key'),
  18. 'rsa_private_key' => sysConfig('f2fpay_private_key'),
  19. 'notify_url' => route('payment.notify', ['method' => 'f2fpay']),
  20. ];
  21. }
  22. public function purchase($request): JsonResponse
  23. {
  24. $payment = $this->creatNewPayment(Auth::id(), $request->input('id'), $request->input('amount'));
  25. $data = [
  26. 'subject' => sysConfig('subject_name') ?: sysConfig('website_name'),
  27. 'out_trade_no' => $payment->trade_no,
  28. 'total_amount' => $payment->amount,
  29. ];
  30. try {
  31. $gateWay = new AlipayF2F(self::$aliConfig);
  32. $result = $gateWay->qrCharge($data);
  33. $payment->update(['qr_code' => 1, 'url' => $result['qr_code']]);
  34. } catch (Exception $e) {
  35. $payment->failed();
  36. Log::alert('【支付宝当面付】支付错误: '.$e->getMessage());
  37. exit;
  38. }
  39. return Response::json(['status' => 'success', 'data' => $payment->trade_no, 'message' => '创建订单成功!']);
  40. }
  41. public function notify($request): void
  42. {
  43. try {
  44. $result = (new AlipayF2F(self::$aliConfig))->tradeQuery($request->only('out_trade_no', 'trade_no'));
  45. Log::notice('【支付宝当面付】回调验证查询:'.var_export($result, true));
  46. } catch (Exception $e) {
  47. Log::alert('【支付宝当面付】回调信息错误: '.$e->getMessage());
  48. exit;
  49. }
  50. if ($result['code'] === '10000' && $result['msg'] === 'Success') {
  51. if ($request->has('out_trade_no') && in_array($request->input('trade_status'), ['TRADE_FINISHED', 'TRADE_SUCCESS'])) {
  52. if ($this->paymentReceived($request->input('out_trade_no'))) {
  53. exit('success');
  54. }
  55. } else {
  56. Log::error('【支付宝当面付】交易失败:'.var_export($request->all(), true));
  57. }
  58. } else {
  59. Log::error('【支付宝当面付】验证失败:'.var_export($result, true));
  60. }
  61. // 返回验证结果
  62. exit('fail');
  63. }
  64. }