当前位置:   article > 正文

SpringBoot实现微信支付,微信扫码支付,Native支付(全网最详细教程)_springboot native支付

springboot native支付

1.添加微信支付依赖

  1. <!--微信支付-->
  2. <dependency>
  3. <groupId>com.github.wxpay</groupId>
  4. <artifactId>wxpay-sdk</artifactId>
  5. <version>RELEASE</version>
  6. </dependency>
  7. <!--httpclient-->
  8. <dependency>
  9. <groupId>org.apache.httpcomponents</groupId>
  10. <artifactId>httpclient</artifactId>
  11. </dependency>

2.添加配置yml配置文件 如果使用的不是yml 可自行配置

  1. weixin:
  2. appid: wx09c076475bb9xxxx #公众账号ID
  3. partner: 164544xxxx #商户号
  4. partnerkey: zebrainfo20230809wwwcomcn1xxxxxx #商户密钥
  5. notifyurl: http://8de92n.xxxx.cc/pay/info #回调地址

3.自行添加一个service层 (我这边叫PayService)

  1. public interface PayService {
  2. public Object createUrl(CustomerOrder CustomerOrder);
  3. }

4.实现此service层 (其中生成随机订单号使用了一个工具类,可私信我来拿,或你们把随机订单号代码注释,自己去找一个即可)

  1. package com.banma.wxpay;
  2. import com.banma.entity.CustomerOrder;
  3. import com.banma.entity.Enterprise;
  4. import com.banma.mapper.CustomerOrderMapper;
  5. import com.banma.mapper.EnterpriseMapper;
  6. import com.banma.util.HttpClient;
  7. import com.banma.util.OrderCodeFactory;
  8. import com.banma.wxpay.service.PayService;
  9. import com.github.wxpay.sdk.WXPayUtil;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.beans.factory.annotation.Value;
  12. import org.springframework.stereotype.Service;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. @Service
  16. public class PayServiceImpl implements PayService {
  17. //获取yml中的公众账号ID
  18. @Value("${weixin.appid}")
  19. private String appid;
  20. //获取yml中的商户号
  21. @Value("${weixin.partner}")
  22. private String partner;
  23. @Value("${weixin.partnerkey}")
  24. private String partnerkey;
  25. @Value("${weixin.notifyurl}")
  26. private String notifyurl;
  27. @Autowired
  28. EnterpriseMapper EnterpriseMapper;
  29. @Autowired
  30. CustomerOrderMapper CustomerOrderMapper;
  31. @Override
  32. public Object createUrl(CustomerOrder CustomerOrder) {
  33. //orderId:订单的id,price:需要支付的金额(默认单位为 分 )
  34. Map<String,Object> map = new HashMap<String,Object>();
  35. try {
  36. //判断付款用户是否注册企业
  37. Enterprise enterprise = EnterpriseMapper.findEnterpriseAndEnterpriseIdByEnterpriseName(CustomerOrder.getEnterpriseName());
  38. //查询到没有该企业返回404
  39. if(enterprise == null){
  40. map.put("code","404");
  41. return map;
  42. }
  43. //生成随机订单号
  44. String orderCode = OrderCodeFactory.getOrderCode(enterprise.getEnterpriseId().longValue());
  45. CustomerOrder.setCustomerOrderNumber(orderCode);
  46. CustomerOrder.setEnterpriseId(enterprise.getEnterpriseId());
  47. //设置订单待支付状态
  48. CustomerOrder.setCustomerOrderStart(2);
  49. CustomerOrderMapper.addCustomerOrder(CustomerOrder);
  50. Map<String, String> param = new HashMap<>();
  51. param.put("appid", "wx09c076475bb9xxxx");
  52. param.put("mch_id", "164544xxxx");
  53. param.put("nonce_str", WXPayUtil.generateNonceStr());
  54. param.put("body", "企业助力宝套餐购买"+CustomerOrder.getCustomerOrderTransactionNumber());
  55. param.put("out_trade_no", orderCode);
  56. String money = String.valueOf(CustomerOrder.getCustomerOrderPrice()*100);
  57. if (money.indexOf(".") > 0) {
  58. // 去掉多余的0
  59. money = money.replaceAll("0+?$", "");
  60. // 如果最后一位是. 则去掉
  61. money = money.replaceAll("[.]$", "");
  62. }
  63. System.out.println(money);
  64. param.put("total_fee", money);
  65. param.put("spbill_create_ip", "127.0.0.1");
  66. param.put("notify_url", "https://www.zebraxxxx.com.cn/activity/info");
  67. param.put("trade_type", "NATIVE");
  68. System.out.println(param.toString());
  69. //调取微信支付的接口
  70. String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
  71. //将map数据转换为xml格式
  72. String xml = WXPayUtil.generateSignedXml(param, "zebrainfo20230809wwwcomcn1xxxxxx");
  73. //创建网络链接
  74. HttpClient httpClient = new HttpClient(url);
  75. httpClient.setXmlParam(xml);
  76. httpClient.setHttps(true);
  77. httpClient.post();
  78. String content = httpClient.getContent();
  79. return WXPayUtil.xmlToMap(content);
  80. } catch (Exception e) {
  81. e.printStackTrace();
  82. }
  83. return null;
  84. }
  85. }

5.编写Controller层调用API

  1. package com.banma.controller.wechatPayController;
  2. import com.banma.entity.CustomerOrder;
  3. import com.banma.util.Result;
  4. import com.banma.wxpay.PayServiceImpl;
  5. import com.banma.wxpay.service.PayService;
  6. import com.github.wxpay.sdk.WXPayUtil;
  7. import io.swagger.annotations.Api;
  8. import io.swagger.annotations.ApiOperation;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.web.bind.annotation.*;
  11. import javax.servlet.http.HttpServletRequest;
  12. import java.io.ByteArrayOutputStream;
  13. import java.io.InputStream;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. @RestController
  17. @RequestMapping("/activity")
  18. @CrossOrigin
  19. @Api(tags = "微信支付API")
  20. public class WeChatPayController {
  21. @Autowired
  22. PayService PayService;
  23. @ApiOperation("微信统一收单下单支付接口API")
  24. @PostMapping("/wxPay")
  25. public Result wxPay(@RequestBody CustomerOrder CustomerOrder){
  26. Map<String,Object> map = (Map<String, Object>) PayService.createUrl(CustomerOrder);
  27. Integer code = map.get("code")==null?0:404;
  28. if(code != 0 && code == 404){
  29. return Result.fail("检测到您还未注册企业,请前往企业注册!");
  30. }
  31. System.out.println(map);
  32. return Result.succ("调用成功",map);
  33. }
  34. @RequestMapping("/info")
  35. public String info(HttpServletRequest httpServletRequest){
  36. try {
  37. //读取支付回调数据
  38. InputStream inputStream = httpServletRequest.getInputStream();
  39. ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
  40. byte[] bytes=new byte[1024];
  41. int len=0;
  42. while ((len=inputStream.read(bytes))!=-1){
  43. byteArrayOutputStream.write(bytes,0,len);
  44. }
  45. byteArrayOutputStream.close();
  46. inputStream.close();
  47. String res = new String(byteArrayOutputStream.toByteArray(), "UTF-8");
  48. System.out.println(res);
  49. //返回格式必须是这样
  50. Map<String,String> map=new HashMap<>();
  51. map.put("return_code", "SUCCESS");
  52. map.put("return_msg", "OK");
  53. System.out.println("发送成功!");
  54. return WXPayUtil.mapToXml(map);
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. }
  58. return "";
  59. }
  60. }

调用上面的统一下单接口返回的Url在前端转换为二维码即可实现扫码支付亲测可用!

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

闽ICP备14008679号