时间:2021-07-01 10:21:17 帮助过:7人阅读
定义好商户支付信息
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开发电影网站视频教程课程介绍(仿爱奇艺)
以上就是基于php开发app微信支付接口的详细内容,更多请关注Gxl网其它相关文章!