之前有寫過幾篇文章將微信支付和退款:
1.PHP實現微信支付(jsapi支付)流程
2.ThinkPHP中實現微信支付(jsapi支付)流程
3.PHP實現微信申請退款
這幾篇都是使用了微信官方給的PHP版本的SDK,進行支付的時候寫代碼可以省不少事,步驟也挺簡化,但是集成SDK有很多坑,很多人說引入的SDK老報錯,或者說官方SDK本身有不少錯誤,改起來很麻煩,也確實挺麻煩的,對于新手搞支付很容易被繞進去,那么今天就來講講不集成支付SDK直接調用支付接口實現支付和退款。
前期準備:
1.當然了,還是要有一個微信認證服務號,并且開通了微信支付;
2.在微信商戶后臺配置好支付授權目錄,同時準備好支付的Api證書(支付用不到,退款的時候使用)
3.調用接口支付的話,必須要先知道該用戶的openid,所以要先知道怎么獲取用戶的openid,這個也很簡單,我之前也有文章講怎么獲取用戶的openid,詳見文章微信公眾號獲取用戶的openid。
好了,話不多說,直接貼上主要代碼:
09 | public function wxpay( $openid , $total_fee , $body , $order_sn ){ |
10 | $config = $this ->config; |
12 | $unifiedorder = array ( |
13 | 'appid' => $config [ 'appid' ], |
14 | 'mch_id' => $config [ 'mch_id' ], |
15 | 'nonce_str' => self::getNonceStr(), |
17 | 'out_trade_no' => $order_sn , |
18 | 'total_fee' => $total_fee * 100, |
19 | 'spbill_create_ip' => self::getip(), |
20 | 'notify_url' => 'http://' . $_SERVER [ 'HTTP_HOST' ]. '/notify.php' , |
21 | 'trade_type' => 'JSAPI' , |
24 | $unifiedorder [ 'sign' ] = self::makeSign( $unifiedorder ); |
27 | $xmldata = self::array2xml( $unifiedorder ); |
29 | $res = self::curl_post_ssl( $url , $xmldata ); |
31 | return array ( 'status' =>0, 'msg' => "Can't connect the server" ); |
35 | $content = self::xml2array( $res ); |
36 | if ( strval ( $content [ 'result_code' ]) == 'FAIL' ){ |
37 | return array ( 'status' =>0, 'msg' => strval ( $content [ 'err_code' ]). ':' . strval ( $content [ 'err_code_des' ])); |
39 | if ( strval ( $content [ 'return_code' ]) == 'FAIL' ){ |
40 | return array ( 'status' =>0, 'msg' => strval ( $content [ 'return_msg' ])); |
43 | settype( $time , "string" ); |
45 | 'appId' => strval ( $content [ 'appid' ]), |
46 | 'nonceStr' => strval ( $content [ 'nonce_str' ]), |
47 | 'package' => 'prepay_id=' . strval ( $content [ 'prepay_id' ]), |
51 | $resdata [ 'paySign' ] = self::makeSign( $resdata ); |
52 | return json_encode( $resdata ); |
62 | public function refund( $transaction_id , $out_refund_no , $total_fee , $refund_fee ){ |
63 | $config = $this ->config; |
66 | 'appid' => $config [ 'appid' ], |
67 | 'mch_id' => $config [ 'mch_id' ], |
68 | 'nonce_str' => self::getNonceStr(), |
69 | 'transaction_id' => $transaction_id , |
70 | 'out_refund_no' => $out_refund_no , |
71 | 'total_fee' => $total_fee * 100, |
72 | 'refund_fee' => $refund_fee * 100 |
74 | $refundorder [ 'sign' ] = self::makeSign( $refundorder ); |
76 | $xmldata = self::array2xml( $refundorder ); |
78 | $res = self::curl_post_ssl( $url , $xmldata ); |
80 | return array ( 'status' =>0, 'msg' => "Can't connect the server" ); |
84 | $content = self::xml2array( $res ); |
85 | if ( strval ( $content [ 'result_code' ]) == 'FAIL' ){ |
86 | return array ( 'status' =>0, 'msg' => strval ( $content [ 'err_code' ]). ':' . strval ( $content [ 'err_code_des' ])); |
88 | if ( strval ( $content [ 'return_code' ]) == 'FAIL' ){ |
89 | return array ( 'status' =>0, 'msg' => strval ( $content [ 'return_msg' ])); |
這是封裝好的類,使用起來也超級簡單:
02 | require_once "wxpay.class.php" ; |
04 | 'wxappid' => 'wx123456789876' , |
05 | 'mch_id' => '123456789' , |
06 | 'pay_apikey' => '123456789876123456789876123456789876' |
08 | $wxpay = new WxPay( $config ); |
09 | $result = $wxpay ->paytest(); |
13 | <meta http-equiv= "content-type" content= "text/html;charset=utf-8" /> |
14 | <meta name= "viewport" content= "width=device-width, initial-scale=1" /> |
16 | <script type= "text/javascript" > |
20 | WeixinJSBridge.invoke( |
21 | 'getBrandWCPayRequest' ,<?php echo $result ; ?>, |
23 | WeixinJSBridge.log(res.err_msg); |
25 | if (res.err_msg == "get_brand_wcpay_request:ok" ){ |
27 | } else if (res.err_msg == "get_brand_wcpay_request:cancel" ){ |
37 | if (typeof WeixinJSBridge == "undefined" ){ |
38 | if ( document.addEventListener ){ |
39 | document.addEventListener( 'WeixinJSBridgeReady' , jsApiCall, false); |
40 | } else if (document.attachEvent){ |
41 | document.attachEvent( 'WeixinJSBridgeReady' , jsApiCall); |
42 | document.attachEvent( 'onWeixinJSBridgeReady' , jsApiCall); |
52 | <font color= "#9ACD32" ><b>該筆訂單支付金額為<span style= "color:#f00;font-size:50px" >1分</span>錢</b></font><br/><br/> |
53 | <font color= "#9ACD32" ><b><span style= "color:#f00;font-size:50px;margin-left:40%;" >1分</span>錢也是愛</b></font><br/><br/> |
55 | <button style= "width:210px; height:50px; border-radius: 15px;background-color:#FE6714; border:0px #FE6714 solid; cursor: pointer; color:white; font-size:16px;" type= "button" onclick= "callpay()" >果斷買買買^_^</button> |
至于支付回調驗證,這里就不過多講了,不明白的可以看ThinkPHP中實現微信支付(jsapi支付)流程,這里詳細講了如何處理回調。
總結
以上所述是小編給大家介紹的PHP實現微信支付(jsapi支付)和退款(無需集成支付SDK)流程教程詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!