RoutineRefund.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace service;
  12. /**
  13. * 小程序退款
  14. * Class RoutineRefund
  15. * @package service
  16. */
  17. class RoutineRefund
  18. {
  19. public static function options(){
  20. $config = SystemConfigService::more(['pay_routine_appid','pay_routine_appsecret','pay_routine_mchid','pay_routine_key','pay_routine_client_cert','pay_routine_client_key']);
  21. return $config;
  22. }
  23. /**
  24. * 退款
  25. * @param float $totalFee 订单金额 单位元
  26. * @param float $refundFee 退款金额 单位元
  27. * @param string $refundNo 退款单号
  28. * @param string $wxOrderNo 微信订单号
  29. * @param string $orderNo 商户订单号
  30. * @param string $refundDesc 退款原因
  31. * @return string
  32. */
  33. public static function doRefund($totalFee, $refundFee, $refundNo, $wxOrderNo='',$orderNo='',$refundDesc = '')
  34. {
  35. $config = array(
  36. 'mch_id' => self::options()['pay_routine_mchid'],
  37. 'appid' => self::options()['pay_routine_appid'],
  38. 'key' => self::options()['pay_routine_key'],
  39. );
  40. $unified = array(
  41. 'appid' => $config['appid'],
  42. 'mch_id' => $config['mch_id'],
  43. 'nonce_str' => self::createNonceStr(),
  44. 'total_fee' => intval($totalFee * 100), //订单金额 单位 转为分
  45. 'refund_fee' => intval($refundFee * 100), //退款金额 单位 转为分
  46. 'sign_type' => 'MD5', //签名类型 支持HMAC-SHA256和MD5,默认为MD5
  47. 'transaction_id'=>$wxOrderNo, //微信订单号
  48. 'out_trade_no'=>$orderNo, //商户订单号
  49. 'out_refund_no'=>$refundNo, //商户退款单号
  50. 'refund_desc'=>$refundDesc, //退款原因(选填)
  51. );
  52. $unified['sign'] = self::getSign($unified, $config['key']);
  53. $responseXml = self::curlPost('https://api.mch.weixin.qq.com/secapi/pay/refund', self::arrayToXml($unified));
  54. $unifiedOrder = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
  55. if ($unifiedOrder === false) {
  56. die('parse xml error');
  57. }
  58. if ($unifiedOrder->return_code != 'SUCCESS') {
  59. die($unifiedOrder->return_msg);
  60. }
  61. if ($unifiedOrder->result_code != 'SUCCESS') {
  62. die($unifiedOrder->err_code);
  63. }
  64. return true;
  65. }
  66. public static function curlGet($url = '', $options = array())
  67. {
  68. $ch = curl_init($url);
  69. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  70. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  71. if (!empty($options)) {
  72. curl_setopt_array($ch, $options);
  73. }
  74. //https请求 不验证证书和host
  75. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  76. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  77. $data = curl_exec($ch);
  78. curl_close($ch);
  79. return $data;
  80. }
  81. public static function curlPost($url = '', $postData = '', $options = array())
  82. {
  83. if (is_array($postData)) {
  84. $postData = http_build_query($postData);
  85. }
  86. $ch = curl_init();
  87. curl_setopt($ch, CURLOPT_URL, $url);
  88. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  89. curl_setopt($ch, CURLOPT_POST, 1);
  90. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  91. curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
  92. if (!empty($options)) {
  93. curl_setopt_array($ch, $options);
  94. }
  95. //https请求 不验证证书和host
  96. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  97. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  98. //第一种方法,cert 与 key 分别属于两个.pem文件
  99. //默认格式为PEM,可以注释
  100. curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
  101. // curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/cert/apiclient_cert.pem');
  102. curl_setopt($ch,CURLOPT_SSLCERT,realpath('.'.self::options()['pay_routine_client_cert']));
  103. //默认格式为PEM,可以注释
  104. curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
  105. // curl_setopt($ch,CURLOPT_SSLKEY,getcwd().'/cert/apiclient_key.pem');
  106. curl_setopt($ch,CURLOPT_SSLKEY,realpath('.'.self::options()['pay_routine_client_key']));
  107. //第二种方式,两个文件合成一个.pem文件
  108. // curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/all.pem');
  109. $data = curl_exec($ch);
  110. curl_close($ch);
  111. return $data;
  112. }
  113. public static function createNonceStr($length = 16)
  114. {
  115. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  116. $str = '';
  117. for ($i = 0; $i < $length; $i++) {
  118. $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  119. }
  120. return $str;
  121. }
  122. public static function arrayToXml($arr)
  123. {
  124. $xml = "<xml>";
  125. foreach ($arr as $key => $val) {
  126. if (is_numeric($val)) {
  127. $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
  128. } else
  129. $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
  130. }
  131. $xml .= "</xml>";
  132. return $xml;
  133. }
  134. public static function getSign($params, $key)
  135. {
  136. ksort($params, SORT_STRING);
  137. $unSignParaString = self::formatQueryParaMap($params, false);
  138. $signStr = strtoupper(md5($unSignParaString . "&key=" . $key));
  139. return $signStr;
  140. }
  141. protected static function formatQueryParaMap($paraMap, $urlEncode = false)
  142. {
  143. $buff = "";
  144. ksort($paraMap);
  145. foreach ($paraMap as $k => $v) {
  146. if (null != $v && "null" != $v) {
  147. if ($urlEncode) {
  148. $v = urlencode($v);
  149. }
  150. $buff .= $k . "=" . $v . "&";
  151. }
  152. }
  153. $reqPar = '';
  154. if (strlen($buff) > 0) {
  155. $reqPar = substr($buff, 0, strlen($buff) - 1);
  156. }
  157. return $reqPar;
  158. }
  159. }
  160. ?>