1.掃碼支付
/**
* @author chantrans
* 本頁面的作用是生成商品二維碼鏈接
*/
echo '';
/**
* 二維碼掃碼鏈接構(gòu)造方式:
* weixin://wxpay/bizpayurl?sign=XXXXX&appid=XXXXXX&productid=XXXXXX×tamp=XXXXXX&noncestr=XXXXXX
*
appid 是字段名稱:公眾號(hào)id;字段來源:商戶注冊(cè)具有支付權(quán)限的公眾號(hào)成功后即可獲得;傳入方式:由商戶直接傳入。
timestamp 是字段名稱:時(shí)間戳;字段來源:商戶生成從1970 年1 月1 日00:00:00 至今的秒數(shù),即當(dāng)前的時(shí)間;由商戶生成后傳入。取值范圍:32 字符以下
noncestr 是字段名稱:隨機(jī)字符串;字段來源:商戶生成的隨機(jī)字符串;取值范圍:長(zhǎng)度為32 個(gè)字符以下。由商戶生成后傳入。取值范圍:32 字符以下
productid 是字段名稱:商品唯一id;字段來源:商戶需要定義并維護(hù)自己的商品id,這個(gè)id 與一張訂單等價(jià),微信后臺(tái)憑借該id 通過Post商戶后臺(tái)獲取交易必須信息。由商戶生成后傳入。取值范圍:32字符以下
sign 是字段名稱:簽名;字段來源:對(duì)前面的其他字段與appKey 按照字典序排序后,使用SHA1 算法得到的結(jié)果。由商戶生成后傳入。參與sign 簽名的字段包括:appid、timestamp、noncestr、productid 以及appkey。
*/
function createUrl($productid)
{
$app_id = "wx426b3015555a46be"; //公眾號(hào)appid
$app_key = "8934e7d15453e97507ef794cf7b0519d"; //公眾號(hào)支付請(qǐng)求中用于加密的密鑰Key,可驗(yàn)證商戶唯一身份,PaySignKey對(duì)應(yīng)于支付場(chǎng)景中的appKey值。
$mch_id = "1900009851";
$nonce_str = createNoncestr();
$time_stamp = time();
//訪問微信獲得信息
$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
$pp['appid'] = "wx426b3015555a46be"; //公眾號(hào)appid
$pp['mch_id'] = $mch_id;
$pp['device_info'] = $productid;
$pp['body'] = $productid;
$pp['nonce_str'] = $nonce_str;
$pp['out_trade_no'] = $productid;
$pp['total_fee'] = 100;
$pp['notify_url'] = "http://paysdk.weixin.qq.com/example/notify.php";
$pp['trade_type'] = "NATIVE";
$pp['time_stamp'] = $time_stamp;
$pp['sign'] = getSign($pp, $app_key);
$xml = arrayToXml($pp);
$xml_res = postXmlCurl($xml, $url);
$return_result = xmlToArray($xml_res); //把xml轉(zhuǎn)為數(shù)組
echo "<pre>";
var_dump($return_result);
return urlencode($return_result['code_url']);
$wx['appid'] = $app_id;
$wx['timeStamp'] = time();
$wx['nonceStr'] = $nonce_str;
$wx['package'] = "prepay_id=" . $return_result['prepay_id'];
$wx['signType'] = "MD5";
$wx['paySign'] = getSign($pp, $app_key);
}
/**
* 作用:生成簽名
*/
function getSign($Obj, $key)
{
foreach ($Obj as $k => $v) {
$Parameters[$k] = $v;
}
//簽名步驟一:按字典序排序參數(shù)
ksort($Parameters);
$String = 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>";
//簽名步驟四:所有字符轉(zhuǎn)為大寫
$result_ = strtoupper($String);
//echo "【result】 ".$result_."</br>";
return $result_;
}
/**
* 作用:格式化參數(shù),簽名過程需要使用
*/
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;
}
/**
* 作用:產(chǎn)生隨機(jī)字符串,不長(zhǎng)于32位
*/
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;
}
/**
* 作用:array轉(zhuǎn)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;
}
/**
* 作用:以post方式提交xml到對(duì)應(yīng)的接口url
*/
function postXmlCurl($xml, $url, $second = 30)
{
//初始化curl
$ch = curl_init();
//設(shè)置超時(shí)
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
//這里設(shè)置代理,如果有的話
//curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
//curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //嚴(yán)格校驗(yàn)2
//設(shè)置header
curl_setopt($ch, CURLOPT_HEADER, false);
//要求結(jié)果為字符串且輸出到屏幕上
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//post提交方式
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
//運(yùn)行curl
$data = curl_exec($ch);
//返回結(jié)果
if ($data) {
curl_close($ch);
return $data;
} else {
$error = curl_errno($ch);
echo "curl出錯(cuò),錯(cuò)誤碼:$error" . "<br>";
echo "<a ;
curl_close($ch);
return false;
}
}
/**
* 作用:將xml轉(zhuǎn)為array
*/
function xmlToArray($xml)
{
//將XML轉(zhuǎn)為array
$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $array_data;
}