1.微信公眾平臺(tái)開通微信支付贬媒,審核通過(guò)。
2.微信商戶平臺(tái)配置支付授權(quán)目錄
3.微信交易單號(hào)獲取類
<?php
/*
* 生活服務(wù)_微信付款
*/
namespace Apis\Controller;
use Common\Controller\AppframeController;
class WxpayController extends AppframeController {
protected $mchid;
protected $appid;
protected $key;
public function _initialize() {
parent::_initialize();
$this->mchid = '1430532102'; // 微信支付商戶號(hào) PartnerID 通過(guò)微信支付商戶資料審核后郵件發(fā)送
$this->appid = 'wx20d62ad060e66486'; //公眾號(hào)APPID 通過(guò)微信支付商戶資料審核后郵件發(fā)送
$this->key = '8934e7d15453e97507ef7mao521125ab'; //https://pay.weixin.qq.com 帳戶設(shè)置-安全設(shè)置-API安全-API密鑰-設(shè)置API密鑰
}
public function createJsBizPackage($openid, $totalFee, $outTradeNo, $orderName, $notifyUrl, $timestamp) {
$config = array(
'mch_id' => $this->mchid,
'appid' => $this->appid,
'key' => $this->key,
);
$unified = array(
'appid' => $config['appid'],
'attach' => '支付', //商家數(shù)據(jù)包肘习,原樣返回
'body' => $orderName,
'mch_id' => $config['mch_id'],
'nonce_str' => $this->createNonceStr(),
'notify_url' => $notifyUrl,
'openid' => $openid, //rade_type=JSAPI际乘,此參數(shù)必傳
'out_trade_no' => $outTradeNo,
'spbill_create_ip' => '127.0.0.1',
'total_fee' => intval($totalFee * 100), //單位 轉(zhuǎn)為分
'trade_type' => 'JSAPI',
);
$unified['sign'] = $this->getSign($unified, $config['key']);
$responseXml = $this->curlPost('https://api.mch.weixin.qq.com/pay/unifiedorder', $this->arrayToXml($unified));
$unifiedOrder = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
if ($unifiedOrder === false) {
die('parse xml error');
}
if ($unifiedOrder->return_code != 'SUCCESS') {
die($unifiedOrder->return_msg);
}
if ($unifiedOrder->result_code != 'SUCCESS') {
die($unifiedOrder->err_code);
/*
NOAUTH 商戶無(wú)此接口權(quán)限
NOTENOUGH 余額不足
ORDERPAID 商戶訂單已支付
ORDERCLOSED 訂單已關(guān)閉
SYSTEMERROR 系統(tǒng)錯(cuò)誤
APPID_NOT_EXIST APPID不存在
MCHID_NOT_EXIST MCHID不存在
APPID_MCHID_NOT_MATCH appid和mch_id不匹配
LACK_PARAMS 缺少參數(shù)
OUT_TRADE_NO_USED 商戶訂單號(hào)重復(fù)
SIGNERROR 簽名錯(cuò)誤
XML_FORMAT_ERROR XML格式錯(cuò)誤
REQUIRE_POST_METHOD 請(qǐng)使用post方法
POST_DATA_EMPTY post數(shù)據(jù)為空
NOT_UTF8 編碼格式錯(cuò)誤
*/
}
//$unifiedOrder->trade_type 交易類型 調(diào)用接口提交的交易類型,取值如下:JSAPI漂佩,NATIVE脖含,APP
//$unifiedOrder->prepay_id 預(yù)支付交易會(huì)話標(biāo)識(shí) 微信生成的預(yù)支付回話標(biāo)識(shí)罪塔,用于后續(xù)接口調(diào)用中使用,該值有效期為2小時(shí)
//$unifiedOrder->code_url 二維碼鏈接 trade_type為NATIVE是有返回养葵,可將該參數(shù)值生成二維碼展示出來(lái)進(jìn)行掃碼支付
$arr = array(
"appId" => $config['appid'],
"timeStamp" => $timestamp,
"nonceStr" => $this->createNonceStr(),
"package" => "prepay_id=" . $unifiedOrder->prepay_id,
"signType" => 'MD5',
);
$arr['paySign'] = $this->getSign($arr, $config['key']);
return $arr;
}
public function notify() {
$config = array(
'mch_id' => $this->mchid,
'appid' => $this->appid,
'key' => $this->key,
);
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
if ($postObj === false) {
die('parse xml error');
}
if ($postObj->return_code != 'SUCCESS') {
die($postObj->return_msg);
}
if ($postObj->result_code != 'SUCCESS') {
die($postObj->err_code);
}
$arr = (array) $postObj;
unset($arr['sign']);
if ($this->getSign($arr, $config['key']) == $postObj->sign) {
// $mch_id = $postObj->mch_id; //微信支付分配的商戶號(hào)
// $appid = $postObj->appid; //微信分配的公眾賬號(hào)ID
// $openid = $postObj->openid; //用戶在商戶appid下的唯一標(biāo)識(shí)
// $transaction_id = $postObj->transaction_id;//微信支付訂單號(hào)
// $out_trade_no = $postObj->out_trade_no;//商戶訂單號(hào)
// $total_fee = $postObj->total_fee; //訂單總金額征堪,單位為分
// $is_subscribe = $postObj->is_subscribe; //用戶是否關(guān)注公眾賬號(hào),Y-關(guān)注关拒,N-未關(guān)注佃蚜,僅在公眾賬號(hào)類型支付有效
// $attach = $postObj->attach;//商家數(shù)據(jù)包,原樣返回
// $time_end = $postObj->time_end;//支付完成時(shí)間
echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
return $postObj;
}
}
/**
* curl get
*
* @param string $url
* @param array $options
* @return mixed
*/
public static function curlGet($url = '', $options = array()) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
if (!empty($options)) {
curl_setopt_array($ch, $options);
}
//https請(qǐng)求 不驗(yàn)證證書和host
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
public static function curlPost($url = '', $postData = '', $options = array()) {
if (is_array($postData)) {
$postData = http_build_query($postData);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //設(shè)置cURL允許執(zhí)行的最長(zhǎng)秒數(shù)
if (!empty($options)) {
curl_setopt_array($ch, $options);
}
//https請(qǐng)求 不驗(yàn)證證書和host
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
public static function createNonceStr($length = 16) {
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$str = '';
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
public static 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;
}
/**
* 例如:
* appid: wxd930ea5d5a258f4f
* mch_id: 10000100
* device_info: 1000
* Body: test
* nonce_str: ibuaiVcKdpRxkhJA
* 第一步:對(duì)參數(shù)按照 key=value 的格式着绊,并按照參數(shù)名 ASCII 字典序排序如下:
* stringA="appid=wxd930ea5d5a258f4f&body=test&device_info=1000&mch_i
* d=10000100&nonce_str=ibuaiVcKdpRxkhJA";
* 第二步:拼接支付密鑰:
* stringSignTemp="stringA&key=192006250b4c09247ec02edce69f6a2d"
* sign=MD5(stringSignTemp).toUpperCase()="9A0A8659F005D6984697E2CA0A9CF3B7"
*/
public static function getSign($params, $key) {
ksort($params, SORT_STRING);
$unSignParaString = self::formatQueryParaMap($params, false);
$signStr = strtoupper(md5($unSignParaString . "&key=" . $key));
return $signStr;
}
protected static function formatQueryParaMap($paraMap, $urlEncode = false) {
$buff = "";
ksort($paraMap);
foreach ($paraMap as $k => $v) {
if (null != $v && "null" != $v) {
if ($urlEncode) {
$v = urlencode($v);
}
$buff .= $k . "=" . $v . "&";
}
}
$reqPar = '';
if (strlen($buff) > 0) {
$reqPar = substr($buff, 0, strlen($buff) - 1);
}
return $reqPar;
}
}
//方法調(diào)用
$res = $wx->createJsBizPackage($openid, $totalFee, $outTradeNo, $orderName, $notifyUrl, $timestamp);
3.回調(diào)函數(shù)
<?php
header('Content-Type: text/html; charset=utf-8');
$mysql_server_name='localhost'; //改成自己的mysql數(shù)據(jù)庫(kù)服務(wù)器
$mysql_username='root'; //改成自己的mysql數(shù)據(jù)庫(kù)用戶名
$mysql_password='Pa1mtrends'; //改成自己的mysql數(shù)據(jù)庫(kù)密碼
$mysql_database='cloud'; //
$conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password) or die("error connecting") ; //連接數(shù)據(jù)庫(kù)
mysql_query("set names 'utf8'"); //數(shù)據(jù)庫(kù)輸出編碼 應(yīng)該與你的數(shù)據(jù)庫(kù)編碼保持一致.南昌網(wǎng)站建設(shè)公司百恒網(wǎng)絡(luò)PHP工程師建議用UTF-8 國(guó)際標(biāo)準(zhǔn)編碼.
mysql_select_db($mysql_database); //打開數(shù)據(jù)庫(kù)
$mchid = '143053xxxx'; // 微信支付商戶號(hào) PartnerID 通過(guò)微信支付商戶資料審核后郵件發(fā)送
$appid = 'wx20d62ad06xxxxx'; //公眾號(hào)APPID 通過(guò)微信支付商戶資料審核后郵件發(fā)送
$key = '8934e7d15453e97507ef7mao5xxxxxx'; //https://pay.weixin.qq.com 帳戶設(shè)置-安全設(shè)置-API安全-API密鑰-設(shè)置API密鑰
$config = array(
'mch_id' => $mchid,
'appid' => $appid,
'key' => $key,
);
$postStr = file_get_contents("php://input");
if (!$postStr) {
$postStr = "maozi";
}
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
if ($postObj === false) {
die('parse xml error');
}
if ($postObj->return_code != 'SUCCESS') {
die($postObj->return_msg);
}
if ($postObj->result_code != 'SUCCESS') {
die($postObj->err_code);
}
$arr = (array) $postObj;
unset($arr['sign']);
if (getSign($arr, $config['key']) == $postObj->sign) {
$mch_id = $postObj->mch_id; //微信支付分配的商戶號(hào)
$appid = $postObj->appid; //微信分配的公眾賬號(hào)ID
$openid = $postObj->openid; //用戶在商戶appid下的唯一標(biāo)識(shí)
$transaction_id = $postObj->transaction_id;//微信支付訂單號(hào)
$out_trade_no = $postObj->out_trade_no;//商戶訂單號(hào)
$total_fee = $postObj->total_fee; //訂單總金額谐算,單位為分
$total_fee = floatval($total_fee/100);
$time_end = $postObj->time_end;//支付完成時(shí)間
$time_end = strtotime(substr($time_end,0,4)."-".substr($time_end, 4,2)."-".substr($time_end,6,2)." ".substr($time_end,8,2).":". substr($time_end, 10, 2).":".substr($time_end, 12, 2));
//用戶id
$res = mysql_query("select * from cmf_market_order where sn='{$out_trade_no}'",$conn);
$oinfo = mysql_fetch_assoc($res);
$uid = $oinfo['uid'];
//查詢訂單是否存在
$has_sql = "select * from cmf_market_order_wx where wx_sn='{$transaction_id}'";
$has = mysql_query($has_sql,$conn);
$has = mysql_fetch_assoc($has);
if ($has) {
echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
exit;
}
//插入微信訂單信息
$sql = "insert into cmf_market_order_wx (mch_id,appid,openid,uid,sn,wx_sn,total,time) values ('{$mch_id}','{$appid}','{$openid}','{$uid}','{$out_trade_no}','{$transaction_id}','{$total_fee}','{$time_end}')";
$result = mysql_query($sql,$conn);
if ($result) {//更新訂單狀態(tài)
$up_sql = "update cmf_market_order set haspay=1,status=1,paytime='{$time_end}' where sn='{$out_trade_no}'";
$result_update = mysql_query($up_sql,$conn);
}
echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
return $postObj;
}
function getSign($params, $key) {
ksort($params, SORT_STRING);
$unSignParaString = formatQueryParaMap($params, false);
$signStr = strtoupper(md5($unSignParaString . "&key=" . $key));
return $signStr;
}
function formatQueryParaMap($paraMap, $urlEncode = false) {
$buff = "";
ksort($paraMap);
foreach ($paraMap as $k => $v) {
if (null != $v && "null" != $v) {
if ($urlEncode) {
$v = urlencode($v);
}
$buff .= $k . "=" . $v . "&";
}
}
$reqPar = '';
if (strlen($buff) > 0) {
$reqPar = substr($buff, 0, strlen($buff) - 1);
}
return $reqPar;
}
?>
4.支付頁(yè)面,直接調(diào)用支付接口
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<title>微信安全支付</title>
<script type="text/javascript">
//調(diào)用微信JS api 支付
function jsApiCall()
{
WeixinJSBridge.invoke(
'getBrandWCPayRequest',
{
"appId": "{$info['appId']}",
"timeStamp": "{$info['timeStamp']}",
"nonceStr": "{$info['nonceStr']}",
"package": "{$info['package']}",
"signType": "MD5",
"paySign": "{$info['paySign']}"
},
function(res){
if (res.err_msg == 'get_brand_wcpay_request:ok') {
alert("支付成功");
window.location.;
} else {
alert("錯(cuò)誤");
}
}
);
}
function callpay()
{
if (typeof WeixinJSBridge == "undefined"){
if( document.addEventListener ){
document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
}else if (document.attachEvent){
document.attachEvent('WeixinJSBridgeReady', jsApiCall);
document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
}
}else{
jsApiCall();
}
}
</script>
</head>
<body>
</br></br></br></br>
<div align="center">
<button style="width:210px; height:30px; background-color:#FE6714; border:0px #FE6714 solid; cursor: pointer; color:white; font-size:16px;" type="button" onclick="callpay()" >貢獻(xiàn)一下11111111</button>
</div>
</body>
</html>