Config.php 4.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace app\admin\controller\xmwechat\payment;
  3. use app\common\controller\Backend;
  4. class Config extends Backend
  5. {
  6. static string $configFile = 'xmwechat.php';
  7. protected array $xmwechat;
  8. public function initialize(): void
  9. {
  10. parent::initialize();
  11. $this->xmwechat = config('xmwechat');
  12. }
  13. /**
  14. * @return void
  15. * @author jsjxsz
  16. */
  17. public function getConfig(): void
  18. {
  19. $this->success('', [
  20. 'payment' => $this->xmwechat['payment'],
  21. 'partnerPayment' => $this->xmwechat['partnerPayment'],
  22. ]);
  23. }
  24. /**
  25. * @return void
  26. * @author jsjxsz
  27. */
  28. public function saveConfig(): void
  29. {
  30. $type = $this->request->get('type', '');
  31. $data = $this->request->post();
  32. if (!$type) {
  33. $this->error(__('Parameter error'));
  34. }
  35. $xmwechat = $this->xmwechat;
  36. $configPath = config_path() . self::$configFile;
  37. $configContent = @file_get_contents($configPath);
  38. if ($type == 'payment') {
  39. if (!isset($xmwechat['payment']) || !is_array($xmwechat['payment'])) {
  40. $this->error(__('Parameter error'));
  41. }
  42. $notify_url = str_replace('/', '\/', $xmwechat['payment']['notify_url']);
  43. $certificate = str_replace('/', '\/', $xmwechat['payment']['certificate']);
  44. $private_key = str_replace('/', '\/', $xmwechat['payment']['private_key']);
  45. $configContent = preg_replace("/'mch_id'(\s+)=>(\s+)'{$xmwechat['payment']['mch_id']}'/", "'mch_id'\$1=>\$2'{$data['mch_id']}'", $configContent);
  46. $configContent = preg_replace("/'secret_key'(\s+)=>(\s+)'{$xmwechat['payment']['secret_key']}'/", "'secret_key'\$1=>\$2'{$data['secret_key']}'", $configContent);
  47. $configContent = preg_replace("/'notify_url'(\s+)=>(\s+)'{$notify_url}'/", "'notify_url'\$1=>\$2'{$data['notify_url']}'", $configContent);
  48. $configContent = preg_replace("/'certificate'(\s+)=>(\s+)'{$certificate}'/", "'certificate'\$1=>\$2'{$data['certificate']}'", $configContent);
  49. $configContent = preg_replace("/'private_key'(\s+)=>(\s+)'{$private_key}'/", "'private_key'\$1=>\$2'{$data['private_key']}'", $configContent);
  50. $result = @file_put_contents($configPath, $configContent);
  51. } elseif ($type == 'partnerPayment') {
  52. if (!isset($xmwechat['partnerPayment']) || !is_array($xmwechat['partnerPayment'])) {
  53. $this->error(__('Parameter error'));
  54. }
  55. $refunds_notify_url = str_replace('/', '\/', $xmwechat['partnerPayment']['refunds_notify_url']);
  56. $notify_url = str_replace('/', '\/', $xmwechat['partnerPayment']['notify_url']);
  57. $certificate = str_replace('/', '\/', $xmwechat['partnerPayment']['certificate']);
  58. $private_key = str_replace('/', '\/', $xmwechat['partnerPayment']['private_key']);
  59. $configContent = preg_replace("/'refunds_notify_url'(\s+)=>(\s+)'{$refunds_notify_url}'/", "'refunds_notify_url'\$1=>\$2'{$data['refunds_notify_url']}'", $configContent);
  60. $configContent = preg_replace("/'notify_url'(\s+)=>(\s+)'{$notify_url}'/", "'notify_url'\$1=>\$2'{$data['notify_url']}'", $configContent);
  61. $configContent = preg_replace("/'certificate'(\s+)=>(\s+)'{$certificate}'/", "'certificate'\$1=>\$2'{$data['certificate']}'", $configContent);
  62. $configContent = preg_replace("/'private_key'(\s+)=>(\s+)'{$private_key}'/", "'private_key'\$1=>\$2'{$data['private_key']}'", $configContent);
  63. $configContent = preg_replace("/'secret_key'(\s+)=>(\s+)'{$xmwechat['partnerPayment']['secret_key']}'/", "'secret_key'\$1=>\$2'{$data['secret_key']}'", $configContent);
  64. $configContent = preg_replace("/'sp_mchid'(\s+)=>(\s+)'{$xmwechat['partnerPayment']['sp_mchid']}'/", "'sp_mchid'\$1=>\$2'{$data['sp_mchid']}'", $configContent);
  65. $configContent = preg_replace("/'sp_appid'(\s+)=>(\s+)'{$xmwechat['partnerPayment']['sp_appid']}'/", "'sp_appid'\$1=>\$2'{$data['sp_appid']}'", $configContent);
  66. $configContent = preg_replace("/'sub_mchid'(\s+)=>(\s+)'{$xmwechat['partnerPayment']['sub_mchid']}'/", "'sub_mchid'\$1=>\$2'{$data['sub_mchid']}'", $configContent);
  67. $configContent = preg_replace("/'secret_key'(\s+)=>(\s+)'{$xmwechat['partnerPayment']['secret_key']}'/", "'secret_key'\$1=>\$2'{$data['secret_key']}'", $configContent);
  68. $result = @file_put_contents($configPath, $configContent);
  69. }
  70. if (!$result) {
  71. $this->error(__('Configuration write failed: %s', ['config/' . self::$configFile]));
  72. }
  73. $this->success();
  74. }
  75. }