| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\utils\Wechat;
- use app\models\Option;
- use app\models\SaasUser;
- use app\models\Store;
- use app\models\WechatConfig;
- use EasyWeChat\Factory;
- use yii\helpers\Json;
- class WechatProfit
- {
- /**
- * 具体类型
- */
- const WECHAT_KIND_MINI = 'mini';
- const WECHAT_KIND_OFFICIAL = 'official';
- const WECHAT_KIND_PAY = 'pay';
- /**
- * @var array
- * 合法类型
- */
- protected static $validRequest = [
- self::WECHAT_KIND_MINI,
- self::WECHAT_KIND_OFFICIAL,
- self::WECHAT_KIND_PAY
- ];
- /**
- * @var \EasyWeChat\MiniProgram\Application
- */
- public static $wechat_mini;
- /**
- * @var \EasyWeChat\OfficialAccount\Application
- */
- public static $wechat_official;
- /**
- * @var \EasyWeChat\Payment\Application
- */
- public static $wechat_pay;
- /**
- * 初始化
- * @param string $type
- * @param integer $from
- * @throws \Exception
- */
- public static function init($store_id = null, $is_platform = 0, $navite = 0, $from = WechatConfig::TYPE_CONFIG_MINI) {
- if (!$store_id) {
- $store_id = get_store_id();
- }
- // 获取微信配置进行初始化
- // if ($navite == 1) {
- // $wechat_config = Option::getSaasWechat2();
- // } else {
- $wechat_config = Option::getSaasWechat();
- // }
- if (empty($wechat_config)) {
- throw new \Exception('Wechat App Is Null');
- }
- if ($navite == 1) {
- $store_id = 0;
- }
- $store_wechat = WechatConfig::findOne(['store_id' => $store_id, 'type' => $from]);
- $config = [
- 'app_id' => $wechat_config['sp_appid'],
- 'sub_appid' => $store_wechat->app_id,
- 'sub_mch_id' => $store_wechat->mch_id,
- 'response_type' => 'array'
- ];
- // if ($navite == 1) {
- // $config['mch_id'] = $wechat_config['sp_mch_id'];
- // $config['sub_mch_id'] = $wechat_config['mch_id'];
- // $config['sub_appid'] = $wechat_config['sub_appid'];
- // }
- if ($is_platform) {
- $config['sub_appid'] = Option::get('platform_appid', 0, 'saas')['value'];
- }
- $self_mini = null;
- if ($store_id > 0) {
- $self_mini = \app\models\Option::get('self_mini', $store_id, 'store', 0)['value'];
- }
- if (\Yii::$app->prod_is_dandianpu() && !$self_mini && intval($from) === WechatConfig::TYPE_CONFIG_MINI) {
- $config['sub_appid'] = Option::get('one_store_wechat_appid', 0, 'saas', '')['value'];
- }
- if (\Yii::$app->prod_is_dandianpu() && intval($from) === WechatConfig::TYPE_CONFIG_MP) {
- $config['sub_appid'] = Option::get('one_store_wechat_official_appid', 0, 'saas', '')['value'];
- }
- // //
- // if($store_id>0){
- // $storeInfo = Store::findOne($store_id);
- // if($storeInfo->business_model != 1){
- // $config = Option::getBusinessWechat();
- // $config['sub_mch_id'] = $store_wechat->mch_id;
- // }
- // }
- $storeInfo = Store::findOne($store_id);
- if($storeInfo->business_model != 1){
- unset($config['sub_appid']);
- if ($from == WechatConfig::TYPE_CONFIG_APP) {
- $platform_appid = Option::get('platform_app_appid', 0, 'saas')['value'];
- } else {
- $platform_appid = Option::get('platform_appid', 0, 'saas')['value'];
- }
- $config['app_id'] = $platform_appid;
- $config['mch_id'] = $wechat_config['sp_mch_id'];
- }
- // 证书
- 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 ($wechat_config['sp_apiclient_cert']) {
- $cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_config['sp_apiclient_cert']);
- if (!file_exists($cert_pem_file)) {
- file_put_contents($cert_pem_file, $wechat_config['sp_apiclient_cert']);
- }
- }
- $key_pem_file = null;
- if ($wechat_config['sp_apiclient_key']) {
- $key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_config['sp_apiclient_key']);
- if (!file_exists($key_pem_file)) {
- file_put_contents($key_pem_file, $wechat_config['sp_apiclient_key']);
- }
- }
- $config['key'] = $wechat_config['sp_key'];
- $config['mch_id'] = $wechat_config['sp_mch_id'];
- $config['cert_path'] = $cert_pem_file;
- $config['key_path'] = $key_pem_file;
- if(\Yii::$app->isSaas() && intval($from) === WechatConfig::TYPE_CONFIG_MP){
- if (!\Yii::$app->prod_is_dandianpu()) {
- $config['sub_appid'] = $store_wechat->app_id;
- $config['sub_mch_id'] = $store_wechat->mch_id;
- }
- }
- if (\Yii::$app->prod_is_dandianpu() && intval($from) === WechatConfig::TYPE_CONFIG_H5){
- unset($config['sub_appid']);
- }
- \Yii::error($config);
- self::$wechat_pay = Factory::payment($config);
- }
- /**
- * 初始化
- * @param string $type
- * @param integer $from
- * @throws \Exception
- */
- public static function initSaas($type = self::WECHAT_KIND_MINI) {
- if (!in_array($type, self::$validRequest)) {
- throw new \Exception('Input object type not available');
- }
- $wechat_config = [
- 'platform_appid' => Option::get('platform_appid', 0, 'saas')['value'],
- 'platform_mch_id' => Option::get('platform_mch_id', 0, 'saas')['value'],
- 'platform_key' => Option::get('platform_key', 0, 'saas')['value'],
- 'platform_apiclient_cert' => Option::get('platform_apiclient_cert', 0, 'saas')['value'],
- 'platform_apiclient_key' => Option::get('platform_apiclient_key', 0, 'saas')['value'],
- ];
- $config = [
- 'app_id' => $wechat_config['platform_appid'],
- 'secret' => $wechat_config['platform_key'],
- 'response_type' => 'array'
- ];
- if ($type != self::WECHAT_KIND_PAY) {
- if ($type == self::WECHAT_KIND_MINI) {
- self::$wechat_mini = Factory::miniProgram($config);
- } else {
- self::$wechat_official = Factory::officialAccount($config);
- }
- } else {
- // 证书
- if (!is_dir(\Yii::$app->runtimePath . '/saas/wechat/pem')) {
- mkdir(\Yii::$app->runtimePath . '/saas/wechat/pem');
- file_put_contents(\Yii::$app->runtimePath . '/saas/wechat/pem/index.html', '');
- }
- $cert_pem_file = null;
- if (!empty($wechat_config['platform_apiclient_cert'])) {
- $cert_pem_file = \Yii::$app->runtimePath . '/saas/wechat/pem/' . md5($wechat_config['platform_apiclient_cert']);
- if (!file_exists($cert_pem_file)) {
- file_put_contents($cert_pem_file, $wechat_config['platform_apiclient_cert']);
- }
- }
- $key_pem_file = null;
- if (!empty($wechat_config['platform_apiclient_key'])) {
- $key_pem_file = \Yii::$app->runtimePath . '/saas/wechat/pem/' . md5($wechat_config['platform_apiclient_key']);
- if (!file_exists($key_pem_file)) {
- file_put_contents($key_pem_file, $wechat_config['platform_apiclient_key']);
- }
- }
- $config['key'] = $wechat_config['platform_key'];
- $config['mch_id'] = $wechat_config['platform_mch_id'];
- $config['cert_path'] = $cert_pem_file;
- $config['key_path'] = $key_pem_file;
- \Yii::error($config);
- self::$wechat_pay = Factory::payment($config);
- }
- }
- /**
- * Undocumented function
- *
- * @Author LGL 24963@qq.com
- * @DateTime 2021-02-03
- * @desc: 实例化支付类
- * @return void
- */
- public static function initBusiness ($_from = WechatConfig::TYPE_CONFIG_MINI)
- {
- $wechat_config = new WechatConfig();
- $keys = [
- 'sp_name',
- 'sp_appid',
- 'sp_mch_id',
- 'sp_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');
- $wechat_config->app_id = $data['platform_appid'];
- $wechat_config->app_secret = $data['platform_serial_no'];
- $wechat_config->mch_id = $data['sp_mch_id'];
- $wechat_config->pay_key = $data['sp_key'];
- $wechat_config->cert_pem = $data['sp_apiclient_cert'];
- $wechat_config->key_pem = $data['sp_apiclient_key'];
- $config = [
- 'app_id' => $data['sp_appid'],
- //'secret' => $wechat_config->app_secret,
- 'key' => $wechat_config->pay_key,
- 'mch_id' => $wechat_config->mch_id,
- 'response_type' => 'array'
- ];
- if ($_from === WechatConfig::TYPE_CONFIG_APP) {
- $config['app_id'] = $data['platform_app_appid'];
- $config['app_secret'] = 'ec4cdee90d78680516eeef25f86b23a1';
- }
-
- // 证书
- 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 ($wechat_config->cert_pem) {
- $cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_config->cert_pem);
- if (!file_exists($cert_pem_file)) {
- file_put_contents($cert_pem_file, $wechat_config->cert_pem);
- }
- }
- $key_pem_file = null;
- if ($wechat_config->key_pem) {
- $key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_config->key_pem);
- if (!file_exists($key_pem_file)) {
- file_put_contents($key_pem_file, $wechat_config->key_pem);
- }
- }
- $config['cert_path'] = $cert_pem_file;
- $config['key_path'] = $key_pem_file;
- $config['sub_appid'] = $wechat_config->app_id;
- self::$wechat_pay = Factory::payment($config);
- }
- }
|