PHP實(shí)現(xiàn)微信支付(jsapi支付)和退款(無(wú)需集成支付SDK)流程教程詳解
之前有寫(xiě)過(guò)幾篇文章將微信支付和退款:
1.PHP實(shí)現(xiàn)微信支付(jsapi支付)流程
2.ThinkPHP中實(shí)現(xiàn)微信支付(jsapi支付)流程
3.PHP實(shí)現(xiàn)微信申請(qǐng)退款
這幾篇都是使用了微信官方給的PHP版本的SDK,進(jìn)行支付的時(shí)候?qū)懘a可以省不少事,步驟也挺簡(jiǎn)化,但是集成SDK有很多坑,很多人說(shuō)引入的SDK老報(bào)錯(cuò),或者說(shuō)官方SDK本身有不少錯(cuò)誤,改起來(lái)很麻煩,也確實(shí)挺麻煩的,對(duì)于新手搞支付很容易被繞進(jìn)去,那么今天就來(lái)講講不集成支付SDK直接調(diào)用支付接口實(shí)現(xiàn)支付和退款。
前期準(zhǔn)備:
1.當(dāng)然了,還是要有一個(gè)微信認(rèn)證服務(wù)號(hào),并且開(kāi)通了微信支付;
2.在微信商戶(hù)后臺(tái)配置好支付授權(quán)目錄,同時(shí)準(zhǔn)備好支付的Api證書(shū)(支付用不到,退款的時(shí)候使用)
3.調(diào)用接口支付的話(huà),必須要先知道該用戶(hù)的openid,所以要先知道怎么獲取用戶(hù)的openid,這個(gè)也很簡(jiǎn)單,我之前也有文章講怎么獲取用戶(hù)的openid,詳見(jiàn)文章微信公眾號(hào)獲取用戶(hù)的openid。
好了,話(huà)不多說(shuō),直接貼上主要代碼:
/** * 微信支付請(qǐng)求接口(POST) * @param string $openid openid * @param string $body 商品簡(jiǎn)單描述 * @param string $order_sn 訂單編號(hào) * @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; //請(qǐng)求數(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是用來(lái)查看服務(wù)器返回的結(jié)果 測(cè)試完可以刪除了 //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支付界面,時(shí)間戳必須為字符串格式 $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 在微信支付的時(shí)候,微信服務(wù)器生成的訂單流水號(hào),在支付通知中有返回 * @param string $out_refund_no 商品簡(jiǎn)單描述 * @param string $total_fee 微信支付的時(shí)候支付的總金額(單位:分) * @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); //請(qǐng)求數(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是用來(lái)查看服務(wù)器返回的結(jié)果 測(cè)試完可以刪除了 //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; }
這是封裝好的類(lèi),使用起來(lái)也超級(jí)簡(jiǎn)單:
<?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("用戶(hù)取消支付!"); }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>錢(qián)</b></font><br/><br/> <font color="#9ACD32"><b><span style="color:#f00;font-size:50px;margin-left:40%;">1分</span>錢(qián)也是愛(ài)</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()" >果斷買(mǎi)買(mǎi)買(mǎi)^_^</button> </div> </body> </html>
至于支付回調(diào)驗(yàn)證,這里就不過(guò)多講了,不明白的可以看ThinkPHP中實(shí)現(xiàn)微信支付(jsapi支付)流程,這里詳細(xì)講了如何處理回調(diào)。
類(lèi)文件以及使用實(shí)例代碼源碼下載:http://xiazai.jb51.net/201803/yuanma/php_jsapi_jb51.rar
總結(jié)
以上所述是小編給大家介紹的PHP實(shí)現(xiàn)微信支付(jsapi支付)和退款(無(wú)需集成支付SDK)流程教程詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
tp5(thinkPHP5)操作mongoDB數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了tp5(thinkPHP5)操作mongoDB數(shù)據(jù)庫(kù)的方法,結(jié)合實(shí)例形式簡(jiǎn)單分析了mongoDB數(shù)據(jù)庫(kù)及thinkPHP5連接、查詢(xún)MongoDB數(shù)據(jù)庫(kù)的基本操作技巧,需要的朋友可以參考下2018-01-01PHP使用SOAP調(diào)用.net的WebService數(shù)據(jù)
需要和一個(gè).net系統(tǒng)進(jìn)行數(shù)據(jù)交換,對(duì)方提供了一個(gè)WebService接口,使用PHP如何調(diào)用這個(gè)數(shù)據(jù)呢,下面就看看使用SOAP調(diào)用的方法吧2013-11-11Zend Framework入門(mén)教程之Zend_Config組件用法詳解
這篇文章主要介紹了Zend Framework入門(mén)教程之Zend_Config組件用法,結(jié)合實(shí)例形式分析了Zend_Config組件針對(duì)各種類(lèi)型配置文件操作的相關(guān)技巧,需要的朋友可以參考下2016-12-12Yii框架學(xué)習(xí)筆記之應(yīng)用組件操作示例
這篇文章主要介紹了Yii框架學(xué)習(xí)筆記之應(yīng)用組件操作,結(jié)合實(shí)例形式分析了Yii框架自定義組件的創(chuàng)建與使用相關(guān)操作技巧,需要的朋友可以參考下2019-11-11php發(fā)送post請(qǐng)求函數(shù)分享
這篇文章主要介紹了一個(gè)php發(fā)送post請(qǐng)求的函數(shù),開(kāi)發(fā)中經(jīng)常會(huì)用到,需要的朋友可以參考下2014-03-03