最近公司接入微信刷卡支付,网上根本没见到很直接的教程(可能眼拙),一直摸滚打爬,加班加点才走通,忍不了必须写一写

微信 刷卡支付/查询/撤销... 必须要有公众号然后去申请,申请自己去看文档,这里主要演示java对接代码

必要准备:

名称 变量名 示例 描述
公众账号ID appid wx8888888888888888 微信分配的公众账号ID,可在微信公众平台-->开发-->基本配置里面查看,商户的微信支付审核通过邮件中也会包含该字段值
商户号 mch_id 1900000109 微信支付分配的商户号
子商户号 sub_mch_id 1900000109 微信支付分配的子商户号,开发者模式下必填
证书路径 certPath x/xx/apiclient_cert.p12  本地证书
 API密钥 key

dyjs3tlWXCs1eBzs5ihGrmK8w0HdvXcR

保证key不会被泄漏。商户可根据邮件提示登录微信商户平台进行设置。也可按一下路径设置:微信商户平台(pay.weixin.qq.com)-->账户中心-->账户设置-->API安全-->密钥设置 

这里用SSM框架

先导入微信的SDK (SKD下载点这里)

配置一个xxx.properties

 //这里写的数据都是乱写的,不要暴露出去
wechat_appid=wx888888888888
wechat_mch_id=19000000000190
wechat_sub_mch_id=19000000000191
wechat_certPath=D:/cert/apiclient_cert.p12
wechat_key=adasdfsfwADEdDWw3E344Ee32  
7 wechat_httpConnectionTimeoutMs=8000
8 wechat_httpReadTimeoutMs=10000

配置微信初始化的类 WechatPayConstants.java

 package com.test.bean;

 import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.InetAddress; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import com.github.wxpay.sdk.WXPayConfig;
//这里要实现微信的配置
public class WechatPayConstants implements WXPayConfig{ public WechatPayConstants(String appId,String mchId,String subMchId,
String certPath,int httpConnectionTimeoutMs,int httpReadTimeoutMs,String key) throws Exception{
try {
File file = new File(certPath);
InputStream certStream = new FileInputStream(file);
this.certData = new byte[(int) file.length()];
certStream.read(this.certData);
certStream.close();
} catch (Exception e) {
Logger logger = LoggerFactory.getLogger( WechatPayConstants.class);
logger.info("文件:"+certPath+"没找到");
}
this.openApiDomain = openApiDomain;
this.appId = appId;
this.mchId = mchId;
this.subMchId = subMchId;
this.httpConnectionTimeoutMs = httpConnectionTimeoutMs;
this.httpReadTimeoutMs = httpReadTimeoutMs;
this.key = key;
this.serviceIP = InetAddress.getLocalHost().getHostAddress();
} private byte[] certData; private String appId; private String mchId; private String subMchId; private String serviceIP; private int httpConnectionTimeoutMs;//连接超时时间(毫秒) private int httpReadTimeoutMs;//读取超时时间(毫秒) private String key;
@Override
public String getAppID() {
return appId;
} @Override
public String getMchID() {
return mchId;
} public String getSubMchID() {
return subMchId;
} public String getServiceIP(){
return serviceIP;
} @Override
public InputStream getCertStream() {
ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
return certBis;
} @Override
public int getHttpConnectTimeoutMs() {
return httpConnectionTimeoutMs;
} @Override
public int getHttpReadTimeoutMs() {
return httpReadTimeoutMs;
} @Override
public String getKey() {
return key;
} }

配置spring-xxx.xml 进行初始化

 <bean id="wechatPayConstants" class="com.test.bean.WechatPayConstants">
<constructor-arg index="0" value="${wechat_appid}"/>
<constructor-arg index="1" value="${wechat_mch_id}"/>
<constructor-arg index="2" value="${wechat_sub_mch_id}"/>
<constructor-arg index="3" value="${wechat_certPath}"/>
<constructor-arg index="4" value="${wechat_httpConnectionTimeoutMs}"/>
<constructor-arg index="5" value="${wechat_httpReadTimeoutMs}"/>
<constructor-arg index="6" value="${wechat_key}"/>
</bean> <bean id="wxPay" class="com.github.wxpay.sdk.WXPay">
<constructor-arg ref="wechatPayConstants"/>
</bean>

实现层java代码

 //这里只给实现方法--刷卡支付
@Autowired
private WechatPayConstants wechatPayConstants; @Autowired
WXPay wxPay; public String WXPayment(BigDecimal totalFee,authCode,out_trade_no){ Map<String, String> request = new HashMap<String,String>();
request.put("appid", wechatPayConstants.getAppID());
request.put("mch_id", wechatPayConstants.getMchID());
request.put("sub_mch_id", wechatPayConstants.getSubMchID());
request.put("nonce_str", System.currentTimeMillis() / 1000 + "");
request.put("body", "xxx店面消费");
request.put("out_trade_no",out_trade_no);
request.put("total_fee", totalFee.intValue());//金额单位:分
request.put("auth_code", authCode);
request.put("spbill_create_ip", wechatPayConstants.getServiceIP());
String sign = "";
try {
sign = WXPayUtil.generateSignedXml(request, "sign");
} catch (Exception e) {
e.printStackTrace();
}
request.put("sign", sign);
Map<String, String> response = null;
try {
response = wxPay.microPay(request);
} catch (Exception e) {
e.printStackTrace();
throw new Exception("网络错误,请重新支付");
}
if (response != null) {
String return_code = response.get("return_code");
if (!"SUCCESS".equals(return_code)){
return "通讯失败!"
}else{
String result_code = response.get("result_code");
if ("SUCCESS".equals(result_code)) {
return "支付成功!";
}else{
String err_code = response.get("err_code");
if ("USERPAYING".equals(err_code)) {
//支付中调查询接口
return queryWXPayment(out_trade_no,null);
}else{
return response.get("err_code_des");
}
}
} }else{
return "网络传输异常";
} }
 //查询 实现 out_trade_no
@Override
public String queryWXPayment(String out_trade_no,String transaction_id) {
Map<String, String> request = new HashMap<String,String>();
request.put("appid", wechatPayConstants.getAppID());
request.put("mch_id", wechatPayConstants.getMchID());
request.put("sub_mch_id", wechatPayConstants.getSubMchID());
request.put("nonce_str", System.currentTimeMillis() / 1000 + "");
request.put("out_trade_no", out_trade_no);
if (!StringUtils.isBlank(transaction_id)) {
request.put("transaction_id", transaction_id);
}
String sign = "";
try {
sign = WXPayUtil.generateSignedXml(request, "sign");
} catch (Exception e) {
e.printStackTrace();
}
request.put("sign", sign);
Map<String, String> response = null;
try {
response = wxPay.orderQuery(request);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new QtException("订单查询失败");
}
if (response != null) {
String return_code = response.get("return_code");
String result_code = response.get("result_code"); if ("SUCCESS".equals(return_code) && "SUCCESS".equals(result_code)) {
if ("SUCCESS".equals(response.get("trade_state"))) {
return "支付成功";
} } else if("REVOKED".equals(response.get("trade_state"))){
return "撤销成功";
}else{
// } }else{
return "网络通讯异常";
} //这里写个超时处理,先判断支付时间和现在时间,再调用查询接口,建议使用短连接,返回支付中给前台,前台判断支付中继续发送查询接口,直到后台判断超时调用撤销接口
if(超时){
return cancelWXPayment(out_trade_no,transaction_id);
}else{
return "支付中";
} }
//撤销
@Override
public String cancelWXPayment(String out_trade_no,String transaction_id) {
Map<String, String> request = new HashMap<String,String>();
request.put("appid", wechatPayConstants.getAppID());
request.put("mch_id", wechatPayConstants.getMchID());
request.put("sub_mch_id", wechatPayConstants.getSubMchID());
request.put("nonce_str", System.currentTimeMillis() / 1000 + "");
request.put("out_trade_no", out_trade_no);
if (!StringUtils.isBlank(transaction_id)) {
request.put("transaction_id", transaction_id);
}
String sign = "";
try {
sign = WXPayUtil.generateSignedXml(request, "sign");
} catch (Exception e) {
e.printStackTrace();
}
request.put("sign", sign);
Map<String, String> response = null;
try {
response = wxPay.reverse(request);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception("撤销失败");
}
if (response != null) {
String return_code = response.get("return_code");
String result_code = response.get("result_code");
if(!"SUCCESS".equals(result_code)){
queryWXPayment(out_trade_no,transaction_id);
}else{
return "撤销成功";
} }else{
return "网络通讯异常";
} }

方法的实现自己定义,我这里图简单,只写了几个需要的,参数最好是一个对象,这里只是简单的举例子,以上

java版微信支付/查询/撤销的更多相关文章

  1. Java 后端微信支付demo

    Java 后端微信支付demo 一.导入微信SDK 二.在微信商户平台下载证书放在项目的resources目录下的cert文件夹下(cert文件夹需要自己建) 三.实现微信的WXPayConfig接口 ...

  2. java实现微信支付

    java实现微信支付 package com.hk.wx.pay.service.impl; @Service public class PayServiceImpl implements PaySe ...

  3. java版微信公众号支付(H5调微信内置API)

    最近需要做微信公众号支付,网上找了大堆的代码,大多都只说了个原理,自己踩了太多坑,所有的坑,都会再下面的文章中标注,代码我也贴上最全的(叫我雷锋)!!! 第一步:配置支付授权目录 你需要有将你公司的微 ...

  4. java版微信公众平台自定义菜单创建代码实现

    微信公众平台自定义菜单创建代码实现—java版 搞了两天的自定义菜单,终于搞定了,现在分享下心得,以便后来者少走弯路...... 好了,先看先微信官方的API 官方写的很详细,但是我看完后很茫然,不知 ...

  5. .Net版微信支付

    一. 案例介绍 这里模拟一个实际业务场景,进行介绍微信支付,业务功能包括:登录.注册.充值.查看充值记录. 页面图:       二. 概要设计 1.数据库设计 这里数据库包括两张表:用户表和订单表. ...

  6. JAVA实现微信支付V3

    喜欢的朋友可以关注下,粉丝也缺. 相信很多的码友在项目中都需要接入微信支付,虽说微信支付已成为一个普遍的现象,但是接入的过程中难免会遇到各种各样的坑,这一点支付宝的SDK就做的很好,已经完成的都知道了 ...

  7. java对接微信支付

    对接微信扫码支付(模式2),前端使用velocity技术 (1)调用微信支付接口(view层)  此部分业务逻辑部分可以省略 @RequestMapping("/wxpay.htm" ...

  8. java做微信支付notify_url异步通知服务端的写法

    最近团队在接入微信支付,APP和JSAPI的接口都需要填写一个notify_url回调地址,但是坑爹的官方文档并没有找到JSAPI模式的java版的demo,所以不得不自己看文档写了一个接受微信异步通 ...

  9. Java之微信支付(扫码支付模式二)案例实战

    摘要:最近的一个项目中涉及到了支付业务,其中用到了微信支付和支付宝支付,在做的过程中也遇到些问题,所以现在总结梳理一下,分享给有需要的人,也为自己以后回顾留个思路. 一:微信支付接入准备工作: 首先, ...

随机推荐

  1. HTTPS的常见错误及解决方案Chrome篇

    Chrome浏览器错误代码 问题原因 解决方法 NET::ERR_CERT_DATE_INVALID 网站的ssl证书有效期过期导致的 重新申请新的SSL证书 NET::ERR_CERT_COMMON ...

  2. ax2+bx+c=0的根的算法

    每日一练作业 写一个函数,接受三个整数a, b, c,计算ax2+bx+c=0 的根. 另外,在计算时应当判断 b2 - 4ac 是否大于0. 我们什么都没有,唯一的本钱就是青春.梦想让我与众不同,奋 ...

  3. es6 Object.assign(target, ...sources)

    Object.assign() 方法用于将所有可枚举属性(对象属性)的值从一个或多个源对象复制到目标对象.它将返回目标对象. 语法 Object.assign(target, ...sources) ...

  4. 解决eclipse部署maven项目无法导入lib的问题

    eclipse版本为2018-12(4.10.0) 1.默认tomcat的server配置 改成: 2.项目部署 按上面的配置,项目会部署到你配置的本地tomcat的webapps目录下. 部署了项目 ...

  5. AtCoder AGC002F Leftmost Ball (DP、组合计数)

    题目链接: https://atcoder.jp/contests/agc002/tasks/agc002_f 题解: 讲一下官方题解的做法: 就是求那个图(官方题解里的)的拓扑序个数,设\(dp[i ...

  6. 【转】博弈论——acm

    转自http://blog.csdn.net/lgdblue/article/details/15809893 序:博弈是信息学和数学试题中常会出现的一种类型,算法灵活多变是其最大特点,而其中有一类试 ...

  7. 《第40天 : JQuery - 手风琴列表》

    源码下载地址:链接:https://pan.baidu.com/s/1x9c1... 提取码:2bzr 如果有赞就很幸福了. 今天要和你们分享的是我看了JQuery库的手风琴列表样式.它的核心在于它的 ...

  8. 微服务SpringCloud系列

    https://my.oschina.net/hmilyylimh?tab=newest&catalogId=5703366

  9. 一、Appium+python环境搭建

    一.环境准备 1.jdk1.8. (64位) 2.android-sdk_r24.3.4-windows 3.python:2.7(3.6也可以) 4.appium:1.4.13.1 5.Node.j ...

  10. Vue知识整理15:组件注册

    采用局部注册组件: 将代码放在vue的一个实例中,而不是单列申明.