| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\controllers;
- use app\models\Option;
- use app\models\Store;
- use app\models\WechatConfig;
- use ByteDance\Kernel\Exceptions\Exception;
- use EasyWeChat\Factory;
- use Yansongda\Pay\Pay;
- use yii\helpers\Json;
- use yii\web\Controller;
- use yii\helpers\ArrayHelper;
- use app\modules\alliance\behaviors\Auth;
- /**
- * @property WechatConfig $wechat_config
- * @property Factory $wechat
- */
- class BaseController extends Controller
- {
- public $store_id;
- public $wechat_config; //微信config
- public $wechat; //微信处理类
- public $wechatPay;
- public $wechatMini; // 微信小程序
- public $platform; // wechat(微信), alipay(支付宝), douyin(抖音)
- public $is_platform; // yes or no; 是否是平台小程序
- const IS_PLATFORM_YES = 'yes';
- const IS_PLATFORM_NOT = 'no';
- /**
- * @var \ByteDance\MiniProgram\Application $byteDance
- */
- public $byteDance;
- public $alipay_config;
- public $aliPay;
- /**
- * @return array
- */
- public function behaviors()
- {
- $merge = [
- [
- 'class' => 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);
- }
- }
- }
|