springboot集成微信支付

pay: wxpay: appID: ****** mchID: ***** key: ***** notifyUrl: ***** appSecret: *****

建立配置类:

@ConfigurationProperties(prefix = "pay.wxpay")
public class MyWXPayConfig implements WXPayConfig {

	private String appSecret;
	
	/** 公众账号ID */
	private String appID;

	/** 商户号 */
	private String mchID;

	/** API 密钥 */
	private String key;

	/** API 沙箱环境密钥 */
	private String sandboxKey;

	/** API证书绝对路径 */
	private String certPath;

	/** 退款异步通知地址 */
	private String notifyUrl;

	private Boolean useSandbox;

	/** HTTP(S) 连接超时时间,单位毫秒 */
	private int httpConnectTimeoutMs = 80000;

	/** HTTP(S) 读数据超时时间,单位毫秒 */
	private int httpReadTimeoutMs = 100000;

	/**
	 * 获取商户证书内容
	 *
	 * @return 商户证书内容
	 */
	@Override
	public InputStream getCertStream() {
		File certFile = new File(certPath);
		InputStream inputStream = null;
		try {
			inputStream = new FileInputStream(certFile);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return inputStream;
	}

	@Override
	public String getKey() {
		if (useSandbox) {
			return sandboxKey;
		}

		return key;
	}

	public String getAppSecret() {
		return appSecret;
	}

	public void setAppSecret(String appSecret) {
		this.appSecret = appSecret;
	}

	public String getAppID() {
		return appID;
	}

	public void setAppID(String appID) {
		this.appID = appID;
	}

	public String getMchID() {
		return mchID;
	}

	public void setMchID(String mchID) {
		this.mchID = mchID;
	}

	public String getSandboxKey() {
		return sandboxKey;
	}

	public void setSandboxKey(String sandboxKey) {
		this.sandboxKey = sandboxKey;
	}

	public String getCertPath() {
		return certPath;
	}

	public void setCertPath(String certPath) {
		this.certPath = certPath;
	}

	public String getNotifyUrl() {
		return notifyUrl;
	}

	public void setNotifyUrl(String notifyUrl) {
		this.notifyUrl = notifyUrl;
	}

	public Boolean getUseSandbox() {
		return useSandbox;
	}

	public void setUseSandbox(Boolean useSandbox) {
		this.useSandbox = useSandbox;
	}

	public int getHttpConnectTimeoutMs() {
		return httpConnectTimeoutMs;
	}

	public void setHttpConnectTimeoutMs(int httpConnectTimeoutMs) {
		this.httpConnectTimeoutMs = httpConnectTimeoutMs;
	}

	public int getHttpReadTimeoutMs() {
		return httpReadTimeoutMs;
	}

	public void setHttpReadTimeoutMs(int httpReadTimeoutMs) {
		this.httpReadTimeoutMs = httpReadTimeoutMs;
	}

	public void setKey(String key) {
		this.key = key;
	}

}

二、建立生成支付二维码的类:

public class PayUtil {

    /**
     * 根据url生成二位图片对象
     *
     * @param codeUrl
     * @return
     * @throws WriterException
     */
    public static BufferedImage getQRCodeImge(String codeUrl) throws WriterException {
        Map<EncodeHintType, Object> hints = new Hashtable();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.CHARACTER_SET, "UTF8");
        int width = 256;
        BitMatrix bitMatrix = (new MultiFormatWriter()).encode(codeUrl, BarcodeFormat.QR_CODE, width, width, hints);
        BufferedImage image = new BufferedImage(width, width, 1);
        for(int x = 0; x < width; ++x) {
            for(int y = 0; y < width; ++y) {
                image.setRGB(x, y, bitMatrix.get(x, y) ? -16777216 : -1);
            }
        }

        return image;
    }
}

三、建立服务类

四、建立controller

其中precreate方法就是创建支付二维码的方法,precreateNotify方法为支付过后的回调地址

经验分享 程序员 微信小程序 职场和发展