微信支付之退款查询

  1. <?php
  2. header('Content-type:text/html; Charset=utf-8');
  3. $mchid = 'xxxxx'; //微信支付商户号 PartnerID 通过微信支付商户资料审核后邮件发送
  4. $appid = 'xxxxx'; //微信支付申请对应的公众号的APPID
  5. $apiKey = 'xxxxx'; //https://pay.weixin.qq.com 帐户设置-安全设置-API安全-API密钥-设置API密钥
  6. //以下四个单号四选一。查询的优先级是: 微信退款单号 > 商户退款订单号 > 微信订单号 > 商户订单号
  7. $orderNo = ''; //商户订单号
  8. $wxOrderNo = ''; //微信订单号
  9. $refundNo=''; //商户退款订单号
  10. $refundId = ''; //微信退款单号(微信生成的退款单号,在申请退款接口有返回)
  11. $wxPay = new WxpayService($mchid,$appid,$apiKey);
  12. $result = $wxPay->doRefundQuery($refundNo, $wxOrderNo,$orderNo,$refundId);
  13. if($result===true){
  14. echo 'refund success';exit();
  15. }
  16. echo 'refund fail';
  17. class WxpayService
  18. {
  19. protected $mchid;
  20. protected $appid;
  21. protected $apiKey;
  22. public $data = null;
  23. public function __construct($mchid, $appid, $key)
  24. {
  25. $this->mchid = $mchid; //https://pay.weixin.qq.com 产品中心-开发配置-商户号
  26. $this->appid = $appid; //微信支付申请对应的公众号的APPID
  27. $this->apiKey = $key; //https://pay.weixin.qq.com 帐户设置-安全设置-API安全-API密钥-设置API密钥
  28. }
  29. /**
  30. * 退款查询
  31. * @param string $refundNo 商户退款单号
  32. * @param string $wxOrderNo 微信订单号
  33. * @param string $orderNo 商户订单号
  34. * @param string $refundId 微信退款单号
  35. * @return string
  36. */
  37. public function doRefundQuery($refundNo='', $wxOrderNo='',$orderNo='',$refundId='')
  38. {
  39. $config = array(
  40. 'mch_id' => $this->mchid,
  41. 'appid' => $this->appid,
  42. 'key' => $this->apiKey,
  43. );
  44. $unified = array(
  45. 'appid' => $config['appid'],
  46. 'mch_id' => $config['mch_id'],
  47. 'nonce_str' => self::createNonceStr(),
  48. 'sign_type' => 'MD5', //签名类型 支持HMAC-SHA256和MD5,默认为MD5
  49. 'transaction_id'=>$wxOrderNo, //微信订单号
  50. 'out_trade_no'=>$orderNo, //商户订单号
  51. 'out_refund_no'=>$refundNo, //商户退款单号
  52. 'refund_id'=>$refundId, //微信退款单号
  53. );
  54. $unified['sign'] = self::getSign($unified, $config['key']);
  55. $responseXml = $this->curlPost('https://api.mch.weixin.qq.com/pay/refundquery', self::arrayToXml($unified));
  56. file_put_contents('2.txt',$responseXml);
  57. $unifiedOrder = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
  58. if ($unifiedOrder === false) {
  59. die('parse xml error');
  60. }
  61. if ($unifiedOrder->return_code != 'SUCCESS') {
  62. die($unifiedOrder->return_msg);
  63. }
  64. if ($unifiedOrder->result_code != 'SUCCESS') {
  65. die($unifiedOrder->err_code);
  66. }
  67. return true;
  68. }
  69. public static function curlGet($url = '', $options = array())
  70. {
  71. $ch = curl_init($url);
  72. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  73. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  74. if (!empty($options)) {
  75. curl_setopt_array($ch, $options);
  76. }
  77. //https请求 不验证证书和host
  78. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  79. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  80. $data = curl_exec($ch);
  81. curl_close($ch);
  82. return $data;
  83. }
  84. public function curlPost($url = '', $postData = '', $options = array())
  85. {
  86. if (is_array($postData)) {
  87. $postData = http_build_query($postData);
  88. }
  89. $ch = curl_init();
  90. curl_setopt($ch, CURLOPT_URL, $url);
  91. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  92. curl_setopt($ch, CURLOPT_POST, 1);
  93. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  94. curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
  95. if (!empty($options)) {
  96. curl_setopt_array($ch, $options);
  97. }
  98. //https请求 不验证证书和host
  99. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  100. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  101. $data = curl_exec($ch);
  102. curl_close($ch);
  103. return $data;
  104. }
  105. public static function createNonceStr($length = 16)
  106. {
  107. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  108. $str = '';
  109. for ($i = 0; $i < $length; $i++) {
  110. $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  111. }
  112. return $str;
  113. }
  114. public static function arrayToXml($arr)
  115. {
  116. $xml = "<xml>";
  117. foreach ($arr as $key => $val) {
  118. if (is_numeric($val)) {
  119. $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
  120. } else
  121. $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
  122. }
  123. $xml .= "</xml>";
  124. return $xml;
  125. }
  126. public static function getSign($params, $key)
  127. {
  128. ksort($params, SORT_STRING);
  129. $unSignParaString = self::formatQueryParaMap($params, false);
  130. $signStr = strtoupper(md5($unSignParaString . "&key=" . $key));
  131. return $signStr;
  132. }
  133. protected static function formatQueryParaMap($paraMap, $urlEncode = false)
  134. {
  135. $buff = "";
  136. ksort($paraMap);
  137. foreach ($paraMap as $k => $v) {
  138. if (null != $v && "null" != $v) {
  139. if ($urlEncode) {
  140. $v = urlencode($v);
  141. }
  142. $buff .= $k . "=" . $v . "&";
  143. }
  144. }
  145. $reqPar = '';
  146. if (strlen($buff) > 0) {
  147. $reqPar = substr($buff, 0, strlen($buff) - 1);
  148. }
  149. return $reqPar;
  150. }
  151. }
  152. ?>


评论 6

发表评论


匿名

不错呢!

11个月前 · 江苏 苏州

Top