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;//退款訂單
}
/**
* 通過微信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微信開發(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開發(fā)中的經(jīng)驗(yàn)與常識(shí)小結(jié)
- PHP設(shè)計(jì)模式之抽象工廠模式實(shí)例分析
- PHP設(shè)計(jì)模式之簡(jiǎn)單工廠和工廠模式實(shí)例分析
- PHP實(shí)現(xiàn)無限極分類的兩種方式示例【遞歸和引用方式】
- PHP中l(wèi)ocaleconv()函數(shù)的用法
相關(guān)文章
PHP實(shí)現(xiàn)數(shù)據(jù)庫統(tǒng)計(jì)時(shí)間戳按天分組輸出數(shù)據(jù)的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)數(shù)據(jù)庫統(tǒng)計(jì)時(shí)間戳按天分組輸出數(shù)據(jù)的方法,涉及php基于時(shí)間的運(yùn)算與數(shù)據(jù)庫查詢相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
PHP基于接口技術(shù)實(shí)現(xiàn)簡(jiǎn)單的多態(tài)應(yīng)用完整實(shí)例
這篇文章主要介紹了PHP基于接口技術(shù)實(shí)現(xiàn)簡(jiǎn)單的多態(tài)應(yīng)用,結(jié)合完整實(shí)例形式分析了php接口的定義、繼承、調(diào)用及多態(tài)的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-04-04
php常用字符串查找函數(shù)strstr()與strpos()實(shí)例分析
這篇文章主要介紹了php常用字符串查找函數(shù)strstr()與strpos(),結(jié)合具體實(shí)例形式分析了php字符串查找函數(shù)strstr()與strpos()的具體功能、用法、區(qū)別及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-06-06
phpmailer在服務(wù)器上不能正常發(fā)送郵件的解決辦法
這篇文章主要介紹了phpmailer在服務(wù)器上不能正常發(fā)送郵件的解決辦法,本文的原因是服務(wù)器的安全設(shè)置造成,服務(wù)器中屏蔽fsockopen函數(shù)的使用權(quán)限,所以導(dǎo)致發(fā)送失敗,需要的朋友可以參考下2014-07-07
php使用array_rand()函數(shù)從數(shù)組中隨機(jī)選擇一個(gè)或多個(gè)元素
這篇文章主要介紹了php使用array_rand()函數(shù)從數(shù)組中隨機(jī)選擇一個(gè)或多個(gè)元素,需要的朋友可以參考下2014-04-04

