小编给大家分享一下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来显示该二维码,且该页面用定时器定时查询订单支付状态
看完了这篇文章,相信你对“java如何实现微信扫码支付功能”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。