Micro-channel pay refund

tp5 micro-channel word does not refund less directly on the code:

Micro-channel refund method
public function Tuikuan ($ order_no) {
$ Order = Db ( 'order form') -> WHERE ( 'order_no', $ order_no) -> Find ();
IF (empty ($ Order)) {
return. 1 ; // order does not exist
}

$ the OrderData = Db ( 'pay order form') -> where ( 'statuss ', 0) -> where (. 'out_trade_no', 'like', $ order_no '%') -> select ();
IF (empty ($ the OrderData)) {
return. 1; // no payment orders
}
$ config = Array (
'mch_id' => 'business number',
'AppID' => 'micro-channel public number ID',
'Key '=>' key payments micro-channel ',
);

the foreach ($ K AS = the OrderData $> $ V) {
$ RES = $ this-> TuiKuanOrder (V $, $ config);
}
}
// call the method refund
public function TuiKuanOrder ($ arr,$config){
$refundNo='tuikuan'.$arr['id'].'-'.time().rand(1000,9999);
$unified = array(
'AppID' => $ config [ 'AppID'],
'mch_id' => $ config [ 'mch_id'],
'nonce_str' => Self :: createNonceStr (),
'total_fee' => $ ARR [ 'total_fee'] // order amount divided into units
'refund_fee' => $ arr [ 'total_fee'], // refund amounts paid into how many points back
'sign_type' => 'MD5' , // signature type support HMAC -SHA256 and MD5, the default is the MD5
'transaction_id in' => $ ARR [ 'transaction_id in'], // number of micro-channel Order
'out_trade_no' => $ arr [ 'out_trade_no'], // merchant order number
'out_refund_no' => $ refundNo, // merchant credit note number
'refund_desc' => 'goods sold Out', // refund reason (optional)
);
$ Unified [ 'Sign'] :: = Self getSign (Unified $, $ config [ 'key']);//获取签名
$responseXml = $this->curlPost('https://api.mch.weixin.qq.com/secapi/pay/refund', self::arrayToXml($unified));
$unifiedOrder = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
dump($unified);
dump($unifiedOrder);
if ($unifiedOrder === false) {
die('parse xml error');
}
if ($unifiedOrder->return_code != 'SUCCESS') {
die($unifiedOrder->return_msg);
}
if ($unifiedOrder->result_code != 'SUCCESS') {
die($unifiedOrder->err_code);
}

//dump($unifiedOrder);
//echo true;
return true;
//return $unified;
}
//随机字符串,不长于32位
public static function createNonceStr($length = 16) {
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$str = '';
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
//获取sign签名
public static function getSign($params, $key) {
ksort($params, SORT_STRING);
$unSignParaString = self::formatQueryParaMap($params, false);
$signStr = strtoupper(md5($unSignParaString . "&key=" . $key));
return $signStr;
}
//生成签名
protected static 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;
}
//curl post提交退款
public function curlPost($url = '', $postData = '', $options = array()) {
if (is_array($postData)) {
$postData = http_build_query($postData);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
if (!empty($options)) {
curl_setopt_array (CH $, $ Options);
}
// HTTPS request does not verify the certificate and Host
curl_setopt ($ CH, CURLOPT_SSL_VERIFYPEER, to false);
curl_setopt ($ CH, CURLOPT_SSL_VERIFYHOST, to false);
// first method, cert and key, respectively, belonging to two .pem file
// default format is PEM, can comment
curl_setopt ($ CH, CURLOPT_SSLCERTTYPE, 'PEM');
curl_setopt ($ CH, CURLOPT_SSLCERT, "/ the WWW / ... absolute path /apiclient_cert.pem"); // absolute path
// default format is PEM, can comment
curl_setopt ($ CH, CURLOPT_SSLKEYTYPE, 'PEM');
curl_setopt ($ CH, CURLOPT_SSLKEY, "/ the WWW / ... absolute path t / apiclient_key.pem"); / / absolute path
// second embodiment, the two files into one file .pem
// curl_setopt ($ CH, CURLOPT_SSLCERT, getcwd () '/ all.pem'.);
$ Data = the curl_exec ($ CH);
IF ( Data $) {
curl_close ($ CH);
return $ Data;
} else {
$error = curl_errno($ch);
echo "curl出错,错误码:$error" . "<br>";
curl_close($ch);
return false;
}
}
//生成xml文件
public 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;
}

 Micro-channel payment and refund Detailed View link https://www.jianshu.com/p/0ceef29f2707/

Guess you like

Origin www.cnblogs.com/xinyixuan/p/11527761.html