This article mainly introduces the development of PHP based on the application payment interface, has a certain reference value, now share to everyone, the need for friends can refer to
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-Through encryption string
Define (Notify_url, $return _url); Successful callback URL
The signature required $signArray = Array ( ' appid ' = = $payment [' AppID '],//appid ' mch_id ' = ' + $payment [' Partnerid '], ' nonce_str ' + self::createnoncestr (), ' out_trade_no ' = $order [' order_sn '], ' body ' and ' = ' $order [ ' Body '], "total_fee" = $order [' Order_amount '], "Notify_url" and "= $return _url, " spbill_create_ip "= > $_server["REMOTE_ADDR"], "Trade_type" and "APP", );
$sign =self::getsign ($signArray); Unify the order $goPay = Array ( ' appid ' = = $payment [' AppID '],//appid ' mch_id ' + $payment [' Partnerid '], ' nonce_str ' = $signArray [' nonce_str '],//Generate random string ' sign ' and $sign, ' out_trade_no ' and $order [' Order_sn '], "total_fee" = = $order [' Order_amount '], ' body ' and ' = ' $order [' body '], ' notify_url ' = $ Return_url, "spbill_create_ip" = $_server["REMOTE_ADDR"], "Trade_type" and "APP", ); Convert XML $goPayXml =self::arraytoxml ($goPay); $result =self::sendprepaycurl ($goPayXml); return $result;
Generating random String functions
Randomly generated string public function Createnoncestr ($length = +) { $chars = "abcdefghijklmnopqrstuvwxyz0123456789"; $str = ""; for ($i = 0; $i < $length; $i + +) { $str. = substr ($chars, Mt_rand (0, strlen ($chars)-1), 1); } return $str; }
Signature functions
/** * Function: Generate signature */Public Function getsign ($OBJ) {foreach ($Obj as $k = = $v) {//if ($k = = ' code ') continue; if ($k = = ' from ') continue; $Parameters [$k] = $v; }//Signature step one: sorted by dictionary order parameter Ksort ($Parameters); $String = Self::formatbizqueryparamap ($Parameters, false); Echo ' "string1" '. $String. ' </br> '; Signature Step Two: Add key $String = $String after String. "&key=". KEY; echo "string2" ". $String." </br> "; Signature Step Three: MD5 encryption $String = MD5 ($String); echo "String3" ". $String." </br> "; Signature Step four: All characters to uppercase $result _ = Strtoupper ($String); echo "Result". $result _. " </br> "; return $result _; }
The parameters required to format the signature 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 requested parameter into the XML
/** * Function: Array to XML, assemble request parameters XML */function Arraytoxml ($arr) {$xml = "<xm L> "; foreach ($arr as $key = + $val) {if (Is_numeric ($val)) {$xml. = "<". $key. " > ". $val." </". $key." > "; } else $xml. = "<". $key. " ><! [cdata[]. $val. "] ></". $key." > "; } $xml. = "</xml>"; return $xml; }
//a function that sends data to an interface via Curl Public function Sendprepaycurl ($xmlData) {$url = "http S://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 Format data parsing function public static function Xmldataparse ($data) {$msg = array (); $msg = (array) simplexml_load_string ($data, ' simplexmlelement ', libxml_nocdata); return $msg; }