微信支付封装类 [ 技术分享 ]
<?php
namespace app\extensions\wechatcash;
use Yii;
error_reporting(E_ALL^E_NOTICE);
class Cashwallet{
/**
* hbname 红包名称 fee 红包金额 /元 body 内容 openid 微信用户id
* @return
*/
//=======【证书路径设置】=====================================
//证书路径,注意应该填写绝对路径
/* protected $SSLCERT_PATH = __DIR__ .'/wechatcashkey/apiclient_cert.pem';
protected $SSLKEY_PATH = __DIR__.'/wechatcashkey/apiclient_key.pem';
protected $ROOTCA = __DIR__.'/wechatcashkey/rootca.pem';*/
//=======【基本信息设置】=====================================
//微信公众号身份的唯一标识。审核通过后,在微信发送的邮件中查看
protected $APPID = '';
//受理商ID,身份标识(微信支付分配的商户号)
// protected $MCHID = '';
protected $MCHID = '';
//商户支付密钥Key。审核通过后,在微信发送的邮件中查看
/*protected $KEY = '';*/
protected $KEY = 'r';
//JSAPI接口中获取openid,审核后在公众平台开启开发模式后可查看
protected $APPSECRET = '';
protected $amt_type; //分裂红包参数,在sendgroup中进行定义,是常量 ALL_RAND
protected $total_num = 1;//发放人数。固定值1,不可修改
//JSAPI接口地址
protected $APPURL="https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
protected $APPGROUPURL="https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack";
//发送单个现金红包
public function sendhongbaoto($arr){
//$comm = new Common_util_pub();
$Parameters['mch_id'] = $this->MCHID;//微信支付分配的商户号
$Parameters['mch_billno'] = $this->MCHID.date("Ymd",time()).date("His",time()).rand(1111,9999);//商户订单号(每个订单号必须唯一)
$Parameters['nonce_str'] = self::createNoncestr();//随机字符串,不长于32位
$Parameters['re_openid'] = $arr['openid'];//接受收红包的用户,用户在wxappid下的openid
$Parameters['wxappid'] = $this->APPID;//商户appid
$Parameters['nick_name'] = $arr['hbname'];//提供方名称
$Parameters['send_name'] = $arr['hbname'];//红包发送者名称
$Parameters['total_amount'] = $arr['fee']*100;//付款金额,单位分
$Parameters['min_value'] = $arr['fee']*100;//最小红包金额,单位分
$Parameters['max_value'] = $arr['fee']*100;//最大红包金额,单位分
$Parameters['total_num'] = 1;//红包发放总人数
$Parameters['client_ip'] = $_SERVER['REMOTE_ADDR'];//调用接口的机器Ip地址
$Parameters['act_name'] = $arr['act_name'];//活动名称
$Parameters['remark'] = 'rebark';//备注信息
$Parameters['wishing'] = $arr['body'];//红包祝福语
if(!$Parameters['re_openid']) {
$rearr['return_msg']='absence user openid';
return $rearr;
}
//非必填
//$Parameters['logo_imgurl'] = '商户logo的url';//商户logo的url
//$Parameters['share_content']= '备注信息';//分享文案
//$Parameters['share_url']= '备注信息';//分享链接
//$Parameters['share_imgurl']= '备注信息';//分享的图片url
$Parameters['sign'] = self::getSign($Parameters);
$xml = self::arrayToXml($Parameters);
//var_dump($xml);
$re = self::postXmlSSLCurl($xml,$this->APPURL);
$rearr = self::xmlToArray($re);
return $rearr;
}
/**
* WXHongBao::sendGroup()
* 发送裂变红包,参数为裂变数量
* @param integer $num 3-20
* @return
*/
public function sendGroup($arr,$num=3){
$this->amt_type = "ALL_RAND";//$amt; 固定值。发送裂变红包组文档指定参数,随机
return $this->sendgrouphongbaoto($arr,$num);
}
public function sendgrouphongbaoto($arr,$total_num=1){
//$comm = new Common_util_pub();
$Parameters['mch_id'] = $this->MCHID;//微信支付分配的商户号
$Parameters['mch_billno'] = $this->MCHID.date("Ymd",time()).date("His",time()).rand(1111,9999);//商户订单号(每个订单号必须唯一)
$Parameters['nonce_str'] = self::createNoncestr();//随机字符串,不长于32位
$Parameters['re_openid'] = $arr['openid'];//接受收红包的用户,用户在wxappid下的openid
$Parameters['wxappid'] = $this->APPID;//商户appid
$Parameters['nick_name'] = $arr['hbname'];//提供方名称
$Parameters['send_name'] = $arr['hbname'];//红包发送者名称
$Parameters['total_amount'] = 3*100;//付款金额,单位分
$Parameters['min_value'] = $arr['fee']*100;//最小红包金额,单位分
$Parameters['max_value'] = 3*100;//最大红包金额,单位分
$Parameters['total_num'] = 3;//红包发放总人数
$Parameters['amt_type'] = "ALL_RAND";//红包发放总人数
$Parameters['client_ip'] = $_SERVER['REMOTE_ADDR'];//调用接口的机器Ip地址
$Parameters['act_name'] = $arr['act_name'];//活动名称
$Parameters['remark'] = 'rebark';//备注信息
$Parameters['wishing'] = $arr['body'];//红包祝福语
if(!$Parameters['re_openid']) {
$rearr['return_msg']='absence user openid';
return $rearr;
}
$this->total_num = $total_num;
//非必填
//$Parameters['logo_imgurl'] = '商户logo的url';//商户logo的url
//$Parameters['share_content']= '备注信息';//分享文案
//$Parameters['share_url']= '备注信息';//分享链接
//$Parameters['share_imgurl']= '备注信息';//分享的图片url
$Parameters['sign'] = self::getSign($Parameters);
$xml = self::arrayToXml($Parameters);
//var_dump($xml);
$re = self::postXmlSSLCurl($xml,$this->APPGROUPURL);
$rearr = self::xmlToArray($re);
return $rearr;
}
function trimString($value)
{
$ret = null;
if (null != $value)
{
$ret = $value;
if (strlen($ret) == 0)
{
$ret = null;
}
}
return $ret;
}
/**
* 作用:产生随机字符串,不长于32位
*/
public function createNoncestr( $length = 32 )
{
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str ="";
for ( $i = 0; $i < $length; $i++ ) {
$str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
}
return $str;
}
/**
* 作用:格式化参数,签名过程需要使用
*/
function formatBizQueryParaMap($paraMap, $urlencode)
{
$buff = "";
ksort($paraMap);
foreach ($paraMap as $k => $v)
{
if($urlencode)
{
$v = urlencode($v);
}
//$buff .= strtolower($k) . "=" . $v . "&";
$buff .= $k . "=" . $v . "&";
}
$reqPar;
if (strlen($buff) > 0)
{
$reqPar = substr($buff, 0, strlen($buff)-1);
}
return $reqPar;
}
/**
* 作用:生成签名
*/
public function getSign($Obj)
{
foreach ($Obj as $k => $v)
{
$Parameters[$k] = $v;
}
//签名步骤一:按字典序排序参数
ksort($Parameters);
$String = $this->formatBizQueryParaMap($Parameters, false);
//echo '【string1】'.$String.'</br>';
//签名步骤二:在string后加入KEY
// $String = $String."&key="."254554sefg4exxxxxxxxs5cds1";
$String = $String."&key=".$this->KEY; // 商户后台设置的key
//echo "【string2】".$String."</br>";
//签名步骤三:MD5加密
$String = md5($String);
//echo "【string3】 ".$String."</br>";
//签名步骤四:所有字符转为大写
$result_ = strtoupper($String);
//echo "【result】 ".$result_."</br>";
return $result_;
}
/**
* 作用:array转xml
*/
public function arrayToXml($arr)
{
$xml = "<xml>";
foreach ($arr as $key=>$val)
{
/*if (is_numeric($val))
{
$xml.="<".$key.">".$val."</".$key.">";
}
else*/
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
}
$xml.="</xml>";
return $xml;
}
/**
* 作用:将xml转为array
*/
public function xmlToArray($xml)
{
//将XML转为array
$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $array_data;
}
public function wxHttpsRequestPem( $vars,$url, $second=30,$aHeader=array()){
$ch = curl_init();
//超时时间
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
//这里设置代理,如果有的话
//curl_setopt($ch,CURLOPT_PROXY, '10.206.30.98');
//curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
//以下两种方式需选择一种
//第一种方法,cert 与 key 分别属于两个.pem文件
//默认格式为PEM,可以注释
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLCERT,Yii::$app->params['wechatcash']['apiclient_cert']);
//默认格式为PEM,可以注释
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLKEY,Yii::$app->params['wechatcash']['apiclient_key']);
curl_setopt($ch,CURLOPT_CAINFO,'PEM');
curl_setopt($ch,CURLOPT_CAINFO,Yii::$app->params['wechatcash']['rootca']);
//第二种方式,两个文件合成一个.pem文件
//curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/all.pem');
if( count($aHeader) >= 1 ){
curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
}
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
$data = curl_exec($ch);
//返回结果
if($data){
curl_close($ch);
return $data;
}
else {
$error = curl_errno($ch);
echo "call faild, errorCode:$error\n";
echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>error cause select</a></br>";
curl_close($ch);
return false;
}
}
/**
* 作用:使用证书,以post方式提交xml到对应的接口url
*/
function postXmlSSLCurl($xml,$url,$second=30)
{
$ch = curl_init();
//超时时间
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
//这里设置代理,如果有的话
//curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
//curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
//设置header
curl_setopt($ch,CURLOPT_HEADER,FALSE);
//要求结果为字符串且输出到屏幕上
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
//设置证书
//第一种方法,cert 与 key 分别属于两个.pem文件
//默认格式为PEM,可以注释
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLCERT,Yii::$app->params['wechatcash']['apiclient_cert']);
//默认格式为PEM,可以注释
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLKEY,Yii::$app->params['wechatcash']['apiclient_key']);
curl_setopt($ch,CURLOPT_CAINFO,'PEM');
curl_setopt($ch,CURLOPT_CAINFO,Yii::$app->params['wechatcash']['rootca']);
//post提交方式
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);
$data = curl_exec($ch);
//返回结果
if($data){
curl_close($ch);
return $data;
}
else {
$error = curl_errno($ch);
echo "curl出错,错误码:$error"."<br>";
echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>错误原因查询</a></br>";
curl_close($ch);
return false;
}
}
}
共 0 条回复
没有找到数据。
cleargo 北京
注册时间:2016-12-05
最后登录:2024-11-11
在线时长:53小时29分
最后登录:2024-11-11
在线时长:53小时29分
- 粉丝33
- 金钱21705
- 威望30
- 积分22535