当前位置:   article > 正文

微信支付(weixin-java-pay)

weixin-java-pay

1. 项目依赖

增加pom.xml文件中增加依赖包。weixin-java-pay是GitHub开源的一个微信支付工具包

  1. <dependency>
  2. <groupId>com.github.binarywang</groupId>
  3. <artifactId>weixin-java-pay</artifactId>
  4. <version>3.0.0</version>
  5. </dependency>
  6. 复制代码

2. contorller代码

  1. package com.xxx.sub.controller;
  2. import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
  3. import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
  4. import com.github.binarywang.wxpay.bean.order.WxPayAppOrderResult;
  5. import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
  6. import com.github.binarywang.wxpay.bean.result.WxPayBillBaseResult;
  7. import com.github.binarywang.wxpay.config.WxPayConfig;
  8. import com.github.binarywang.wxpay.exception.WxPayException;
  9. import com.github.binarywang.wxpay.service.WxPayService;
  10. import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
  11. import com.github.binarywang.wxpay.util.SignUtils;
  12. import com.github.wxpay.sdk.WXPay;
  13. import com.github.wxpay.sdk.WXPayConfig;
  14. import com.github.wxpay.sdk.WXPayUtil;
  15. import com.google.gson.Gson;
  16. import com.xxx.sub.entity.SubWorkerInfoEntity;
  17. import com.xxx.sub.entity.SysPayInfoEntity;
  18. import com.xxx.sub.service.SubWorkerInfoServiceI;
  19. import com.xxx.sub.service.SysPayInfoServiceI;
  20. import com.xxx.sub.util.SubCodeUtil;
  21. import com.sun.star.bridge.oleautomation.Decimal;
  22. import io.swagger.annotations.Api;
  23. import io.swagger.annotations.ApiOperation;
  24. import io.swagger.annotations.ApiParam;
  25. import org.apache.commons.io.IOUtils;
  26. import org.apache.log4j.Logger;
  27. import org.jeecgframework.core.common.controller.BaseController;
  28. import org.jeecgframework.core.util.PropertiesUtil;
  29. import org.jeecgframework.jwt.util.ResponseMessage;
  30. import org.jeecgframework.jwt.util.Result;
  31. import org.jeecgframework.p3.core.util.MD5Util;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.stereotype.Controller;
  34. import org.springframework.web.bind.annotation.*;
  35. import javax.servlet.http.HttpServletRequest;
  36. import javax.servlet.http.HttpServletResponse;
  37. import java.math.BigDecimal;
  38. import java.util.*;
  39. /**
  40. * @Title: Controller
  41. * @Description: 微信支付
  42. * @author onlineGenerator
  43. * @date 2018-02-07 17:45:09
  44. * @version V1.0
  45. *
  46. */
  47. @Api(value="wxPayController",description="微信支付",tags="RESTWxPayController")
  48. @Controller
  49. @RequestMapping("/wxPayController")
  50. public class RESTWxPayController extends BaseController {
  51. /**
  52. * Logger for this class
  53. */
  54. private static final Logger logger = Logger.getLogger(RESTWxPayController.class);
  55. @Autowired
  56. WXPayConfig config;
  57. @Autowired
  58. private SubWorkerInfoServiceI subWorkerInfoService;
  59. /**
  60. * 产生订单接口
  61. * @param payType 支付类型
  62. * @param id 支付信息id
  63. * @return
  64. * @throws WxPayException
  65. */
  66. @RequestMapping(value = "/generatePay/{payType}/{id}",method = RequestMethod.POST)
  67. @ResponseBody
  68. @ApiOperation(value="微信统一下单接口1")
  69. public ResponseMessage<?> generatePay2(@ApiParam(required=true) @PathVariable(value="payType") String payType,@ApiParam(required=true) @PathVariable(value="id") String id) throws WxPayException {
  70. String tradeNo = SubCodeUtil.getNum();
  71. String busId = "123131313";
  72. Integer fee = 1;
  73. WxPayUnifiedOrderRequest.WxPayUnifiedOrderRequestBuilder builder = WxPayUnifiedOrderRequest.newBuilder();
  74. WxPayUnifiedOrderRequest request = builder.body(payType)
  75. .totalFee(fee).outTradeNo(tradeNo)
  76. .productId("1000000123")
  77. .spbillCreateIp("0.0.0.0")
  78. .tradeType("APP").build();
  79. request.setSignType("MD5");
  80. WxPayService wxPayService = getWxPayService();
  81. WxPayAppOrderResult o = wxPayService.createOrder(request);
  82. System.out.println(o.toString());
  83. // 存储支付信息到数据库
  84. SysPayInfoEntity payInfoEntity = new SysPayInfoEntity();
  85. payInfoEntity.setBusId(Integer.valueOf(id));
  86. payInfoEntity.setBusType(payType);
  87. payInfoEntity.setBusCode(busId);
  88. payInfoEntity.setCreateTime(new Date());
  89. payInfoEntity.setOrderNo(tradeNo);
  90. payInfoEntity.setPayState(0);//支付状态:0未支付1支付成功2支付失败3退款成功
  91. payInfoEntity.setPayType("wxPay");
  92. payInfoEntity.setOrderDetail(new Gson().toJson(0));
  93. try {
  94. sysPayInfoServiceI.save(payInfoEntity);
  95. } catch (Exception e) {
  96. e.printStackTrace();
  97. }
  98. return Result.success(o);
  99. }
  100. /**
  101. * 接收支付返回的消息
  102. * @param
  103. */
  104. @RequestMapping(value="/parseOrderNotifyResult",method = RequestMethod.POST)
  105. @ApiOperation(value="接收支付返回的消息")
  106. @ResponseBody
  107. public String parseOrderNotifyResult(HttpServletRequest request, HttpServletResponse response) {
  108. System.out.println("============支付回调开始");
  109. try {
  110. String xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
  111. WxPayService wxPayService = getWxPayService();
  112. WxPayOrderNotifyResult result = wxPayService.parseOrderNotifyResult(xmlResult);
  113. System.out.println(result.toString());
  114. // 结果正确
  115. String orderId = result.getOutTradeNo();
  116. String tradeNo = result.getTransactionId();
  117. //String totalFee = WxPayBaseResult.feeToYuan(result.getTotalFee());
  118. //自己处理订单的业务逻辑,需要判断订单是否已经支付过,否则可能会重复调用
  119. System.out.println("============支付回调结束");
  120. return WxPayNotifyResponse.success("处理成功!");
  121. } catch (Exception e) {
  122. logger.error("微信回调结果异常,异常原因{}" + e.getMessage());
  123. return WxPayNotifyResponse.fail(e.getMessage());
  124. }
  125. }
  126. /**
  127. * 加载配置文件,生成微信payservice对象
  128. * @return
  129. */
  130. private WxPayService getWxPayService() {
  131. PropertiesUtil util = new PropertiesUtil("wx.properties");
  132. WxPayConfig payConfig = new WxPayConfig();
  133. payConfig.setAppId(util.readProperty("appid"));
  134. payConfig.setMchId(util.readProperty("mchid"));
  135. payConfig.setMchKey(util.readProperty("mchkey"));
  136. payConfig.setNotifyUrl(util.readProperty("notifyurl"));
  137. WxPayService wxPayService = new WxPayServiceImpl();
  138. wxPayService.setConfig(payConfig);
  139. return wxPayService;
  140. }
  141. }
  142. 复制代码

3. service、entity代码省略

service就是业务操作、entity主要是存储支付订单信息的实体

4. util

  1. package com.nyis.sub.util;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. /**
  5. * 流水号生成
  6. */
  7. public class SubCodeUtil {
  8. /**
  9. * 获取现在时间
  10. * @return返回字符串格式yyyyMMddHHmmss
  11. */
  12. public static String getStringDate() {
  13. Date currentTime = new Date();
  14. SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
  15. String dateString = formatter.format(currentTime);
  16. System.out.println("TIME:::"+dateString);
  17. return dateString;
  18. }
  19. /**
  20. * 由年月日时分秒+3位随机数
  21. * 生成流水号
  22. * @return
  23. */
  24. public static String getNum(){
  25. String t = getStringDate();
  26. int x=(int)(Math.random()*900)+100;
  27. String serial = t + x;
  28. return serial;
  29. }
  30. }
  31. 复制代码

转载于:https://juejin.im/post/5ae19366518825671f2f79a1

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/874176?site
推荐阅读
相关标签
  

闽ICP备14008679号