Pay.php支付控制器php
<?php // +---------------------------------------------------------------------- // | YFCMS [ WE CAN DO IT MORE SIMPLE ] // +---------------------------------------------------------------------- // | Copyright (c) 2015-2016 http://xueding.vip All rights reserved. // +---------------------------------------------------------------------- // | Author: XueDing_ <361169632@qq.com> // +---------------------------------------------------------------------- namespace app\phone\controller; use think\Db; use think\Request; use think\Session; use think\Cookie; use app\phone\model\Weixin; use app\phone\model\Weixin_pay; use app\phone\model\Alipay; class Pay extends controller { public function _initialize() { $this->alipay_config = array( //應用ID,您的APPID。 'app_id' => 'your app_id', //支付寶公鑰,查看地址:https://openhome.alipay.com/platform/keyManage.htm 對應APPID下的支付寶公鑰。 'alipay_public_key' => 'your alipay_public_key', //商戶私鑰,您的原始格式RSA私鑰 'merchant_private_key' => 'your merchant_private_key', //異步通知地址 'notify_url' => "your notify_url", //同步跳轉 'return_url' => "your return_url", //編碼格式 'charset' => "UTF-8", //簽名方式 'sign_type' => "RSA2", //支付寶網關 'gatewayUrl' => "https://openapi.alipay.com/gateway.do", //支付寶partner,2088開頭數字 'partner' => 'your partner', //支付寶密鑰 'md5_key' => 'your key' ); $this->wechat_config = array( //您的APPID 'app_id' => 'your app_id', //您的md5_key 'md5_key' => 'your md5_key', //您的mch_id 'mch_id' => 'your mch_id', //您的appsecret 'appsecret' => 'your appsecret', //您的回調地址能夠在這裏配置使用方法的時候傳,也能夠在模型裏面配置。 'notify_url' => 'your notify_url', ); } /* 微信h5支付 */ public function wechat_pay() { if (input('post.token') == TOKEN) { // 判斷用戶是在微信瀏覽器打開仍是其餘瀏覽器打開。 $userAgent = $_SERVER['HTTP_USER_AGENT']; if (strpos($userAgent, 'MicroMessenger')) { $wechat = new Weixin($this->wechat_config['appsecret'], $this->wechat_config['app_id']); $wechat_pay = new Weixin_pay($this->wechat_config['appsecret'], $this->wechat_config['app_id'], $this->wechat_config['md5_key'], $this->wechat_config['mch_id']); $code = input('post.code'); if (session::has('openid')) { $openid = session::get('openid'); $total_fee = 1; $out_trade_no = '12345678945641231'; $body = "充值金幣支付測試"; $res = $wechat_pay->pay($openid, $total_fee, $body, $out_trade_no); $arr['errcode'] = '0'; $arr['errmsg'] = 'ok'; $arr['url'] = '本控制器下的地址/' . 'wx?data=' . $res; $data = json_encode($arr); } else { if (!empty($code)) { $res = $wechat->get_access_token($code); $userinfo = $wechat->get_openid_userinfo($res['access_token'], $res['openid']); $openid = $userinfo['openid']; session::set('openid', $openid); $total_fee = 1; $out_trade_no = '12345678945641231'; $body = "充值金幣支付測試"; $res = $wechat_pay->pay($openid, $total_fee, $body, $out_trade_no); $arr['errcode'] = '0'; $arr['errmsg'] = 'ok'; $arr['url'] = '本控制器下的地址/' . 'wx?data=' . $res; $data = json_encode($arr); } else { $link = 'http://' . $_SERVER['HTTP_HOST'] . '/#/cz'; // 前端充值金幣頁面地址。 cookie::set('url', $link, 300); //$link 這個地址是在微信打開你項目時纔會須要的,地址後面帶上獲取的code進行獲取access_token。 $res = $wechat->code_shouquan($link); $arr['errcode'] = '0'; $arr['errmsg'] = 'ok'; $arr['url'] = $res; $data = json_encode($arr); } } } else { //非微信瀏覽器 $wechat_pay = new Weixin_pay($this->wechat_config['appsecret'], $this->wechat_config['app_id'], $this->wechat_config['md5_key'], $this->wechat_config['mch_id']); //h5支付不用傳遞openid 此處與微信jsapi支付不一樣 $openid = ""; $total_fee = 1; $out_trade_no = '12345678945641231'; $body = "充值金幣h5支付測試"; $res = $wechat_pay->payh5($openid, $total_fee, $body, $out_trade_no); $arr['errcode'] = '0'; $arr['errmsg'] = 'ok'; $arr['url'] = $res['mweb_url'];//微信喚起H5支付地址 $data = json_encode($arr); } } else { $data = '{"errcode":"1001","errmsg":"TOKEN無效"}'; } echo $data; } /* 在微信打開的時候喚起微信公衆號支付。 */ public function wx() { $data = input('get.data'); $this->assign('data', $data); return view(); } /* 獲取微信code */ public function code() { $code = input('param.code'); $this->redirect(cookie::get('url') . '?code=' . $code); } /* 微信回調 */ public function wechat_notify() { $ccc = file_get_contents('php://input'); $Weixin_pay = new Weixin_pay(); $data = $Weixin_pay->xmlToArray($ccc); if ($data['result_code'] == 'SUCCESS' && $data['return_code'] == 'SUCCESS') { //獲取訂單號 $Ordersn = $data['out_trade_no'];//訂單號 // 處理支付成功後的邏輯業務 } } /* 微信APP支付 */ public function app_wechat_pay() { if (input('post.token') == TOKEN) { $wechat_pay = new Weixin_pay($this->wechat_config['appsecret'], $this->wechat_config['app_id'], $this->wechat_config['md5_key'], $this->wechat_config['mch_id']); //h5支付不用傳遞openid 此處與微信jsapi支付不一樣 $openid = ""; $total_fee = 1; $body = "充值金幣APP支付測試"; $order_sn = 123456789;//訂單號 $res = $wechat_pay->app_pay($body, $order_sn, $total_fee); if ($res['prepay_id']) {//判斷返回參數中是否有prepay_id $order = $wechat_pay->getOrder($res['prepay_id']);//執行二次簽名返回參數 $arr['errcode'] = '0'; $arr['errmsg'] = 'ok'; $arr['order'] = $order; $data = json_encode($arr); } else { $arr['errcode'] = '1003'; $arr['errmsg'] = $res['err_code_des']; $data = json_encode($arr); } } else { $data = '{"errcode":"1001","errmsg":"TOKEN無效"}'; } echo $data; } /* 支付寶開始支付 */ public function alipay() {//發起支付寶支付 if (input('post.token') == TOKEN) { $link = 'http://' . $_SERVER['HTTP_HOST'] . '/#/cz'; cookie::set('url', $link, 300); $res['out_trade_no'] = 123456798123; $res['subject'] = '充值'; $res['body'] = '測試'; $res['money'] = 0.01; $Pay = new Alipay($this->alipay_config['partner'], $this->alipay_config['md5_key']); $result = $Pay->alipay([ 'notify_url' => 'your notify_url', 'return_url' => 'your return_url', 'out_trade_no' => $res['out_trade_no'], 'subject' => $res['subject'], 'total_fee' => $res['money'], 'body' => $res['body'], ]); $arr['errcode'] = '0'; $arr['errmsg'] = 'ok'; return $result['msg']; } else { $data = '{"errcode":"1001","errmsg":"TOKEN無效"}'; } echo $data; } /* 支付寶回調 */ public function alipay_notify() {//異步訂單通知 $wx = sys_config_get('payment'); $Pay = new Alipay($wx['aliwappay']['partner'], $wx['aliwappay']['md5_key']); $out_trade_no = input('out_trade_no'); $trade_status = input('trade_status'); if ($trade_status == 'TRADE_SUCCESS') { // 處理支付成功後的邏輯業務 } else { // 處理支付失敗後的邏輯業務 } } }
模型:Weixin.phphtml
<?php namespace app\phone\model; use think\Db; use think\Validate; use think\Loader; use think\Model; class Weixin extends model { protected $appScrect; protected $appId; public function __construct($appScrect = "", $appId = "") { $this->appScrect = $appScrect; $this->appId = $appId; } /* * 由於本項目需求,前端是Vue後臺是TP5兩個項目不在一個服務器因此個人url是return 回控制器經過接口返回給前端 * 若是是正常html頁面能夠使用header("Location: $url"); 如下方法都是 * */ public function code_shouquan($link) { $redirect_uri = urlencode('網址/pay/code');//微信獲取網頁受權地址 // 一、引導用戶進入受權頁面贊成受權,獲取code // 二、經過code換取網頁受權access_token // 三、若是須要,開發者能夠刷新網頁受權access_token,避免過時 // 四、經過網頁受權access_token和openid獲取用戶基本信息(支持UnionID機制) $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $this->appId . "&redirect_uri=" . $redirect_uri . "&response_type=code&scope=snsapi_userinfo&state=1234#wechat_redirect"; return $url; } public function get_access_token($code) { //檢測緩存中是否有access_token(2小時),若是存在直接返回,不存在則檢測緩存中的refresh_token(30天), // refresh_token若是存在調用刷新緩存;若是不存在從新發起受權code受權 $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->appId . "&secret=" . $this->appScrect . "&code=" . $code . "&grant_type=authorization_code"; $res = $this->sendRequest($url); return $res; } public function get_refresh_token($refresh_token) { $url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" . $this->appId . "&grant_type=refresh_token&refresh_token=" . $refresh_token; $res = $this->sendRequest($url); return $res; } public function get_openid_userinfo($access_token, $openid) { $url = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $access_token . "&openid=" . $openid . "&lang=zh_CN"; $res = $this->sendRequest($url); return $res; } /* 發送請求 */ public function sendRequest($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); return json_decode($output, true); } }
Weixin.php前端
<?php namespace app\phone\model; use think\Db; use think\Validate; use think\Loader; use think\Model; class Weixin_pay extends model { protected $appScrect;//微信公衆平臺的appscrect protected $appId;//微信公衆平臺appid protected $key;//微信商戶平臺配置的祕鑰 protected $mch_id;//微信商戶號 protected $values = array(); public function __construct($appScrect="",$appId="", $key="",$mch_id =''){ $this->appScrect=$appScrect; $this->appId=$appId; $this->key=$key; $this->mch_id=$mch_id; } public function app_pay($body, $out_trade_no, $total_fee){ $url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; $notify_url = '';//能夠在這填寫回調地址也能夠增長一個參數傳過來。也能夠直接在構造裏面加一個notify_url地址 $onoce_str = $this->createNoncestr(); $data["appid"] = $this->appId; $data["body"] = $body; $data["mch_id"] = $this->mch_id; $data["nonce_str"] = $onoce_str; $data["notify_url"] = $notify_url; $data["out_trade_no"] = $out_trade_no; $data["spbill_create_ip"] = $this->get_client_ip(); $data["total_fee"] = $total_fee; $data["trade_type"] = "APP"; $sign = $this->getSign($data); $data["sign"] = $sign; $xml = $this->arrayToXml($data); $response = $this->postXmlCurl($xml, $url); // return $xml; //將微信返回的結果xml轉成數組 $response = $this->xmlToArray($response); //返回數據 return $response; } //執行第二次簽名,才能返回給客戶端使用 public function getOrder($prepayId){ $data["appid"] = $this->appId; $data["noncestr"] = $this->createNoncestr();; $data["package"] = "Sign=WXPay"; $data["partnerid"] = $this->mch_id; $data["prepayid"] = $prepayId; $data["timestamp"] = time(); $s = $this->getSign($data, false); $data["sign"] = $s; return $data; } /* 公衆號支付 */ public function pay($openid,$total_fee,$body,$out_trade_no){ $url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; $notify_url = ''; $onoce_str = $this->createNoncestr(); $data["appid"] = $this->appId; $data["body"] = $body; $data["mch_id"] = $this->mch_id; $data["nonce_str"] = $onoce_str; $data["notify_url"] = $notify_url; $data["out_trade_no"] = $out_trade_no; $data["spbill_create_ip"] = $this->get_client_ip(); $data["total_fee"] = $total_fee; $data["trade_type"] = "JSAPI"; $data["openid"] = $openid; $sign = $this->getSign($data); $data["sign"] = $sign; $xml = $this->arrayToXml($data); $response = $this->postXmlCurl($xml, $url); //將微信返回的結果xml轉成數組 $response = $this->xmlToArray($response); $jsapi=array(); $timeStamp = time(); $jsapi['appId']=($response["appid"]); $jsapi['timeStamp']=("$timeStamp"); $jsapi['nonceStr']=($this->createNoncestr()); $jsapi['package']=("prepay_id=" . $response['prepay_id']); $jsapi['signType']=("MD5"); $jsapi['paySign']=($this->getSign($jsapi)); $parameters = json_encode($jsapi); // halt($jsapi); //請求數據,統一下單 return $parameters; } public function payh5($openid,$total_fee,$body,$out_trade_no){ $url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; $notify_url = ''; $onoce_str = $this->createNoncestr(); $data["appid"] = $this->appId; $data["body"] = $body; $data["mch_id"] = $this->mch_id; $data["nonce_str"] = $onoce_str; $data["notify_url"] = $notify_url; $data["out_trade_no"] = $out_trade_no; $data["spbill_create_ip"] = $this->get_client_ip(); $data["total_fee"] = $total_fee; $data["trade_type"] = "MWEB"; $data["scene_info"] = "{'h5_info': {'type':'Wap','wap_url': $notify_url,'wap_name': '測試充值'}}"; $sign = $this->getSign($data); $data["sign"] = $sign; $xml = $this->arrayToXml($data); // return $data; $response = $this->postXmlCurl($xml, $url); //將微信返回的結果xml轉成數組 $response = $this->xmlToArray($response); //請求數據,統一下單 return $response; } public static function getNonceStr($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){ $Parameters[$k] = $v; } //簽名步驟一:按字典序排序參數 ksort($Parameters); $String = $this->formatBizQueryParaMap($Parameters, false); //echo '【string1】'.$String.'</br>'; //簽名步驟二:在string後加入KEY $String = $String."&key=".$this->key; //echo "【string2】".$String."</br>"; //簽名步驟三:MD5加密 $String = md5($String); //echo "【string3】 ".$String."</br>"; //簽名步驟四:全部字符轉爲大寫 $result_ = strtoupper($String); //echo "【result】 ".$result_."</br>"; return $result_; } /** * 做用:產生隨機字符串,不長於32位 */ 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; } //數組轉xml public 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; } /** * 做用:將xml轉爲array */ public function xmlToArray($xml){ //將XML轉爲array $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); return $array_data; } /** * 做用:以post方式提交xml到對應的接口url */ public function postXmlCurl($xml,$url,$second=30){ //初始化curl $ch = curl_init(); //設置超時 curl_setopt($ch, CURLOPT_TIMEOUT, $second); //這裏設置代理,若是有的話 //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); //設置header curl_setopt($ch, CURLOPT_HEADER, FALSE); //要求結果爲字符串且輸出到屏幕上 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //post提交方式 curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); //運行curl $data = curl_exec($ch); //返回結果 if($data){ curl_close($ch); return $data; }else{ $error = curl_errno($ch); echo "curl出錯,錯誤碼:$error"."<br>"; curl_close($ch); return false; } } /* 獲取當前服務器的IP */ public function get_client_ip(){ if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'),'unknown')) { $ip = getenv('HTTP_CLIENT_IP'); } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'),'unknown')) { $ip = getenv('HTTP_X_FORWARDED_FOR'); } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'),'unknown')) { $ip = getenv('REMOTE_ADDR'); } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) { $ip = $_SERVER['REMOTE_ADDR']; } return preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : ''; } /** * 做用:格式化參數,簽名過程須要使用 */ public function formatBizQueryParaMap($paraMap, $urlencode){ $buff = ""; ksort($paraMap); foreach ($paraMap as $k => $v){ if($urlencode){ $v = urlencode($v); } $buff .= $k . "=" . $v . "&"; } $reqPar; if (strlen($buff) > 0){ $reqPar = substr($buff, 0, strlen($buff)-1); } return $reqPar; } public function MakeSign($unifiedorder) { $this->values=$unifiedorder; //簽名步驟一:按字典序排序參數 // ksort($this->values); $string = $this->ToUrlParams(); //halt($string); //簽名步驟二:在string後加入KEY $string = $string . "&key=".$this->key; //簽名步驟三:MD5加密 $string = md5($string); //簽名步驟四:全部字符轉爲大寫 $result = strtoupper($string); return $result; } public function ToUrlParams() { $buff = ""; foreach ($this->values as $k => $v) { if($k != "sign" && $v != "" && !is_array($v)){ $buff .= $k . "=" . $v . "&"; } } $buff = trim($buff, "&"); return $buff; } function array2xml($array) { $xml='<xml>'; foreach($array as $key=>$val){ if(is_numeric($key)){ $key="item id=\"$key\""; }else{ //去掉空格,只取空格以前文字爲key list($key,)=explode(' ',$key); } $xml.="<$key>"; $xml.=is_array($val)?$this->_array2xml($val):$val; //去掉空格,只取空格以前文字爲key list($key,)=explode(' ',$key); $xml.="</$key>"; } $xml.="</xml>"; return $xml; } function xml2array($xml) { //禁止引用外部xml實體 libxml_disable_entity_loader(true); $values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); return $values; } public function request_post($url = '', $param = '') { if (empty($url) || empty($param)) { return false; } $postUrl = $url; $curlPost = $param; $ch = curl_init(); //初始化curl curl_setopt($ch, CURLOPT_URL, $postUrl); //抓取指定網頁 curl_setopt($ch, CURLOPT_HEADER, 0); //設置header curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求結果爲字符串且輸出到屏幕上 curl_setopt($ch, CURLOPT_POST, 1); //post提交方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); $data = curl_exec($ch); //運行curl curl_close($ch); return $data; } function curl_post_ssl($url, $vars, $second=30,$aHeader=array()) { $ch = curl_init(); //curl_setopt($ch,CURLOPT_VERBOSE,'1'); curl_setopt($ch,CURLOPT_TIMEOUT,$second); curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM'); curl_setopt($ch,CURLOPT_SSLCERT,'/data/cert/php.pem'); curl_setopt($ch,CURLOPT_SSLCERTPASSWD,'1234'); curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM'); curl_setopt($ch,CURLOPT_SSLKEY,'/data/cert/php_private.pem'); if( count($aHeader) >= 1 ){ curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader); } curl_setopt($ch,CURLOPT_POST, 1); curl_setopt($ch,CURLOPT_POSTFIELDS,$vars); $data = curl_exec($ch); curl_close($ch); if($data){ return $data; }else{ return false; } } }
Alipay.phpweb
<?php namespace app\phone\model; use think\Validate; use think\Log; use think\Model; use lib\AlipaySubmit; class Alipay extends Model { protected $partner; protected $md5_key; public function __construct($partner = "", $md5_key = "") { $this->partner = $partner; $this->md5_key = $md5_key; } public static $alipay_config = [ // 'partner' => '',//支付寶partner,2088開頭數字 // 'seller_id' => '',//支付寶partner,2088開頭數字 // 'key' => '',//支付寶密鑰 'sign_type' => 'MD5', 'input_charset' => 'utf-8', 'cacert' => '', 'transport' => 'http', 'payment_type' => '1', 'service' => '',//https://docs.open.alipay.com/api_1 看你須要哪一個API接口 'anti_phishing_key' => '', 'exter_invoke_ip' => '', 'app_pay' => 'Y', ]; public function alipay($data = []) {//發起支付寶支付 $validate = new Validate([ ['out_trade_no', 'require|alphaNum', '訂單編號輸入錯誤1|訂單編號輸入錯誤2'], ['total_fee', 'require|number|gt:0', '金額輸入錯誤|金額輸入錯誤|金額輸入錯誤'], ['subject', 'require', '請輸入標題'], ['body', 'require', '請輸入描述'], ['notify_url', 'require', '異步通知地址不爲空'], ]); if (!$validate->check($data)) { return ['code' => 0, 'msg' => $validate->getError()]; } $config = self::$alipay_config; $config['partner'] = $this->partner; $config['seller_id'] = $this->partner; $config['key'] = $this->md5_key; vendor('alipay.alipay'); $parameter = [ "service" => $config['service'], "partner" => $config['partner'], "seller_id" => $config['seller_id'], "payment_type" => $config['payment_type'], "notify_url" => $data['notify_url'], "return_url" => $data['return_url'], "anti_phishing_key" => $config['anti_phishing_key'], "exter_invoke_ip" => $config['exter_invoke_ip'], "out_trade_no" => $data['out_trade_no'], "subject" => $data['subject'], "total_fee" => $data['total_fee'], "body" => $data['body'], "_input_charset" => $config['input_charset'], 'app_pay' => 'Y', ]; $alipaySubmit = new AlipaySubmit($config); return ['code' => 1, 'msg' => $alipaySubmit->buildRequestForm($parameter, "get", "確認")]; } } ?>
支付寶須要的submit文件:點擊下載json
wx.html在使用微信客戶端支付時須要的頁面api
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>微信公衆號支付</title> </head> <body> <script> function getOrder() { onBridgeReady(); } function onBridgeReady() { var data = {$data}; WeixinJSBridge.invoke( 'getBrandWCPayRequest', data, function (res) { if (res.err_msg == "get_brand_wcpay_request:ok") { // 使用以上方式判斷前端返回,微信團隊鄭重提示:res.err_msg將在用戶支付成功後返回 ok,但並不保證它絕對可靠。 location.href = 'http://book.yueyuecms.com/#/me' } else { // alert(res.err_code+res.err_desc+res.err_msg); // 顯示錯誤信息 location.href = 'http://book.yueyuecms.com/#/me' } } ); } if (typeof WeixinJSBridge == "undefined") { if (document.addEventListener) { document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false); } else if (document.attachEvent) { document.attachEvent('WeixinJSBridgeReady', onBridgeReady); document.attachEvent('onWeixinJSBridgeReady', onBridgeReady); } } else { onBridgeReady(); } </script> </body> </html>
Author:XueDing_
Email:361169632@qq.com
註明出處:http://www.javashuo.com/article/p-kcllqrqx-e.html