All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zopen.wechat.pay.dto.JsApiPayClientDto Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
package com.zopen.wechat.pay.dto;

import com.zopen.wechat.pay.util.SignUtil;

import java.util.Map;
import java.util.TreeMap;

/**
 * 小程序/微信浏览器H5 统一下单后,需要传给 小程序/H5客户端 的内容
 *
 * @author [email protected]
 * @since 2020/5/6
 */
public class JsApiPayClientDto {

    // 微信分配的小程序ID
    private String appId;

    // 时间戳:1490840662
    private String timeStamp;

    // 随机串
    private String nonceStr;

    // 数据包:prepay_id=wx2017033010242291fcfe0db70013231072
    private String packageValue;

    // 签名方式
    private String signType;

    // 签名
    private String paySign;

    public static JsApiPayClientDto formPayResponse(PayResponse payResponse, String appKey) {
        if (payResponse == null || !payResponse.success()) {
            return null;
        }
        JsApiPayClientDto dto = new JsApiPayClientDto();
        dto.setAppId(payResponse.getAppid());
        dto.setTimeStamp(String.valueOf((int) (System.currentTimeMillis() / 1000)));
        dto.setNonceStr(payResponse.getNonce_str());
        dto.setPackageValue("prepay_id=" + payResponse.getPrepay_id());
        dto.setSignType("MD5");

        Map treeMap = new TreeMap<>();
        treeMap.put("appId", dto.getAppId());
        treeMap.put("nonceStr", dto.getNonceStr());
        treeMap.put("package", dto.getPackageValue());
        treeMap.put("signType", dto.getSignType());
        treeMap.put("timeStamp", dto.getTimeStamp());
        String sign = SignUtil.sign(treeMap, appKey);
        dto.setPaySign(sign);

        return dto;
    }

    @Override
    public String toString() {
        return "JsApiPayClientDto{" +
                "appId='" + appId + '\'' +
                ", timeStamp='" + timeStamp + '\'' +
                ", nonceStr='" + nonceStr + '\'' +
                ", packageValue='" + packageValue + '\'' +
                ", signType='" + signType + '\'' +
                ", paySign='" + paySign + '\'' +
                '}';
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getTimeStamp() {
        return timeStamp;
    }

    public String getPaySign() {
        return paySign;
    }

    public void setPaySign(String paySign) {
        this.paySign = paySign;
    }

    public void setTimeStamp(String timeStamp) {
        this.timeStamp = timeStamp;
    }

    public String getNonceStr() {
        return nonceStr;
    }

    public void setNonceStr(String nonceStr) {
        this.nonceStr = nonceStr;
    }

    public String getPackageValue() {
        return packageValue;
    }

    public void setPackageValue(String packageValue) {
        this.packageValue = packageValue;
    }

    public String getSignType() {
        return signType;
    }

    public void setSignType(String signType) {
        this.signType = signType;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy