一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java實現微信掃碼支付功能

java實現微信掃碼支付功能

2021-05-17 15:09品學兼憂 Java教程

這篇文章主要為大家詳細介紹了java實現微信掃碼支付功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产成人综合一区人人 | 5g影院天天影院天天爽影院网站 | 女性全身裸露无遮挡 | 国产日韩精品一区二区三区 | 变态 另类 人妖小说 | 欧美日韩一区二区三区在线观看 | 91香蕉国产在线观看免费永久 | 日韩免费在线视频 | 特黄特黄一级高清免费大片 | 男女天堂| 日韩资源在线 | 日韩欧美精品一区二区 | 亚洲一区二区福利视频 | 3d美女触手怪爆羞羞漫画 | 欧美成年黄网站色高清视频 | 成人免费播放 | 日韩精品免费一区二区三区 | 我的奶头被客人吸的又肿又红 | 久久电影精品久久99久久 | 午夜桃色剧场 | 古装全套 毛片 | 日本剧情片在线播放中文版 | 506rr亚洲欧美 | 插鸡视频在线观看 | 日韩高清在线免费观看 | 91久久综合 | 91porny丨首页 | 亚洲系列国产精品制服丝袜第 | 513热点网深夜影院影院诶 | 日韩一区二区中文字幕 | 亚洲欧美在线观看一区二区 | 天堂一区二区在线观看 | 美女18隐私羞羞视频网站 | 免费一级日本c片完整版 | 顶级尤物极品女神福利视频 | 免费一级片在线 | 国产精品免费小视频 | 成人国产精品视频频 | 日本人添下面的全过程 | 成人啪精品视频免费网站 | 99在线精品免费视频 |