springBoot集成微信小程序支付

public AjaxResult buyWxCoinPay(HttpServletRequest request){
String orderNo = HttpUtil.createCode(32);
//获取客户端的ip地址
//调用微信接口 生成订单
OrderPay orderPay = new OrderPay();
orderPay.setBody("购买了东西");//商品描述
orderPay.setNonce_str(HttpUtil.createCode(6));//随机字符串
orderPay.setOut_trade_no("123320010");//订单号
orderPay.setSpbill_create_ip(spbillCreateIp);
BigDecimal totalFee = buyCoinSettingDto.getAmount().multiply(new BigDecimal(100));//支付金额需要转化成分 所以x100
orderPay.setTotal_fee(totalFee.stripTrailingZeros().toPlainString());
orderPay.setOpenid(mobileUserDto.getOpenId());
orderPay.setNotify_url(notify_url);
}
//返回给下单接口
Map<String, Object> objectMap = wxPay(orderPay);
return AjaxResult.success(objectMap);

}

下面是wxpay()方法

public AjaxResult wxPay(OrderPay orderPay) {
//controller层封装的对象传递过来
orderPay.setAppid(appid);
orderPay.setMch_id(mchid);
orderPay.setTrade_type("JSAPI");
orderPay.setKey(key);
orderPay.setPay_url(pay_url);
try {
//参数封装成一个map
Map<String, String> packageParams = new HashMap<String, String>();
packageParams.put("appid", orderPay.getAppid());
packageParams.put("mch_id", orderPay.getMch_id());
packageParams.put("nonce_str", orderPay.getNonce_str());
packageParams.put("body", orderPay.getBody());
packageParams.put("out_trade_no", orderPay.getOut_trade_no());
packageParams.put("total_fee", orderPay.getTotal_fee());
packageParams.put("spbill_create_ip", orderPay.getSpbill_create_ip());
packageParams.put("notify_url", orderPay.getNotify_url());
packageParams.put("trade_type", orderPay.getTrade_type());
packageParams.put("openid", orderPay.getOpenid());
// 除去数组中的空值和签名参数
packageParams = WXApi.paraFilter(packageParams);
String prestr = WXApi.createLinkString(packageParams); // 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
//MD5运算生成签名,这里是第一次签名,用于调用统一下单接口
String mysign = WXApi.sign(prestr, orderPay.getKey(), "utf-8").toUpperCase();
//拼接统一下单接口使用的xml数据,要将上一步生成的签名一起拼接进去
String xml = "<xml>"
+"<appid>" + orderPay.getAppid() + "</appid>"
+ "<mch_id>" + orderPay.getMch_id() + "</mch_id>"
+ "<nonce_str>" + orderPay.getNonce_str() + "</nonce_str>"
+ "<body><![CDATA[" + orderPay.getBody() + "]]></body>"
+ "<out_trade_no>" + orderPay.getOut_trade_no() + "</out_trade_no>"
+ "<total_fee>" + orderPay.getTotal_fee() + "</total_fee>"
+ "<spbill_create_ip>" + orderPay.getSpbill_create_ip() + "</spbill_create_ip>"
+ "<notify_url>" + orderPay.getNotify_url() + "</notify_url>"
+ "<trade_type>" + orderPay.getTrade_type() + "</trade_type>"
+ "<openid>" + orderPay.getOpenid() + "</openid>"
+ "<sign>" + mysign + "</sign>"
+ "</xml>";
//调用统一下单接口,并接受返回的结果
String result = WXApi.httpRequest(orderPay.getPay_url(), "POST", xml);
// 将解析结果存储在HashMap中
Map map = WXApi.doXMLParse(result);
//返回状态码
String return_code = (String) map.get("return_code");
//返回给移动端需要的参数
Map<String, Object> response = new HashMap<String, Object>();
if (return_code == "SUCCESS" || return_code.equals(return_code)) {
// 业务结果
String prepay_id = (String) map.get("prepay_id");//返回的预付单信息
response.put("nonceStr", orderPay.getNonce_str());
response.put("package", "prepay_id=" + prepay_id);
response.put("prepay_id",prepay_id);
Long timeStamp = System.currentTimeMillis() / 1000;
response.put("timeStamp", timeStamp + "");
//这边要将返回的时间戳转化成字符串,不然小程序端调用wx.requestPayment方法会报签名错误
String stringSignTemp = "appId=" + orderPay.getAppid() + "&nonceStr=" + orderPay.getNonce_str() + "&package=prepay_id=" + prepay_id + "&signType=" + "MD5" + "&timeStamp=" + timeStamp;
//再次签名,这个签名用于小程序端调用wx.requesetPayment方法
String paySign = WXApi.sign(stringSignTemp, orderPay.getKey(), "utf-8").toUpperCase();
response.put("paySign", paySign);
response.put("id",orderPay.getOrderFormId());
}
//返回处理结果 下单到此就处理完成了
response.put("appid", orderPay.getOpenid());
return AjaxResult.success(response);
} catch (Exception e) {
e.printStackTrace();
}
return AjaxResult.error("支付服务关闭,请联系管理员");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot 可以集成微信小程序实现小程序的开发和部署。以下是 Spring Boot 集成微信小程序的步骤: 1. 注册微信小程序开发者账号,并创建小程序。 2. 在小程序后台获取 AppID 和 AppSecret。 3. 在 Spring Boot 项目中添加相关依赖,如: ``` <dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-miniapp</artifactId> <version>3.4.0</version> </dependency> ``` 4. 在 Spring Boot 项目中配置小程序相关信息,如: ``` wx.miniapp.appid=your-appid wx.miniapp.secret=your-secret wx.miniapp.token=your-token wx.miniapp.aeskey=your-aeskey ``` 5. 编写小程序相关代码,如: ``` @Service public class WxMiniappService { @Autowired private WxMaService wxMaService; public String getUserOpenId(String code) { try { WxMaJscode2SessionResult session = wxMaService.getUserService().getSessionInfo(code); return session.getOpenid(); } catch (WxErrorException e) { throw new RuntimeException(e); } } public String getAccessToken() { try { return wxMaService.getAccessToken(); } catch (WxErrorException e) { throw new RuntimeException(e); } } public void sendTemplateMessage(WxMaTemplateMessage templateMessage) { try { wxMaService.getMsgService().sendTemplateMsg(templateMessage); } catch (WxErrorException e) { throw new RuntimeException(e); } } } ``` 6. 部署 Spring Boot 项目。 7. 在小程序中调用 Spring Boot 接口,实现小程序与后端的交互。 以上就是 Spring Boot 集成微信小程序的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值