BillingController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controllers\Admin\Setting;
  4. use App\Controllers\BaseController;
  5. use App\Models\Config;
  6. use App\Services\Payment;
  7. use Exception;
  8. use Psr\Http\Message\ResponseInterface;
  9. use Slim\Http\Response;
  10. use Slim\Http\ServerRequest;
  11. use Stripe\Exception\ApiErrorException;
  12. use Stripe\Stripe;
  13. use Stripe\WebhookEndpoint;
  14. use function json_decode;
  15. use function json_encode;
  16. final class BillingController extends BaseController
  17. {
  18. private static array $update_field = [
  19. // 支付宝当面付
  20. 'f2f_pay_app_id',
  21. 'f2f_pay_pid',
  22. 'f2f_pay_public_key',
  23. 'f2f_pay_private_key',
  24. 'f2f_pay_notify_url',
  25. // Stripe
  26. 'stripe_api_key',
  27. 'stripe_endpoint_secret',
  28. 'stripe_currency',
  29. 'stripe_card',
  30. 'stripe_alipay',
  31. 'stripe_wechat',
  32. 'stripe_min_recharge',
  33. 'stripe_max_recharge',
  34. // EPay
  35. 'epay_url',
  36. 'epay_pid',
  37. 'epay_key',
  38. 'epay_sign_type',
  39. 'epay_alipay',
  40. 'epay_wechat',
  41. 'epay_qq',
  42. 'epay_usdt',
  43. // PayPal
  44. 'paypal_mode',
  45. 'paypal_client_id',
  46. 'paypal_client_secret',
  47. 'paypal_currency',
  48. 'paypal_locale',
  49. ];
  50. /**
  51. * @throws Exception
  52. */
  53. public function index(ServerRequest $request, Response $response, array $args): ResponseInterface
  54. {
  55. $settings = Config::getClass('billing');
  56. return $response->write(
  57. $this->view()
  58. ->assign('update_field', self::$update_field)
  59. ->assign('settings', $settings)
  60. ->assign('payment_gateways', self::returnGatewaysList())
  61. ->assign('active_payment_gateway', self::returnActiveGateways())
  62. ->fetch('admin/setting/billing.tpl')
  63. );
  64. }
  65. public function save(ServerRequest $request, Response $response, array $args): ResponseInterface
  66. {
  67. $gateway_in_use = [];
  68. foreach (self::returnGatewaysList() as $value) {
  69. $payment_enable = $request->getParam($value);
  70. if ($payment_enable === 'true') {
  71. $gateway_in_use[] = $value;
  72. }
  73. }
  74. $gateway = (new Config())->where('item', 'payment_gateway')->first();
  75. $gateway->value = json_encode($gateway_in_use);
  76. if (! $gateway->save()) {
  77. return $response->withJson([
  78. 'ret' => 0,
  79. 'msg' => '保存支付网关时出错',
  80. ]);
  81. }
  82. foreach (self::$update_field as $item) {
  83. if (! Config::set($item, $request->getParam($item))) {
  84. return $response->withJson([
  85. 'ret' => 0,
  86. 'msg' => '保存 ' . $item . ' 时出错',
  87. ]);
  88. }
  89. }
  90. return $response->withJson([
  91. 'ret' => 1,
  92. 'msg' => '保存成功',
  93. ]);
  94. }
  95. public function setStripeWebhook(ServerRequest $request, Response $response, array $args): ResponseInterface
  96. {
  97. $stripe_api_key = $request->getParam('stripe_api_key');
  98. Stripe::setApiKey($stripe_api_key);
  99. try {
  100. WebhookEndpoint::create([
  101. 'url' => $_ENV['baseUrl'] . '/payment/notify/stripe',
  102. 'enabled_events' => [
  103. 'payment_intent.succeeded',
  104. ],
  105. ]);
  106. return $response->withJson([
  107. 'ret' => 1,
  108. 'msg' => '设置 Stripe Webhook 成功',
  109. ]);
  110. } catch (ApiErrorException) {
  111. return $response->withJson([
  112. 'ret' => 0,
  113. 'msg' => '设置 Stripe Webhook 失败',
  114. ]);
  115. }
  116. }
  117. public function returnGatewaysList(): array
  118. {
  119. $result = [];
  120. foreach (Payment::getAllPaymentMap() as $payment) {
  121. $result[$payment::_name()] = $payment::_name();
  122. }
  123. return $result;
  124. }
  125. public function returnActiveGateways()
  126. {
  127. $payment_gateways = (new Config())->where('item', 'payment_gateway')->first();
  128. return json_decode($payment_gateways->value);
  129. }
  130. }