BillingController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controllers\Admin\Setting;
  4. use App\Controllers\BaseController;
  5. use App\Models\Setting;
  6. use App\Services\Payment;
  7. use Exception;
  8. use function json_decode;
  9. use function json_encode;
  10. final class BillingController extends BaseController
  11. {
  12. private static array $update_field = [
  13. // 支付宝当面付
  14. 'f2f_pay_app_id',
  15. 'f2f_pay_pid',
  16. 'f2f_pay_public_key',
  17. 'f2f_pay_private_key',
  18. 'f2f_pay_notify_url',
  19. // Stripe
  20. 'stripe_card',
  21. 'stripe_alipay',
  22. 'stripe_wechat',
  23. 'stripe_currency',
  24. 'stripe_pk',
  25. 'stripe_sk',
  26. 'stripe_webhook_key',
  27. 'stripe_min_recharge',
  28. 'stripe_max_recharge',
  29. // EPay
  30. 'epay_url',
  31. 'epay_pid',
  32. 'epay_key',
  33. 'epay_sign_type',
  34. 'epay_alipay',
  35. 'epay_wechat',
  36. 'epay_qq',
  37. 'epay_usdt',
  38. // PayPal
  39. 'paypal_mode',
  40. 'paypal_client_id',
  41. 'paypal_client_secret',
  42. 'paypal_currency',
  43. 'paypal_locale',
  44. ];
  45. /**
  46. * @throws Exception
  47. */
  48. public function index($request, $response, $args)
  49. {
  50. $settings = Setting::getClass('billing');
  51. return $response->write(
  52. $this->view()
  53. ->assign('update_field', self::$update_field)
  54. ->assign('settings', $settings)
  55. ->assign('payment_gateways', self::returnGatewaysList())
  56. ->assign('active_payment_gateway', self::returnActiveGateways())
  57. ->fetch('admin/setting/billing.tpl')
  58. );
  59. }
  60. public function save($request, $response, $args)
  61. {
  62. $gateway_in_use = [];
  63. foreach (self::returnGatewaysList() as $value) {
  64. $payment_enable = $request->getParam($value);
  65. if ($payment_enable === 'true') {
  66. $gateway_in_use[] = $value;
  67. }
  68. }
  69. $gateway = Setting::where('item', 'payment_gateway')->first();
  70. $gateway->value = json_encode($gateway_in_use);
  71. if (! $gateway->save()) {
  72. return $response->withJson([
  73. 'ret' => 0,
  74. 'msg' => '保存支付网关时出错',
  75. ]);
  76. }
  77. foreach (self::$update_field as $item) {
  78. if (! Setting::set($item, $request->getParam($item))) {
  79. return $response->withJson([
  80. 'ret' => 0,
  81. 'msg' => '保存 ' . $item . ' 时出错',
  82. ]);
  83. }
  84. }
  85. return $response->withJson([
  86. 'ret' => 1,
  87. 'msg' => '保存成功',
  88. ]);
  89. }
  90. public function returnGatewaysList(): array
  91. {
  92. $result = [];
  93. foreach (Payment::getAllPaymentMap() as $payment) {
  94. $result[$payment::_name()] = $payment::_name();
  95. }
  96. return $result;
  97. }
  98. public function returnActiveGateways()
  99. {
  100. $payment_gateways = Setting::where('item', 'payment_gateway')->first();
  101. return json_decode($payment_gateways->value);
  102. }
  103. }