BaseController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\controllers;
  8. use app\models\Option;
  9. use app\models\Store;
  10. use app\models\WechatConfig;
  11. use ByteDance\Kernel\Exceptions\Exception;
  12. use EasyWeChat\Factory;
  13. use Yansongda\Pay\Pay;
  14. use yii\helpers\Json;
  15. use yii\web\Controller;
  16. use yii\helpers\ArrayHelper;
  17. use app\modules\alliance\behaviors\Auth;
  18. /**
  19. * @property WechatConfig $wechat_config
  20. * @property Factory $wechat
  21. */
  22. class BaseController extends Controller
  23. {
  24. public $store_id;
  25. public $wechat_config; //微信config
  26. public $wechat; //微信处理类
  27. public $wechatPay;
  28. public $wechatMini; // 微信小程序
  29. public $platform; // wechat(微信), alipay(支付宝), douyin(抖音)
  30. public $is_platform; // yes or no; 是否是平台小程序
  31. const IS_PLATFORM_YES = 'yes';
  32. const IS_PLATFORM_NOT = 'no';
  33. /**
  34. * @var \ByteDance\MiniProgram\Application $byteDance
  35. */
  36. public $byteDance;
  37. public $alipay_config;
  38. public $aliPay;
  39. /**
  40. * @return array
  41. */
  42. public function behaviors()
  43. {
  44. $merge = [
  45. [
  46. 'class' => Auth::class,
  47. ]
  48. ];
  49. return ArrayHelper::merge($merge, parent::behaviors());
  50. }
  51. /**
  52. * 初始化操作
  53. */
  54. public function init()
  55. {
  56. parent::init();
  57. $this->store_id = get_params('store_id', 1);
  58. $this->platform = input_params('platform', 'wechat');
  59. $this->is_platform = input_params('is_platform', 'no');
  60. if ($this->platform != 'alipay') {
  61. $this->initWXPay();
  62. }
  63. // 抖音小程序
  64. if ($this->platform == 'bytedance') {
  65. $this->initBytedance();
  66. }
  67. }
  68. public function initWX()
  69. {
  70. // TODO: 小程序为例
  71. // 获取微信配置进行初始化
  72. $_form = input_params('_form', 'mini');
  73. $type = 1;
  74. if ($_form == 'h5') {
  75. $type = 2;
  76. }
  77. if ($_form == 'app') {
  78. $type = 3;
  79. }
  80. $this->wechat_config = WechatConfig::findOne(['store_id' => get_store_id(), 'type' => $type]);
  81. if ($this->wechat_config) {
  82. $config = [
  83. 'app_id' => $this->wechat_config->app_id,
  84. 'secret' => $this->wechat_config->app_secret,
  85. 'response_type' => 'array'
  86. ];
  87. $this->wechat = Factory::miniProgram($config);
  88. }
  89. }
  90. /**
  91. * Undocumented function
  92. *
  93. * @Author LGL 24963@qq.com
  94. * @DateTime 2021-02-03
  95. * @desc: 实例化支付类
  96. * @return void
  97. */
  98. protected function initWXPay ()
  99. {
  100. $this->wechat_config = new WechatConfig();
  101. $keys = [
  102. 'sp_name',
  103. 'sp_appid',
  104. 'sp_mch_id',
  105. 'sp_key',
  106. 'platform_key',
  107. 'sp_apiclient_cert',
  108. 'sp_apiclient_key',
  109. 'platform_api_key',
  110. 'platform_serial_no',
  111. 'platform_appid',
  112. 'platform_app_appid'
  113. ];
  114. $data = Option::get($keys, 0, 'saas');
  115. $data = array_column($data, 'value','name');
  116. $this->wechat_config->app_id = $data['sp_appid'];
  117. $this->wechat_config->app_secret = $data['platform_serial_no'];
  118. $this->wechat_config->mch_id = $data['sp_mch_id'];
  119. $this->wechat_config->pay_key = $data['sp_key'];
  120. $this->wechat_config->cert_pem = $data['sp_apiclient_cert'];
  121. $this->wechat_config->key_pem = $data['sp_apiclient_key'];
  122. $config = [
  123. // 'app_id' => $this->wechat_config->app_id,
  124. 'secret' => $this->wechat_config->app_secret,
  125. 'key' => $this->wechat_config->pay_key,
  126. // 'mch_id' => $this->wechat_config->mch_id,
  127. 'response_type' => 'array'
  128. ];
  129. $config['app_id'] = $data['platform_appid'];
  130. $config['mch_id'] = $data['sp_mch_id'];
  131. $_from = input_params('_from');
  132. if ($_from === 'app') {
  133. $config['app_id'] = $data['platform_app_appid'];
  134. }
  135. // 证书
  136. if (!is_dir(\Yii::$app->runtimePath . '/pem')) {
  137. mkdir(\Yii::$app->runtimePath . '/pem');
  138. file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', '');
  139. }
  140. $cert_pem_file = null;
  141. if ($this->wechat_config->cert_pem) {
  142. $cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($this->wechat_config->cert_pem);
  143. if (!file_exists($cert_pem_file)) {
  144. file_put_contents($cert_pem_file, $this->wechat_config->cert_pem);
  145. }
  146. }
  147. $key_pem_file = null;
  148. if ($this->wechat_config->key_pem) {
  149. $key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($this->wechat_config->key_pem);
  150. if (!file_exists($key_pem_file)) {
  151. file_put_contents($key_pem_file, $this->wechat_config->key_pem);
  152. }
  153. }
  154. $config['cert_path'] = $cert_pem_file;
  155. $config['key_path'] = $key_pem_file;
  156. $this->wechatPay = Factory::payment($config);
  157. $config = [
  158. 'app_id' => $data['platform_appid'],
  159. 'secret' => $data['platform_key']
  160. ];
  161. $this->wechatMini = Factory::miniProgram($config);
  162. }
  163. /**
  164. * 支付宝初始化操作
  165. */
  166. public function initAlipay() {
  167. $config_cache = \Yii::$app->cache->get('alipay_config_cache_' . get_store_id());
  168. if ($config_cache) {
  169. $this->alipay_config = Json::decode($config_cache);
  170. } else {
  171. $this->alipay_config = Json::decode(Option::get(Option::OPTOPN_KEY, get_store_id(), 'alipay')['value']);
  172. }
  173. $config = [
  174. 'app_id' => $this->alipay_config['app_id'] ?? '',
  175. 'notify_url' => 'www.baidu.com',
  176. 'return_url' => '',
  177. 'ali_public_key' => $this->alipay_config['alipay_public_key'] ?? '',
  178. 'private_key' => preg_replace('# #', '', $this->alipay_config['app_private_key'] ?? ''),
  179. // 'log' => [ // optional
  180. // 'file' => '~/var/logs/alipay.log',
  181. // 'level' => 'info', // 建议生产环境等级调整为 info,开发环境为 debug
  182. // 'type' => 'single', // optional, 可选 daily.
  183. // 'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
  184. // ],
  185. 'http' => [
  186. 'timeout' => 5.0,
  187. 'connect_timeout' => 5.0,
  188. ],
  189. // 'mode' => 'dev', // optional,设置此参数,将进入沙箱模式
  190. ];
  191. $this->aliPay = Pay::alipay($config);
  192. }
  193. // 抖音初始化操作
  194. public function initBytedance() {
  195. $config = [
  196. 'app_id' => '',
  197. 'app_secret' => '',
  198. 'response_type' => 'array'
  199. ];
  200. if ($this->is_platform == self::IS_PLATFORM_NOT) {
  201. $bytedance_config = Option::get('douyin', $this->store_id, 'store');
  202. if (!empty($bytedance_config['value'])) {
  203. $bytedance_config = Json::decode($bytedance_config['value']);
  204. $config['app_id'] = $bytedance_config['app_id'];
  205. $config['app_secret'] = $bytedance_config['app_secret'];
  206. }
  207. } else {
  208. $bytedance_config = Option::getSaasPlatformBytedance();
  209. $config['app_id'] = $bytedance_config['appid'];
  210. $config['app_secret'] = $bytedance_config['key'];
  211. }
  212. if (!empty($config['app_id']) && !empty($config['app_secret'])) {
  213. $this->byteDance = \ByteDance\Factory::miniProgram($config);
  214. }
  215. }
  216. }