栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java实现微信扫码支付功能

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java实现微信扫码支付功能

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

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


  com.github.wxpay
  wxpay-sdk
  0.0.3

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;
 

@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;
 
  
  @Override
  public String getAppID() {
    return appId;
  }
 
  
  @Override
  public String getMchID() {
    return mchId;
  }
 
  
  @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;
 

@Service
public class WeWxPayService {
  protected Logger logger = LoggerFactory.getLogger(this.getClass());
  @Value("${project.url}")
  private String projectUrl;
  @Autowired
  private WeWxConfig weWxConfig;
 
 
  
  public Map unifiedOrder(Product product, Order order) {
    Map 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 resp = wxpay.unifiedOrder(data);
      logger.debug(JSONObject.toJSonString(resp));
      return resp;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
 
  
  public Map orderQuery(String orderNo) {
    Map reqData = Maps.newHashMap();
    reqData.put("out_trade_no", orderNo);
    WXPay wxpay = new WXPay(weWxConfig);
    try {
      Map 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 data = new HashMap();
    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 resp = wxpay.unifiedOrder(data);
      System.out.println(resp);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return "";
  }
}

4、调用:


Map resData = weWxPayService.unifiedOrder(product, order);

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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/140463.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号