Home > Article > Backend Development > Develop app WeChat payment interface based on PHP
This article mainly introduces the development of app WeChat payment interface based on php, which has certain reference value. Now I share it with everyone. Friends in need can refer to it
Define merchant payment information
define(APPID, $payment ['appId']); // appid
define(APPSECRET, $payment ['appSecret']); // appSecret
define(MCHID , $payment ['partnerId']); // Merchant number
define(KEY, $payment ['partnerKey']); // Pass encrypted string
define(NOTIFY_URL, $return_url ); // Success callback url
//签名所需 $signArray = array( 'appid' => $payment['appId'], //appid 'mch_id' => $payment['partnerId'], 'nonce_str' => self::createNoncestr(), 'out_trade_no' => $order['order_sn'], 'body' => $order['body'], "total_fee" => $order ['order_amount'], "notify_url" => $return_url, "spbill_create_ip" => $_SERVER["REMOTE_ADDR"], "trade_type" => "APP", );
$sign=self::getSign($signArray); //统一下单 $goPay = array( 'appid' => $payment['appId'], //appid 'mch_id' => $payment['partnerId'], 'nonce_str' => $signArray['nonce_str'], //生成随机字符串 'sign' => $sign, 'out_trade_no' => $order['order_sn'], "total_fee" => $order ['order_amount'], 'body' => $order['body'], "notify_url" => $return_url, "spbill_create_ip" => $_SERVER["REMOTE_ADDR"], "trade_type" => "APP", ); //转化xml $goPayXml=self::arrayToXml($goPay); $result=self::sendPrePayCurl($goPayXml); return $result;
Generate random string function
//随机生成字符串 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; }
Signature function
/** * 作用:生成签名 */ public function getSign($Obj) { foreach ($Obj as $k => $v) { //if($k == 'code') continue; //if($k == 'from') continue; $Parameters[$k] = $v; } //签名步骤一:按字典序排序参数 ksort($Parameters); $String = self::formatBizQueryParaMap($Parameters, false); //echo '【string1】'.$String.'</br>'; //签名步骤二:在string后加入KEY $String = $String . "&key=" . KEY; //echo "【string2】".$String."</br>"; //签名步骤三:MD5加密 $String = md5($String); //echo "【string3】 ".$String."</br>"; //签名步骤四:所有字符转为大写 $result_ = strtoupper($String); //echo "【result】 ".$result_."</br>"; return $result_; }
//格式化签名所需参数 public 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; }
Assemble the required request parameters into xml
/** * 作用:array转xml,把请求参数组装xml */ 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; }
//通过curl发送数据给微信接口的函数 public function sendPrePayCurl($xmlData) { $url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; $header[] = "Content-type: text/xml"; $curl = curl_init(); curl_setopt($curl, CURLOPT_HTTPHEADER, $header); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $xmlData); $data = curl_exec($curl); if (curl_errno($curl)) { print curl_error($curl); } curl_close($curl); return self::XMLDataParse($data); } //xml格式数据解析函数 public static function XMLDataParse($data) { $msg = array(); $msg = (array) simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA); return $msg; }
Related recommendations :
php video tutorial course introduction for developing movie website (imitation of iQiyi)
The above is the detailed content of Develop app WeChat payment interface based on PHP. For more information, please follow other related articles on the PHP Chinese website!