| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- <?php
- namespace addons\YunfastPay\common\services;
- use Yii;
- use yii\helpers\Json;
- use addons\YunfastPay\common\enums\YunfastPayEnum;
- use addons\YunfastPay\common\enums\YunfastPayTypeEnum;
- /**
- * 设置
- * @package addons\YunfastPay\common\services
- */
- class SettingService extends BaseService
- {
- const BASIC = 'basic';
- /**
- * @var array
- */
- protected $setting;
- /**
- * @var array
- */
- protected $json_fields = [
- 'trade_channel', 'channel_alternative'
- ];
- /**
- * @var array
- */
- protected $open_fields = [
- YunfastPayTypeEnum::WEIXIN_GZH => 'wechat_open',
- YunfastPayTypeEnum::WEIXIN_XCX => 'mp_wx_open',
- YunfastPayTypeEnum::WEIXIN_H5 => 'wechat_h5_open',
- YunfastPayTypeEnum::ALIPAY_APP => 'alipay_app_open',
- YunfastPayTypeEnum::ALIPAY_SCAN => 'alipay_scan_open',
- YunfastPayTypeEnum::WEIXIN_SCAN => 'wechat_scan_open',
- YunfastPayTypeEnum::W_MINI_PROGRAM_SCAN => 'wechat_mp_scan_open',
- ];
- /**
- * 设置配置
- *
- * @param array $setting 设置
- * @return boolean
- */
- public function setSetting(array $setting)
- {
- try {
- Yii::$app->services->setting->addons->set(
- $this->mall_id, YunfastPayEnum::ADDONS_NAME_YUNFAST_PAY, [static::BASIC => $setting]
- );
- $this->setCacheSetting($setting);
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- return true;
- }
- /**
- * 获取配置信息
- *
- * @param string $key
- * @return array
- */
- public function getSetting()
- {
- if (empty($this->setting)) {
- $this->setting = $this->getCacheSetting();
- $this->jsonFormat();
- }
- return $this->setting;
- }
- /**
- * @return array
- */
- public function getDbSetting()
- {
- return Yii::$app->services->setting->addons->get(
- $this->mall_id, YunfastPayEnum::ADDONS_NAME_YUNFAST_PAY, static::BASIC
- );
- }
- /**
- * @return string
- */
- private function getCacheField()
- {
- return YunfastPayEnum::YUNFAST_PAY . $this->mall_id . 'ceche';
- }
- /**
- * 商户编号
- *
- * @return string
- */
- public function getMchtCd()
- {
- $config = $this->getSetting();
- return !empty($config['mcht_cd'])
- ? $config['mcht_cd']
- : '';
- }
- /**
- * 商户编号
- *
- * @return string
- */
- public function getOnlineMchtCd()
- {
- $config = $this->getSetting();
- return !empty($config['online_mcht_cd'])
- ? $config['online_mcht_cd']
- : '';
- }
- /**
- * 组织机构代码
- *
- * @return string
- */
- public function getOrgCd()
- {
- $config = $this->getSetting();
- return !empty($config['org_cd'])
- ? $config['org_cd']
- : '';
- }
- /**
- * @return array
- */
- private function getCacheSetting()
- {
- $redis = Yii::$app->redis;
- $setting = $redis->get($this->getCacheField());
- if (empty($setting)) {
- $setting = $this->getDbSetting();
- $this->setCacheSetting($setting);
- } else {
- $setting = Json::decode($setting);
- }
- return $setting;
- }
- /**
- * @param array $setting
- * @return void
- */
- private function setCacheSetting(array $setting)
- {
- $redis = Yii::$app->redis;
- $redis->setex($this->getCacheField(), 60, Json::encode($setting));
- }
- /**
- * 格式化json数据
- *
- * @return void
- */
- private function jsonFormat()
- {
- foreach ($this->json_fields as $field) {
- $this->setting[$field] = !empty($this->setting[$field])
- && is_string($this->setting[$field])
- ? Json::decode($this->setting[$field])
- : ($this->setting[$field] ?? []);
- }
- }
- /**
- * 是否开启
- *
- * @return boolean
- */
- public function isOpen()
- {
- $config = $this->getSetting();
- return (!empty($config['switch']) && $config['switch'])
- ? true
- : false;
- }
- /**
- * 代付是否开启
- *
- * @return boolean
- */
- public function isOpenAgent()
- {
- $config = $this->getSetting();
-
- return (!empty($config['alternative_open']) && $config['alternative_open'])
- ? true
- : false;
- }
- /**
- * 获取支付渠道
- *
- * @return array
- */
- public function getTradeChannel()
- {
- $config = $this->getSetting();
- return $config['trade_channel'];
- }
- /**
- * 是否选择该渠道
- *
- * @param integer $channel
- * @return boolean
- */
- public function isSelectChannel(int $channel)
- {
- return in_array($channel, $this->getTradeChannel());
- }
- /**
- * 获取开始的支付
- *
- * @param array $payments
- * @return array
- */
- public function getOpenPayment(array $payments)
- {
- $result = [];
- foreach ($payments as $payment) {
- // 支付方式开启
- if ($this->currentPaymentIsOpen($payment['trade_type'])) {
- // 使用小写字母作为下标
- $field = strtolower($payment['trade_type']);
- $result[$field] = $payment;
- }
- }
- return $result;
- }
- /**
- * 检查当前支付方式是否开启
- *
- * @param string $trade_type
- * @return boolean
- */
- public function currentPaymentIsOpen(string $trade_type)
- {
- $open_field = $this->open_fields[$trade_type];
- return (boolean)$this->setting[$open_field];
- }
- }
|