Java版开发原生App支付
微信开发者平台网址:https://open.weixin.qq.com/
微信支付开发前要先去开发者平台创建移动应用,获取APPID和秘钥,然后申请获取支付接口能力(个人无法申请,必须企业才能申请),申请商户号
业务流程:
后台主要做的接口:
1.调用微信的统一下单接口,获取预支付订单支付信息和签名
2.支付成功回调接口的处理
/**
* 微信支付接口(调用统一下单接口)
*
* @param totalAmount
* 总金额
* @param description
* 商品描述
* @param request
* @return
* @throws UnsupportedEncodingException
*/
@ApiOperation(value = "获取预付单信息和签名")
@GetMapping("getSign")
@ResponseBody
public ErrorObject getSign(@RequestParam BigDecimal totalAmount, @RequestParam String description, String deedsn,
HttpServletRequest request) throws UnsupportedEncodingException {
ErrorObject error = new ErrorObject();
// 订单号
String tradeNo = UUID.randomUUID().toString().replaceAll("-", "").toLowerCase().substring(0, 32);
// 回调地址
String notifyUrl = currentPath + "/api/v2/wx/json/money/wxpay/succ";
// 自定义参数
Long userId = 100L; // 对应用户id自己修改
JSONObject jsAtt = new JSONObject();
jsAtt.put("uid", userId);
String attach = jsAtt.toJSONString();
// 返回预支付参数
SortedMap<String, Object> map = new TreeMap<>();
map = PayCommonUtil.WxPublicPay(tradeNo, totalAmount, description, attach, notifyUrl, request);
WxPayDto dto = new WxPayDto();
dto.setAppid((String) map.get("appId"));
dto.setPartnerid((String) map.get("partnerid"));
dto.setPrepayid((String) map.get("prepayid"));
dto.setPackage((String) map.get("package"));
dto.setNoncestr((String) map.get("nonceStr"));
dto.setTimestamp(map.get("timeStamp") + "");
// 获取二次签名sign
SortedMap<String, Object> parameterMap = new TreeMap<>();
parameterMap.put("appid", dto.getAppid());
parameterMap.put("noncestr", dto.getNoncestr());
parameterMap.put("package", dto.getPackage());
parameterMap.put("partnerid", dto.getPartnerid());
parameterMap.put("prepayid", dto.getPrepayid());
parameterMap.put("timestamp", dto.getTimestamp());
String sign = PayToolUtil.createSign("UTF-8", parameterMap, PayCommonUtil.API_KEY);
dto.setSign(sign);
error.setMap(UtilPublic.toModelMap(dto));
// 生成订单
TblOrder order = new TblOrder();
order.setBizOrderNo(tradeNo);// 订单号
order.setPayStatus((byte) 0);
order.setAmount(totalAmount.longValue());// 商品金额
order.setGoodsName(description);// 商品名称
order.setDeedsn(deedsn);
TblUser user = TblUserHelper.currentUser();
if (user == null) {
error.setSuccess(false);
error.setMsg("token失效,请重新登录!");
return error;
}
order.setUid(user.getUid());
tblOrderService.save(order);
return error;
}
@ApiOperation(value = "支付成功回调地址")
@PostMapping("/json/money/wxpay/succ")
public String wxpaySucc(HttpServletRequest request) throws IOException {
log.info("微信支付成功异步通知参数:");
InputStream inStream = request.getInputStream();
ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outSteam.write(buffer, 0, len);
}
String resultxml = new String(outSteam.toByteArray(), "utf-8");
Map<String, String> params = null;
try {
params = PayCommonUtil.doXMLParse(resultxml);
} catch (JDOMException e) {
e.printStackTrace();
}
outSteam.close();
inStream.close();
if (!PayCommonUtil.isTenpaySign(params)) {
// 支付失败
return "fail";
} else {
log.info("===============付款成功==============");
// ------------------------------
// 处理业务开始
// ------------------------------
// 此处处理订单状态,结合自己的订单数据完成订单状态的更新
// ------------------------------
log.warn("微信付款成功:");
log.warn("微信支付成功异步通知参数:");
String total_fee = params.get("total_fee");// 总金额
log.warn("total_fee= {}", total_fee);
double v = Double.valueOf(total_fee) / 100;
// 取出用户id
String attach = params.get("attach");
String out_trade_no = params.get("out_trade_no");// 商户订单号
String openid = params.get("openid");
log.warn("out_trade_no= {}", out_trade_no);
log.warn("openid= {}", openid);
TblOrder order = tblOrderService.findByBizOrderNo(out_trade_no);
order.setPayStatus((byte) 1);
tblOrderService.save(order);
// TODO修改合同状态并同步到erp
return "success";
}
}
完整代码的截图: