AopF2F.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Services\Gateway;
  4. use App\Models\Paylist;
  5. use App\Models\Setting;
  6. use App\Services\Auth;
  7. use App\Services\View;
  8. use Exception;
  9. use Omnipay\Common\Exception\InvalidRequestException;
  10. use Omnipay\Common\GatewayInterface;
  11. use Omnipay\Omnipay;
  12. use Psr\Http\Message\ResponseInterface;
  13. use Slim\Http\Response;
  14. use Slim\Http\ServerRequest;
  15. use voku\helper\AntiXSS;
  16. final class AopF2F extends AbstractPayment
  17. {
  18. public static function _name(): string
  19. {
  20. return 'f2f';
  21. }
  22. public static function _enable(): bool
  23. {
  24. return self::getActiveGateway('f2f');
  25. }
  26. public static function _readableName(): string
  27. {
  28. return 'Alipay F2F';
  29. }
  30. public function purchase(ServerRequest $request, Response $response, array $args): ResponseInterface
  31. {
  32. $antiXss = new AntiXSS();
  33. $price = $antiXss->xss_clean($request->getParam('amount'));
  34. $invoice_id = $antiXss->xss_clean($request->getParam('invoice_id'));
  35. if ($price <= 0) {
  36. return $response->withJson([
  37. 'ret' => 0,
  38. 'msg' => '非法的金额',
  39. ]);
  40. }
  41. $user = Auth::getUser();
  42. $pl = new Paylist();
  43. $pl->userid = $user->id;
  44. $pl->total = $price;
  45. $pl->invoice_id = $invoice_id;
  46. $pl->tradeno = self::generateGuid();
  47. $pl->gateway = self::_readableName();
  48. $pl->save();
  49. $gateway = $this->createGateway();
  50. $request = $gateway->purchase();
  51. $request->setBizContent([
  52. 'subject' => $pl->tradeno,
  53. 'out_trade_no' => $pl->tradeno,
  54. 'total_amount' => $pl->total,
  55. ]);
  56. $aliResponse = $request->send();
  57. // 获取收款二维码内容
  58. $qrCodeContent = $aliResponse->getQrCode();
  59. return $response->withJson([
  60. 'ret' => 1,
  61. 'qrcode' => $qrCodeContent,
  62. 'amount' => $pl->total,
  63. 'pid' => $pl->tradeno,
  64. ]);
  65. }
  66. /**
  67. * @throws InvalidRequestException
  68. */
  69. public function notify($request, $response, $args): ResponseInterface
  70. {
  71. $gateway = $this->createGateway();
  72. $aliRequest = $gateway->completePurchase();
  73. $aliRequest->setParams($_POST);
  74. $aliResponse = $aliRequest->send();
  75. $pid = $aliResponse->data('out_trade_no');
  76. if ($aliResponse->isPaid()) {
  77. $this->postPayment($pid);
  78. return $response->withJson([
  79. 'ret' => 1,
  80. 'msg' => '支付成功',
  81. ]);
  82. }
  83. return $response->withJson([
  84. 'ret' => 0,
  85. 'msg' => '支付失败',
  86. ]);
  87. }
  88. /**
  89. * @throws Exception
  90. */
  91. public static function getPurchaseHTML(): string
  92. {
  93. return View::getSmarty()->fetch('gateway/f2f.tpl');
  94. }
  95. private function createGateway(): GatewayInterface
  96. {
  97. $configs = Setting::getClass('billing');
  98. $gateway = Omnipay::create('Alipay_AopF2F');
  99. $gateway->setSignType('RSA2'); //RSA/RSA2
  100. $gateway->setAppId($configs['f2f_pay_app_id']);
  101. $gateway->setPrivateKey($configs['f2f_pay_private_key']); // 可以是路径,也可以是密钥内容
  102. $gateway->setAlipayPublicKey($configs['f2f_pay_public_key']); // 可以是路径,也可以是密钥内容
  103. if ($configs['f2f_pay_notify_url'] === '') {
  104. $notifyUrl = self::getCallbackUrl();
  105. } else {
  106. $notifyUrl = $configs['f2f_pay_notify_url'];
  107. }
  108. $gateway->setNotifyUrl($notifyUrl);
  109. return $gateway;
  110. }
  111. }