Java SpringBoot

1、引入微信SDK

  1. <dependency>
  2. <groupId>com.github.wxpay</groupId>
  3. <artifactId>wxpay-sdk</artifactId>
  4. <version>0.0.3</version>
  5. </dependency>

2、微信支付基本配置类

  1. public static final String APP_ID = "你的appid";
  2. public static final String KEY = "你的api秘钥,不是appSecret";
  3. public static final String MCH_ID = "你的商户id";
  4. String certPath = ClassUtils.getDefaultClassLoader().getResource("").getPath()+"/weixin/apiclient_cert.p12";//从微信商户平台下载的安全证书存放的路径。

3、编写配置类

  1. package com.foochane.awpay.test.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.springframework.boot.autoconfigure.condition.ConditionalOnClass;
  6. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. @Configuration
  10. @ConditionalOnClass(WxPayService.class)
  11. public class MyWxPayConfig {
  12. @Bean
  13. @ConditionalOnMissingBean
  14. public WxPayService wxService() {
  15. WxPayConfig payConfig = new WxPayConfig();
  16. payConfig.setAppId("xxxxxx");
  17. payConfig.setMchId("xxxxx");
  18. payConfig.setMchKey("xxxxxxxxxx");
  19. payConfig.setKeyPath("D:\\xx\\xx\\xxxx\\apiclient_cert.p12");
  20. payConfig.setUseSandboxEnv(false); //不使用沙箱环境
  21. WxPayService wxPayService = new WxPayServiceImpl();
  22. wxPayService.setConfig(payConfig);
  23. return wxPayService;
  24. }
  25. }

4、支付代码

  1. package com.foochane.awpay.test.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.request.WxPayRefundRequest;
  5. import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
  6. import com.github.binarywang.wxpay.bean.result.*;
  7. import com.github.binarywang.wxpay.exception.WxPayException;
  8. import com.github.binarywang.wxpay.service.WxPayService;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.web.bind.annotation.*;
  11. @RestController
  12. public class WxPayController {
  13. @Autowired
  14. private WxPayService wxPayService;
  15. /**
  16. * 添加支付订单
  17. * @param request 请求参数
  18. * 至少要包含如下参数,请求示例(扫描支付):
  19. * {
  20. * "tradeType": "NATIVE",
  21. * "body": "商品购买",
  22. * "outTradeNo": "P22321112130097578901",
  23. * "productId": "12342424242323233",
  24. * "totalFee": 1,
  25. * "spbillCreateIp": "12.3.44.4",
  26. * "notifyUrl":"http://www.xxxx.com:/wx/order/notify"
  27. * }
  28. * @return 返回支付信息
  29. * @throws WxPayException
  30. */
  31. @ResponseBody
  32. @RequestMapping(value = "wx/pay/order/create",method = RequestMethod.POST)
  33. public WxPayUnifiedOrderResult unifiedOrder(@RequestBody WxPayUnifiedOrderRequest request) throws WxPayException {
  34. return this.wxPayService.unifiedOrder(request);
  35. }
  36. /**
  37. * 支付回调通知处理
  38. * @param xmlData
  39. * @return
  40. * @throws WxPayException
  41. */
  42. @PostMapping("wx/order/notify")
  43. public String parseOrderNotifyResult(@RequestBody String xmlData) throws WxPayException {
  44. final WxPayOrderNotifyResult notifyResult = this.wxPayService.parseOrderNotifyResult(xmlData);
  45. // TODO 根据自己业务场景需要构造返回对象
  46. return WxPayNotifyResponse.success("成功");
  47. }
  48. /**
  49. * 查询订单
  50. * @param transactionId 微信订单号
  51. * @param outTradeNo 商户系统内部的订单号,当没提供transactionId时需要传这个,两个参数二选一即可
  52. */
  53. @GetMapping("/wx/par/order/query")
  54. public WxPayOrderQueryResult queryOrder(@RequestParam(required = false) String transactionId,
  55. @RequestParam(required = false) String outTradeNo)
  56. throws WxPayException {
  57. return this.wxPayService.queryOrder(transactionId, outTradeNo);
  58. }
  59. /**
  60. * 申请退款
  61. * @param request 请求对象
  62. * 请求示例(至少包含如下参数):
  63. * {
  64. * "outRefundNo": "rxx34343121",
  65. * "outTradeNo": "p22321213009757890",
  66. * "refundAccount": "REFUND_SOURCE_UNSETTLED_FUNDS",
  67. * "refundDesc": "退款",
  68. * "refundFee": 1,
  69. * "totalFee": 1,
  70. * "notifyUrl": "http://www.xxxx.com/wx/notify
  71. * }
  72. * @return 退款操作结果
  73. */
  74. @PostMapping("/wx/refund/order/create")
  75. public WxPayRefundResult refund(@RequestBody WxPayRefundRequest request) throws WxPayException {
  76. return this.wxPayService.refund(request);
  77. }
  78. /**
  79. * 微信支付-查询退款
  80. * 以下四个参数四选一
  81. *
  82. * @param transactionId 微信订单号
  83. * @param outTradeNo 商户订单号
  84. * @param outRefundNo 商户退款单号
  85. * @param refundId 微信退款单号
  86. * @return 退款信息
  87. */
  88. @GetMapping("/wx/refund/order/query")
  89. public WxPayRefundQueryResult refundQuery(@RequestParam(required = false) String transactionId,
  90. @RequestParam(required = false) String outTradeNo,
  91. @RequestParam(required = false) String outRefundNo,
  92. @RequestParam(required = false) String refundId)
  93. throws WxPayException {
  94. return this.wxPayService.refundQuery(transactionId, outTradeNo, outRefundNo, refundId);
  95. }
  96. /**
  97. * 关闭订单
  98. * @param outTradeNo 商户系统内部的订单号
  99. */
  100. @GetMapping("/wx/order/close{outTradeNo}")
  101. public WxPayOrderCloseResult closeOrder(@PathVariable String outTradeNo) throws WxPayException {
  102. return this.wxPayService.closeOrder(outTradeNo);
  103. }
  104. }

5、支付实现结果

只需要修改配置文件中自己的appid等信息就可以使用了。文中没有Java后台的业务逻辑,可以自己添加,这里主要实现支付功能。(为了隐私,截图中屏蔽了主体信息。)
2021-05-30-10-00-49-968373.png