| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\utils\Wechat;
- use app\models\WechatConfig;
- use app\models\Option;
- use EasyWeChat\Factory;
- use yii\base\Exception;
- use app\models\Store;
- class Wechat
- {
- /**
- * 具体类型
- */
- 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 wechat对象配置
- */
- public static $wechat_config;
- /**
- * @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, $type = self::WECHAT_KIND_MINI, $from = WechatConfig::TYPE_CONFIG_MINI) {
- if (!in_array($type, self::$validRequest)) {
- throw new \Exception('Input object type not available');
- }
- // 获取微信配置进行初始化
- if($store_id == -1){ //商盟微信配置
- self::$wechat_config = new WechatConfig();
- $keys = [
- 'platform_appid',
- 'platform_mch_id',
- 'platform_key',
- 'platform_pay_key',
- 'platform_apiclient_cert',
- 'platform_apiclient_key',
- 'platform_api_key',
- 'platform_mch_appid',
- 'platform_mch_key'
- ];
- $data = Option::get($keys, 0, 'saas');
- $data = array_column($data, 'value','name');
- self::$wechat_config->app_id = $data['platform_appid'];
- self::$wechat_config->app_secret = $data['platform_key'];
- self::$wechat_config->mch_id = $data['platform_mch_id'];
- self::$wechat_config->pay_key = $data['platform_pay_key'];
- self::$wechat_config->cert_pem = $data['platform_apiclient_cert'];
- self::$wechat_config->key_pem = $data['platform_apiclient_key'];
- if (get_params('mini_type') === 'merchant' || \Yii::$app->prod_is_dandianpu()) {
- self::$wechat_config->app_id = $data['platform_mch_appid'];
- self::$wechat_config->app_secret = $data['platform_mch_key'];
- }
- }else{
- $self_mini = Option::get('self_mini', $store_id, 'store', 0)['value'];
- if (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming($store_id) && !intval($self_mini)) {
- if ((int)$from === 3) { //app逻辑
- $appid = Option::get('one_store_app_wechat_appid', 0, 'saas', '');
- $secret = Option::get('one_store_app_wechat_secret', 0, 'saas', '');
- $mch_id = Option::get('one_store_app_mch_id', 0, 'saas', '');
- $pay_key = Option::get('one_store_app_pay_key', 0, 'saas', '');
- $apiclient_cert = Option::get('one_store_app_apiclient_cert', 0, 'saas', '');
- $apiclient_key = Option::get('one_store_app_apiclient_key', 0, 'saas', '');
- } else {
- $appid = Option::get('one_store_wechat_appid', 0, 'saas', '');
- $secret = Option::get('one_store_wechat_secret', 0, 'saas', '');
- $mch_id = Option::get('one_store_mch_id', 0, 'saas', '');
- $pay_key = Option::get('one_store_pay_key', 0, 'saas', '');
- $apiclient_cert = Option::get('one_store_apiclient_cert', 0, 'saas', '');
- $apiclient_key = Option::get('one_store_apiclient_key', 0, 'saas', '');
- }
- self::$wechat_config = (object)[
- 'app_id' => $appid['value'],
- 'app_secret' => $secret['value'],
- 'mch_id' => $mch_id['value'],
- 'pay_key' => $pay_key['value'],
- 'cert_pem' => $apiclient_cert['value'],
- 'key_pem' => $apiclient_key['value'],
- ];
- } elseif (\Yii::$app->isSaas() && !Store::hasIncoming($store_id) && intval($from) !== WechatConfig::TYPE_CONFIG_H5) {
- $appid = Option::get('platform_appid', 0, 'saas', '');
- $secret = Option::get('platform_key', 0, 'saas', '');
- $mch_id = Option::get('platform_mch_id', 0, 'saas', '');
- $pay_key = Option::get('platform_pay_key', 0, 'saas', '');
- $apiclient_cert = Option::get('platform_apiclient_cert', 0, 'saas', '');
- $apiclient_key = Option::get('platform_apiclient_key', 0, 'saas', '');
- self::$wechat_config = (object)[
- 'app_id' => $appid['value'],
- 'app_secret' => $secret['value'],
- 'mch_id' => $mch_id['value'],
- 'pay_key' => $pay_key['value'],
- 'cert_pem' => $apiclient_cert['value'],
- 'key_pem' => $apiclient_key['value'],
- ];
- } else {
- //店铺微信配置
- self::$wechat_config = WechatConfig::findOne(['store_id' => $store_id, 'type' => $from]);
- }
-
- if (\Yii::$app->prod_is_dandianpu() && intval($from) === WechatConfig::TYPE_CONFIG_H5) {
- if(Store::hasIncoming($store_id)){
- self::$wechat_config = WechatConfig::findOne(['store_id' => $store_id, 'type' => $from]);
- }else{
- $wechat_config = Option::getSaasWechat();
- $sub_mch_id = Option::get('one_store_wechat_h5_sub_mch_id', 0, 'saas', '')['value'];
- self::$wechat_config = (object)[
- 'app_id' => $wechat_config['sp_appid'],
- 'app_secret' => $wechat_config['sp_key'],
- 'key' => $wechat_config['sp_key'],
- 'mch_id' => $wechat_config['sp_mch_id'],
- 'sub_mch_id' => $sub_mch_id,
- 'pay_key' => $wechat_config['sp_key'],
- 'cert_pem' => $wechat_config['sp_apiclient_cert'],
- 'key_pem' => $wechat_config['sp_apiclient_key'],
- ];
- }
- }
- if (\Yii::$app->prod_is_dandianpu() && intval($from) === WechatConfig::TYPE_CONFIG_MP) {
- $one_store_wechat_official_appid = Option::get('one_store_wechat_official_appid', 0, 'saas', '')['value'];
- $one_store_wechat_official_secret = Option::get('one_store_wechat_official_secret', 0, 'saas', '')['value'];
- $one_store_official_pay_key = Option::get('one_store_official_pay_key', 0, 'saas', '')['value'];
- $one_store_official_apiclient_cert = Option::get('one_store_official_apiclient_cert', 0, 'saas', '')['value'];
- $one_store_official_apiclient_key = Option::get('one_store_official_apiclient_key', 0, 'saas', '')['value'];
- $one_store_wechat_official_sub_mch_id = Option::get('one_store_wechat_official_sub_mch_id', 0, 'saas', '')['value'];
- self::$wechat_config = (object)[
- 'app_id' => $one_store_wechat_official_appid,
- 'app_secret' => $one_store_wechat_official_secret,
- 'mch_id' => $one_store_wechat_official_sub_mch_id,
- 'pay_key' => $one_store_official_pay_key,
- 'cert_pem' => $one_store_official_apiclient_cert,
- 'key_pem' => $one_store_official_apiclient_key,
- ];
- }
- }
- if (!self::$wechat_config) {
- throw new \Exception('Wechat App Is Null');
- }
- $config = [
- 'app_id' => self::$wechat_config->app_id,
- 'secret' => self::$wechat_config->app_secret,
- '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 . '/pem')) {
- mkdir(\Yii::$app->runtimePath . '/pem');
- file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', '');
- }
- $cert_pem_file = null;
- if (self::$wechat_config->cert_pem) {
- $cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5(self::$wechat_config->cert_pem);
- if (!file_exists($cert_pem_file)) {
- file_put_contents($cert_pem_file, self::$wechat_config->cert_pem);
- }
- }
- $key_pem_file = null;
- if (self::$wechat_config->key_pem) {
- $key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5(self::$wechat_config->key_pem);
- if (!file_exists($key_pem_file)) {
- file_put_contents($key_pem_file, self::$wechat_config->key_pem);
- }
- }
- $config['key'] = self::$wechat_config->pay_key;
- $config['mch_id'] = self::$wechat_config->mch_id;
- if(isset(self::$wechat_config->sub_mch_id)) {
- self::$wechat_config->sub_mch_id && $config['sub_mch_id'] = self::$wechat_config->sub_mch_id;
- }
- $config['cert_path'] = $cert_pem_file;
- $config['key_path'] = $key_pem_file;
- self::$wechat_pay = Factory::payment($config);
- if(\app\modules\admin\models\WastoreForm::$test && get_mini_id() == 88){
- $config = [
- 'app_id' => 'wx1ce41243db1e4373',
- 'secret' => '6563aceb2ef97e0d2f5b884f11e43544',
- 'response_type' => 'array'
- ];
- // $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']);
- // }
- $config['key'] = 'b1f76e762183a45e10e74ba0e16b4234';
- $config['mch_id'] = '1480763552';
- $config['cert_path'] = $cert_pem_file;
- $config['key_path'] = $key_pem_file;
- self::$wechat_pay = \EasyWeChat\Factory::payment($config);
- }
- \Yii::error($config);
- }
- }
- /**
- * jsapi ticket
- */
- public static function getTicketConfig (array $api_array = [], $url = null) {
- try {
- self::init(get_store_id(), self::WECHAT_KIND_OFFICIAL, WechatConfig::TYPE_CONFIG_MP);
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- $app = WechatMini::getWechatConfig(get_store_id(), 0, WechatMini::TYPE_OFFICIAL);
- if (!$app) {
- return [
- 'code' => 1,
- 'msg' => '没有对应的配置项'
- ];
- }
- $jsapi_params = $app->jssdk->getConfigArray($api_array, false, false, [], $url);
- \Yii::error($jsapi_params);
- return $jsapi_params;
- }
- }
|