Epay.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Copyright (c) 2019.
  5. * Author:Alone88
  6. * Github:https://github.com/anhao
  7. */
  8. namespace App\Services\Gateway;
  9. use App\Models\Paylist;
  10. use App\Models\Setting;
  11. use App\Services\Auth;
  12. use App\Services\Gateway\Epay\EpayNotify;
  13. use App\Services\Gateway\Epay\EpaySubmit;
  14. use App\Services\View;
  15. use Exception;
  16. use Psr\Http\Message\ResponseInterface;
  17. use Slim\Http\Response;
  18. use Slim\Http\ServerRequest;
  19. use voku\helper\AntiXSS;
  20. final class Epay extends AbstractPayment
  21. {
  22. protected array $epay = [];
  23. public function __construct()
  24. {
  25. $this->epay['apiurl'] = Setting::obtain('epay_url');//易支付API地址
  26. $this->epay['partner'] = Setting::obtain('epay_pid');//易支付商户pid
  27. $this->epay['key'] = Setting::obtain('epay_key');//易支付商户Key
  28. $this->epay['sign_type'] = strtoupper('SHA256'); //签名方式
  29. $this->epay['input_charset'] = strtolower('utf-8');//字符编码
  30. $this->epay['transport'] = 'https';//协议 http 或者https
  31. }
  32. public static function _name(): string
  33. {
  34. return 'epay';
  35. }
  36. public static function _enable(): bool
  37. {
  38. return self::getActiveGateway('epay');
  39. }
  40. public static function _readableName(): string
  41. {
  42. return 'EPay';
  43. }
  44. public function purchase(ServerRequest $request, Response $response, array $args): ResponseInterface
  45. {
  46. $antiXss = new AntiXSS();
  47. $price = $antiXss->xss_clean($request->getParam('price'));
  48. $invoice_id = $antiXss->xss_clean($request->getParam('invoice_id'));
  49. // EPay 特定参数
  50. $type = $antiXss->xss_clean($request->getParam('type'));
  51. if ($price <= 0) {
  52. return $response->withJson([
  53. 'ret' => 0,
  54. 'msg' => '非法的金额',
  55. ]);
  56. }
  57. $user = Auth::getUser();
  58. $pl = new Paylist();
  59. $pl->userid = $user->id;
  60. $pl->total = $price;
  61. $pl->invoice_id = $invoice_id;
  62. $pl->tradeno = self::generateGuid();
  63. $type_text = match ($type) {
  64. 'qqpay' => 'QQ',
  65. 'wxpay' => 'WeChat',
  66. 'epusdt' => 'USDT',
  67. default => 'Alipay',
  68. };
  69. $pl->gateway = self::_readableName() . ' ' . $type_text;
  70. $pl->save();
  71. //请求参数
  72. $data = [
  73. 'pid' => trim($this->epay['partner']),
  74. 'type' => $type,
  75. 'out_trade_no' => $pl->tradeno,
  76. 'notify_url' => $_ENV['baseUrl'] . '/payment/notify/epay',
  77. 'return_url' => $_ENV['baseUrl'] . '/user/payment/return/epay',
  78. 'name' => $pl->tradeno,
  79. 'money' => $price,
  80. 'sitename' => $_ENV['appName'],
  81. ];
  82. $epaySubmit = new EpaySubmit($this->epay);
  83. $html_text = $epaySubmit->buildRequestForm($data);
  84. return $response->write($html_text);
  85. }
  86. public function notify($request, $response, $args): ResponseInterface
  87. {
  88. $epayNotify = new EpayNotify($this->epay);
  89. $verify_result = $epayNotify->verifyNotify();
  90. if ($verify_result) {
  91. $out_trade_no = $_GET['out_trade_no'];
  92. $type = $_GET['type'];
  93. $type = match ($type) {
  94. 'qqpay' => 'QQ',
  95. 'wxpay' => 'WeChat',
  96. 'epusdt' => 'USDT',
  97. default => 'Alipay',
  98. };
  99. $trade_status = $_GET['trade_status'];
  100. if ($trade_status === 'TRADE_SUCCESS') {
  101. $this->postPayment($out_trade_no);
  102. return $response->withJson(['state' => 'success', 'msg' => '支付成功']);
  103. }
  104. return $response->withJson(['state' => 'fail', 'msg' => '支付失败']);
  105. }
  106. return $response->write('非法请求');
  107. }
  108. /**
  109. * @throws Exception
  110. */
  111. public static function getPurchaseHTML(): string
  112. {
  113. return View::getSmarty()->fetch('gateway/epay.tpl');
  114. }
  115. public function getReturnHTML($request, $response, $args): ResponseInterface
  116. {
  117. $money = $_GET['money'];
  118. $html = <<<HTML
  119. 你已成功充值 {$money} 元,正在跳转..
  120. <script>
  121. setTimeout(function() {
  122. location.href="/user/invoice";
  123. },500)
  124. </script>
  125. HTML;
  126. return $response->write($html);
  127. }
  128. }