這篇文章主要介紹了關於基於php開發app微信支付接口,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下
定義好商家支付資訊
define(APPID, $payment ['appId']); // appid
define(APPSECRET, $payment ['appSecret']); // appSecret
define(MCHID , $payment ['partnerId']);//商家號碼
define(KEY, $payment ['partnerKey']); // 通加密字串
define(NOTIFY_URL, $return_url ); // 成功回呼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;
產生隨機字串函數
//随机生成字符串 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; }
簽名函數
/** * 作用:生成签名 */ 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; }
把所要請求參數組裝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; }
相關推薦:
#以上是基於php開發app微信支付接口的詳細內容。更多資訊請關注PHP中文網其他相關文章!