服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - java实现微信支付结果通知

java实现微信支付结果通知

2021-07-01 14:42东边的小山 Java教程

这篇文章主要为大家详细介绍了java实现微信支付结果通知,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

支付完成后,微信会把相关支付结果和用户信息发送给商户,商户需要接收处理,并返回应答。

对后台通知交互时,如果微信收到商户的应答不是成功或超时,微信认为通知失败,微信会通过一定的策略定期重新发起通知,尽可能提高通知的成功率,但微信不保证通知最终能成功。 (通知频率为15/15/30/180/1800/1800/1800/1800/3600,单位:秒)

注意:同样的通知可能会多次发送给商户系统。商户系统必须能够正确处理重复的通知。
推荐的做法是,当收到通知进行处理时,首先检查对应业务数据的状态,判断该通知是否已经处理过,如果没有处理过再进行处理,如果处理过直接返回结果成功。在对业务数据进行状态检查和处理之前,要采用数据锁进行并发控制,以避免函数重入造成的数据混乱。

特别提醒:商户系统对于支付结果通知的内容一定要做签名验证,防止数据泄漏导致出现“假通知”,造成资金损失。

 

java" id="highlighter_262719">
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//支付结果通知接口
 
  @requestmapping("/qlydweixinotify.do")
  public void weixinotify(httpservletrequest request,
      httpservletresponse response) {
    printwriter out = null;
    stringbuffer xmlstr = new stringbuffer();
    try {
      bufferedreader reader = request.getreader();
      string line = null;
      while ((line = reader.readline()) != null) {
        xmlstr.append(line);
      
      logger.getlogger(getclass()).debug("支付回调通知:"+xmlstr.tostring());
      //检查xml是否有效
      boolean flag=signature.checkissignvalidfromresponsestring(xmlstr.tostring());
      weixinnotifyresult result=null;
      if(flag){
        notifyresdata wxdata=(notifyresdata) util.getobjectfromxml(xmlstr.tostring(),notifyresdata.class);
        if(wxdata !=null){
          if("success".equals(wxdata.getreturn_code())&&"success".equals(wxdata.getresult_code())){
            orderpayinfo orderpayinfo = new orderpayinfo();
            orderpayinfo.setordernum(wxdata.getout_trade_no());
            orderpayinfo.setpaynum(wxdata.gettransaction_id());
            orderpayinfo.setpayprice((double)wxdata.gettotal_fee()/100+"");
            orderpayinfo.setpaysource(wxdata.getopenid());
            orderpayinfo.setpaytime(wxdata.gettime_end());
            orderpayinfo.setpaytype("2");//1支付宝,2微信支付
            ordermessage returnmessage = orderproductserver
                .completeproductorder(orderpayinfo);
            if (orderstatus.fail.equals(returnmessage
                .getorderstatus())) {
              logger.getlogger(getclass()).error("远程接口完成订单失败");
              result=new weixinnotifyresult("fail");
              result.setreturn_msg("远程接口完成订单失败");
            } else {
              result=new weixinnotifyresult("success");
              result.setreturn_msg("成功");
            }
          }else{
            result=new weixinnotifyresult("fail");
            result.setreturn_msg("失败");
          }
        }else{
          result=new weixinnotifyresult("fail");
          result.setreturn_msg("解析参数格式失败");
        }
      }else{
        result=new weixinnotifyresult("fail");
        result.setreturn_msg("签名失败");
      }
      response.getwriter().write(result.tostring());
    } catch (exception e) {
      logger.getlogger(getclass()).error("qlydweixinotify.do", e);
      responedeal.getinstance().sendresponsestr(response, "404", "连接超时");
    } finally {
      if (out != null) {
        out.close();
      }
    }
  }

模拟http请求工具类:

httpsrequestutil.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.qlwb.weixin.util;
 
import java.io.ioexception;
 
import org.apache.commons.httpclient.httpclient;
import org.apache.commons.httpclient.httpexception;
import org.apache.commons.httpclient.methods.postmethod;
import org.apache.commons.httpclient.methods.requestentity;
import org.apache.commons.httpclient.methods.stringrequestentity;
import org.apache.log4j.logger;
 
import com.qlwb.weixin.common.configure;
import com.qlwb.weixin.common.util;
import com.qlwb.weixin.protocol.pay_protocol.wxpayreqdata;
import com.qlwb.weixin.protocol.payquery_protocol.payqueryreqdata;
 
public class httpsrequestutil {
 
  /**
   *
   * @方法名称:sendwxpayrequest
   * @内容摘要: <发送统一下单请求>
   * @param body
   * @param outtradeno
   * @param totalfee
   * @param spbillcreateip
   * @return
   * string
   * @exception
   * @author:鹿伟伟
   * @创建日期:2016年2月19日-下午2:24:05
   */
  public string sendwxpayrequest(string body,string detail,string outtradeno,int totalfee,string spbillcreateip
      )
 
  {
    // 构造http请求
    httpclient httpclient = new httpclient();
 
    postmethod postmethod = new postmethod(configure.pay_api);
 
    wxpayreqdata wxdata = new wxpayreqdata(body,detail,outtradeno,totalfee,spbillcreateip);
 
    string requeststr="";
    requeststr=util.convertobj2xml(wxdata);
    // 发送请求
    string strresponse = null;
    try {
      requestentity entity = new stringrequestentity(
          requeststr.tostring(), "text/xml", "utf-8");
      postmethod.setrequestentity(entity);
      httpclient.executemethod(postmethod);
      strresponse = new string(postmethod.getresponsebody(), "utf-8");
      logger.getlogger(getclass()).debug(strresponse);
    } catch (httpexception e) {
      logger.getlogger(getclass()).error("sendwxpayrequest", e);
    } catch (ioexception e) {
      logger.getlogger(getclass()).error("sendwxpayrequest", e);
    } finally {
      postmethod.releaseconnection();
    }
    return strresponse;
  }
  /**
   *
   * @方法名称:orderqueryrequest
   * @内容摘要: <查询订单信息>
   * @param transaction_id 微信的订单号,优先使用
   * @return
   * string
   * @exception
   * @author:鹿伟伟
   * @创建日期:2016年2月19日-下午2:44:11
   */
  public string orderqueryrequest(string transactionid, string outtradeno
      )
 
  {
    // 构造http请求
    httpclient httpclient = new httpclient();
 
    postmethod postmethod = new postmethod(configure.pay_query_api);
 
    payqueryreqdata wxdata = new payqueryreqdata(transactionid,outtradeno);
 
    string requeststr="";
    requeststr=util.convertobj2xml(wxdata);
    // 发送请求
    string strresponse = null;
    try {
      requestentity entity = new stringrequestentity(
          requeststr.tostring(), "text/xml", "utf-8");
      postmethod.setrequestentity(entity);
      httpclient.executemethod(postmethod);
      strresponse = new string(postmethod.getresponsebody(), "utf-8");
 
    } catch (httpexception e) {
      logger.getlogger(getclass()).error("orderqueryrequest", e);
    } catch (ioexception e) {
      logger.getlogger(getclass()).error("orderqueryrequest", e);
    } finally {
      postmethod.releaseconnection();
    }
    return strresponse;
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/yelin042/article/details/80636540

延伸 · 阅读

精彩推荐