佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > PHP實(shí)現(xiàn)微信支付(jsapi支付)和退款(無(wú)需集成支付SDK)流程教程詳解

PHP實(shí)現(xiàn)微信支付(jsapi支付)和退款(無(wú)需集成支付SDK)流程教程詳解

熱門(mén)標(biāo)簽:Mysql連接數(shù)設(shè)置 服務(wù)器配置 銀行業(yè)務(wù) 科大訊飛語(yǔ)音識(shí)別系統(tǒng) Linux服務(wù)器 團(tuán)購(gòu)網(wǎng)站 阿里云 電子圍欄

之前有寫(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.在微信商戶后臺(tái)配置好支付授權(quán)目錄,同時(shí)準(zhǔn)備好支付的Api證書(shū)(支付用不到,退款的時(shí)候使用)

3.調(diào)用接口支付的話,必須要先知道該用戶的openid,所以要先知道怎么獲取用戶的openid,這個(gè)也很簡(jiǎn)單,我之前也有文章講怎么獲取用戶的openid,詳見(jiàn)文章微信公眾號(hào)獲取用戶的openid。

好了,話不多說(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("用戶取消支付!"); 
 }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)站的支持!

您可能感興趣的文章:
  • Thinkphp集成抖音SDK的實(shí)現(xiàn)方法
  • 微信sdk實(shí)現(xiàn)禁止微信分享(使用原生php實(shí)現(xiàn))
  • PHP如何實(shí)現(xiàn)阿里云短信sdk靈活應(yīng)用在項(xiàng)目中的方法
  • thinkPHP微信分享接口JSSDK用法實(shí)例
  • thinkPHP簡(jiǎn)單導(dǎo)入和使用阿里云OSSsdk的方法
  • php一個(gè)文件搞定微信jssdk配置
  • PHP實(shí)現(xiàn)微信JS-SDK接口選擇相冊(cè)及拍照并上傳的方法
  • 微信支付PHP SDK之微信公眾號(hào)支付代碼詳解
  • 微信公眾平臺(tái)開(kāi)發(fā)接口PHP SDK完整版
  • PHP sdk實(shí)現(xiàn)在線打包代碼示例

標(biāo)簽:衡水 萍鄉(xiāng) 廣元 江蘇 蚌埠 棗莊 大理 衢州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP實(shí)現(xiàn)微信支付(jsapi支付)和退款(無(wú)需集成支付SDK)流程教程詳解》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢(xún)

    • 400-1100-266
    砚山县| 临洮县| 康平县| 永春县| 犍为县| 阳山县| 贵阳市| 海南省| 辽阳县| 瑞丽市| 南京市| 江城| 海宁市| 汉源县| 南江县| 襄垣县| 福泉市| 祁门县| 准格尔旗| 水富县| 嵊州市| 双鸭山市| 江口县| 清涧县| 临西县| 错那县| 宁强县| 绥阳县| 咸丰县| 辽中县| 吉木乃县| 伊宁县| 乐山市| 涡阳县| 肃北| 沙洋县| 中西区| 焦作市| 鹿邑县| 临邑县| 景德镇市|