AopF2F.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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\Alipay\AbstractAopGateway;
  10. use Omnipay\Alipay\AopF2FGateway;
  11. use Omnipay\Omnipay;
  12. use Psr\Http\Message\ResponseInterface;
  13. use Slim\Http\Response;
  14. use Slim\Http\ServerRequest;
  15. final class AopF2F extends AbstractPayment
  16. {
  17. public static function _name(): string
  18. {
  19. return 'f2fpay';
  20. }
  21. public static function _enable(): bool
  22. {
  23. return self::getActiveGateway('f2fpay');
  24. }
  25. public static function _readableName(): string
  26. {
  27. return '支付宝在线充值';
  28. }
  29. public function purchase(Request $request, Response $response, array $args): ResponseInterface
  30. {
  31. $amount = $request->getParam('amount');
  32. $user = Auth::getUser();
  33. if ($amount === '') {
  34. return $response->withJson([
  35. 'ret' => 0,
  36. 'msg' => '订单金额错误:' . $amount,
  37. ]);
  38. }
  39. $pl = new Paylist();
  40. $pl->userid = $user->id;
  41. $pl->tradeno = self::generateGuid();
  42. $pl->total = $amount;
  43. $pl->save();
  44. $gateway = $this->createGateway();
  45. /** @var \Omnipay\Alipay\Requests\AopTradePreCreateRequest $request */
  46. $request = $gateway->purchase();
  47. $request->setBizContent([
  48. 'subject' => $pl->tradeno,
  49. 'out_trade_no' => $pl->tradeno,
  50. 'total_amount' => $pl->total,
  51. ]);
  52. /** @var \Omnipay\Alipay\Responses\AopTradePreCreateResponse $aliResponse */
  53. $aliResponse = $request->send();
  54. // 获取收款二维码内容
  55. $qrCodeContent = $aliResponse->getQrCode();
  56. return $response->withJson([
  57. 'ret' => 1,
  58. 'qrcode' => $qrCodeContent,
  59. 'amount' => $pl->total,
  60. 'pid' => $pl->tradeno,
  61. ]);
  62. }
  63. public function notify($request, $response, $args): ResponseInterface
  64. {
  65. $gateway = $this->createGateway();
  66. /** @var \Omnipay\Alipay\Requests\AopCompletePurchaseRequest $aliRequest */
  67. $aliRequest = $gateway->completePurchase();
  68. $aliRequest->setParams($_POST);
  69. try {
  70. /** @var \Omnipay\Alipay\Responses\AopCompletePurchaseResponse $aliResponse */
  71. $aliResponse = $aliRequest->send();
  72. $pid = $aliResponse->data('out_trade_no');
  73. if ($aliResponse->isPaid()) {
  74. $this->postPayment($pid, '支付宝当面付 ' . $pid);
  75. return $response->write('success');
  76. }
  77. } catch (Exception $e) {
  78. return $response->write('fail');
  79. }
  80. return $response->write('unknown');
  81. }
  82. public static function getPurchaseHTML(): string
  83. {
  84. return View::getSmarty()->fetch('gateway/aopf2f.tpl');
  85. }
  86. private function createGateway(): AbstractAopGateway
  87. {
  88. $configs = Setting::getClass('f2f');
  89. /** @var AopF2FGateway $gateway */
  90. $gateway = Omnipay::create('Alipay_AopF2F');
  91. $gateway->setSignType('RSA2'); //RSA/RSA2
  92. $gateway->setAppId($configs['f2f_pay_app_id']);
  93. $gateway->setPrivateKey($configs['f2f_pay_private_key']); // 可以是路径,也可以是密钥内容
  94. $gateway->setAlipayPublicKey($configs['f2f_pay_public_key']); // 可以是路径,也可以是密钥内容
  95. if ($configs['f2f_pay_notify_url'] === '') {
  96. $notifyUrl = self::getCallbackUrl();
  97. } else {
  98. $notifyUrl = $configs['f2f_pay_notify_url'];
  99. }
  100. $gateway->setNotifyUrl($notifyUrl);
  101. return $gateway;
  102. }
  103. }