1、需要微信開放平台、微信商戶平台、微信公眾號的賬號。
2、將這三個平台建立綁定關系。
①微信開放平台需關聯微信商戶平台,設置關聯可獲取unionid
②微信公眾號需關聯微信商品平台
③微信商戶平台上需設置公眾號支付授權目錄,目錄精確到某個控制器
例如:http://ios.m.huisou.com/app/pay/
3、php代碼
步驟:支付方式選擇了微信支付時,調起接口,返回訂單號、金額、回調地址等信息給前端,前端獲取數據,並將數據和openid一起請求接口 wechatpay。
/**
* 微信支付
* @param array $orderDatas [訂單數據數組]
* @return array [客戶端所需的數據]
*/
public function wechatpay()
{
$orderDatas = json_decode(I('get.data'), true);
$conf['weixin_appid'] = C('WEIXIN_APPID'); // 微信公眾號appid
$conf['weixin_appsecret'] = C('WEIXIN_APPSECRET'); // 微信公眾號appsecret;
$conf['weixin_mchid'] = C('WEIXIN_OPEN_MCHID'); // 微信公眾號綁定的微信商戶號;
$conf['weixin_key'] = C('WEIXIN_OPEN_KEY'); // 微信公眾號綁定的微信商戶密鑰;
vendor('Wxpay.WxPay', '', '.JsApiPay.php');
$tools = new \JsApiPay();
/* 設置微信支付參數 */
vendor('Wxpay.WxPay', '', '.Api.php');
/* 統一下單 */
$input = new \WxPayUnifiedOrder();
/* 設置商戶分配的唯一32位key */
\WxPayConfig::$KEY = $conf['weixin_key'];
/* 微信分配的公眾賬號ID */
$input->SetAppid($conf['weixin_appid']);
/* 微信支付分配的商戶號 */
$input->SetMch_id($conf['weixin_mchid']);
/* 商品描述 */
$input->SetBody($orderDatas['title']);
/* 支付單號 */
$input->SetOut_trade_no($orderDatas['order_id']);
/* 設置訂單總金額,單位為分,只能為整數 */
$input->SetTotal_fee(intval(strval($orderDatas['order_total_price'] * 100)));
/* 接收微信支付異步通知回調地址 */
$input->SetNotify_url($orderDatas['notify_url']);
/* 交易類型 */
$input->SetTrade_type('JSAPI');
/* 用戶唯一標識/token */
$input->SetOpenid($orderDatas['openid']); // 此openid為微信授權登錄獲取的openid,即用戶標識token
/* 驗證方式 */
$input->SetSign_type('MD5');
/* 接收xml數據解析為array數組 */
$result = \WxPayApi::unifiedOrder($input);
/* 判斷錯誤信息 */
$result['return_code'] !== 'SUCCESS' && $this->ajaxJson('70000', $result['return_msg']);
$result['result_code'] !== 'SUCCESS' && $this->ajaxJson('70000', $result['err_code_des']);
$jsApiParameters = $tools->GetJsApiParameters($result); // 獲取前端需要的參數,用於調起微信支付頁面樣式
$this->assign('order_id', $orderDatas['order_id']); // 返回訂單id
$this->assign('token', $orderDatas['openid']); // 返回openid
$this->assign('jsApiParameters', $jsApiParameters); // 返回前端所需參數
$this->display('Pay:jsApiCall'); // 返回的前端頁面,點擊支付,簽名等通過后,會自動跳轉到此頁面
}
4、jsApiCall前端頁面
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>微信支付</title>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
</head>
<body>
<script type="text/javascript">
window.onload = function(){
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();
}
}
//調用微信JS api 支付
function jsApiCall()
{
WeixinJSBridge.invoke(
'getBrandWCPayRequest',
{$jsApiParameters},
function(res){
var recharge = '{$recharge}';
// alert(res.err_desc); // 打印返回的錯誤提示
if(recharge){
var recharge_url = '{$recharge_url}';
if(res.err_msg == 'get_brand_wcpay_request:ok'){
window.location = recharge_url;
}else{
window.location = recharge_url;
}
}else{
var order_id = '{$order_id}';
if(res.err_msg == 'get_brand_wcpay_request:ok'){
// 支付成功的跳轉鏈接
window.location.replace('http://{ios}.m.huisou.com/app/pay/successback?out_trade_no='+order_id);
}else{
// 支付失敗的跳轉鏈接
window.location.replace('http://{ios}.m.huisou.com/app/pay/failback?out_trade_no='+order_id);
}
}
}
);
}
</script>
</body>
</html>