微信订单退款接口

短命女 2023-03-05 12:36 131阅读 0赞
  1. //退款
  2. public static function tui($transaction_id,$tui_no,$order_money,$money){
  3. $param = [
  4. 'appid' => Config::get('app.appid'),
  5. 'mch_id' => Config::get('app.mch_id'), //商户id
  6. 'nonce_str' => self::getRand(20, true), //随机字符串
  7. 'transaction_id'=>$transaction_id, //微信订单号
  8. 'out_refund_no' => $tui_no, //退款商家订单号
  9. 'total_fee' => $order_money * 100, //订单金额
  10. 'refund_fee' => $money * 100, //退款金额
  11. 'notify_url' => Config::get('app.url').'/api/pay/quxiaosuccess'
  12. ];
  13. $sign = self::makeSign($param, Config::get('app.mch_key'));
  14. $param['sign'] = $sign;
  15. try {
  16. //将数组转换成xml数据
  17. $xml = self::ToXmls($param);
  18. } catch (\Exception $e) {
  19. return $e->getMessage();
  20. }
  21. $url = "https://api.mch.weixin.qq.com/secapi/pay/refund";
  22. //请求微信支付接口
  23. $getXml = self::curl_posts($url, $xml);
  24. //将微信返回值转换成数组
  25. $getArr = self::FromXmls($getXml);
  26. if ($getArr[1]['return_code'] == 'SUCCESS' && $getArr[1]['result_code'] == 'SUCCESS') {
  27. //请求成功
  28. $data['code'] = 'success';
  29. } else if ($getArr['return_code'] == 'SUCCESS' && $getArr['result_code'] == 'FAIL') {
  30. $data['code'] = $getArr['err_code'];
  31. $data['msg'] = $getArr['err_code_des'];
  32. } else {
  33. $data['code'] = $getArr['return_code'];
  34. $data['msg'] = $getArr['return_msg'];
  35. }
  36. return $data;
  37. }
  38. //获取随机字符串
  39. public static function getRand($length = 6, $isAll = false)
  40. {
  41. if ($isAll) {
  42. $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  43. } else {
  44. $chars = '0123456789';
  45. }
  46. $randStr = '';
  47. for ($i = 0; $i < $length; $i++) {
  48. $randStr .= $chars[mt_rand(0, strlen($chars) - 1)];
  49. }
  50. return $randStr;
  51. }
  52. //生成签名
  53. public static function makeSign($param, $key)
  54. {
  55. ksort($param);
  56. $string = self::toUrlParams($param);
  57. $string = $string . '&key=' . $key;
  58. $string = md5($string);
  59. $result = strtoupper($string);
  60. return $result;
  61. }
  62. //将数组转换成xml数据
  63. public static function ToXmls($data)
  64. {
  65. if (!is_array($data)
  66. || count($data) <= 0) {
  67. throw new Exception("数组数据异常!");
  68. }
  69. $xml = "<xml>";
  70. foreach ($data as $key => $val) {
  71. $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
  72. }
  73. $xml .= "</xml>";
  74. return $xml;
  75. }
  76. private function curl_posts($url,$post_data)
  77. {
  78. if (is_array($post_data)) {
  79. $postData = http_build_query($post_data);
  80. }
  81. $ch = curl_init();
  82. curl_setopt($ch, CURLOPT_URL, $url);
  83. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  84. curl_setopt($ch, CURLOPT_POST, 1);
  85. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  86. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  87. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  88. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  89. curl_setopt($ch,CURLOPT_SSLCERT, public_path().Config::get('app.cert'));
  90. curl_setopt($ch,CURLOPT_SSLKEY,public_path().Config::get('app.key')); //注意证书路径一定是绝对路径或相对路径,我搞成链接了,坑了我好长时间
  91. $data = curl_exec($ch);
  92. curl_close($ch);
  93. return $data;
  94. }
  95. //将xml转换成数组
  96. public static function FromXmls($xml)
  97. {
  98. if (!$xml) {
  99. return ['false', 'xml数据异常!'];
  100. }
  101. libxml_disable_entity_loader(true);
  102. $data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  103. return ['true', $data];
  104. }

发表评论

表情:
评论列表 (有 0 条评论,131人围观)

还没有评论,来说两句吧...

相关阅读

    相关 退款

    1. 微信退款需要双向证书,可以到微信商户平台下载![70][] 2. 下载微信官方SDK,放在extend 目录下(TP5)![70 1][] 3. 配置文件![70

    相关 退款接口开发

    步骤: 发送退款请求到微信->同步告诉你请求成功还是失败->异步回调告诉你退款成功还是失败 说明:微信退款是需要证书的,先要下载证书.如何下载百度一下就可以,很简单 //