用户端uniapp接入微信app支付,服务端使用php处理。
1、微信支付统一下单
调用接口:https://api.mch.weixin.qq.com/pay/unifiedorder
需提前到微信开放平台(微信开放平台)申请移动应用获得应用Appid;前往微信商户(微信支付 - 中国领先的第三方支付平台 | 微信支付提供安全快捷的支付方式)申请开通App支付权限,获得商户mchid,v2支付秘钥。
2、创建支付订单
在项目controller目录中wxpay.php,并且创建支付订单
//创建微信支付订单
public function index(){
$WX_PAY_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder"; //统一下单请求链接
$NOTIFY_URL = "https://xxxx/notify"; //回调链接
$WX_APPID = ''; //微信开放平台appid
$WX_MCHID = ''; //商户号
$WX_MCHKEY = ''; //微信商户v2支付秘钥
$out_trade_no=$this->getOuttRadeno(); //获取商户内部订单号
$nonce_str = $this->randCode(); //生成随机字符串
$data['appid'] = $WX_APPID; //微信开放平台appid
$data['mch_id'] = $WX_MCHID; //商户号
$data['body'] = '商品1'; //支付描述
$data['spbill_create_ip'] = $this->getIp(); //ip地址
$data['total_fee'] = '100'; //金额:单位分
$data['out_trade_no'] =$out_trade_no; //商户内部订单号
$data['nonce_str'] = $nonce_str; //随机字符串
$data['notify_url'] = $NOTIFY_URL; //回调地址,必须为能直接访问的网址,不能拼接参数
$data['trade_type'] = 'APP'; //支付方式
$data['sign'] = $this->getSign($data,$WX_MCHKEY); //获取签名
$xml = $this->ToXml($data); //数组转xml
//发起请求
$data = $this->curlHttpRequest($WX_PAY_URL, $xml);
//返回结果
if($data){
//返回成功,将xml数据转换为数组.
$re = $this->FromXml($data);
if($re['return_code'] != 'SUCCESS'){
echo json_encode(array("code"=>0,'msg'=>'签名失败'));die;
}else{
//创建新订单
//接收微信返回的数据,传给uniapp客户端
$arr =array('prepayid' =>$re['prepay_id'],'appid' => $WX_APPID,'partnerid' => $WX_MCHID,'package' => 'Sign=WXPay','noncestr' => $nonce_str,'timestamp' =>time());
//第二次生成签名
$sign = $this->getSign($arr,$WX_MCHKEY);
$arr['sign'] = $sign;
echo json_encode(array("code"=>1,'orderinfo'=>$arr));die;
}
} else {
$error = curl_errno($ch);
curl_close($ch);
echo json_encode(array("code"=>0,'msg'=>'签名失败'.$error));die;
}
}
生成商家内部订单号:$this->getOuttRadeno();
//生成商家订单号,根据项目自行生成,不必和下面一致
public function getOuttRadeno(){
$str = 'YZ'.time().mt_rand(1000,9999);
return $str;
}
生成随机字符串:$this->randCode();
//生成32位随机字符串
public function randCode(){
$str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';//62个字符
$str = str_shuffle($str);
$str = substr($str,0,32);
return $str;
}
获取用户端Ip地址:$this->getIp();
//获取ip地址
public function getIp(){
if ($_SERVER['REMOTE_ADDR']) {
$ip = $_SERVER['REMOTE_ADDR'];
} elseif (getenv("REMOTE_ADDR")) {
$ip = getenv("REMOTE_ADDR");
} elseif (getenv("HTTP_CLIENT_IP")) {
$ip = getenv("HTTP_CLIENT_IP");
} else {
$ip = "unknown";
}
return $ip;
}
获取微信支付签名:$this->getSign();
//获取微信支付签名
public function getSign($params,$skey){
ksort($params);
$string = '';
foreach ($params as $key => $value) {
if ($key != 'sign' && $value != '') {
$string .= "{$key}={$value}&";
}
}
$string .= "key=" . $skey;
return strtoupper(md5($string));
}
将数组转为xml:$this->ToXml();
//数组转xml
public function ToXml($array){
if(!is_array($array)|| count($array) <= 0){
return ;
}
$xml = '<xml version="1.0">';
foreach ($array as $key=>$val){
if (is_numeric($val)){
$xml.="<".$key.">".$val."</".$key.">";
}else{
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
}
}
$xml.="</xml>";
return $xml;
}
发起请求:$this->curlHttpRequest();
//发起请求
public function curlHttpRequest($url,$xml){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
if(stripos($url,"https://")!==FALSE){
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
} else {
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//严格校验
}
//设置header
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//设置超时
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, TRUE);
//传输文件
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$result = curl_exec($ch);
curl_close($ch);
//返回结果
return $result;
}
返回成功,将xml数据转换为数组;$this->FromXml();
//xml格式转换为数组
public function FromXml($xml){
if(!$xml){
throw new WxPayException("xml数据异常!");
}
//将XML转为array
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
$this->values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $this->values;
}
最后在微信App支付回调进行交易验签
// 微信支付回调
public function notify(){
$xmlData = file_get_contents('php://input');
//将xml格式转换为数组
$data = $this->FromXml($xmlData);
$WX_MCHKEY = ''; //微信商户v2支付秘钥
//为了交易安全需进行验证签名。
$sign = $data['sign'];
unset($data['sign']);
//校验签名
if($sign == $this->getSign($data,$WX_MCHKEY)){
//交易成功
if ($data['result_code'] == 'SUCCESS' && $data['return_code'] == 'SUCCESS') {
//根据返回的订单号做业务逻辑
//向微信返回交易成功
exit('<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>');
}else{
}
}
}
三、uniapp客户端发起支付
首先uniapp勾选微信支付权限
uniapp创建微信支付
//orderinfo 服务端返回的订单信息
uni.requestPayment({
"provider": "wxpay",
"orderInfo": {
"appid": orderinfo.appid,
"noncestr": orderinfo.noncestr, // 随机字符串
"package": "Sign=WXPay", // 固定值
"partnerid": orderinfo.partnerid,
"prepayid": orderinfo.prepayid,
"timestamp": orderinfo.timestamp, // 时间戳(单位:秒)
"sign": orderinfo.sign // 签名
},
success(res) {
//console.log('success:' + JSON.stringify(res));
//支付成功,为了确保支付安全在,请确定服务端支付回调notify验证是否交易成功
//处理其他业务
},
fail(err) {
}
})
以上就是全部教程了。