微信支付java端之小程序
<!-- 微信支付sdk -->
<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>0.0.3</version>
</dependency>。
@Slf4j
public class WeChatConfig {
@ApiModelProperty(value = "微信支付分配的公众账号ID")
public final static String appId="";
@ApiModelProperty(value = "secret")
public final static String secret="";
@ApiModelProperty(value = "微信支付分配的商户号")
public final static String mchId="";
@ApiModelProperty(value = "商户私钥")
public final static String privateKey="";
@ApiModelProperty(value = "获取token地址")
public final static String tokenUrl="https://api.weixin.qq.com/sns/oauth2/access_token";
@ApiModelProperty(value = "支付地址")
public final static String payUrl="https://api.mch.weixin.qq.com/pay/unifiedorder";
@ApiModelProperty(value = "支付回调地址")
public final static String notifyUrl="";
public static Map<String,String> getWeChatToken(String code){
log.info("微信登录code:"+code);
String url=tokenUrl+"?appid="+appId+"&secret="+secret+"&js_code="+code+"&grant_type=authorization_code";
Map<String,String> hashMap= JSON.parseObject(HttpClient.doGet(url), HashMap.class);
log.info("微信登录返回参数:"+hashMap);
return hashMap;
}
public static String getOpenId(String code){
Map<String,String> map=getWeChatToken(code);
if (map.containsKey("errcode")){
return map.toString();
}
return map.get("openid");
}
}
@Data
public class WeChatPay {
private String signType;
@ApiModelProperty(value = "商品描述")
private String body;
@ApiModelProperty(value = "商户系统内部订单号")
private String outTradeNo;
@ApiModelProperty(value = "订单总金额(单位为分)")
private Integer totalFee;
@ApiModelProperty(value = "终端IP")
private String spbillCreateIp;
@ApiModelProperty(value = "交易类型")
private String tradeType;
@ApiModelProperty(value = "用户标识")
private String openId;
public WeChatPay(){
super();
}
public WeChatPay(String outTradeNo, Integer totalFee, String spbillCreateIp, String openId) {
this.signType="MD5";
this.body = "众云-在线点餐";
this.outTradeNo = outTradeNo;
this.totalFee = totalFee;
this.spbillCreateIp = spbillCreateIp;
this.tradeType = "JSAPI";
this.openId = openId;
}
}
public static Map<String, String> wxOrder(WeChatPay pay) {
try {
log.info("pay"+pay);
Map<String, String> paraMap = new HashMap<>();
paraMap.put("trade_type",pay.getTradeType());
paraMap.put("openid", pay.getOpenId());
paraMap.put("appid", WeChatConfig.appId);
paraMap.put("mch_id", WeChatConfig.mchId);
String nonceStr=WXPayUtil.generateNonceStr();
paraMap.put("nonce_str",nonceStr);
paraMap.put("body", pay.getBody());
paraMap.put("out_trade_no", pay.getOutTradeNo());
paraMap.put("total_fee", String.valueOf(pay.getTotalFee()));
paraMap.put("spbill_create_ip", pay.getSpbillCreateIp());
paraMap.put("notify_url", WeChatConfig.notifyUrl);
String sign = WXPayUtil.generateSignature(paraMap, WeChatConfig.privateKey);
paraMap.put("sign", sign);
String xml = WXPayUtil.mapToXml(paraMap);
String url = WeChatConfig.payUrl;
log.info("微信支付下单参数:"+xml);
String xmlStr = HttpClient.httpsRequest(url,"POST", xml);
log.info("微信支付返回参数:"+xmlStr);
if (xmlStr.indexOf("SUCCESS") != -1) {
Map<String, String> map = WXPayUtil.xmlToMap(xmlStr);
Map<String, String> payMap = new HashMap<String, String>();
payMap.put("appId", WeChatConfig.appId);
payMap.put("timeStamp", WXPayUtil.getCurrentTimestamp() + "");
payMap.put("nonceStr", WXPayUtil.generateNonceStr());
payMap.put("signType", "MD5");
payMap.put("package", "prepay_id=" + map.get("prepay_id"));
String paySign = WXPayUtil.generateSignature(payMap, WeChatConfig.privateKey);
payMap.put("paySign", paySign);
log.info("微信支付返回给前端参数:"+payMap);
return payMap;
}else {
return null;
}
} catch (Exception e) {
log.info("微信支付下单异常");
e.printStackTrace();
return null;
}