当前位置:   article > 正文

springBoot(微信支付)_com.github.binarywang

com.github.binarywang

微信支付

pom包的GitHub地址

微信支付文档

pom包

  1. <dependency>
  2. <groupId>com.github.binarywang</groupId>
  3. <artifactId>weixin-java-pay</artifactId>
  4. <version>4.0.0</version>
  5. </dependency>

配置

  1. #微信配置
  2. wx:
  3. pay:
  4. appid: ************** #公众账号ID
  5. mchId: ************** #商户号
  6. key: ************** #商户号的支付key
  7. keyPath: C:\Users\admin\Desktop\apiclient_cert.p12 #微信支付证书(退款、关闭订单 时需要)
  8. notifyUrl: https://**************/***/*** #支付通知地址(必须是外网可以访问的地址)
  9. refundUrl: https://**************/***/*** #退款通知地址(必须是外网可以访问的地址)

配置文件

获取配置

  1. package com.zt.edu.userapi.weChat.config;
  2. import lombok.Data;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.boot.context.properties.ConfigurationProperties;
  5. import org.springframework.stereotype.Component;
  6. /**
  7. * @author <a href="https://github.com/binarywang">Binary Wang</a>
  8. */
  9. @ConfigurationProperties(prefix = "wx.pay")
  10. @Data
  11. public class WxMaProperty {
  12. /**
  13. * 公众账号ID
  14. */
  15. private String appid;
  16. /**
  17. * 设置微信小程序的Secret
  18. */
  19. private String mchId;
  20. /**
  21. * 支付通知地址
  22. */
  23. private String notifyUrl;
  24. /**
  25. * 退款通知地址
  26. */
  27. private String refundUrl;
  28. /**
  29. * 支付key
  30. */
  31. private String key;
  32. /**
  33. * 支付key
  34. */
  35. private String keyPath;
  36. }

配置文件

  1. package com.zt.edu.userapi.weChat.config;
  2. import com.github.binarywang.wxpay.config.WxPayConfig;
  3. import com.github.binarywang.wxpay.service.WxPayService;
  4. import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
  5. import org.apache.commons.lang3.StringUtils;
  6. import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
  7. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  8. import org.springframework.context.annotation.Configuration;
  9. import javax.annotation.PostConstruct;
  10. import javax.annotation.Resource;
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. /**
  14. * @author ybwei
  15. * @Description 支付配置
  16. * @date 2021/3/22 14:09
  17. **/
  18. @Configuration
  19. @ConditionalOnClass(WxPayService.class)
  20. @EnableConfigurationProperties(WxMaProperty.class)
  21. public class WxPayConfiguration {
  22. @Resource
  23. WxMaProperty wxMaProperty;
  24. private static Map<String, WxPayService> wxPayServices = new HashMap<>();
  25. /**
  26. * @Description 默认公众号appid
  27. * @author: ybwei
  28. * @Date: 2021/3/22 15:17
  29. */
  30. private String defaultAppid=null;
  31. @PostConstruct
  32. public void init() {
  33. //1、赋值wxPayServices
  34. WxPayConfig payConfig = new WxPayConfig();
  35. payConfig.setAppId(StringUtils.trimToNull(wxMaProperty.getAppid()));
  36. payConfig.setMchId(StringUtils.trimToNull(wxMaProperty.getMchId()));
  37. payConfig.setMchKey(StringUtils.trimToNull(wxMaProperty.getKey()));
  38. payConfig.setKeyPath(StringUtils.trimToNull(wxMaProperty.getKeyPath()));
  39. // 可以指定是否使用沙箱环境
  40. payConfig.setUseSandboxEnv(false);
  41. WxPayService wxPayService = new WxPayServiceImpl();
  42. wxPayService.setConfig(payConfig);
  43. wxPayServices.put(wxMaProperty.getAppid(),wxPayService);
  44. }
  45. /**
  46. * @Description 根据appid获取WxPayService
  47. * @Author ybwei
  48. * @Date 2021/3/22 15:01
  49. * @Param [appid]
  50. * @Return com.github.binarywang.wxpay.service.WxPayService
  51. * @Exception
  52. */
  53. public WxPayService getMaService(String appid) {
  54. //1、如果appid为空,默认首个公众号appid
  55. if(appid==null){
  56. appid=defaultAppid;
  57. }
  58. //2、路由
  59. WxPayService wxService = wxPayServices.get(appid);
  60. if (wxService == null) {
  61. throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", appid));
  62. }
  63. return wxService;
  64. }
  65. }

路径转二维码(扫码支付需要)

pom

  1. <dependency>
  2. <groupId>com.google.zxing</groupId>
  3. <artifactId>javase</artifactId>
  4. <version>3.3.0</version>
  5. </dependency>

工具类

  1. package com.zt.edu.userapi.util;
  2. import com.google.zxing.BarcodeFormat;
  3. import com.google.zxing.EncodeHintType;
  4. import com.google.zxing.MultiFormatWriter;
  5. import com.google.zxing.client.j2se.MatrixToImageWriter;
  6. import com.google.zxing.common.BitMatrix;
  7. import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.OutputStream;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. /**
  13. * 生成二维码
  14. */
  15. public class QRCodeUtil {
  16. /**
  17. *
  18. * @param response
  19. * @param codeUrl 需要转二维码的URL
  20. */
  21. public static void createQrCode(HttpServletResponse response,String codeUrl) {
  22. try {
  23. //生成二维码配置
  24. Map<EncodeHintType,Object> hints = new HashMap<>();
  25. //设置纠错等级
  26. hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
  27. //设置编码类型
  28. hints.put(EncodeHintType.CHARACTER_SET,"UTF-8");
  29. //构造图片对象
  30. BitMatrix bitMatrix = new MultiFormatWriter().encode(codeUrl, BarcodeFormat.QR_CODE,400,400,hints);
  31. //输出流
  32. OutputStream out = response.getOutputStream();
  33. MatrixToImageWriter.writeToStream(bitMatrix,"png",out);
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. }

示例

  1. package com.zt.edu.userapi.weChat.service.impl;
  2. import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
  3. import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult;
  4. import com.zt.edu.userapi.dto.ZtbOrderDTO;
  5. import com.zt.edu.userapi.service.ZtbPayResultService;
  6. import com.zt.edu.userapi.weChat.config.WxMaProperty;
  7. import com.zt.edu.userapi.weChat.config.WxPayConfiguration;
  8. import com.zt.edu.userapi.weChat.service.WeChatPayService;
  9. import com.zt.edu.userapi.weChat.util.WXPayUtil;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.springframework.stereotype.Service;
  12. import javax.annotation.Resource;
  13. import java.math.BigDecimal;
  14. @Service
  15. @Slf4j
  16. public class WeChatPayServiceImpl implements WeChatPayService {
  17. @Resource
  18. private WxMaProperty wxMaProperty;
  19. @Resource
  20. private WxPayConfiguration wxPayConfiguration;
  21. @Resource
  22. private ZtbPayResultService ztbPayResultService;
  23. /**
  24. * 统一下单
  25. * @return
  26. * @throws Exception
  27. */
  28. @Override
  29. public String unifiedOrder(ZtbOrderDTO orderDTO) throws Exception {
  30. String spbillCreateIp = WXPayUtil.getLocalIp();
  31. WxPayUnifiedOrderRequest orderRequestequest = WxPayUnifiedOrderRequest.newBuilder()
  32. .body(orderDTO.getCourseName())//订单名称
  33. .outTradeNo(orderDTO.getOrderNo())//商户的订单号
  34. .totalFee(orderDTO.getPrice().multiply(new BigDecimal("100")).intValue())//需要支付的金额(单位:分)
  35. .spbillCreateIp(spbillCreateIp)//APP和网页支付提交用户端ip
  36. .notifyUrl(wxMaProperty.getNotifyUrl())//支付结果回调地址
  37. .tradeType("NATIVE")//支付类型:JSAPI--公众号支付、NATIVE--原生扫码支付、APP--app支付
  38. .productId(String.valueOf(orderDTO.getId()))//支付的商品id(商户自定义)
  39. .build();
  40. //获取指定appId的WxPayService(com.github.binarywang.wxpay.service.WxPayService)调用统一下单
  41. WxPayUnifiedOrderResult result = wxPayConfiguration.getMaService(wxMaProperty.getAppid()).unifiedOrder(orderRequestequest);
  42. //返回的是微信的支付链接(需要转成二维码返给前端)
  43. return result.getCodeURL();
  44. }
  45. /**
  46. * 支付回调
  47. * @param xmlData
  48. */
  49. @Override
  50. @Transactional
  51. public String payNotifywx(String xmlData) throws Exception {
  52. log.info("PayController parseOrderNotifyResult xmlData:{}", xmlData);
  53. //返回结果
  54. WxPayOrderNotifyResult notifyResult = wxPayConfiguration.getMaService(wxMaProperty.getAppid()).parseOrderNotifyResult(xmlData);
  55. //处理订单业务
  56. //通知微信订单处理成功
  57. return WxPayNotifyResponse.success("成功");
  58. }
  59. }

回调的controller接口

  1. @RequestMapping("/payNotifywx")
  2. public String payNotifywx(@RequestBody String xmlData) throws Exception {
  3. return weChatPayService.payNotifywx(xmlData);
  4. }

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

闽ICP备14008679号