微信如何申请退款流程(微信退款开发)

上一章我们介绍了微信支付的整个流程。在这一章里,我们整理了微信退款(JSAPI支付)相关的知识,趁热打铁。

前提:从微信微信官方账号获取appid、mchid、paternerKey,以备后用。

1、微信申请退款的实现

应用场景:

当交易后的某段时间内,买方或卖方需要退款时,卖方可以通过退款接口将货款退还给买方。微信支付收到退款请求并验证成功后,会根据退款规则,按原方式将货款退还至买家账户。

注意:

1.交易超过一年的订单不能退款。

微信如何申请退款

2.微信支付退款支持单笔交易多次退款。多次退款需要提交原支付订单的商户订单号,并设置不同的退款订单号。退款申请的总金额不能超过订单金额。退款失败后,请重新提交。请不要更改退款号码。请使用原始的商家退款号码。

3.请求频率限制:150qps,即正常的每秒退款请求数不超过150。

错误或无效请求频率限制:6qps,即每秒不超过6个异常或错误的退款请求。

4.每笔支付订单的部分退款次数不能超过50次。

5.如果同一用户有多次退款,建议分批次退款,避免并发退款导致退款失败。

需要证书吗:

申请java开发退款需要双向证书(apiclient_cert.p12证书)。如下图所示,登录微信商家平台(https://pay.weixin.qq.com/)。




退款申请接口调用:

@ GET @ Path(value = & # 34;退款订单& # 34;) @产生(MediaType。APPLICATION _ JSON) 公共响应refundOrder(@ query param(& # 34;outTradeNo & # 34)String outTradeNo){ Map & lt;字符串,对象& gtresult = new HashMap & lt字符串,对象& gt(); 试试{ // 1,拿到参数,然后申请退款 图

@ xmlroot element(name = & # 34;xml & # 34) 公共类WeChatOrderResponse { 私有字符串return _ code 私有字符串return _ msg //当return_code成功时,以下字段已返回 私有字符串result _ code 私有字符串err _ code 私有字符串err _ code _ des 私有字符串appid 私有字符串mch _ id 私有字符串nonce _ str 私有字符串符号; 私有字符串transaction _ id 私有字符串out _ trade _ no 私串out _ refund _ no 私串退款_ id 私串退款_费用; 私串结算_退款_费用; 私有字符串total _ fee 私串结算_合计_费用; 私有字符串fee _ type 私串cash _ fee 私有字符串cash _ fee _ type 私串cash _退款_费用; 私有字符串coupon _ type _ $ n; 私串优惠券_退款_费用; 私串优惠券_退款_费用_ $ n; 私串优惠券_退款_计数; 私串coupon _ refund _ id _ $ n; //当return_code和result_code都成功时,以下字段返回 私有字符串trade _ type 私有字符串prepay _ id 私有字符串code _ url 私有字符串openid //参数 的get/set方法.....................................................................................................................................

公共类we chat util { /* * * Post请求+证书 * @param url微信申请退款接口链接 * @param entity微信申请退款接口参数 */ 公共http响应http post (string URL,string entity){ //注意PKCS12证书来自微信商家平台->账号设置-& gt; keystore keystore = keystore . getinstance(& # 34;PKCS12 & # 34); //加载本地证书进行https加密传输,keystorpath是证书 文件输入流的绝对路径instream = new file input stream(new file(keystorpath)); try{ //设置证书密码,keystorePassword:下载证书时的密码,默认密码为您的mch ID keystore . load(in stream,keystore password . tochararray()); } finally { instream . close(); } /Java主动信任证书 //keystorePassword:下载证书时的密码,默认密码是您的mch ID sslcontext sslcontext = sslcontext s . custom() 。loadkeymaterial (keystore,keystore password.tochararray())。build(); SSLConnectionSocketFactory SSL SF = new SSLConnectionSocketFactory( SSL context,new String[]{ & # 34;TLSv1 & # 34},null, SSLConnectionSocketFactory。浏览器_兼容_主机名_验证器); //CloseableHttpClient加载证书以访问https网站 closeable http client http client = http clients . custom()。Setsslsocketfactory (sslsf)。build(); try { //设置响应头信息,发送post请求 HTTP POST HTTP POST = new HTTP POST(URL); request config request config = request config . custom() 。setSocketTimeout(5000) 。setConnectTimeout(5000) 。setconnectionrequesttime out(5000) 。build(); http post . set config(request config); http post . setentity(new string entity(entity,& # 34;UTF-8 & # 34;)); http post . set header(& # 34;接受& # 34;, "*/*"); http post . set header(& # 34;内容类型& # 34;, "应用程序/XML & # 34;); HttpResponse resp = http client . execute(http post); return resp; } catch(Exception e){ e . printstacktrace(); 返回null } } /* * string to object * @ param XML JSON string * @ param clazz */ ?& gtclazz){ try { JAXB context context = JAXB context . new instance(clazz); 解组器umMarshaller = context . create Unmarshaller(); Object POJO = ummarshaller . unmarshal(new ByteArrayInputStream(XML . getbytes(& # 34;utf-8 & # 34;))); } catch(Exception e){ e . printstacktrace(); } } }2。查询退款的实现应用场景:

提交退款申请后,调用此接口查询退款状态。退款有一定的延迟。零钱支付的退款20分钟内到账,银行卡支付的退款3个工作日后再次核对。(退款有延迟,可以用轮询来判断退款成功与否)

注:如果单笔支付订单的部分退款次数超过20次,请使用退款单号查询。

退款状态变更:



查询退款接口的具体实现:

@GET @Path(value = "queryRefund") @Produces(MediaType.APPLICATION_JSON) public Response queryRefund(@QueryParam("outTradeNo") String outTradeNo){ Map<String, Object> result = new HashMap<String, Object>(); try{ // 1、查询退款 Map<String, String> paramMap = new HashMap<>(); paramMap.put("appid", appid);// 公众账号ID paramMap.put("mch_id", mchid);// 商户号 paramMap.put("nonce_str", WXPayUtil.generateNonceStr());// 随机字符串 paramMap.put("out_trade_no", outTradeNo + "");// 商户订单号 String sign = WXPayUtil.generateSignature(paramMap, paternerKey); paramMap.put("sign", sign);// 签名 WXPay wxpay = new WXPay(MyConfig.getInstance()); Map<String, String> resp = wxpay.refundQuery(paramMap); if("FAIL".equals(resp.get("return_code"))){ throw new RuntimeException("return_code为fail, " + resp.get("return_msg")); } if("FAIL".equals(resp.get("result_code")) && !"订单已全额退款".equals(resp.get("err_code_des"))){ throw new RuntimeException("result_code为fail, " + resp.get("err_code_des")); } // 2、成功退款后的操作流程 ...... result.put("code", SUCCESS); }catch(Exception e){ e.printStackTrace(); result.put("code", ERROR); result.put("msg", e.getMessage()); } return Response.ok(result).build(); }

您可以还会对下面的文章感兴趣

使用微信扫描二维码后

点击右上角发送给好友