Автор оригинала: David Wong.
В примере этой статьи описано, как реализовать возврат средств wechat в PHP. Чтобы поделиться с вами для вашей справки, следующим образом:
$obj = new wxrefund ('parameter ');
$obj->refundApi();
Вы можете использовать параметры официального аккаунта напрямую, и вы можете только помочь вам попасть сюда.
php
namespace Wechat;
/**
*Wechat refund
* @author zzy
* @version $V1.0.0$
* @date 2018-11-9
*/
class WXRefund
{
protected $SSLCERT_ Path = ''; // certificate
protected $SSLKEY_ Path = ''; // certificate
Protected $opuserid = ''; // merchant No
Protected $key = ''; // API key
protected $appId = '';//appId
function __construct($outTradeNo, $totalFee, $outRefundNo, $refundFee)
{
//Variables required to initialize refund class
$this - > totalfee = $totalfee; // order amount
$this - > refundfee = $refundfee; // refund amount
$this - > outtradeno = $outtradeno; // Order No
$this - > outrefundno = $outrefundno; // refund order
}
/**
*The only external interface for refund process through wechat API
* @return string
*/
public function refundApi()
{
$parma = array(
'appid' => $this->appId,
'mch_id' => $this->opUserId,
'nonce_ STR '= > randoms (32), // this is a random number to encapsulate...
'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;
}
/**
*Array to XML
* @param $arr
* @return string
*/
protected function arrayToXml($arr)
{
$xml = "";
foreach ($arr as $key => $val) {
if (is_numeric($val)) {
$xml .= "<" . $key . ">" . $val . "";
} else {
$xml .= "<" . $key . ">";
}
}
$xml .= "";
return $xml;
}
/**
*Signature encryption
* @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));
}
/**
*Sort
* @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;
}
/**
*Requests that require a certificate
* @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 error, error code: $error". "" < br > ";
curl_close($ch);
return false;
}
}
}
Для получения дополнительной информации о PHP, читатели, которые интересуются PHP, пожалуйста, ознакомьтесь со следующими разделами: краткое описание навыков разработки PHP wechat, краткое описание использования PHP curl, краткое описание навыков сетевого программирования PHP, краткое описание использования строк PHP, краткое описание навыков работы с данными в формате JSON в PHP и краткое описание навыков работы с PHP для XML-файлов
Я надеюсь, что эта статья будет полезна для программирования на PHP.