SpringBoot项目后台对接微信支付开发——微信统一下单接口开发

开始没找到微信支付的sdk。自己根据官方给的接口文档纯手写,各种xml转JSON,JSON转xml,加密解密,签名、、、、整个人都是崩溃的

开发的第三天,发现有官方的sdk。心情一下子豁然开朗,整个人都轻松了一截。

话不多说,直接上代码

首先当然是引入微信支付的依赖包啦

<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>0.0.3</version>
</dependency>

大家可以进去源码里面看看,各个方法都被封装得挺好得,从原生过来的我泪流满面
下面以微信统一下单接口开发为例
  /** @description 微信统一下单接口
* @Author wenjing
* @Date 10:46 2019/5/13
* @Param [wxUnifiedorderModel]
* @return com.zyhp.utils.Result
**/
public Result wxUnifiedorder(WxUnifiedorderModel wxUnifiedorderModel)throws Exception{
    //new一个微信支付,实际开发中最好使用单例模式
MyWxConfig config = new MyWxConfig();
WXPay wxPay = new WXPay(config);
SortedMap<String, String> params = new TreeMap<String, String>();
params.put("device_info", "WEB"); //设备号
params.put("body", wxUnifiedorderModel.getBody());//商品描述
//生成商户订单号
long serialNumMax = redisUtil.incr("outTradeNo");
String outTradeNo = CommonUtil.getDateStr(new Date())+serialNumMax;
wxUnifiedorderModel.setOutTradeNo(outTradeNo);
params.put("out_trade_no", outTradeNo);
params.put("total_fee", wxUnifiedorderModel.getTotalFee().movePointRight(2).toString());
// params.put("time_start", time_start);
params.put("trade_type","JSAPI");
params.put("notify_url",Constans.SERVICE_HOST+"/payInfoBack");
params.put("spbill_create_ip",wxUnifiedorderModel.getSpbillCreateIp());
Map<String,String> resultMap = wxPay.unifiedOrder(params);
if("FAIL".equals(resultMap.get("return_code"))){
return new Result("-1","参数格式错误",null);
}
if("SUCCESS".equals(resultMap.get("result_code"))){
return new Result("-1",resultMap.get("err_code_des"),null);
}
//保存该订单记录
wxUnifiedorderModel.setTimeStart(new Date());
wxUnifiedorderModelMapper.insert(wxUnifiedorderModel);
return new Result("1","成功",resultMap.get("prepay_id"));
}

猜你喜欢

转载自www.cnblogs.com/wenjing-520/p/10870878.html
今日推荐