SpringBoot对接微信H5支付demo

package com.L.Wx.controller;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.exception.ServiceException;
import com.wechat.pay.java.service.payments.h5.H5Service;
import com.wechat.pay.java.service.payments.h5.model.*;
import com.wechat.pay.java.service.payments.model.Transaction;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("wechat/h5")
public class H5PayController {

    public static H5Service service;

    /**
     * 商户号
     */
    public static final String merchantId = "";
    /**
     * 商户API私钥路径
     */
    public static final String privateKeyPath = "";
    /**
     * 商户证书序列号
     */
    public static final String merchantSerialNumber = "";
    /**
     * 商户APIV3密钥
     */
    public static final String apiV3key = "";

    /**
     * 下单接口
     * 下单以后我们不需要做任何操作了,只需要把微信给的url传给前端,然后等待回调接口接收微信支付接口的响应即可
     * 后续操作到WechatPayCallBack这个回调类里面操作即可
     * */
    @PostMapping("/pay")
    public PrepayResponse H5Pay(Integer total, String appid, String mchid, String description, String callback) {
        Config config =
                new RSAAutoCertificateConfig.Builder()
                        .merchantId(merchantId)
                        .privateKeyFromPath(privateKeyPath)
                        .merchantSerialNumber(merchantSerialNumber)
                        .apiV3Key(apiV3key)
                        .build();
        service = new H5Service.Builder().config(config).build();
        // request.setXxx(val)设置所需参数,具体参数可见Request定义
        PrepayRequest request = new PrepayRequest();
        Amount amount = new Amount();
        /** 总金额 说明:订单总金额,单位为分 */
        amount.setTotal(total);
        request.setAmount(amount);

        /** 公众号ID 说明:公众号ID */
        request.setAppid(appid);

        /** 直连商户号 说明:直连商户号 */
        request.setMchid(mchid);

        /** 商品描述 说明:商品描述 */
        request.setDescription(description);

        /** 通知地址 说明:有效性:1. HTTPS;2. 不允许携带查询串。 */
        //todo 这个通知的地址是在支付以后微信支付后台给我们发消息的地址(或接口),接到消息后再返回结果
        request.setNotifyUrl(callback);

        /** 商户订单号 说明:商户订单号 */
        request.setOutTradeNo("out_trade_no_001");
        /**这里是场景信息,有时候会出校验失败的问题,这里为了测试流通就加上了,本不是必填字段*/
        SceneInfo sceneInfo = new SceneInfo();
        //这里是客户端的IP
        sceneInfo.setPayerClientIp("14.23.150.211 ");
        request.setSceneInfo(sceneInfo);
        //调用支付方法发送给微信支付后台,返回url,这个url需要传给前端进行支付的页面跳转
        PrepayResponse response = service.prepay(request);
        /** h5Url 说明:支付跳转链接 */
        String h5Url = response.getH5Url();
        System.out.println("h5Url = " + h5Url);
        return response;
    }


    /**
     * 商户订单号查询订单
     */
    @PostMapping("/selectByOutTradeNo")
    public static Transaction queryOrderByOutTradeNo(String mchid, String transactionId) {

        QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
        request.setMchid(mchid);
        request.setOutTradeNo(transactionId);

        Transaction result = null;
        try {
            result = service.queryOrderByOutTradeNo(request);
        } catch (ServiceException e) {
            // API返回失败, 例如ORDER_NOT_EXISTS
            System.out.printf("code=[%s], message=[%s]\n", e.getErrorCode(), e.getErrorMessage());
            System.out.printf("reponse body=[%s]\n", e.getResponseBody());
        }
        return result;
    }


    /**
     * 微信支付订单号查询订单
     */
    @PostMapping("/selectById")
    public static Transaction queryOrderById() {

        QueryOrderByIdRequest request = new QueryOrderByIdRequest();
        // 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
        // 调用接口
        request.setMchid("190000****");
        request.setTransactionId("4200001569202208304701234567");

        try {
            Transaction result = service.queryOrderById(request);
            System.out.println(result.getTradeState());
        } catch (ServiceException e) {
            // API返回失败, 例如ORDER_NOT_EXISTS
            System.out.printf("code=[%s], message=[%s]\n", e.getErrorCode(), e.getErrorMessage());
            System.out.printf("reponse body=[%s]\n", e.getResponseBody());
        }
        return service.queryOrderById(request);
    }


    /**
     * 关闭订单
     */
    @PostMapping("/closeOrder")
    public static void CloseOrder(String mchid, String transactionId) {
        CloseOrderRequest closeRequest = new CloseOrderRequest();
        closeRequest.setMchid("190000****");
        closeRequest.setOutTradeNo("out_trade_no_001");
        // 方法没有返回值,意味着成功时API返回204 No Content
        service.closeOrder(closeRequest);
    }


}

引入jar包

    <!--        微信支付-->
        <dependency>
            <groupId>com.github.wechatpay-apiv3</groupId>
            <artifactId>wechatpay-java</artifactId>
            <version>0.2.5</version>
        </dependency>

这里一定要注意,这个包里的okhttp版本要和springboot版本对应上,否则会报错

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值