一、微信支付太坑爹,废话不说了,下面是我的服务端微信支付开发过程和代码记录
二、首先去微信申请账户,这里有两个平台
1、微信公众平台
2、微信开放平台(https://open.weixin.qq.com)这里选择第二个
三、账户开通、开发者认证之后就可以进行微信支付开发了
1、微信统一下单接口调用获取预支付id
/**
* 获取微信支付所需信息(统一下单接口调用)
* @param backURL
* @param mDealerOrderEntity
* @param mCarInfoEntity
* @return
* @throws JSONException
* @throws IOException
* @throws JDOMException
*/
public Map<String, String> getWechatOrderInfo(String notifyUrl, MDealerOrderEntity mDealerOrderEntity, String body, HttpServletRequest request, HttpServletResponse response) throws Exception {
Map<String, String> resultMap = new HashMap<String, String>();
//生成payPreId
Map<String, String> payPreIdMap = WechatUtil.getPayPreId(mDealerOrderEntity.getGoodorderno(), body, notifyUrl, request.getRemoteAddr(), String.valueOf((int)(mDealerOrderEntity.getMoney()*100)));
String prePayId = payPreIdMap.get("prepay_id");
if(StringUtils.isNotEmpty(prePayId)) {
//生成调用微信APP参数
resultMap = WechatUtil.genPayReq(prePayId);
}
return resultMap;
}
此方法返回的数据如下
{ "appid": "123132131", "noncestr": "416e5cf0acb7e553a880b7647903da6e", "packageValue ": "Sign=WXPay", "partnerid ": "1276000000", "prepayid ": "wx2015101611341514a3cbbbf90572184370", "timestamp ": "1444966497", "sign": "1DD72B07607B0B41D2827954150D89E9" }
2、服务器端接受微信支付结果通知
/**
* 处理微信支付通知
* @param request
* @return
* @throws Exception
*/
public String saveWechatNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
Map<String, String> noticeMap = XMLUtil.parseXml(request);
String transactionId = noticeMap.get("transaction_id");
MWechatInfoEntity wechatInfoEntity = this.findEntityByProperty(MWechatInfoEntity.class, "transactionId", transactionId);
//如果wechatInfoEntity存在,说明请求已经处理过,直接返回
if(wechatInfoEntity != null) {
return "SUCCESS";
}
String sign = noticeMap.get("sign");
noticeMap.remove("sign");
// 验签通过
if (WechatUtil.getSignVeryfy(noticeMap, sign)) {
// 通信成功此字段是通信标识,非交易标识,交易是否成功需要查看result_code来判断
if ("SUCCESS".equals(noticeMap.get("return_code"))) {
// 交易成功
if ("SUCCESS".equals(noticeMap.get("result_code"))) {
// 商户订单号
String goodorderno = noticeMap.get("out_trade_no");
MDealerOrderEntity mDealerOrderEntity = this.findEntityByProperty(MDealerOrderEntity.class, "goodorderno", goodorderno);
MCarInfoEntity mCarInfoEntity = this.get(MCarInfoEntity.class, mDealerOrderEntity.getCarid());
// 订单更新时间
mDealerOrderEntity.setUpdatetime(new Date());
// ------------------------------
// 处理业务开始
// ------------------------------
// 这里写自己业务相关
// ----------------------------