当前位置:   article > 正文

App微信支付(Java)_binarywang app支付

binarywang app支付

这里使用的是Binary Wang 所写的开源项目 weixin-java-pay

github  Home · Wechat-Group/WxJava Wiki · GitHub

1、导入maven文件

  1. <!--微信支付-->
  2. <dependency>
  3. <groupId>com.github.binarywang</groupId>
  4. <artifactId>wx-java-pay-spring-boot-starter</artifactId>
  5. <version>4.1.0</version>
  6. </dependency>

2、application.yml配置(对应的信息自行去获取,微信支付不需要使用p12证书)

  1. wx:
  2. pay:
  3. appId: wxxxxxxxxxxxxx #微信公众号或者小程序等的appid
  4. mchId: xxxxxxxxx #微信支付商户号
  5. mchKey: xxxxxxxxxxxxxx #微信支付商户密钥
  6. subAppId: #服务商模式下的子商户公众账号ID
  7. subMchId: #服务商模式下的子商户号
  8. keyPath: classpath:/apiclient_cert.p12 # p12证书的位置,可以指定绝对路径,也可以指定类路径(以classpath:开头)

3、WxPayConfiguration配置

将 com.github.binarywang.wxpay.service.WxPayService 作为Bean注入到项目中

  1. /**
  2. * @author Binary Wang
  3. */
  4. @Configuration
  5. @ConditionalOnClass(WxPayService.class)
  6. @EnableConfigurationProperties(WxPayProperties.class)
  7. @AllArgsConstructor
  8. @Slf4j
  9. public class WxPayConfiguration {
  10. private WxPayProperties properties;
  11. @SneakyThrows
  12. @Bean
  13. @ConditionalOnMissingBean
  14. public WxPayService wxService() {
  15. WxPayConfig payConfig = new WxPayConfig();
  16. payConfig.setAppId(StringUtils.trimToNull(this.properties.getAppId()));
  17. payConfig.setMchId(StringUtils.trimToNull(this.properties.getMchId()));
  18. payConfig.setMchKey(StringUtils.trimToNull(this.properties.getMchKey()));
  19. payConfig.setSubAppId(StringUtils.trimToNull(this.properties.getSubAppId()));
  20. payConfig.setSubMchId(StringUtils.trimToNull(this.properties.getSubMchId()));
  21. payConfig.setKeyPath(StringUtils.trimToNull(this.properties.getKeyPath()));
  22. // 可以指定是否使用沙箱环境
  23. payConfig.setUseSandboxEnv(false);
  24. WxPayService wxPayService = new WxPayServiceImpl();
  25. wxPayService.setConfig(payConfig);
  26. return wxPayService;
  27. }
  28. }

4、WxPayProperties配置

  1. import lombok.Data;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. /**
  4. * wxpay pay properties.
  5. *
  6. * @author Binary Wang
  7. */
  8. @Data
  9. @ConfigurationProperties(prefix = "wx.pay")
  10. public class WxPayProperties {
  11. /**
  12. * 设置微信公众号或者小程序等的appid
  13. */
  14. private String appId;
  15. /**
  16. * 微信支付商户号
  17. */
  18. private String mchId;
  19. /**
  20. * 微信支付商户密钥
  21. */
  22. private String mchKey;
  23. /**
  24. * 服务商模式下的子商户公众账号ID,普通模式请不要配置,请在配置文件中将对应项删除
  25. */
  26. private String subAppId;
  27. /**
  28. * 服务商模式下的子商户号,普通模式请不要配置,最好是请在配置文件中将对应项删除
  29. */
  30. private String subMchId;
  31. /**
  32. * apiclient_cert.p12文件的绝对路径,或者如果放在项目中,请以classpath:开头指定
  33. */
  34. private String keyPath;
  35. }

5、IWxPayService(统一下单)

  1. import com.github.binarywang.wxpay.bean.order.WxPayAppOrderResult;
  2. import java.math.BigDecimal;
  3. /**
  4. * @author jiavDad
  5. */
  6. public interface IWxPayService {
  7. WxPayAppOrderResult createDefault(String tradeNo, BigDecimal price, Integer type, String body, Long userId);
  8. }

使用方法:

  1. @Autowired
  2. private IWxPayService wxPayService;
  1. WxPayAppOrderResult wxPayAppOrderResult = wxPayService.createDefault("自己定义的订单号", "金额", "类型", body, "用户id");
  2. (当前支付类型是app,返回类型为WxPayAppOrderResult ,如果是其他支付就用其他类型的)

6、WxPayServiceImpl

  1. /**
  2. * @author jiavDad
  3. */
  4. @Service
  5. @Slf4j
  6. public class WxPayServiceImpl implements IWxPayService {
  7. @Autowired
  8. private WxPayService wxService;
  9. @Value("${wxPay.callbackPath:/}")
  10. private String wxPayPath;
  11. @Override
  12. public WxPayAppOrderResult createDefault(String tradeNo, BigDecimal price, Integer type, String body, Long userId) {
  13. WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
  14. //签名类型
  15. orderRequest.setSignType(WxPayConstants.SignType.MD5);
  16. //终端IP
  17. orderRequest.setSpbillCreateIp(PayUtil.getIp());
  18. //商品描述 例如: 腾讯充值中心-QQ会员充值
  19. orderRequest.setBody(body);
  20. //商户订单号 商户系统内部的订单号,32个字符内、可包含字母
  21. orderRequest.setOutTradeNo(tradeNo);
  22. //回调地址
  23. // orderRequest.setNotifyUrl(wxPayPath + tradeNo + "/" + type);
  24. // Integer type = memberRewardByUserReq.getType().equals(1) ? IWxPayService.Type.REWARDPOST : IWxPayService.Type.REWARDCOMMENT;
  25. orderRequest.setNotifyUrl(wxPayPath + tradeNo + "/" + type + "/" + userId);
  26. //支付类型
  27. orderRequest.setTradeType("APP"); //如果你不是app支付就填其他的
  28. // orderRequest.setOpenid(openId); //app支付不需要openId
  29. orderRequest.setTotalFee(price.multiply(new BigDecimal(100)).intValue());
  30. log.error("支付请求参数:[{}]", orderRequest);
  31. try {
  32. log.error("生成订单参数:[{}]", orderRequest);
  33. WxPayAppOrderResult wxPayAppOrderResult = wxService.createOrder(orderRequest);
  34. return wxPayAppOrderResult;
  35. } catch (WxPayException e) {
  36. log.error("微信支付失败!原因:{}", e.getMessage());
  37. }
  38. throw new RuntimeException("微信支付失败!");
  39. }
  40. }

7、设置支付回调接口的路径 application.yml

  1. wxPay:
  2. callbackPath: http://localhost:8080/api/wxPay/notify/order/

8、WxPayController

  1. import com.github.binarywang.wxpay.exception.WxPayException;
  2. import com.mdframework.module.wxpay.IWxPayService;
  3. import io.swagger.annotations.Api;
  4. import io.swagger.annotations.ApiOperation;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.validation.annotation.Validated;
  8. import org.springframework.web.bind.annotation.*;
  9. /**
  10. * @author jiavDad
  11. */
  12. @Validated
  13. @RestController
  14. @RequestMapping("/wxPay")
  15. @Slf4j
  16. @Api(description = "微信支付", tags = "微信支付")
  17. public class WxPayController {
  18. @Autowired
  19. private IWxPayService wxPayService;
  20. /**
  21. * @param xmlData
  22. * @param type 类型
  23. * @return
  24. * @throws WxPayException
  25. */
  26. @PostMapping("/notify/order/{tradeNo}/{type}/{userId}")
  27. @ResponseBody
  28. @ApiOperation(value = "微信支付回调", notes = "微信支付回调")
  29. public String parseOrderNotifyResult(@RequestBody String xmlData, @PathVariable String tradeNo, @PathVariable Integer type, @PathVariable Long userId) throws WxPayException {
  30. return wxPayService.parseOrderNotifyResult(xmlData, tradeNo, type, userId);
  31. }
  32. }

9、IWxPayService

  1. public interface IWxPayService {
  2. String parseOrderNotifyResult(String xmlData, String tradeNo, Integer type, Long userId);
  3. }

10、WxPayServiceImpl

  1. /**
  2. * @author jiavDad
  3. */
  4. @Service
  5. @Slf4j
  6. public class WxPayServiceImpl implements IWxPayService {
  7. @Autowired
  8. private WxPayService wxService;
  9. @Autowired
  10. private IAdviceService adviceService;
  11. @Value("${wxPay.callbackPath:/}")
  12. private String wxPayPath;
  13. @SneakyThrows
  14. @Override
  15. @Transactional(rollbackFor = Exception.class)
  16. public String parseOrderNotifyResult(String xmlData, String tradeNo, Integer type, Long userId) {
  17. log.info("手机微信支付回调,xml:[{}],tradeNo:[{}],type:[{}],userId:[{}]", xmlData, tradeNo, type, userId);
  18. WxPayOrderNotifyResult notifyResult = wxService.parseOrderNotifyResult(xmlData);
  19. notifyResult.checkResult(wxService, "MD5", true);
  20. String openid = notifyResult.getOpenid();
  21. String transactionId = notifyResult.getTransactionId();
  22. MemberInfo memberInfo = memberInfoService.getById(userId);
  23. AssertHelper.isTrue(Objects.isNull(memberInfo));
  24. /**
  25. /业务逻辑
  26. **/
  27. return WxPayNotifyResponse.success("成功");
  28. }
  29. }
  30. }

11、支付返回结果:wxPayAppOrderResult

可以自行测试返回来的签名是否正确

地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=20_1

 

 注意:app端支付前一定要按照下面的地址配好相应的东西,不然肯定会支付失败!!!!!!

APP端开发步骤icon-default.png?t=M276https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_5

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

闽ICP备14008679号