RoutineRefund.php 5.9 KB

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