微信對支付寶的鏈接屏蔽了,
https://mapi.alipay.com/gateway.do?_input_charset=utf-8¬ify_url=http%3A%2F%2Fzhudianbao.yunlutong.com%2Findex.php%3Fg%3DPay%26m%3DAlipay%26a%3Dnotify_url&out_trade_no=2016061410473300007521&partner=2088421211906705&payment_type=1&return_url=http%3A%2F%2Fzhudianbao.yunlutong.com%2Findex.php%3Fg%3DPay%26m%3DAlipay%26a%3Dreturn_url&seller_id=2088421211906705&service=alipay.wap.create.direct.pay.by.user&show_url=%E5%8A%A9%E5%BA%97%E5%AE%9D%E7%9A%84%E5%95%86%E5%93%81%E8%AE%A2%E5%8D%95&subject=%E5%8A%A9%E5%BA%97%E5%AE%9D%E7%9A%84%E5%95%86%E5%93%81%E8%AE%A2%E5%8D%95&total_fee=0.01&sign=4d9d7f1c4ab82f4a80edd38b5e9c8d74&sign_type=MD5
這種地址,直接無法訪問。
1.通過iframe避開屏蔽。
原有代碼
//建立請求
$alipaySubmit = new AlipaySubmit($alipay_config);
$html_text = $alipaySubmit->buildRequestForm($parameter,"get", "確認");
echo $html_text;
我們來看看buildRequestForm方法中的內(nèi)容,
/**
* 建立請求,以表單HTML形式構造(默認)
* @param $para_temp 請求參數(shù)數(shù)組
* @param $method 提交方式。兩個值可選:post、get
* @param $button_name 確認按鈕顯示文字
* @return 提交表單HTML文本
*/
function buildRequestForm($para_temp, $method, $button_name) {
//待請求參數(shù)數(shù)組
$para = $this->buildRequestPara($para_temp);
$sHtml = "<form id='alipaysubmit' name='alipaysubmit' action='".$this->alipay_gateway_new."_input_charset=".trim(strtolower($this->alipay_config['input_charset']))."' method='".$method."'>";
while (list ($key, $val) = each ($para)) {
$sHtml.= "<input type='hidden' name='".$key."' value='".$val."'/>";
}
//submit按鈕控件請不要含有name屬性
$sHtml = $sHtml."<input type='submit' value='".$button_name."' style='display:none;'></form>";
$sHtml = $sHtml."<script>document.forms['alipaysubmit'].submit();</script>";
return $sHtml;
}
它會自動生成一個表單提交,提交到https://mapi.alipay.com/gateway.do這個頁面去處理,這個時候,微信就屏蔽掉了。
改造
//建立請求
$alipaySubmit = new AlipaySubmit($alipay_config);
$html_text = $alipaySubmit->getHtml($parameter);
$content = '<iframe src="'.$html_text.'" name="iframepage" id="iframepage" scrolling="no" frameborder="0"></iframe>';
$this->assign('content',$content);
$this->display();
這里的核心就是getHtml中的內(nèi)容,這個方法是新加的,它可以拼接出支付寶調(diào)用支付的地址及所需要的各種參數(shù)。
/**
* 獲取地址,用于微信中iframe嵌入使用
*/
function getHtml($para_temp) {
$para = $this->buildRequestPara($para_temp);
$init='';
while (list ($key, $val) = each ($para)) {
$init.="&".$key."=".urlencode($val);
}
$init=$this->alipay_gateway_new."_input_charset=".trim(strtolower($this->alipay_config['input_charset'])).$init;
return $init;
}
這里的urlencode很重要,可以對內(nèi)容進行一些url處理。還有就是buildRequestPara方法,會自動生成簽名,簽名很重要,不然無法完成支付寶支付流程。
/**
* 生成要請求給支付寶的參數(shù)數(shù)組
* @param $para_temp 請求前的參數(shù)數(shù)組
* @return 要請求的參數(shù)數(shù)組
*/
function buildRequestPara($para_temp) {
//除去待簽名參數(shù)數(shù)組中的空值和簽名參數(shù)
$para_filter = paraFilter($para_temp);
//對待簽名參數(shù)數(shù)組排序
$para_sort = argSort($para_filter);
//生成簽名結果
$mysign = $this->buildRequestMysign($para_sort);
//簽名結果與簽名方式加入請求提交參數(shù)組中
$para_sort['sign'] = $mysign;
$para_sort['sign_type'] = strtoupper(trim($this->alipay_config['sign_type']));
return $para_sort;
}
生成簽名para_temp中的參數(shù)一個都不能少,
//構造要請求的參數(shù)數(shù)組,無需改動
$parameter = array(
"service" => $alipay_config['service'],
"partner" => $alipay_config['partner'],
"seller_id" => $alipay_config['seller_id'],
"payment_type" => $alipay_config['payment_type'],
"notify_url" => C('site_url').U('Pay/Alipay/notify_url'),
"return_url" => C('site_url').U('Pay/Alipay/return_url'),
"_input_charset" => trim(strtolower($alipay_config['input_charset'])),
"out_trade_no" => $out_trade_no,
"subject" => $subject,
"total_fee" => $total_fee,
"show_url" => $show_url,
"body" => $body,
//其他業(yè)務參數(shù)根據(jù)在線開發(fā)文檔,添加參數(shù).文檔地址:https://d oc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.2Z6TSk&treeId=60&articleId=103693&docType=1
//如"參數(shù)名"=> "參數(shù)值" 注:上一個參數(shù)末尾需要“,”逗號。
);
為了讓頁面好看一些,我沒有直接echo輸出,而是寫了一個頁面,pay.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Cache-Control" content="no-Cache" />
<meta http-equiv="Cache-Control" content="max-age=0" />
<meta name="viewport" content="width=device-width,inital-scale=1.0,maximum-scale=1.0,user-scalable=no;">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<link href="/Public/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<script src="/Public/js/jquery-1.10.2.min.js" type="text/javascript"></script>
<title>支付寶支付</title>
</head>
<style>
.scroll-wrapper{
position: fixed;
right: 0;
bottom: 0;
left: 0;
top: 0;
-webkit-overflow-scrolling: touch;
overflow-y: scroll;
}
.scroll-wrapper iframe {
height: 100%;
width: 100%;
}
</style>
<body>
<div class="scroll-wrapper">
{sh:$content}
</div>
</body>
</html>
有這個頁面,能夠更好的處理iframe的自適應效果。
總結:
調(diào)試過程中遇到一些問題,
1.iframe顯示效果,不能很好的自適應,通過上面的css很好的解決了。
2.如何使用iframe。一開始不太懂,其實iframe中的src就是一個連接,把微信的屏蔽連接放上去就可以了。
3.總是報簽名錯誤,在getHtml的參數(shù)中加上urlencode處理好了url地址。
4.支付成功后,頁面跳轉不出iframe,有待優(yōu)化!基本ok了。
補充:
通過js實現(xiàn)跳出iframe。通常支付成功后會轉到訂單列表頁,可以根據(jù)你的實際情況,到相應的頁面添加下面的js代碼。很神奇。親測可用。
// 跳出iframe
// if(top.location!=self.location)
// {
// top.location="{sh::U('Store/Order/orders')}";
// }
if(top.location!=self.location)
{
top.location=self.location;
}
完美了!