微信支付

下面来记录下微信支付的学习

微信支付api需要的几样东西如下:

appid:    微信公众号ID

secret:   微信秘钥

mchid:   商户号ID

mchKey:商户号秘钥

 

需要调用第三方api的话,引入依赖是必不可少的啦!

  <!--WXPay api-->
        <dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>weixin-java-pay</artifactId>
            <version>${weixin.version}</version>
        </dependency>
        <dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>weixin-java-miniapp</artifactId>
            <version>${weixin.version}</version>
        </dependency>
        <dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>weixin-java-mp</artifactId>
            <version>${weixin.version}</version>
        </dependency>

        <!--注解器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

下面来准备配置工作

我们在resources下新建几个配置文件,然后引用这些配置文件

ma.properties  

ma.appid=
ma.secret=

pay.properties

pay.mchId=
pay.mchKey=
pay.keyPath=classpath:xxx.p12
pay.keyPath:微信支付签名地址:单支付不需要,如果查询退款的话就需要了......

api.properties
api.datacenterId=
api.workerId=
api.domainName=http://xxx.com
api.domainName:是支付成功调用的地址,因为微信不支持本地地址......公网的就可以......

 

小程序参数类如下:

@Data
@Component
@PropertySource("classpath:ma.properties")
@ConfigurationProperties(prefix = "ma")
public class WxMiniApp {

    /**
     * 设置微信小程序的
     */
    private String appid;

    /**
     * 设置微信小程序的Secret
     */
    private String secret;

}

微信支付参数类如下:

@Data
@Component
@PropertySource("classpath:pay.properties")
@ConfigurationProperties(prefix = "pay")
public class WxPay {

    /**
     * 微信支付mchId
     */
    private String mchId;

    /**
     * 微信支付mchKey
     */
    private String mchKey;

    /**
     * 签名类型
     */
    private String signType;

    /**
     * 支付证书路径
     */
    private String keyPath;

}

下面来编写wxPay Api了

 private final PayService payService;

    private final ApiConfig apiConfig;

    private final WxPayService wxMiniPayService;

    
    /**
     * 微信支付 统一下单
     * <p>
     * WxPayMpOrderResult:微信订单返回结果类,前端根据以下参数发起支付页面
     * appId         小程序Id;
     * timeStamp     时间戳;
     * nonceStr      随机串;
     * packageValue  数据包 统一下单接口返回的prepay_id参数值;
     * signType      签名方式 默认MD5,支持HMAC-SHA256和MD5;
     * paySign       支付标志;
     *
     * @param payParam 支付参数类
     * @return
     */
    @PostMapping("/pay")
    @ApiOperation(value = "根据订单号进行支付", notes = "根据订单号进行支付")
    @SneakyThrows
    public ResponseEntity<WxPayMpOrderResult> pay(@RequestBody PayParam payParam) {
        System.out.println("payParam-->" + payParam);

        YamiUser user = SecurityUtils.getUser();
        String userId = user.getUserId();
        String openId = user.getBizUserId();

        //订单执行支付
        PayInfoDto payInfo = payService.pay(userId, payParam);

        WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
        orderRequest.setBody(payInfo.getBody());
        orderRequest.setOutTradeNo(payInfo.getPayNo());
        orderRequest.setTotalFee((int) Arith.mul(payInfo.getPayAmount(), 100));
        orderRequest.setSpbillCreateIp(IPHelper.getIpAddr());
        //微信支付成功回调地址
        orderRequest.setNotifyUrl(apiConfig.getDomainName() + "/notice/pay/order");
        orderRequest.setTradeType(WxPayConstants.TradeType.JSAPI);
        orderRequest.setOpenid(openId);

        return ResponseEntity.ok(wxMiniPayService.createOrder(orderRequest));
    }

 前端只需要拿到那五个参数就可以调起支付界面了

PayService

  /**
     * 微信支付
     * 不同的订单号,同一个支付流水号
     *
     * @param userId 用户id
     * @param payParam 支付参数类
     * @return
     */
    PayInfoDto pay(String userId, PayParam payParam);

PayService实现层

    /**
     * 微信支付
     * 不同的订单号,同一个支付流水号
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PayInfoDto pay(String userId, PayParam payParam) {
        // 不同的订单号的产品名称
        StringBuilder prodName = new StringBuilder();
        // 支付单号
        String payNo = String.valueOf(snowflake.nextId());
        String[] orderNumbers = payParam.getOrderNumbers().split(StrUtil.COMMA);
        // 修改订单信息
        for (String orderNumber : orderNumbers) {
            OrderSettlement orderSettlement = new OrderSettlement();
            orderSettlement.setPayNo(payNo);
            orderSettlement.setPayType(payParam.getPayType());
            orderSettlement.setUserId(userId);
            orderSettlement.setOrderNumber(orderNumber);
            //根据订单号和用户id修改订单结算信息
            orderSettlementMapper.updateByOrderNumberAndUserId(orderSettlement);

            //根据订单号查询订单
            Order orders = orderMapper.getOrderByOrderNumber(orderNumber);
            prodName.append(orders.getProdName()).append(StrUtil.COMMA);
        }
        
        //根据系统内部的订单号查询订单结算信息
        List<OrderSettlement> settlements = orderSettlementMapper.getSettlementsByPayNo(payNo);
        // 应支付的总金额
        double payAmount = 0.0;
        for (OrderSettlement orderSettlement : settlements) {
            payAmount = Arith.add(payAmount, orderSettlement.getPayAmount());
        }

        prodName.substring(0, Math.min(100, prodName.length() - 1));

        PayInfoDto payInfoDto = new PayInfoDto();
        payInfoDto.setBody(prodName.toString());
        payInfoDto.setPayAmount(payAmount);
        payInfoDto.setPayNo(payNo);
        return payInfoDto;
    }

支付成功回调api

import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.yami.shop.service.PayService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@ApiIgnore
@RestController
@RequestMapping("/notice/pay")
@AllArgsConstructor
public class PayNoticeController {

    /**
     * 小程序支付
     */
    private final WxPayService wxMiniPayService;

    private final PayService payService;


    /**
     * 订单支付回调地址
     *
     * @param xmlData 回调返回信息
     * @return
     * @throws WxPayException
     */
    @RequestMapping("/order")
    public ResponseEntity<Void> submit(@RequestBody String xmlData) throws WxPayException {
        System.out.println("--------------------------------进入支付回调api--------------------------------");
        WxPayOrderNotifyResult parseOrderNotifyResult = wxMiniPayService.parseOrderNotifyResult(xmlData);
        String payNo = parseOrderNotifyResult.getOutTradeNo();
        String bizPayNo = parseOrderNotifyResult.getTransactionId();

        // 根据内部订单号更新order settlement
        payService.paySuccess(payNo, bizPayNo);
        return ResponseEntity.ok().build();
    }
paySuccess就是处理回调的业务处理
比如:更新订单中的各种状态,订单结算信息等等...... 根据支付单号去操作足以

还有一种简单的下单支付方法就是在统一下单支付成功后直接调用本地的api去处理支付成功之后的业务,
这样就不需要支付回调啦,测试阶段可以这么做的,用内网穿透代替下公网也是可以的......



还有哪里不清楚请下方留言哦!
原文地址:https://www.cnblogs.com/ckfeng/p/14953135.html