cxfoot/models/common/PluginService.php
2023-10-24 14:54:18 +08:00

89 lines
2.8 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2021-6-14
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\models\common;
use app\models\wechat\WechatApp;
use Wechat\Wechat;
use app\models\PaymentConfig;
use app\components\SysConst;
class PluginService
{
/**
* 获取微信小程序
* @param integer $cx_mch_id 平台商户ID
* return @Wechat
* @throws Exception
*/
public function getWxmpService($cx_mch_id = 0)
{
$wechat_app = WechatApp::findOne([
'cx_mch_id' => $cx_mch_id,
'is_delete' => 0
]);
if(!$wechat_app){
throw new \Exception("小程序信息尚未配置");
}
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_app->cert_pem) {
$cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_app->cert_pem);
if (!file_exists($cert_pem_file))
file_put_contents($cert_pem_file, $wechat_app->cert_pem);
}
$key_pem_file = null;
if ($wechat_app->key_pem) {
$key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_app->key_pem);
if (!file_exists($key_pem_file))
file_put_contents($key_pem_file, $wechat_app->key_pem);
}
$wechat_mp = new Wechat([
'appId' => $wechat_app->app_id,
'appSecret' => $wechat_app->app_secret,
'mchId' => $wechat_app->mch_id,
'apiKey' => $wechat_app->key,
'certPem' => $cert_pem_file,
'keyPem' => $key_pem_file,
'apiKey_three' => $wechat_app->key_three,
]);
return $wechat_mp;
}
/**
* 获取银联支付
* @param integer $cx_mch_id 平台商户ID
* return @Updc
* @throws Exception
*/
public function getUpdcService($cx_mch_id = 0, $is_https = true, $algorithm = \Updc\bean\Algorithm::SHA256withRSA)
{
$res = PaymentConfig::getPaymentConfig(SysConst::$cxPayTypeEnUnionpay, $cx_mch_id);
if($res['code'] != 0){
throw new \Exception($res['msg']);
}
$payment_config = $res['data'];
$updc = new \Updc\Updc([
'appId' => $payment_config['app_id'],
'mchId' => $payment_config['mch_id'],
'url' => $payment_config['api_url'],
'privateKeyPassword' => $payment_config['private_key_password'],
'privateKey' => $payment_config['private_key_pem_file'],
'publicKey' => $payment_config['public_key_pem_file'],
'isHttps' => $is_https,
'algorithm' => $algorithm,
]);
return $updc;
}
}