SettingService.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. namespace addons\YunfastPay\common\services;
  3. use Yii;
  4. use yii\helpers\Json;
  5. use addons\YunfastPay\common\enums\YunfastPayEnum;
  6. use addons\YunfastPay\common\enums\YunfastPayTypeEnum;
  7. /**
  8. * 设置
  9. * @package addons\YunfastPay\common\services
  10. */
  11. class SettingService extends BaseService
  12. {
  13. const BASIC = 'basic';
  14. /**
  15. * @var array
  16. */
  17. protected $setting;
  18. /**
  19. * @var array
  20. */
  21. protected $json_fields = [
  22. 'trade_channel', 'channel_alternative'
  23. ];
  24. /**
  25. * @var array
  26. */
  27. protected $open_fields = [
  28. YunfastPayTypeEnum::WEIXIN_GZH => 'wechat_open',
  29. YunfastPayTypeEnum::WEIXIN_XCX => 'mp_wx_open',
  30. YunfastPayTypeEnum::WEIXIN_H5 => 'wechat_h5_open',
  31. YunfastPayTypeEnum::ALIPAY_APP => 'alipay_app_open',
  32. YunfastPayTypeEnum::ALIPAY_SCAN => 'alipay_scan_open',
  33. YunfastPayTypeEnum::WEIXIN_SCAN => 'wechat_scan_open',
  34. YunfastPayTypeEnum::W_MINI_PROGRAM_SCAN => 'wechat_mp_scan_open',
  35. ];
  36. /**
  37. * 设置配置
  38. *
  39. * @param array $setting 设置
  40. * @return boolean
  41. */
  42. public function setSetting(array $setting)
  43. {
  44. try {
  45. Yii::$app->services->setting->addons->set(
  46. $this->mall_id, YunfastPayEnum::ADDONS_NAME_YUNFAST_PAY, [static::BASIC => $setting]
  47. );
  48. $this->setCacheSetting($setting);
  49. } catch (\Exception $e) {
  50. return $this->error($e->getMessage());
  51. }
  52. return true;
  53. }
  54. /**
  55. * 获取配置信息
  56. *
  57. * @param string $key
  58. * @return array
  59. */
  60. public function getSetting()
  61. {
  62. if (empty($this->setting)) {
  63. $this->setting = $this->getCacheSetting();
  64. $this->jsonFormat();
  65. }
  66. return $this->setting;
  67. }
  68. /**
  69. * @return array
  70. */
  71. public function getDbSetting()
  72. {
  73. return Yii::$app->services->setting->addons->get(
  74. $this->mall_id, YunfastPayEnum::ADDONS_NAME_YUNFAST_PAY, static::BASIC
  75. );
  76. }
  77. /**
  78. * @return string
  79. */
  80. private function getCacheField()
  81. {
  82. return YunfastPayEnum::YUNFAST_PAY . $this->mall_id . 'ceche';
  83. }
  84. /**
  85. * 商户编号
  86. *
  87. * @return string
  88. */
  89. public function getMchtCd()
  90. {
  91. $config = $this->getSetting();
  92. return !empty($config['mcht_cd'])
  93. ? $config['mcht_cd']
  94. : '';
  95. }
  96. /**
  97. * 商户编号
  98. *
  99. * @return string
  100. */
  101. public function getOnlineMchtCd()
  102. {
  103. $config = $this->getSetting();
  104. return !empty($config['online_mcht_cd'])
  105. ? $config['online_mcht_cd']
  106. : '';
  107. }
  108. /**
  109. * 组织机构代码
  110. *
  111. * @return string
  112. */
  113. public function getOrgCd()
  114. {
  115. $config = $this->getSetting();
  116. return !empty($config['org_cd'])
  117. ? $config['org_cd']
  118. : '';
  119. }
  120. /**
  121. * @return array
  122. */
  123. private function getCacheSetting()
  124. {
  125. $redis = Yii::$app->redis;
  126. $setting = $redis->get($this->getCacheField());
  127. if (empty($setting)) {
  128. $setting = $this->getDbSetting();
  129. $this->setCacheSetting($setting);
  130. } else {
  131. $setting = Json::decode($setting);
  132. }
  133. return $setting;
  134. }
  135. /**
  136. * @param array $setting
  137. * @return void
  138. */
  139. private function setCacheSetting(array $setting)
  140. {
  141. $redis = Yii::$app->redis;
  142. $redis->setex($this->getCacheField(), 60, Json::encode($setting));
  143. }
  144. /**
  145. * 格式化json数据
  146. *
  147. * @return void
  148. */
  149. private function jsonFormat()
  150. {
  151. foreach ($this->json_fields as $field) {
  152. $this->setting[$field] = !empty($this->setting[$field])
  153. && is_string($this->setting[$field])
  154. ? Json::decode($this->setting[$field])
  155. : ($this->setting[$field] ?? []);
  156. }
  157. }
  158. /**
  159. * 是否开启
  160. *
  161. * @return boolean
  162. */
  163. public function isOpen()
  164. {
  165. $config = $this->getSetting();
  166. return (!empty($config['switch']) && $config['switch'])
  167. ? true
  168. : false;
  169. }
  170. /**
  171. * 代付是否开启
  172. *
  173. * @return boolean
  174. */
  175. public function isOpenAgent()
  176. {
  177. $config = $this->getSetting();
  178. return (!empty($config['alternative_open']) && $config['alternative_open'])
  179. ? true
  180. : false;
  181. }
  182. /**
  183. * 获取支付渠道
  184. *
  185. * @return array
  186. */
  187. public function getTradeChannel()
  188. {
  189. $config = $this->getSetting();
  190. return $config['trade_channel'];
  191. }
  192. /**
  193. * 是否选择该渠道
  194. *
  195. * @param integer $channel
  196. * @return boolean
  197. */
  198. public function isSelectChannel(int $channel)
  199. {
  200. return in_array($channel, $this->getTradeChannel());
  201. }
  202. /**
  203. * 获取开始的支付
  204. *
  205. * @param array $payments
  206. * @return array
  207. */
  208. public function getOpenPayment(array $payments)
  209. {
  210. $result = [];
  211. foreach ($payments as $payment) {
  212. // 支付方式开启
  213. if ($this->currentPaymentIsOpen($payment['trade_type'])) {
  214. // 使用小写字母作为下标
  215. $field = strtolower($payment['trade_type']);
  216. $result[$field] = $payment;
  217. }
  218. }
  219. return $result;
  220. }
  221. /**
  222. * 检查当前支付方式是否开启
  223. *
  224. * @param string $trade_type
  225. * @return boolean
  226. */
  227. public function currentPaymentIsOpen(string $trade_type)
  228. {
  229. $open_field = $this->open_fields[$trade_type];
  230. return (boolean)$this->setting[$open_field];
  231. }
  232. }