java实现微信扫码支付功能

时间:2022-06-18 00:49:16

本文实例为大家分享了java实现微信扫码支付的具体代码,供大家参考,具体内容如下

1、maven项目的pom.xml中添加如下jar包:

?
1
2
3
4
5
<dependency>
  <groupid>com.github.wxpay</groupid>
  <artifactid>wxpay-sdk</artifactid>
  <version>0.0.3</version>
</dependency>

2、编写wewxconfig类:

?
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
package com.xx.wxpay;
 
import com.github.wxpay.sdk.wxpayconfig;
import org.springframework.beans.factory.annotation.value;
import org.springframework.stereotype.component;
 
import java.io.inputstream;
 
/**
 * 描述:微信支付配置信息
 *
 * @author ssl
 * @create 2018/04/24 19:25
 */
@component
public class wewxconfig implements wxpayconfig {
  @value("${wechat.public.appid}")
  private string appid;
  @value("${wechat.merchant}")
  private string mchid;
  @value("${wechat.public.apikey}")
  private string apikey;
 
  /**
   * 公众账号id:微信支付分配的公众账号id(企业号corpid即为此appid)
   *
   * @return
   */
  @override
  public string getappid() {
    return appid;
  }
 
  /**
   * 商户号:微信支付分配的商户号
   *
   * @return
   */
  @override
  public string getmchid() {
    return mchid;
  }
 
  /**
   * @return
   */
  @override
  public string getkey() {
    return apikey;
  }
 
  @override
  public inputstream getcertstream() {
    return null;
  }
 
  @override
  public int gethttpconnecttimeoutms() {
    return 0;
  }
 
  @override
  public int gethttpreadtimeoutms() {
    return 0;
  }
}

3、编写wewxpayservice:

?
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.xx.wxpay;
 
import com.alibaba.fastjson.jsonobject;
import com.github.wxpay.sdk.wxpay;
import com.google.common.collect.maps;
import com.xx.model.order;
import com.xx.model.product;
import org.slf4j.logger;
import org.slf4j.loggerfactory;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.beans.factory.annotation.value;
import org.springframework.stereotype.component;
import org.springframework.stereotype.service;
 
import java.text.messageformat;
import java.util.hashmap;
import java.util.map;
 
/**
 * 描述:
 *
 * @author ssl
 * @create 2018/04/24 20:15
 */
@service
public class wewxpayservice {
  protected logger logger = loggerfactory.getlogger(this.getclass());
  @value("${project.url}")
  private string projecturl;
  @autowired
  private wewxconfig wewxconfig;
 
 
  /**
   * 统一下单
   *
   * @param product
   * @param order
   * @return
   */
  public map<string, string> unifiedorder(product product, order order) {
    map<string, string> data = maps.newhashmap();
    wxpay wxpay = new wxpay(wewxconfig);
    data.put("body", "xx-" + product.getname());
    data.put("detail", "详细信息");
    data.put("out_trade_no", order.getorderno());
    data.put("device_info", "web");
    data.put("fee_type", "cny");
    data.put("total_fee", order.getamount() + "");
    data.put("spbill_create_ip", "127.0.0.1");
    data.put("notify_url", projecturl + "/base/order/notifyurl");
    data.put("trade_type", "native"); // 此处指定为扫码支付
    data.put("product_id", product.getid() + "");
    try {
      map<string, string> resp = wxpay.unifiedorder(data);
      logger.debug(jsonobject.tojsonstring(resp));
      return resp;
    } catch (exception e) {
      e.printstacktrace();
    }
    return null;
  }
 
  /**
   * 订单查询
   *
   * @param orderno:订单号
   * @return
   */
  public map<string, string> orderquery(string orderno) {
    map<string, string> reqdata = maps.newhashmap();
    reqdata.put("out_trade_no", orderno);
    wxpay wxpay = new wxpay(wewxconfig);
    try {
      map<string, string> resp = wxpay.orderquery(reqdata);
      logger.debug(jsonobject.tojsonstring(resp));
      return resp;
    } catch (exception e) {
      e.printstacktrace();
    }
    return null;
  }
 
  public static string geturl() {
    wxpay wxpay = new wxpay(new wewxconfig());
    map<string, string> data = new hashmap<string, string>();
    data.put("body", "上屏名称");
    data.put("detail", "商品详情");
    data.put("out_trade_no", "2ab9071b06b9f739b950ddb41db2690d");
    data.put("device_info", "");
    data.put("fee_type", "cny");
    data.put("total_fee", "1");
    data.put("spbill_create_ip", "218.17.160.245");
    data.put("notify_url", "http://www.example.com/wxpay/notify");
    data.put("trade_type", "native"); // 此处指定为扫码支付
    data.put("product_id", "12");
 
    try {
      map<string, string> resp = wxpay.unifiedorder(data);
      system.out.println(resp);
    } catch (exception e) {
      e.printstacktrace();
    }
    return "";
  }
}

4、调用:

?
1
2
/** 向微信支付系统下单,并得到二维码返回给用户 */
map<string, string> resdata = wewxpayservice.unifiedorder(product, order);

5、resdata.get("code_url")为微信下单成功后返回的二维码地址,页面中用qrcode.js来显示该二维码,且该页面用定时器定时查询订单支付状态

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

原文链接:https://blog.csdn.net/ZuoYanYouYan/article/details/80225553