Wechat login and payment interface docking arrangement

1. WeChat login

1. Obtain the corresponding openid according to the code

The openid of the same Mini Program user in WeChat is unique, which is an important identifier for determining the identity of the user.

	@ApiModel(value = "微信登陆请求vo")
    public class WxReq {
    
    
        @ApiModelProperty("小程序 appId")
        private String appid;

        @ApiModelProperty("小程序 appSecret")
        private String secret;

        @ApiModelProperty("登录时获取的 code")
        private String js_code;

        @ApiModelProperty("授权类型,此处只需填写 authorization_code")
        private String grant_type = "authorization_code";
    }

    /**
     * 获取openid
     * @param code
     * @return
     */
    public WxRes getOpenidByCode(String code){
    
    
        WxReq req = new WxReq();
        req.setAppid(appid);
        req.setSecret(secret);
        req.setJs_code(code);
        JSONObject object = HttpUtilController.httpGetRequestMethod("https://api.weixin.qq.com/sns/jscode2session",req);
        WxRes res = JSON.parseObject(object.toJSONString(),WxRes.class);
        return res;
    }

The parameter code needs to be obtained by calling the WeChat API from the front end, and each code will be invalid after being used once.
The httpGetRequestMethod method is a self-use general tool class that simulates get calls.

2. Request decryption

If you need to obtain personal information such as the user's mobile phone number, you need to call and obtain it through AES decryption after the user agrees.

    @ApiModel(value = "DecodeVo", description = "微信解密请求实体")
    public class DecodeVo{
    
    

        @ApiModelProperty("加密字符串")
        private String str;

        @ApiModelProperty("微信登录code值")
        private String code;

        @ApiModelProperty("偏移")
        private String vi;
    }

    public String decodeInfo(DecodeVo decodeVo) {
    
    
        //1、获取openid
        WxRes res = getOpenidByCode(decodeVo.getCode());
        String key = res.getSession_key();
        AES aes = new AES(Mode.CBC, Padding.PKCS5Padding, Base64.decode(key), Base64.decode(decodeVo.getVi()));
        String decryptStr = aes.decryptStr(decodeVo.getStr(), CharsetUtil.CHARSET_UTF_8);
        return decryptStr;
    }

AES objects are tool objects in hutool.

2. WeChat payment

Wechat payment here uses a tool class on github, the fragrant yuppie.

        <!-- 微信支付 -->
        <dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>weixin-java-pay</artifactId>
            <version>4.0.0</version>
        </dependency>
1. Unified order interface
	/**
     * 调用统一下单接口,并组装生成支付所需参数对象.
     *
     * @return 返回 {@link com.github.binarywang.wxpay.bean.order}包下的类对象
     */
    public WxPayOrderResult createOrder(WxPayUnifiedOrderRequest request, String openid){
    
    
        WxPayOrderResult wxPayOrderResult = new WxPayOrderResult();
        wxPayOrderResult.setCjsj(LocalDateTime.now());
        try {
    
    
            request.setOpenid(openid);
            request.setSpbillCreateIp(String.valueOf(InetAddress.getLocalHost().getHostAddress()));
            request.setTimeStart(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
            WxPayMpOrderResult payResult = wxService.createOrder(request);
        }catch (Exception e){
    
    
            e.printStackTrace();
        }
    }

    /**
     * 关闭订单
     * @param request
     * @return
     * @throws Exception
     */
    public WxPayOrderCloseResult closeOrder(WxPayOrderCloseRequest request) throws WxPayException {
    
    
        WxPayOrderCloseResult result = this.wxService.closeOrder(request);
        WxPayOrderQueryRequest wxPayOrderQueryRequest = new WxPayOrderQueryRequest();
        wxPayOrderQueryRequest.setOutTradeNo(request.getOutTradeNo());
        queryOrder(wxPayOrderQueryRequest);
        return result;
    }

    /**
     * 查询订单
     * @param request
     * @return
     * @throws Exception
     */
    public WxPayOrderQueryResult queryOrder(WxPayOrderQueryRequest request) throws WxPayException{
    
    
        WxPayOrderQueryResult result = this.wxService.queryOrder(request);
        WxPayOrderResult wxPayOrderResult = wxPayOrderMapper.getByDdbh(request.getOutTradeNo());
        return result;
    }

WxPayUnifiedOrderRequest and WxPayMpOrderResult objects are objects in the tool class, just use them.
Points to note: 1. Orders cannot be generated when the unit price is 0 yuan; 2. Wechat orders only take effect for 2 hours;

Guess you like

Origin blog.csdn.net/qq_16253859/article/details/122237741