您好,登录后才能下订单哦!
app微信支付类封装如何理解,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
<?php
namespace MyClass;
class WechatPay
{
public function createJsBizPackage($totalFee, $outTradeNo, $orderName, $timestamp){
$unified = array(
'appid' => WX_APPID,
'attach' => '支付',
'body' => $orderName,
'mch_id' => WX_MACID,
'nonce_str' => self::createNonceStr(),
'notify_url' => WX_URL,
'out_trade_no' => $outTradeNo,
'spbill_create_ip' => '127.0.0.1',
'total_fee' => intval($totalFee * 100),
'trade_type' => 'APP',
);
$unified['sign'] = self::getSign($unified);
$responseXml = self::curlPost('https://api.mch.weixin.qq.com/pay/unifiedorder', self::arrayToXml($unified));
$unifiedOrder = (array)simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
if ($unifiedOrder === false) {
die('parse xml error');
}
if ($unifiedOrder['return_code'] != 'SUCCESS') {
die($unifiedOrder['return_msg']);
}
if ($unifiedOrder['result_code'] != 'SUCCESS') {
die($unifiedOrder['err_code']);
}
$arr = array(
"appid" => WX_APPID,
'partnerid'=>WX_MACID,
"timestamp" => $timestamp,
"noncestr" => $unifiedOrder['nonce_str'],
"package" => 'Sign=WXPay',
"prepayid"=>$unifiedOrder['prepay_id'],
);
$data['paySign'] = self::getSign($arr);
$data['appId']=WX_APPID;
$data['timeStamp']=$timestamp;
$data['nonceStr']=$unifiedOrder['nonce_str'];
$data['prepay_id']=$unifiedOrder['prepay_id'];
$data['trade_type']=$unifiedOrder['trade_type'];
$data['mchid']=WX_MACID;
return $data;
}
public static function curlGet($url = '', $options = array()){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
if (!empty($options)) {
curl_setopt_array($ch, $options);
}
//https请求 不验证证书和host
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
public static function curlPost($url = '', $postData = '', $options = array()){
if (is_array($postData)) {
$postData = http_build_query($postData);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
if (!empty($options)) {
curl_setopt_array($ch, $options);
}
//https请求 不验证证书和host
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
public static function createNonceStr($length = 16){
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$str = '';
for ($i = 0; $i<$length; $i++){
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
public static 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;
}
public static function getSign($params){
ksort($params, SORT_STRING);
$unSignParaString = self::formatQueryParaMap($params, false);
$signStr = strtoupper(md5($unSignParaString . "&key=" . WX_KEY));
return $signStr;
}
protected static function formatQueryParaMap($paraMap, $urlEncode = false){
$buff = "";
ksort($paraMap);
foreach ($paraMap as $k => $v){
if (null != $v && "null" != $v) {
if ($urlEncode) {
$v = urlencode($v);
}
$buff .= $k . "=" . $v . "&";
}
}
$reqPar = '';
if (strlen($buff)>0) {
$reqPar = substr($buff, 0, strlen($buff) - 1);
}
return $reqPar;
}
}
看完上述内容,你们掌握app微信支付类封装如何理解的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。