PHP實現(xiàn)微信支付(jsapi支付)和退款(無需集成支付SDK)流程教程詳解
之前有寫過幾篇文章將微信支付和退款:
1.PHP實現(xiàn)微信支付(jsapi支付)流程
2.ThinkPHP中實現(xiàn)微信支付(jsapi支付)流程
3.PHP實現(xiàn)微信申請退款
這幾篇都是使用了微信官方給的PHP版本的SDK,進(jìn)行支付的時候?qū)懘a可以省不少事,步驟也挺簡化,但是集成SDK有很多坑,很多人說引入的SDK老報錯,或者說官方SDK本身有不少錯誤,改起來很麻煩,也確實挺麻煩的,對于新手搞支付很容易被繞進(jìn)去,那么今天就來講講不集成支付SDK直接調(diào)用支付接口實現(xiàn)支付和退款。
前期準(zhǔn)備:
1.當(dāng)然了,還是要有一個微信認(rèn)證服務(wù)號,并且開通了微信支付;
2.在微信商戶后臺配置好支付授權(quán)目錄,同時準(zhǔn)備好支付的Api證書(支付用不到,退款的時候使用)
3.調(diào)用接口支付的話,必須要先知道該用戶的openid,所以要先知道怎么獲取用戶的openid,這個也很簡單,我之前也有文章講怎么獲取用戶的openid,詳見文章微信公眾號獲取用戶的openid。
好了,話不多說,直接貼上主要代碼:
/** * 微信支付請求接口(POST) * @param string $openid openid * @param string $body 商品簡單描述 * @param string $order_sn 訂單編號 * @param string $total_fee 金額 * @return json的數(shù)據(jù) */ public function wxpay($openid,$total_fee,$body,$order_sn){ $config = $this->config; //統(tǒng)一下單參數(shù)構(gòu)造 $unifiedorder = array( 'appid' => $config['appid'], 'mch_id' => $config['mch_id'], 'nonce_str' => self::getNonceStr(), 'body' => $body, 'out_trade_no' => $order_sn, 'total_fee' => $total_fee * 100, 'spbill_create_ip' => self::getip(), 'notify_url' => 'http://'.$_SERVER['HTTP_HOST'].'/notify.php', 'trade_type' => 'JSAPI', 'openid' => $openid ); $unifiedorder['sign'] = self::makeSign($unifiedorder); //return $unifiedorder; //請求數(shù)據(jù),統(tǒng)一下單 $xmldata = self::array2xml($unifiedorder); $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; $res = self::curl_post_ssl($url, $xmldata); if(!$res){ return array('status'=>0, 'msg'=>"Can't connect the server" ); } // 這句file_put_contents是用來查看服務(wù)器返回的結(jié)果 測試完可以刪除了 //file_put_contents('./log.txt',$res,FILE_APPEND); $content = self::xml2array($res); if(strval($content['result_code']) == 'FAIL'){ return array('status'=>0, 'msg'=>strval($content['err_code']).':'.strval($content['err_code_des'])); } if(strval($content['return_code']) == 'FAIL'){ return array('status'=>0, 'msg'=>strval($content['return_msg'])); } $time = time(); settype($time, "string"); //jsapi支付界面,時間戳必須為字符串格式 $resdata = array( 'appId' => strval($content['appid']), 'nonceStr' => strval($content['nonce_str']), 'package' => 'prepay_id='.strval($content['prepay_id']), 'signType' => 'MD5', 'timeStamp' => $time ); $resdata['paySign'] = self::makeSign($resdata); return json_encode($resdata); } /** * 微信退款(POST) * @param string(28) $transaction_id 在微信支付的時候,微信服務(wù)器生成的訂單流水號,在支付通知中有返回 * @param string $out_refund_no 商品簡單描述 * @param string $total_fee 微信支付的時候支付的總金額(單位:分) * @param string $refund_fee 此次要退款金額(單位:分) * @return string xml格式的數(shù)據(jù) */ public function refund($transaction_id,$out_refund_no,$total_fee,$refund_fee){ $config = $this->config; //退款參數(shù) $refundorder = array( 'appid' => $config['appid'], 'mch_id' => $config['mch_id'], 'nonce_str' => self::getNonceStr(), 'transaction_id'=> $transaction_id, 'out_refund_no' => $out_refund_no, 'total_fee' => $total_fee * 100, 'refund_fee' => $refund_fee * 100 ); $refundorder['sign'] = self::makeSign($refundorder); //請求數(shù)據(jù),進(jìn)行退款 $xmldata = self::array2xml($refundorder); $url = 'https://api.mch.weixin.qq.com/secapi/pay/refund'; $res = self::curl_post_ssl($url, $xmldata); if(!$res){ return array('status'=>0, 'msg'=>"Can't connect the server" ); } // 這句file_put_contents是用來查看服務(wù)器返回的結(jié)果 測試完可以刪除了 //file_put_contents('./log3.txt',$res,FILE_APPEND); $content = self::xml2array($res); if(strval($content['result_code']) == 'FAIL'){ return array('status'=>0, 'msg'=>strval($content['err_code']).':'.strval($content['err_code_des'])); } if(strval($content['return_code']) == 'FAIL'){ return array('status'=>0, 'msg'=>strval($content['return_msg'])); } return $content; }
這是封裝好的類,使用起來也超級簡單:
<?php require_once "wxpay.class.php"; $config = array( 'wxappid' => 'wx123456789876', 'mch_id' => '123456789', 'pay_apikey' => '123456789876123456789876123456789876' ); $wxpay = new WxPay($config); $result = $wxpay->paytest(); ?> <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 type="text/javascript"> //調(diào)用微信JS api 支付 function jsApiCall() { WeixinJSBridge.invoke( 'getBrandWCPayRequest',<?php echo $result; ?>, function(res){ WeixinJSBridge.log(res.err_msg); //alert(res); if(res.err_msg == "get_brand_wcpay_request:ok"){ alert("支付成功!"); }else if(res.err_msg == "get_brand_wcpay_request:cancel"){ alert("用戶取消支付!"); }else{ alert("支付失敗!"); } } ); } function callpay() { 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(); } } </script> </head> <body> <br/> <font color="#9ACD32"><b>該筆訂單支付金額為<span style="color:#f00;font-size:50px">1分</span>錢</b></font><br/><br/> <font color="#9ACD32"><b><span style="color:#f00;font-size:50px;margin-left:40%;">1分</span>錢也是愛</b></font><br/><br/> <div align="center"> <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> </div> </body> </html>
至于支付回調(diào)驗證,這里就不過多講了,不明白的可以看ThinkPHP中實現(xiàn)微信支付(jsapi支付)流程,這里詳細(xì)講了如何處理回調(diào)。
類文件以及使用實例代碼源碼下載:http://xiazai.jb51.net/201803/yuanma/php_jsapi_jb51.rar
總結(jié)
以上所述是小編給大家介紹的PHP實現(xiàn)微信支付(jsapi支付)和退款(無需集成支付SDK)流程教程詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
tp5(thinkPHP5)操作mongoDB數(shù)據(jù)庫的方法
這篇文章主要介紹了tp5(thinkPHP5)操作mongoDB數(shù)據(jù)庫的方法,結(jié)合實例形式簡單分析了mongoDB數(shù)據(jù)庫及thinkPHP5連接、查詢MongoDB數(shù)據(jù)庫的基本操作技巧,需要的朋友可以參考下2018-01-01PHP使用SOAP調(diào)用.net的WebService數(shù)據(jù)
需要和一個.net系統(tǒng)進(jìn)行數(shù)據(jù)交換,對方提供了一個WebService接口,使用PHP如何調(diào)用這個數(shù)據(jù)呢,下面就看看使用SOAP調(diào)用的方法吧2013-11-11Zend Framework入門教程之Zend_Config組件用法詳解
這篇文章主要介紹了Zend Framework入門教程之Zend_Config組件用法,結(jié)合實例形式分析了Zend_Config組件針對各種類型配置文件操作的相關(guān)技巧,需要的朋友可以參考下2016-12-12Yii框架學(xué)習(xí)筆記之應(yīng)用組件操作示例
這篇文章主要介紹了Yii框架學(xué)習(xí)筆記之應(yīng)用組件操作,結(jié)合實例形式分析了Yii框架自定義組件的創(chuàng)建與使用相關(guān)操作技巧,需要的朋友可以參考下2019-11-11