先上效果圖:我要告訴你我這一篇文章寫的是微信支付之中的(普通商戶而非服務商商戶的統一下單JSPI)微信支付:





其實自己整合SDK失敗了,用了一個博客博主整合的代碼,在這裡寫一下筆記:
前面准備:
1、微信公眾號:
獨特的appid、appscrect、接口權限之中設置可以獲取用戶ID信息權限的域名(每個用戶對於不同公眾都會有一個特有ID,通過這個ID獲取用戶微信賬號基本信息、詳情看微信開發者文檔)、在微信支付按鈕出設置微信支付授權目錄(寫到發起請求的控制器那一層)、設置開發者微信賬號為測試白名單(用微信開發者工具的時候需要)
2、微信支付平台:
商戶平台登陸賬號、支付密鑰(隨時可以自行設置,只能有一個)、
3、整合進去thinkphp之中邏輯:
前端微信支付按鈕設置點擊調用支付發起控制器方法、
控制器運行,引用微信支付類、獲取用戶openid、獲取訂單數據、拼接出所有普通商戶預支付jsp需要的數據,display出那個自定義的支付頁面、
在支付頁面點擊支付、調用微信提供的jspi發起支付的scripet函數發起支付、
支付完成以後頁面會重定向到(在自定義支付頁面的script函數裡設置的跳轉目錄{:U('controller/function)}),並且異步(靜默)設置的異步處理訂單邏輯(記錄支付時間啦、標記為已經支付啦、標記是微信支付啦)之類的、
代碼:
我的訂單頁面的微信支付按鈕:
<a href="{:U('Wxpay/js_api_start',array('order_key_num'=>$v['order_key_num]))}"> 微信支付</a>
發起支付控制器Wxpay:
1 <?php
2 namespace Home\Controller;
3 use Think\Controller;
4 //微信支付類
5 class WxpayController extends Controller {
6 //獲取access_token過程中的跳轉uri,通過跳轉將code傳入jsapi支付頁面
7 public function js_api_start(){
8 if(!empty($_GET['order_key_num'])){
9 // session(array('pay_now_id'=>$_GET['order_key_num'],'expire'=>3600));
10 S('pay_now_id',$_GET['order_key_num'],3600);
11 }
12 vendor('Weixinpay.WxPayPubHelper');
13 //使用jsapi接口
14 $jsApi = new \JsApi_pub();
15 //=========步驟1:網頁授權獲取用戶openid============
16 //通過code獲得openid
17 if($_GET['code'] == ''){
18 //跳轉
19 $redirect_uri = 'https://當前域名+模塊+控制器+方法';
20 $url = 'https://open.weixin.qq.com/connect/oauth2/authorize
?appid=公眾號特有IDredirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect';
21 header("Location: $url");
22 exit();
23 }else{
24 //獲取openid
25 $url = 'https://api.weixin.qq.com/sns/oauth2/access_token
?appid=公眾號ID&secret=公眾號scrept&code='.$_GET['code'].'&grant_type=authorization_code';
26 $openid_arr = json_decode(file_get_contents($url),true);
27 }
28
29 $openid=$openid_arr['openid'];
30 $pay_now_id = S('pay_now_id');
31 if($pay_now_id){
32 $id=$pay_now_id;
33 $o = D('order_info');
34 $order_info = $o->where('order_id = %d',$id)->find();
35 if(empty($order_info['paycode'])){
36 $order_info['paycode'] = 'weixin';
37 }
38 if($order_info['is_pay']){
39 $this->error('當前訂單已經支付');
40 }
41 }else{
42 $this->error("不存在當前訂單編號!");
43 }
44 $res = array(
45 'order_sn' => $order_info['order_sn'],
46 'order_amount' => $order_info['pay_money']
47 );
48 //=========步驟2:使用統一支付接口,獲取prepay_id============
49 //使用統一支付接口
50 $unifiedOrder = new \UnifiedOrder_pub();
51 //設置統一支付接口參數
52 //設置必填參數
53 //appid已填,商戶無需重復填寫
54 //mch_id已填,商戶無需重復填寫
55 //noncestr已填,商戶無需重復填寫
56 //spbill_create_ip已填,商戶無需重復填寫
57 //sign已填,商戶無需重復填寫
58 $total_fee = $order_info['pay_money']*100;
59 // $total_fee = $res['order_amount'];
60 //$total_fee = 1;
61 // var_dump($order_info['pay_money']);die;
62 $body = "訂單支付";
63 $unifiedOrder->setParameter("openid", "$openid");//用戶標識
64 $unifiedOrder->setParameter("body", '商品采購');//商品描述
65 //自定義訂單號,此處僅作舉例
66 $unifiedOrder->setParameter("out_trade_no", $order_info['order_sn']);//商戶訂單號
67 $unifiedOrder->setParameter("total_fee", $total_fee);//總金額
68 //$unifiedOrder->setParameter("attach", "order_sn={$res['order_sn']}");//附加數據
69 $unifiedOrder->setParameter("notify_url", \WxPayConf_pub::NOTIFY_URL);//通知地址
70 $unifiedOrder->setParameter("trade_type", "JSAPI");//交易類型
71 //非必填參數,商戶可根據實際情況選填
72 //$unifiedOrder->setParameter("sub_mch_id","XXXX");//子商戶號
73 //$unifiedOrder->setParameter("device_info","XXXX");//設備號
74 //$unifiedOrder->setParameter("attach","XXXX");//附加數據
75 //$unifiedOrder->setParameter("time_start","XXXX");//交易起始時間
76 //$unifiedOrder->setParameter("time_expire","XXXX");//交易結束時間
77 //$unifiedOrder->setParameter("goods_tag","XXXX");//商品標記
78 //$unifiedOrder->setParameter("openid","XXXX");//用戶標識
79 //$unifiedOrder->setParameter("product_id","XXXX");//商品ID
80 $prepay_id = $unifiedOrder->getPrepayId();
81 // var_dump($prepay_id);die;
82 //=========步驟3:使用jsapi調起支付============
83 $jsApi->setPrepayId($prepay_id);
84 $jsApiParameters = $jsApi->getParameters();
85 $wxconf = json_decode($jsApiParameters, true);
86 if ($wxconf['package'] == 'prepay_id=') {
87 $this->error('當前訂單存在異常!');
88 }
89 $this->assign('res', $res);
90 $this->assign('jsApiParameters', $jsApiParameters);
91 $this->display('jsapi');
92 }
93 //異步通知url,商戶根據實際開發過程設定
94 public function notify_url() {
95 vendor('Weixinpay.WxPayPubHelper');
96 //使用通用通知接口
97 $notify = new \Notify_pub();
98 //存儲微信的回調
99 $xml = $GLOBALS['HTTP_RAW_POST_DATA'];
100 $notify->saveData($xml);
101 //驗證簽名,並回應微信。
102 //對後台通知交互時,如果微信收到商戶的應答不是成功或超時,微信認為通知失敗,
103 //微信會通過一定的策略(如30分鐘共8次)定期重新發起通知,
104 //盡可能提高通知的成功率,但微信不保證通知最終能成功。
105 if($notify->checkSign() == FALSE){
106 $notify->setReturnParameter("return_code", "FAIL");//返回狀態碼
107 $notify->setReturnParameter("return_msg", "簽名失敗");//返回信息
108 }else{
109 $notify->setReturnParameter("return_code", "SUCCESS");//設置返回碼
110 }
111 $returnXml = $notify->returnXml();
112 //==商戶根據實際情況設置相應的處理流程,此處僅作舉例=======
113 //以log文件形式記錄回調信息
114 //$log_name = "notify_url.log";//log文件路徑
115 //$this->log_result($log_name, "【接收到的notify通知】:\n".$xml."\n");
116 $parameter = $notify->xmlToArray($xml);
117 //$this->log_result($log_name, "【接收到的notify通知】:\n".$parameter."\n");
118 if($notify->checkSign() == TRUE){
119 if ($notify->data["return_code"] == "FAIL") {
120 //此處應該更新一下訂單狀態,商戶自行增刪操作
121 //$this->log_result($log_name, "【通信出錯】:\n".$xml."\n");
122 //更新訂單數據【通信出錯】設為無效訂單
123 echo 'error';
124 }
125 else if($notify->data["result_code"] == "FAIL"){
126 //此處應該更新一下訂單狀態,商戶自行增刪操作
127 //$this->log_result($log_name, "【業務出錯】:\n".$xml."\n");
128 //更新訂單數據【通信出錯】設為無效訂單
129 echo 'error';
130 }
131 else{
132 //$this->log_result($log_name, "【支付成功】:\n".$xml."\n");
133 //我這裡用到一個process方法,成功返回數據後處理,返回地數據具體可以參考微信的文檔
134 if ($this->process($parameter)) {
135 //處理成功後輸出success,微信就不會再下發請求了
136 echo 'success';
137 }else {
138 //沒有處理成功,微信會間隔的發送請求
139 echo 'error';
140 }
141 }
142 }
143 }
144 //訂單處理
145 private function process($parameter) {
146 //此處應該更新一下訂單狀態,商戶自行增刪操作
147 /*
148 * 返回的數據最少有以下幾個
149 * $parameter = array(
150 'out_trade_no' => xxx,//商戶訂單號
151 'total_fee' => XXXX,//支付金額
152 'openid' => XXxxx,//付款的用戶ID
153 );
154 */
155 $data = array(
156 'order_sn'=>$parameter['out_trade_no'],
157 'des'=>('訂單交易:'.$parameter['out_trade_no']),
158 'money'=>$parameter['total_fee'],
159 );
160 orderhandlestarysdgdss($data);//這是一個common方法,他會將該訂單狀態設置為已支付之類的
161 return true;
162 }
163 }
164 ?>
發起支付後拼接預支付數據參數(參數列表看微信普通商戶開發者文檔——微信支付——統一下單)display的頁面:
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta http-equiv="X-UA-Compatible" content="IE=edge">
6 <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;" name="viewport" />
7 <meta name="format-detection" content="telephone=no"/>
8 <title>下</title>
9 <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
10 <meta name="keyword" content="">
11 <meta name="description" content="">
12 <script type="text/javascript">
13 var order_sn = "{$res['order_sn']}";
14 //調用微信JS api 支付
15 function jsApiCall(){
16 WeixinJSBridge.invoke(
17 'getBrandWCPayRequest',
18 <?php echo $jsApiParameters; ?>,
19 function(res){
20 //如果支付成功
21 if (res.err_msg == 'get_brand_wcpay_request:ok') {
22 //支付成功後跳轉的地址
23 location.href = "{:U('Home/User/my_order')}";
24 }else if (res.err_msg == 'get_brand_wcpay_request:cancel') {
25 alert('請盡快完成支付哦!');
26 }else if (res.err_msg == 'get_brand_wcpay_request:fail') {
27 alert('支付失敗');
28 }else {
29 alert('意外錯誤');
30 }
31 //WeixinJSBridge.log(res.err_msg);
32 //alert(res.err_code+res.err_desc+res.err_msg);
33 /*if (res.err_msg == 'get_brand_wcpay_request:ok') {
34 alert('支付成功');
35 }else {
36 alert('取消支付');
37 }*/
38 }
39 );
40 }
41 function callpay(){
42 if (typeof WeixinJSBridge == "undefined"){
43 if( document.addEventListener ){
44 document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
45 }else if (document.attachEvent){
46 document.attachEvent('WeixinJSBridgeReady', jsApiCall);
47 document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
48 }
49 }else{
50 jsApiCall();
51 }
52 }
53 </script>
54 <style>
55 *{font-family:'微軟雅黑','Microsoft YaHei';}
56 body #head{position:relative;z-index:99999999999999;padding:0 10px;}
57 body .zh-head{padding:0 0 0 0;height:auto;}
58 .zh-head-conter{position:relative;height:40px;}
59 .zh-logo{position:absolute;left:50%;top:0;margin:0 0 0 -60px;float:none;width:auto;}
60 .zh-logo a{display:block;}
61 .zh-logo img{width:120px;height:40px;display:block;}
62 .heads_fix .zh-logo{}
63 #head{position:fixed!important;left:0;top:0;right:0;z-index:99999;background:#fff;border-bottom:1px solid #ddd;}
64 .zh-logo{height:40px;}
65 .flowpay{margin-top:25%;}
66 .flowpay dt{text-align:center;}
67 .flowpay strong.price{font-size:40px;}
68 .wxLogo{text-align:center;}
69 .wxLogo img{}
70 .flowpay dd{margin:0;padding:20px 0 10px 0;}
71 .flowpay dd input{margin:0 auto;padding:0;width:90%;height:45px;line-height:45px;border:0;border-radius:4px;background:#0CBC0A;color:#fff;font-size:17px;display:block;-webkit-appearance:none;-moz-appearance:none;appearance:none;outline:none;}
72 </style>
73 </head>
74 <body>
75 <!--頭部開始-->
76
88
89
90
91 <div class="flowpay">
92 <dl>
93 <dt>
94 <p class="wxLogo"><img src="__PUBLIC__/home/images/1479953699138120.png" alt=""></p>
95 本次訂單需支付:¥<strong class="price">{$res['order_amount']}</strong> 元
96 </dt>
97 <dd>
98 <input type="button" id="hhhhhh" onclick="callpay()" value="立即支付" />
99 </dd>
100 </dl>
101 </div>
102 <!--尾結束-->
103 </body>
104 </html>
然後就是類文件啦:

那個cacert是證書存放目錄;證書不一定需要的;
vendor文件夾在我的文件裡面找找就可以。