博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php微信退款
阅读量:3921 次
发布时间:2019-05-23

本文共 4100 字,大约阅读时间需要 13 分钟。

totalFee = $totalFee;//订单金额 $this->refundFee = $refundFee;//退款金额 $this->outTradeNo = $outTradeNo;//订单号 $this->outRefundNo = $outRefundNo;//退款订单 $this->opUserId = config('wxpay.mch_id'); $this->key = config('wxpay.key'); $this->appId = config('wxpay.app_id'); $this->SSLCERT_PATH = dirname(__FILE__) . '/cert/apiclient_cert.pem'; $this->SSLKEY_PATH = dirname(__FILE__) . '/cert/apiclient_key.pem'; } /** * 通过微信api进行退款流程 唯一对外接口 * @return string */ public function refundApi() { $parma = array( 'appid' => $this->appId, 'mch_id' => $this->opUserId, 'nonce_str' => $this->randoms(32), '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'); halt($xmlresult); $result = $this->arrayToXml($xmlresult); return $result; } /** * 数组转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; } /** * 签名加密 * @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; } /** * 需要使用证书的请求 * @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" . "
"; curl_close($ch); return false; } } protected function randoms($length = 32) { $chars = "abcdefghijklmnopqrstuvwxyz0123456789"; $str = ""; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; }}
去调用上面的退款$refundObj = new WXRefund($order['order_number'], $order['money'], $order['order_number'], $order['money']);$result = $refundObj->refundApi();

转载地址:http://mohrn.baihongyu.com/

你可能感兴趣的文章
C++ primer 第十七章 异常处理部分
查看>>
C++ primer 第十七章 命名空间部分
查看>>
C++ perimer 第十七章 多重继承与虚继承部分
查看>>
java 堆溢出的解决方法
查看>>
C++ primer 第十八章
查看>>
《浪潮之巅》笔记&小感悟
查看>>
java 重写类的equals方法和hashcode方法
查看>>
java 判断字符串中是否含有字母
查看>>
《产品经理手册》 读书笔记
查看>>
IT人员迅速提升自我效率的十大方法
查看>>
《结网》读书笔记
查看>>
Eclipse里Tomcat的几个设置
查看>>
Ajax发请求的基本流程
查看>>
java类构造函数的继承
查看>>
Google的几个地理位置相关的API
查看>>
DataInputStream和DataOutputStream
查看>>
将amr音频转为flac格式
查看>>
eclipse创建j2me工程注意
查看>>
S40使用J2ME录音
查看>>
神经网络学习
查看>>