欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现微信扫码支付功能

程序员文章站 2023-12-22 16:20:28
本文实例为大家分享了java实现微信扫码支付的具体代码,供大家参考,具体内容如下 1、maven项目的pom.xml中添加如下jar包:

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

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

<dependency>
  <groupid>com.github.wxpay</groupid>
  <artifactid>wxpay-sdk</artifactid>
  <version>0.0.3</version>
</dependency>

2、编写wewxconfig类:

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:

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、调用:

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

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

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

上一篇:

下一篇: