AlipayConfigController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\controllers;
  8. use app\models\Option;
  9. use yii\db\Exception;
  10. use yii\helpers\Json;
  11. use app\constants\OptionSetting;
  12. class AlipayConfigController extends BaseController
  13. {
  14. /**
  15. * 支付宝配置
  16. */
  17. public function actionIndex()
  18. {
  19. if (\Yii::$app->request->isPost) {
  20. $appId = trim(post_params('app_id'));
  21. $alipay_public_key = trim(post_params('alipay_public_key'));
  22. $app_public_key = trim(post_params('app_public_key'));
  23. $app_private_key = trim(post_params('app_private_key'));
  24. $aes_key = trim(post_params('aes_key'));
  25. $user_id = trim(post_params('user_id'));
  26. $name = trim(post_params('name'));
  27. if (empty($appId) || empty($alipay_public_key) || empty($app_public_key)
  28. || empty($app_private_key) || empty($aes_key) || empty($user_id) || empty($name)) {
  29. return $this->asJson([
  30. 'code' => 1,
  31. 'msg' => '请完善必填信息再进行提交修改!'
  32. ]);
  33. }
  34. $data['aes_key'] = $aes_key;
  35. $data['app_id'] = $appId;
  36. $data['alipay_public_key'] = $alipay_public_key;
  37. $data['app_public_key'] = $app_public_key;
  38. $data['app_private_key'] = $app_private_key;
  39. $data['user_id'] = $user_id;
  40. $data['name'] = $name;
  41. if (Option::set(Option::OPTOPN_KEY, Json::encode($data), get_store_id(), 'alipay')) {
  42. \Yii::$app->cache->delete('alipay_config_cache_' . get_store_id());
  43. \Yii::$app->cache->set('alipay_config_cache_' . get_store_id(), Json::encode($data));
  44. }
  45. return $this->asJson([
  46. 'code' => 0,
  47. 'msg' => '保存成功'
  48. ]);
  49. }
  50. if(\Yii::$app->isSaas()){
  51. $config_cache = \Yii::$app->cache->get('alipay_config_cache_business_' . get_store_id());
  52. }else{
  53. $config_cache = \Yii::$app->cache->get('alipay_config_cache_' . get_store_id());
  54. }
  55. if ($config_cache) {
  56. return $this->asJson([
  57. 'code' => 0,
  58. 'msg' => '',
  59. 'data' => Json::decode($config_cache)
  60. ]);
  61. }
  62. $value = Option::get(Option::OPTOPN_KEY, get_store_id(), 'alipay');
  63. if(\Yii::$app->isSaas()){
  64. $b_value = Json::decode($value['value'],true);
  65. unset($b_value['aes_key']);
  66. unset($b_value['alipay_public_key']);
  67. unset($b_value['app_public_key']);
  68. unset($b_value['app_private_key']);
  69. $b_value['is_saas'] = 1;
  70. $value['value'] = json_encode($b_value);
  71. \Yii::$app->cache->set('alipay_config_cache_business_' . get_store_id(), $value['value']);
  72. }else{
  73. \Yii::$app->cache->set('alipay_config_cache_' . get_store_id(), $value['value']);
  74. }
  75. return $this->asJson([
  76. 'code' => 0,
  77. 'msg' => '',
  78. 'data' => Json::decode($value['value'])
  79. ]);
  80. }
  81. /**
  82. * 支付宝配置
  83. */
  84. public function actionCrtIndex()
  85. {
  86. $dandianpuh5 = \Yii::$app->prod_is_dandianpu();
  87. $is_dandianpuh5 = intval(input_params('dandianpuh5'));
  88. $key = $dandianpuh5 && $is_dandianpuh5 ? OptionSetting::ALIPAY_PAY_DANDIANPUH5 : Option::ALIPAY_CONFIG_CERT;
  89. if (\Yii::$app->request->isPost) {
  90. $app_id = trim(post_params('app_id'));
  91. $alipay_public_cert = trim(post_params('alipay_public_cert'));
  92. $app_public_cert = trim(post_params('app_public_cert'));
  93. $alipay_root_cert = trim(post_params('alipay_root_cert'));
  94. $app_private_key = trim(post_params('app_private_key'));
  95. /*if (empty($app_id) || empty($alipay_public_cert) || empty($app_public_cert) || empty($alipay_root_cert)) {
  96. return $this->asJson([
  97. 'code' => 1,
  98. 'msg' => '请完善必填信息再进行提交修改!'
  99. ]);
  100. }*/
  101. $data['app_id'] = $app_id;
  102. $data['alipay_public_cert'] = $alipay_public_cert;
  103. $data['app_public_cert'] = $app_public_cert;
  104. $data['alipay_root_cert'] = $alipay_root_cert;
  105. $data['app_private_key'] = $app_private_key;
  106. try {
  107. Option::set($key, Json::encode($data), get_store_id(), 'alipay');
  108. } catch (Exception $e) {
  109. return $this->asJson([
  110. 'code' => 1,
  111. 'msg' => $e->getMessage()
  112. ]);
  113. }
  114. return $this->asJson([
  115. 'code' => 0,
  116. 'msg' => '保存成功'
  117. ]);
  118. }
  119. $value = Option::get($key, get_store_id(), 'alipay');
  120. if (!empty($value)) {
  121. $data = Json::decode($value['value']);
  122. }
  123. if (empty($data)) {
  124. $data = [
  125. 'app_id' => '',
  126. 'alipay_public_cert' => '',
  127. 'app_public_cert' => '',
  128. 'alipay_root_cert' => '',
  129. 'app_private_key' => ''
  130. ];
  131. }
  132. return $this->asJson([
  133. 'code' => 0,
  134. 'msg' => '',
  135. 'data' => $data
  136. ]);
  137. }
  138. }