canyin-project/ybcy/models/common/ZiJie.php

220 lines
9.2 KiB
PHP
Raw Permalink Normal View History

2024-11-01 16:07:54 +08:00
<?php
namespace app\models\common;
use Yii;
use yii\db\ActiveRecord;
class ZiJie extends ActiveRecord{
public static function login($uniacid,$code){
$set = Config::getSystemSet('toutiao', $uniacid);
$appId = $set['appKey'];
$secret = $set['appSecret'];
$url = "https://developer.toutiao.com/api/apps/jscode2session?appid=" . $appId . "&secret=" . $secret . "&code=" . $code;
$res = httpRequest($url);
$data = json_decode($res, true);
return $data;
}
//老版支付
public static function pay($uniacid,$outTradeNo,$money,$note,$openId,$orderType){
$set = Config::getSystemSet('toutiao', $uniacid);
$secret = $set['paySecret'];
$appId = $set['payAppId'];
$merchantId = $set['mchId'];
if(Yii::$app->params['isDev']==true){
$url=Yii::$app->request->hostInfo.'/addons/yb_wm/index.php/channelApi/pay-notice/ali-notice';
}else{
$url=Yii::$app->request->hostInfo.'/index.php/channelApi/pay-notice/ali-notice';
}
$biz_content = array(
'out_order_no' => $outTradeNo,
'uid' => $openId,
'merchant_id' => $merchantId,
'total_amount' => $money * 100,
'currency' => 'CNY',
'subject' => $note,
'body' => $note,
'trade_time' => time(),
'valid_time' => '60',
'notify_url' => $url,
'risk_info' => '127.0.0.1',
);
$biz_content = json_encode($biz_content);
$data['app_id'] = $appId;
$data['biz_content'] = $biz_content;
$data['charset'] = 'utf-8';
$data['method'] = 'tp.trade.create';
$data['sign_type'] = 'MD5';
$data['timestamp'] = time();
$data['version'] = '1.0';
ksort($data);
$var = '';
foreach ($data as $key => $value) {
$var .= $key . '=' . $value . '&';
}
$var = trim($var, '&');
$c = $var . $secret;
$data['sign'] = MD5($c);
ksort($data);
$vars = 'app_id=' . $data['app_id'] . '&biz_content=' . $data['biz_content'] . '&charset=' . $data['charset'] . '&method=' . $data['method'] . '&sign=' . $data['sign'] . '&sign_type=' . $data['sign_type'] . '&timestamp=' . $data['timestamp'] . '&version=' . $data['version'];
$url = 'https://tp-pay.snssdk.com/gateway';
$res = httpRequest($url, $vars);
// print_R($res);die;
$data['biz_content'] = json_decode($data['biz_content'], true);
$data['trade_no'] = json_decode($res, true)['response']['trade_no'];
$data['aliUrl'] = Ali::zjPay($uniacid,$outTradeNo, $money,$note,$orderType)['data'];
$list['app_id'] = $appId; // test
$list['method'] = 'tp.trade.confirm';
$list['sign_type'] = 'MD5';
$list["uid"] = $openId;
$list["total_amount"] = $money * 100;
$list['timestamp'] = $data['timestamp'];
$list['pay_channel'] = "ALIPAY_NO_SIGN";
$list["pay_type"] = "ALIPAY_APP";
$list["trade_no"] = $data['trade_no'];
$list["merchant_id"] = $merchantId;
$list["risk_info"] = "127.0.0.1";
$arrParam = array("url" => $data['aliUrl']);
$strParam = json_encode($arrParam);
$list["params"] = $strParam;
$list['url'] = $data['aliUrl'];
$signKeys = array("app_id", "sign_type", "timestamp", "trade_no", "merchant_id", "uid", "total_amount", "params");
$signData = array();
foreach ($signKeys as $v) {
$signData[$v] = $list[$v];
}
ksort($signData);
$var = '';
foreach ($signData as $key => $value) {
$var .= $key . '=' . $value . '&';
}
$var = trim($var, '&');
$data['sign'] = MD5($var . $secret);
//$data['sign'] = json_decode($res, true)['sign'];
return $data;
}
public static function sign($map,$salt) {
$rList = array();
foreach($map as $k =>$v) {
if ($k == "other_settle_params" || $k == "app_id" || $k == "sign" || $k == "thirdparty_id")
continue;
$value = trim(strval($v));
$len = strlen($value);
if ($len > 1 && substr($value, 0,1)=="\"" && substr($value,$len, $len-1)=="\"")
$value = substr($value,1, $len-1);
$value = trim($value);
if ($value == "" || $value == "null")
continue;
array_push($rList, $value);
}
array_push($rList, $salt);
sort($rList, 2);
return md5(implode('&', $rList));
}
public static function noticeSign($map,$token) {
$rList = array();
foreach($map as $k =>$v) {
if ($k == "msg_signature" || $k == "type")
continue;
$value = trim(strval($v));
$len = strlen($value);
if ($len > 1 && substr($value, 0,1)=="\"" && substr($value,$len, $len-1)=="\"")
$value = substr($value,1, $len-1);
$value = trim($value);
if ($value == "" || $value == "null")
continue;
array_push($rList, $value);
}
array_push($rList, $token);
sort($rList, 2);
return sha1(implode('', $rList));
}
//新版支付
public static function pay2($uniacid,$outTradeNo,$money,$note,$openId,$orderType,$url){
//https://developer.toutiao.com/api/apps/ecpay/v1/create_order
$set = Config::getSystemSet('toutiao', $uniacid);
$appId = $set['appKey'];
$data['app_id']=$appId;
$data['out_order_no']=$outTradeNo;
$data['total_amount']=intval($money * 100);
$data['subject']=$note;
$data['body']=$note;
$data['valid_time']=15*60;
$data['notify_url']=$url;
$data['cp_extra']=json_encode(['orderType'=>$orderType,'uniacid'=>$uniacid]);
$data['sign']=self::sign($data,$set['salt']);
// var_dump($data);die;
$res=httpRequest('https://developer.toutiao.com/api/apps/ecpay/v1/create_order',json_encode($data));
// file_put_contents('toutiaopay.log',json_encode(json_decode($res,true)['data']));
return json_decode($res,true)['data'];
}
public static function refund($uniacid,$outTradeNo,$money,$refundNo){
// if(Yii::$app->params['isDev']==true){
// $url=Yii::$app->request->hostInfo.'/addons/yb_wm/index.php/channelApi/pay-notice/tou-tiao-refund-notice';
// }else{
// $url=Yii::$app->request->hostInfo.'/index.php/channelApi/pay-notice/tou-tiao-refund-notice';
// }
$set = Config::getSystemSet('toutiao', $uniacid);
$appId = $set['appKey'];
$data['app_id']=$appId;
$data['out_order_no']=strval($outTradeNo);
$data['out_refund_no']=strval($refundNo);
$data['refund_amount']=intval($money * 100);
$data['reason']='退款';
// $data['cp_extra']=json_encode(['orderType'=>$orderType,'uniacid'=>$uniacid]);
// $data['notify_url']=$url;
$data['sign']=self::sign($data,$set['salt']);
$res=httpRequest('https://developer.toutiao.com/api/apps/ecpay/v1/create_refund',json_encode($data));
$res=json_decode($res,true);
if($res['err_no']==0){
return true;
}
echo json_encode(['code'=>2,'msg'=>$res['err_tips']]);die;
}
//今日头条获取token
public static function getTouToken($uniacid){
$cache = Yii::$app->cache;
$cacheData = $cache->get('zijie_token');
if ($cacheData === false) {
$set = Config::getSystemSet('toutiao', $uniacid);
$appId = $set['appKey'];
$secret = $set['appSecret'];
$url='https://developer.toutiao.com/api/apps/token?appid='.$appId.'&secret='.$secret.'&grant_type=client_credential';
$res=httpRequest($url);
$cacheData =json_decode($res,true)['access_token'];
//set方法的第一个参数是我们的数据对应的key值方便我们获取到
//第二个参数即是我们要缓存的数据
//第三个参数是缓存时间如果是0意味着永久缓存。默认是0
$cache->set('zijie_token', $cacheData, 7000);
}
return $cacheData;
}
//今日头条创建二维码
public static function createCode($uniacid,$module,$path,$ident,$appName='toutiao'){
//二维码宽度,单位 px最小 280px最大 1280px默认为 430px
$data['access_token']=self::getTouToken($uniacid);
$data['appname']=$appName;
$data['path']=$path;
$data['width']=430;
$postData=json_encode($data,JSON_UNESCAPED_UNICODE);
$header[]='content-type: application/json';
$res=httpRequest('https://developer.toutiao.com/api/apps/qrcode',$postData,$header);
$dir="web/static/".$module."/".$uniacid."/ziJieCode";
if(!file_exists($dir)){
mkdir($dir,0777,true);
}
$file=$ident.$uniacid.'.jpg';
file_put_contents($dir.'/'.$file,$res);
//微擎版本
if(Yii::$app->params['isDev']==true){
$host=Yii::$app->request->hostInfo.'/addons/'.$module.'/';
}else{
$host=Yii::$app->request->hostInfo.'/';
}
return $host.$dir."/".$file;
}
}