微信小程序支付功能的PHP实例详细信息
前端代码:
wx.request({ (
更改为URL : ' https://www.your host.com/weixin/Wei activity/payjoinfee ',//您自己的链接
header: {
' content-type ' : ' application/x-www-form-urlencoded '
(,
方法: '开机自检',
success :功能(RES ) {
控制台. log (RES.data;
console.log ()取现付款);
wx.requestPayment({ ()
' timestamp ' : RES.data.timestamp,
' nonce str ' : RES.data.nonce str,
' package': res.data.package,
' signType': 'MD5 ',
' paySign': res.data.paySign,
' success':function(RES )
控制台. log (success );
wx.showToast({ (
title:“成功支付”、
icon: 'success ',
duration: 3000
);
(,
'故障' :功能(RES ) {
控制台. log (' fail );
(,
'完成' :功能(RES )
控制台. log (complete );
}
);
(,
故障:功能(RES )
控制台. log (RES.data ) )。
}
);
后端代码:
//支付费用
公共函数支付工具(
$appid='wx888888888 ';
$openid='oCQwY0Q_pzrQpu8888888 ';
$mch_id='141388888 ';
$key='9A0A86888888888 ';
导入(weixin.lib.weixin pay );
$ weixin pay=newweixin pay ($ appid、$openid、$mch_id、$key );
$return=$weixinpay-pay ();
$this-response($return,' json ';
}
微信支付类
/*
*小程序微信支付
*/
class WeixinPay{
保护性$ appid;
保护性$ mch _ id;
保护性$ key;
保护性$ OpenID;
function__construct($appid、$openid、$mch_id、$key ) {
$this-appid=$appid;
$this-openid=$openid;
$this-mch_id=$mch_id;
$this-key=$key;
}
公共函数支付
//统一订单界面
$return=$this-weixinapp (;
返回$返回;
}
//统一订单界面
私有函数统一订单器
$ URL=' https://API.mch.weixin.QQ.com/pay/unified order ';
$parameters=array (
' appID'=$this-appid,//小程序id
' mch_id'=$this-mch_id,//商户编号
' nonce _ str '=$ this-createnoncestr ()、//随机字符串
' body'='测试',//商品说明
' out _ trade _ no '=' 2015450806125346 ',//商户订单编号
' total_Fee'=floatval(0.01*100 ),//以总额为单位
' sp bill _ create _ IP '=$ _ server [ ' remote _ addr ' ],//终端IP
' notify _ URL '=' http://www.weixin.QQ.com/wx pay/pay.PHP ',//通知收件人
' openid'=$this-openid,//用户id
' trade_type'='JSAPI'//事务类型
);
//汇总后下单
$ parameters [ ' sign ' ]=$ this-get sign $ parameters;
$ XML data=arraytoxml ($ parameters;
$ return=xmlto array (postxmlsslcurl ) $XMLdata,$url,60 );
返回$返回;
}
//微信小程序界面
隐私功能weixin app (
//统一订单界面
$ unified order=$ this-unified order (;
$parameters=array (
' appId'=$this-appID,//小程序id
' timeStamp'=''.time ().',//时间戳
' nonceStr'=$this-createNoncestr ()、//随机字符串
' package '=' prepay _ id='.$统一订单[ ' prepay _ id ' ],//数据包
' signType'='MD5'//签名方式
);
//签名
$ parameters [ ' pay sign ' ]=$ this-get sign ($ parameters );
返回$ parameters;
}
//作用:生成随机字符串,不小于32位
私有功能创建器($ length=32 ) {
$ chars=' abcdefghijklmnopqrstuvwxyz 0123456789 ';
$str=' ';
for($I=0; $i $length; $i ) {
$str.=substr($chars,mt_rand(0) 0,strlen ) $chars )-1 ),1 );
}
返回$ str;
}
//角色:生成签名
隐私功能getsign ($ obj ) {
Freach($objas$k=$v ) {
$Parameters[$k]=$v;
}
//签名步骤1 :按词典顺序对参数进行排序
ksort($parameters;
$ string=$ this-formatbizqueryparamap ($ parameters,false );
//在签名步骤string之后输入KEY
$ string=$ string.' key='.$ this-key;
//签名步骤3:MD5加密
$string=MD5($string;
//签名步骤4 :将所有字符大写
$result_=strtoupper($string;
return $result_;
}
//角色:必须设置参数格式并用于签名过程
私有功能表单bizqueryparamap ($ paramap、$urlencode ) {
$buff=' ';
kort($paramap;
Foreach($paramapas$k=$v ) {
if($urlencode ) ) )。
{
$v=urlencode($v;
}
$buff .=$k . '=' . $v.';
}
$reqPar;
if(strlen ) $buff )0) {
$reqpar=substr($buff,0,strlen ) $buff )-1 );
}
返回$ req par;
}
}
感谢您的阅读。 希望对大家有帮助。 感谢您对本网站的支持。