Auth::class, ] ]; return ArrayHelper::merge($merge, parent::behaviors()); } /** * 初始化操作 */ public function init() { parent::init(); $this->store_id = get_params('store_id', 1); $this->platform = input_params('platform', 'wechat'); $this->is_platform = input_params('is_platform', 'no'); if ($this->platform != 'alipay') { $this->initWXPay(); } // 抖音小程序 if ($this->platform == 'bytedance') { $this->initBytedance(); } } public function initWX() { // TODO: 小程序为例 // 获取微信配置进行初始化 $_form = input_params('_form', 'mini'); $type = 1; if ($_form == 'h5') { $type = 2; } if ($_form == 'app') { $type = 3; } $this->wechat_config = WechatConfig::findOne(['store_id' => get_store_id(), 'type' => $type]); if ($this->wechat_config) { $config = [ 'app_id' => $this->wechat_config->app_id, 'secret' => $this->wechat_config->app_secret, 'response_type' => 'array' ]; $this->wechat = Factory::miniProgram($config); } } /** * Undocumented function * * @Author LGL 24963@qq.com * @DateTime 2021-02-03 * @desc: 实例化支付类 * @return void */ protected function initWXPay () { $this->wechat_config = new WechatConfig(); $keys = [ 'sp_name', 'sp_appid', 'sp_mch_id', 'sp_key', 'platform_key', 'sp_apiclient_cert', 'sp_apiclient_key', 'platform_api_key', 'platform_serial_no', 'platform_appid', 'platform_app_appid' ]; $data = Option::get($keys, 0, 'saas'); $data = array_column($data, 'value','name'); $this->wechat_config->app_id = $data['sp_appid']; $this->wechat_config->app_secret = $data['platform_serial_no']; $this->wechat_config->mch_id = $data['sp_mch_id']; $this->wechat_config->pay_key = $data['sp_key']; $this->wechat_config->cert_pem = $data['sp_apiclient_cert']; $this->wechat_config->key_pem = $data['sp_apiclient_key']; $config = [ // 'app_id' => $this->wechat_config->app_id, 'secret' => $this->wechat_config->app_secret, 'key' => $this->wechat_config->pay_key, // 'mch_id' => $this->wechat_config->mch_id, 'response_type' => 'array' ]; $config['app_id'] = $data['platform_appid']; $config['mch_id'] = $data['sp_mch_id']; $_from = input_params('_from'); if ($_from === 'app') { $config['app_id'] = $data['platform_app_appid']; } // 证书 if (!is_dir(\Yii::$app->runtimePath . '/pem')) { mkdir(\Yii::$app->runtimePath . '/pem'); file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', ''); } $cert_pem_file = null; if ($this->wechat_config->cert_pem) { $cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($this->wechat_config->cert_pem); if (!file_exists($cert_pem_file)) { file_put_contents($cert_pem_file, $this->wechat_config->cert_pem); } } $key_pem_file = null; if ($this->wechat_config->key_pem) { $key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($this->wechat_config->key_pem); if (!file_exists($key_pem_file)) { file_put_contents($key_pem_file, $this->wechat_config->key_pem); } } $config['cert_path'] = $cert_pem_file; $config['key_path'] = $key_pem_file; $this->wechatPay = Factory::payment($config); $config = [ 'app_id' => $data['platform_appid'], 'secret' => $data['platform_key'] ]; $this->wechatMini = Factory::miniProgram($config); } /** * 支付宝初始化操作 */ public function initAlipay() { $config_cache = \Yii::$app->cache->get('alipay_config_cache_' . get_store_id()); if ($config_cache) { $this->alipay_config = Json::decode($config_cache); } else { $this->alipay_config = Json::decode(Option::get(Option::OPTOPN_KEY, get_store_id(), 'alipay')['value']); } $config = [ 'app_id' => $this->alipay_config['app_id'] ?? '', 'notify_url' => 'www.baidu.com', 'return_url' => '', 'ali_public_key' => $this->alipay_config['alipay_public_key'] ?? '', 'private_key' => preg_replace('# #', '', $this->alipay_config['app_private_key'] ?? ''), // 'log' => [ // optional // 'file' => '~/var/logs/alipay.log', // 'level' => 'info', // 建议生产环境等级调整为 info,开发环境为 debug // 'type' => 'single', // optional, 可选 daily. // 'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天 // ], 'http' => [ 'timeout' => 5.0, 'connect_timeout' => 5.0, ], // 'mode' => 'dev', // optional,设置此参数,将进入沙箱模式 ]; $this->aliPay = Pay::alipay($config); } // 抖音初始化操作 public function initBytedance() { $config = [ 'app_id' => '', 'app_secret' => '', 'response_type' => 'array' ]; if ($this->is_platform == self::IS_PLATFORM_NOT) { $bytedance_config = Option::get('douyin', $this->store_id, 'store'); if (!empty($bytedance_config['value'])) { $bytedance_config = Json::decode($bytedance_config['value']); $config['app_id'] = $bytedance_config['app_id']; $config['app_secret'] = $bytedance_config['app_secret']; } } else { $bytedance_config = Option::getSaasPlatformBytedance(); $config['app_id'] = $bytedance_config['appid']; $config['app_secret'] = $bytedance_config['key']; } if (!empty($config['app_id']) && !empty($config['app_secret'])) { $this->byteDance = \ByteDance\Factory::miniProgram($config); } } }