PHP實(shí)現(xiàn)微信退款的方法示例
本文實(shí)例講述了PHP實(shí)現(xiàn)微信退款的方法。分享給大家供大家參考,具體如下:
$obj = new WXRefund('參數(shù)'); $obj->refundApi();
直接能用 公眾號(hào)的參數(shù) 自己加上吧 只能幫你們到這了!
<?php namespace Wechat; /** * 微信退款 * @author zzy * @version $V1.0.0$ * @date 2018-11-9 */ class WXRefund { protected $SSLCERT_PATH ='';//證書 protected $SSLKEY_PATH = '';//證書 protected $opUserId = '';//商戶號(hào) protected $key = '';//API密鑰 protected $appId = '';//appId function __construct($outTradeNo, $totalFee, $outRefundNo, $refundFee) { //初始化退款類需要的變量 $this->totalFee = $totalFee;//訂單金額 $this->refundFee = $refundFee;//退款金額 $this->outTradeNo = $outTradeNo;//訂單號(hào) $this->outRefundNo = $outRefundNo;//退款訂單 } /** * 通過(guò)微信api進(jìn)行退款流程 唯一對(duì)外接口 * @return string */ public function refundApi() { $parma = array( 'appid' => $this->appId, 'mch_id' => $this->opUserId, 'nonce_str' => randoms(32),//這個(gè)是隨機(jī)數(shù) 自己封裝去吧。。。 'out_refund_no' => $this->outRefundNo, 'out_trade_no' => $this->outTradeNo, 'total_fee' => intval($this->totalFee * 100), 'refund_fee' => intval($this->refundFee * 100), ); $parma['sign'] = $this->getSign($parma, $this->key); $xmldata = $this->arrayToXml($parma); $xmlresult = $this->postXmlSSLCurl($xmldata, 'https://api.mch.weixin.qq.com/secapi/pay/refund'); $result = $this->arrayToXml($xmlresult); return $result; } /** * 數(shù)組轉(zhuǎn)xml * @param $arr * @return string */ protected function arrayToXml($arr) { $xml = "<xml>"; foreach ($arr as $key => $val) { if (is_numeric($val)) { $xml .= "<" . $key . ">" . $val . "</" . $key . ">"; } else { $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">"; } } $xml .= "</xml>"; return $xml; } /** * 簽名加密 * @param $params * @param $key */ protected function getSign($params, $key) { ksort($params, SORT_STRING); $unSignParaString = $this->formatQueryParaMap($params, false); return $signStr = strtoupper(md5($unSignParaString . "&key=" . $key)); } /** * 排序 * @param $paraMap * @param bool $urlEncode * @return bool|string */ protected function formatQueryParaMap($paraMap, $urlEncode = false) { $buff = ""; ksort($paraMap); foreach ($paraMap as $k => $v) { if (null != $v && "null" != $v) { if ($urlEncode) { $v = urlencode($v); } $buff .= $k . "=" . $v . "&"; } } $reqPar = ''; if (strlen($buff) > 0) { $reqPar = substr($buff, 0, strlen($buff) - 1); } return $reqPar; } /** * 需要使用證書的請(qǐng)求 * @param $xml * @param $url * @param int $second * @return bool|mixed */ protected function postXmlSSLCurl($xml, $url, $second = 30) { $ch = curl_init(); curl_setopt($ch, CURLOPT_TIMEOUT, $second); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM'); curl_setopt($ch, CURLOPT_SSLCERT, $this->SSLCERT_PATH); curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM'); curl_setopt($ch, CURLOPT_SSLKEY, $this->SSLKEY_PATH); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); $data = curl_exec($ch); if ($data) { curl_close($ch); return $data; } else { $error = curl_errno($ch); echo "curl出錯(cuò),錯(cuò)誤碼:$error" . "<br>"; curl_close($ch); return false; } } }
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP微信開(kāi)發(fā)技巧匯總》、《php curl用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP中json格式數(shù)據(jù)操作技巧匯總》及《PHP針對(duì)XML文件操作技巧總結(jié)》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP實(shí)現(xiàn)的支付寶支付功能示例
- PHP微信支付功能示例
- PHP小程序支付功能完整版【基于thinkPHP】
- PHP設(shè)計(jì)模式之單例模式定義與用法分析
- docker搭建php+nginx+swoole+mysql+redis環(huán)境的方法
- php+mysql開(kāi)發(fā)中的經(jīng)驗(yàn)與常識(shí)小結(jié)
- PHP設(shè)計(jì)模式之抽象工廠模式實(shí)例分析
- PHP設(shè)計(jì)模式之簡(jiǎn)單工廠和工廠模式實(shí)例分析
- PHP實(shí)現(xiàn)無(wú)限極分類的兩種方式示例【遞歸和引用方式】
- PHP中l(wèi)ocaleconv()函數(shù)的用法
相關(guān)文章
PHP實(shí)現(xiàn)廣度優(yōu)先搜索算法(BFS,Broad First Search)詳解
這篇文章主要介紹了PHP實(shí)現(xiàn)廣度優(yōu)先搜索算法(BFS,Broad First Search),簡(jiǎn)單描述了廣度優(yōu)先搜索算法的原理并結(jié)合具體實(shí)例分析了php實(shí)現(xiàn)廣度優(yōu)先搜索算法的步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-09-09php日志函數(shù)error_log用法實(shí)例分析
這篇文章主要介紹了php日志函數(shù)error_log用法,結(jié)合實(shí)例形式分析了php日志函數(shù)error_log相關(guān)的配置文件設(shè)置、函數(shù)功能、用法與使用注意事項(xiàng),需要的朋友可以參考下2019-09-09