一、准备工作:
微信支付开发需要用到应用id(appid)、商户号(mch_id)和秘钥(key)、以及证书,这几个重要的参数需要从微信开发平台中去获得,在此不做赘述。
二、用户支付流程:用户在客户端中选择商品或者充值,提交订单,选择微信支付–>服务端收到用户支付订单,调用微信同意下单接口–>统一下单的接口返回appid_应用ID、partnerid_商户号、prepayid_预支付交易会话标识、package、noncestr_随机字符串和timestamp,而且都必须是小写–>客户端唤起微信支付–>服务端接收支付通知,处理业务逻辑–>服务端查询支付结果。
三、服务端代码编写:
在此我使用官方封装的SDK来进行开发,我的项目为maven项目
1、在pom.xml文件中添加如下配置:
<!-- 微信支付插件 -->
<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>0.0.3</version>
</dependency>
2、编写配置文件,在调用官方封装的SDK的时候需要使用,代码如下:
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import com.github.wxpay.sdk.WXPayConfig;
public class WXConfigUtil implements WXPayConfig{
private byte[] certData;
public static final String APP_ID = "应用ID";
public static final String KEY = "秘钥";
public static final String MCH_ID = "商户ID";
public WXConfigUtil() throws Exception {
//String certPath = "D:/weixzhifu/1592702951_20200513_cert/apiclient_cert.pem";//从微信商户平台下载的安全证书存放的路径
String certPath = "/usr/java/weixzhifu/1592702951_20200513_cert/apiclient_cert.pem";
File file = new File(certPath);
InputStream certStream = new FileInputStream(file);
this.certData = new byte[(int) file.length()];
certStream.read(this.certData);
certStream.close();
}
@Override
public String getAppID() {
return APP_ID;
}
//parnerid,商户号
@Override
public String getMchID() {
return MCH_ID;
}
@Override
public String getKey() {
return KEY;
}
@Override
public InputStream getCertStream() {
ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
return certBis;
}
@Override
public int getHttpConnectTimeoutMs() {
return 0;
}
@Override
public int getHttpReadTimeoutMs() {
return 0;
}
}
3、服务端生成预支付订单和客户端支付后的异步回调
3.1、controller层代码:
@Autowired
private PaymentServiceI paymentService;
/**
* 微信支付
* 统一下单
* 官方文档:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1
* @param user_id
* @param total_fee
* @return
* @throws Exception
*/
@ApiOperation(value = "微信支付")
@RequestMapping(value = "wxPay/", method = RequestMethod.POST, produces = "application/json")
@ResponseBody
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name = "phone", dataType = "String", required = true, value = "手机号"),
@ApiImplicitParam(paramType = "query", name = "id", dataType = "String", required = true, value = "充值金额id"),
@ApiImplicitParam(paramType = "query", name = "identification", dataType = "String", required = true, value = "是否优惠")})
public Result<Object> wxPay(HttpServletRequest request,@RequestParam String phone,
@RequestParam String id,@RequestParam String identification) throws Exception {
Result<Object> resu = new Result<Object>();
try {
String attach = "{\"phone\":\"" + phone + "\"}";
//请求预支付订单
Map<String, String> result = paymentService.dounifiedOrder(attach,phone, id,identification);
Map<String, String> map = new HashMap<>();
WxMD5Util md5Util = new WxMD5Util();
//返回APP端的数据
//参加调起支付的签名字段有且只能是6个,分别为appid_应用ID、partnerid_商户号、prepayid_预支付交易会话标识、package、noncestr_随机字符串和timestamp,而且都必须是小写
map.put("appid", result.get("appid"));
map.put("partnerid", result.get("mch_id"));
map.put("prepayid", result.get("prepay_id"));
map.put("package", "Sign=WXPay");
map.put("noncestr", result.get("nonce_str"));
map.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));//单位为秒
//这里不要使用请求预支付订单时返回的签名
map.put("sign", md5Util.getSign(map));
map.put("extdata", attach);
resu.setResult(map);
resu.setMessage("操作成功");
resu.setCode(200);
} catch (Exception e) {
resu.setSuccess(false);
resu.setMessage("操作异常");
resu.setCode(403);
System.out.println(e.getMessage());
}
return resu;
}
/**
* 支付异步结果通知,我们在请求预支付订单时传入的地址 此接口必须为外网可以访问
* 接口测试地址http://coolaf.com/
* 官方文档 :https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_7&index=3
* @param request
* @param response
* @return
*/
@ApiOperation(value = "支付宝结果通知")
@RequestMapping(value = "wxNotify/", produces = "application/json;charset=UTF-8", method = {RequestMethod.POST})
@ResponseBody
public String wxPayNotify(HttpServletRequest request, HttpServletResponse response) {
String resXml = "";
try {
InputStream inputStream = request.getInputStream();
//将InputStream转换成xmlString
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
resXml = sb.toString();
String result = paymentService.payBack(resXml);
return result;
} catch (Exception e) {
System.out.println("微信手机支付失败:" + e.getMessage());
String result = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
return result;
}
}
Result 封装类,返回客户端的数据
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Component;
import io.swagger.annotations.ApiModelProperty;
@Component
public class Result<T>{
/**
* 成功标志
*/
@ApiModelProperty(value = "成功标志")
private boolean success = true;
/**
* 返回代码
*/
@ApiModelProperty(value = "HTTP状态码")
private Integer code = 0;
/**
* 返回处理消息
*/
@ApiModelProperty(value = "返回处理消息")
private String message = "操作成功!";
/**
* 返回数据对象 data
*/
@ApiModelProperty(value = "返回数据对象")
private T result;
/**
* 时间戳
*/
@ApiModelProperty(value = "时间戳")
private long timestamp = System.currentTimeMillis();
public Result() {
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getResult() {
return result;
}
public void setResult(T result) {
this.result = result;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public Result<T> error500(String message) {
this.setMessage(message);
this.setCode(500);
this.setSuccess(false);
return this;
}
public Result<T> success(String message) {
this.setMessage(message);
this.setCode(200);
this.setSuccess(true);
return this;
}
public static Result<Object> ok() {
Result<Object> r = new Result<Object>();
r.setMessage("操作成功");
r.setCode(200);
r.setSuccess(true);
return r;
}
public static Result<Object> ok(String msg) {
Result<Object> r = new Result<Object>();
r.setMessage(msg);
r.setCode(200);
r.setSuccess(true);
return r;
}
public static Result<Object> ok(Map data) {
Result<Object> r = new Result<Object>();
r.setCode(200);
r.setSuccess(true);
r.setResult(data);
return r;
}
public static Result<Object> ok(List data) {
Result<Object> r = new Result<Object>();
r.setCode(200);
r.setSuccess(true);
r.setResult(data);
return r;
}
public static Result<List<?>> okl(List<?> data) {
Result<List<?>> r = new Result<List<?>>();
r.setCode(200);
r.setSuccess(true);
r.setResult(data);
return r;
}
public static Result<Object> ok(Object data) {
Result<Object> r = new Result<Object>();
r.setCode(200);
r.setSuccess(true);
r.setResult(data);
return r;
}
public static Result<Object> error(String msg) {
return error(500, msg);
}
public static Result<Object> error(int code, String msg) {
Result<Object> r = new Result<Object>();
r.setCode(code);
r.setSuccess(false);
r.setMessage(msg);
return r;
}
/**
* 无权限访问返回结果
*/
public static Result<Object> noauth(String msg) {
return error(401, msg);
}
/**
* 无权限访问返回结果
*/
public static Result<List<?>> noauth() {
Result<List<?>> r = new Result<List<?>>();
r.setCode(401);
r.setSuccess(false);
r.setMessage("您没有该接口的权限!");
return r;
}
}
3.2、servie层代码:
// 请求预支付订单
Map<String, String> dounifiedOrder(String attach,String phone, String id,String identification) throws Exception;
// 支付完成返回的xml数据结果
String payBack(String notifyData);
3.3、service实现层:
/**
* 调用官方SDK 获取预支付订单等参数
* @param attach 额外参数
* @param phone 充值人手机号
* @param id 充值优惠表id
* @param identification 是否优惠
* @return
* @throws Exception
*/
@Override
public Map<String, String> dounifiedOrder(String attach,String phone, String id,String identification) throws Exception {
// 根据id、identification得到实际所要充值的金额
String total_fee = "";// 充值金额
// 此处需要注意,微信支付只识别整数,如果你的充值金额是小数的话,需要进行转换
WxMD5Util md5Util = new WxMD5Util();
WXConfigUtil config = new WXConfigUtil();
WXPay wxpay = new WXPay(config);
Map<String, String> data = new HashMap<>();
//生成商户订单号,不可重复
String out_trade_no = "wxpay" + System.currentTimeMillis();
data.put("appid", config.getAppID());
data.put("mch_id", config.getMchID());
data.put("nonce_str", WXPayUtil.generateNonceStr());
String body = "订单支付";
data.put("body", body);
data.put("out_trade_no", out_trade_no);
data.put("total_fee", zhFen);
//自己的服务器IP地址
data.put("spbill_create_ip", SPBILL_CREATE_IP);
//异步通知地址(请注意必须是外网)
data.put("notify_url", NOTIFY_URL);
//交易类型
data.put("trade_type", TRADE_TYPE_APP);
//附加数据,在查询API和支付通知中原样返回,该字段主要用于商户携带订单的自定义数据
data.put("attach", attach);
String sign1 = md5Util.getSign(data);
data.put("sign", sign1);
try {
//使用官方API请求预付订单
Map<String, String> response = wxpay.unifiedOrder(data);
System.out.println(response);
String returnCode = response.get("return_code"); //获取返回码
//若返回码为SUCCESS,则会返回一个result_code,再对该result_code进行判断
if (returnCode.equals("SUCCESS")) {//主要返回以下5个参数
String resultCode = response.get("result_code");
returnMap.put("appid", response.get("appid"));
returnMap.put("mch_id", response.get("mch_id"));
returnMap.put("nonce_str", response.get("nonce_str"));
returnMap.put("sign", response.get("sign"));
if ("SUCCESS".equals(resultCode)) {//resultCode 为SUCCESS,才会返回prepay_id和trade_type
//获取预支付交易回话标志
returnMap.put("trade_type", response.get("trade_type"));
returnMap.put("prepay_id", response.get("prepay_id"));
// 生成消费订单
// 此处可以写自己的业务逻辑 生成未支付订单
return returnMap;
} else {
//此时返回没有预付订单的数据
return returnMap;
}
} else {
return returnMap;
}
} catch (Exception e) {
System.out.println(e);
//系统等其他错误的时候
}
}
return returnMap;
}
/**
*
* @param notifyData 异步通知后的XML数据
* @return
*/
@Override
public String payBack(String notifyData) {
// 当前时间
String date = DateUtils.getDate("yyyy-MM-dd HH:mm:ss");
WXConfigUtil config = null;
try {
config = new WXConfigUtil();
} catch (Exception e) {
e.printStackTrace();
}
WXPay wxpay = new WXPay(config);
String xmlBack = "";
Map<String, String> notifyMap = null;
try {
notifyMap = WXPayUtil.xmlToMap(notifyData); // 调用官方SDK转换成map类型数据
if (wxpay.isPayResultNotifySignatureValid(notifyMap)) {//验证签名是否有效,有效则进一步处理
String return_code = notifyMap.get("return_code");//状态
String out_trade_no = notifyMap.get("out_trade_no");//商户订单号
String total_fee = notifyMap.get("total_fee");//金额
if (return_code.equals("SUCCESS")) {
if (out_trade_no != null) {
// 注意特殊情况:订单已经退款,但收到了支付结果成功的通知,不应把商户的订单状态从退款改成支付成功
// 注意特殊情况:微信服务端同样的通知可能会多次发送给商户系统,所以数据持久化之前需要检查是否已经处理过了,处理了直接返回成功标志
//业务数据持久化
System.err.println("支付成功");
// 处理自己数据库中的业务逻辑
System.out.println("微信手机支付回调成功订单号:{}"+out_trade_no);
xmlBack = "<xml>" + "<return_code><![CDATA[SUCCESS]]></return_code>" + "<return_msg><![CDATA[OK]]></return_msg>" + "</xml> ";
} else {
this.executeSql("update selfs_bill set current_state = ?,updatedate=? where bill_num = ? ", 0,date,out_trade_no);
System.out.println("微信手机支付回调失败订单号:{}"+out_trade_no);
xmlBack = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
}
}
return xmlBack;
} else {
// 签名错误,如果数据里没有sign字段,也认为是签名错误
//失败的数据要不要存储?
System.out.println("手机支付回调通知签名错误");
xmlBack = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
return xmlBack;
}
} catch (Exception e) {
System.out.println("手机支付回调通知失败"+e);
xmlBack = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
}
return xmlBack;
}
3.4、MD5加密:
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import com.github.wxpay.sdk.WXPayConstants;
public class WxMD5Util {
public String getSign(Map<String, String> data) throws Exception {
WXConfigUtil config = new WXConfigUtil();
Set<String> keySet = data.keySet();
String[] keyArray = keySet.toArray(new String[keySet.size()]);
Arrays.sort(keyArray);
StringBuilder sb = new StringBuilder();
for (String k : keyArray) {
if (k.equals(WXPayConstants.FIELD_SIGN)) {
continue;
}
if (data.get(k).trim().length() > 0) // 参数值为空,则不参与签名
sb.append(k).append("=").append(data.get(k).trim()).append("&");
}
sb.append("key=").append(config.getKey());
MessageDigest md = null;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
byte[] array = new byte[0];
try {
array = md.digest(sb.toString().getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
StringBuilder sb2 = new StringBuilder();
for (byte item : array) {
sb2.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
}
return sb2.toString().toUpperCase();
}
}