Base.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Services\Gateway;
  4. use App\Models\Config;
  5. use App\Models\Invoice;
  6. use App\Models\Payback;
  7. use App\Models\Paylist;
  8. use App\Models\User;
  9. use App\Utils\Tools;
  10. use Psr\Http\Message\ResponseInterface;
  11. use Slim\Http\Response;
  12. use Slim\Http\ServerRequest;
  13. use function get_called_class;
  14. use function in_array;
  15. use function json_decode;
  16. use function time;
  17. abstract class Base
  18. {
  19. abstract public function purchase(ServerRequest $request, Response $response, array $args): ResponseInterface;
  20. abstract public function notify(ServerRequest $request, Response $response, array $args): ResponseInterface;
  21. /**
  22. * 支付网关的 codeName
  23. */
  24. abstract public static function _name(): string;
  25. /**
  26. * 是否启用支付网关
  27. */
  28. abstract public static function _enable(): bool;
  29. /**
  30. * 显示给用户的名称
  31. */
  32. abstract public static function _readableName(): string;
  33. public function getReturnHTML(ServerRequest $request, Response $response, array $args): ResponseInterface
  34. {
  35. return $response->write('ok');
  36. }
  37. public function getStatus(ServerRequest $request, Response $response, array $args): ResponseInterface
  38. {
  39. $p = (new Paylist())->where('tradeno', $_POST['pid'])->first();
  40. return $response->withJson([
  41. 'ret' => 1,
  42. 'result' => $p->satatus,
  43. ]);
  44. }
  45. abstract public static function getPurchaseHTML(): string;
  46. public function postPayment($trade_no): void
  47. {
  48. $paylist = (new Paylist())->where('tradeno', $trade_no)->first();
  49. if ($paylist?->status === 0) {
  50. $paylist->datetime = time();
  51. $paylist->status = 1;
  52. $paylist->save();
  53. }
  54. $invoice = (new Invoice())->where('id', $paylist?->invoice_id)->first();
  55. if ($invoice?->status === 'unpaid' && (int) $invoice?->price === (int) $paylist?->total) {
  56. $invoice->status = 'paid_gateway';
  57. $invoice->update_time = time();
  58. $invoice->pay_time = time();
  59. $invoice->save();
  60. }
  61. $user = (new User())->find($paylist?->userid);
  62. // 返利
  63. if ($user !== null && $user->ref_by > 0 && Config::obtain('invitation_mode') === 'after_paid') {
  64. (new Payback())->rebate($user->id, $paylist->total);
  65. }
  66. }
  67. public static function generateGuid(): string
  68. {
  69. return Tools::genRandomChar();
  70. }
  71. protected static function getCallbackUrl(): string
  72. {
  73. return $_ENV['baseUrl'] . '/payment/notify/' . get_called_class()::_name();
  74. }
  75. protected static function getUserReturnUrl(): string
  76. {
  77. return $_ENV['baseUrl'] . '/user/payment/return/' . get_called_class()::_name();
  78. }
  79. protected static function getActiveGateway($key): bool
  80. {
  81. $payment_gateways = (new Config())->where('item', 'payment_gateway')->first();
  82. $active_gateways = json_decode($payment_gateways->value);
  83. if (in_array($key, $active_gateways)) {
  84. return true;
  85. }
  86. return false;
  87. }
  88. }