微信小程序提现实现(企业付款到零钱)

x33g5p2x  于2022-02-12 转载在 其他  
字(7.4k)|赞(0)|评价(0)|浏览(4416)

需求

在微信小程序中实现一个提现功能 ,提现的钱其实来自系统中的其他功能。

当然这个功能,需要一个微信支付的商户号,并且账户中需要有充足的余额。

前期准备

1、登录微信商户平台官网:微信支付 - 中国领先的第三方支付平台 | 微信支付提供安全快捷的支付方式 (qq.com)

2、找到产品中心的企业付款到零钱,并开通企业付款到零钱功能

3、找到AppID账号管理,添加关联AppID

4、将微信商户号和小程序进行绑定

5、申请API证书和APIv2密钥,妥善保存,后面会使用

注:API证书文件的后缀是.p12

6、获取商户号

代码实现(SpringBoot)

1、引入相关官方依赖

<dependency>
    <groupId>com.github.wxpay</groupId>
    <artifactId>wxpay-sdk</artifactId>
    <version>0.0.3</version>
</dependency>

2、在yaml文件添加配置

withDraw:
	apiKey: xxxxxxxx
     mchid: xxxxxx
     mch_appid: xxxxxxxxx
     //此路径代表是在项目的resouce根目录
     certPath: /xxxxxxxx.p12
     count: 10
     quota: 200

3、编写配置类

@Component
public class WithDrawConfig implements WXPayConfig {
    //从yaml注入配置
	@Value("${withDraw.mch_appid}")
	private String mch_appid;
	@Value("${withDraw.apiKey}")
	private String apiKey;
	@Value("${withDraw.mchid}")
	private String mchid;
	@Value("${withDraw.certPath}")
	private String certPath;

	@Override
	public String getAppID() {
		return mch_appid;
	}

	@Override
	public String getMchID() {
		return mchid;
	}

	@Override
	public String getKey() {
		return apiKey;
	}

	@Override
	public InputStream getCertStream() {
		//获取证书,证书建议放到resource目录下
		return this.getClass().getResourceAsStream(certPath);
	}

	@Override
	public int getHttpConnectTimeoutMs() {
		return 8000;
	}

	@Override
	public int getHttpReadTimeoutMs() {
		return 10000;
	}
}

4、编写提现工具类

@Slf4j
@Component
@RequiredArgsConstructor
public class WithDrawUtils {
	@Value("${withDraw.mch_appid}")
	private String mch_appid;

	@Value("${withDraw.mchid}")
	private String mchid;

	@Value("${withDraw.apiKey}")
	private String apiKey;

	private final WithDrawConfig withDrawConfig;

    //生成订单号日期时间+随机字符
	public String getOrderNumber() {
		SimpleDateFormat date = new SimpleDateFormat("yyyyMMddHHmmss");
		return date.format(new Date()) + RandomStringUtils.randomNumeric(4);
	}

	public Map<String, String> fillRequest(Map<String, String> reqData) throws Exception {
		reqData.put("mch_appid", mch_appid);
		reqData.put("mchid", mchid);
		reqData.put("nonce_str", WXPayUtil.generateNonceStr().toUpperCase());
		reqData.put("sign", WXPayUtil.generateSignature(reqData, apiKey, WXPayConstants.SignType.MD5));
		return reqData;
	}

	public String getMd5ByString(String str) {
		StringBuilder hexString = new StringBuilder();
		try {
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(str.getBytes());
			byte[] hash = mdTemp.digest();
			for (byte b : hash) {
				if ((0xff & b) < 0x10) {
					hexString.append("0").append(Integer.toHexString((0xFF & b)));
				} else {
					hexString.append(Integer.toHexString(0xFF & b));
				}
			}
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return hexString.toString();
	}

	/**
	 * 将对象直接转换成String类型的 XML输出
	 *
	 * @param obj
	 * @return
	 */
	public String convertToXml(Object obj) {
		// 创建输出流
		StringWriter sw = new StringWriter();
		try {
			// 利用jdk中自带的转换类实现
			JAXBContext context = JAXBContext.newInstance(obj.getClass());

			Marshaller marshaller = context.createMarshaller();
			// 格式化xml输出的格式
			marshaller.setProperty(Marshaller.JAXB_FRAGMENT,
					Boolean.TRUE);
			// 将对象转换成输出流形式的xml
			marshaller.marshal(obj, sw);
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		return sw.toString();
	}

	public String getRestInstance(String url, String data) throws Exception {
		String UTF8 = "UTF-8";
		URL httpUrl = new URL(url);
		char[] password = mchid.toCharArray();//证书密码
		InputStream certStream = withDrawConfig.getCertStream();//获取证书的流
		KeyStore ks = KeyStore.getInstance("PKCS12");
		ks.load(certStream, password);
		// 实例化密钥库 & 初始化密钥工厂
		KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
		kmf.init(ks, password);
		SSLContext sslContext = SSLContext.getInstance("TLS");
		sslContext.init(kmf.getKeyManagers(), (TrustManager[]) null, new SecureRandom());
		HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
		HttpURLConnection httpURLConnection = (HttpURLConnection) httpUrl.openConnection();
		httpURLConnection.setDoOutput(true);
		httpURLConnection.setRequestMethod("POST");
		httpURLConnection.setConnectTimeout(withDrawConfig.getHttpConnectTimeoutMs());
		httpURLConnection.setReadTimeout(withDrawConfig.getHttpReadTimeoutMs());
		httpURLConnection.connect();
		OutputStream outputStream = httpURLConnection.getOutputStream();
		outputStream.write(data.getBytes(UTF8));
		InputStream inputStream = httpURLConnection.getInputStream();
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, UTF8));
		StringBuilder stringBuffer = new StringBuilder();
		String line;

		while ((line = bufferedReader.readLine()) != null) {
			stringBuffer.append(line);
		}

		String resp = stringBuffer.toString();
		try {
			bufferedReader.close();
		} catch (IOException ignored) {
		}

		try {
			inputStream.close();
		} catch (IOException ignored) {
		}

		try {
			outputStream.close();
		} catch (IOException ignored) {
		}

		if (certStream != null) {
			try {
				certStream.close();
			} catch (IOException ignored) {
			}
		}
		return resp;
	}
}

5、提现的业务实现

官方接口文档:【微信支付】付款开发者文档 (qq.com)

//开始提现,生成订单号
String orderNumber = withDrawUtils.getOrderNumber();
//自定义的将提现所需要的参数封装的实体类
WithDrawDTO withDrawDTO = new WithDrawDTO();
withDrawDTO.setPartner_trade_no(orderNumber);
withDrawDTO.setDesc("xxxxx");
withDrawDTO.setAmount(appletWithDrawDTO.getAmount());
//此参数代表,开启真实姓名校验,也可以关闭,详看官方文档的参数说明
withDrawDTO.setCheck_name("FORCE_CHECK");
withDrawDTO.setRe_user_name(appletWithDrawDTO.getName());
//微信小程序用户的openid
withDrawDTO.setOpenid(wxUser.getOpenid());
Map<String, String> params = JSON.parseObject(JSON.toJSONString(withDrawDTO), new TypeReference<Map<String, String>>() {
});
params = withDrawUtils.fillRequest(params);
withDrawDTO.setNonce_str(params.get("nonce_str"));
withDrawDTO.setMchid(params.get("mchid"));
withDrawDTO.setMch_appid(params.get("mch_appid"));
withDrawDTO.setSign(params.get("sign"));
String url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
String post = withDrawUtils.getRestInstance(url, withDrawUtils.convertToXml(withDrawDTO));
Map<String, String> result = WXPayUtil.xmlToMap(post);
//result为调用接口之后的返回参数,可以根据返回参数判断是否成功

WithDrawEnum resultEnum;
if ("SUCCESS".equals(result.get("result_code"))) {
    //提现成功
    resultEnum = WithDrawEnum.fromText("SUCCESS");
    return resultEnum;
} else {
    //提现失败
    resultEnum = WithDrawEnum.fromText(result.get("err_code"));
    throw new BadRequestException(resultEnum);
}

6、提现返回错误代码的枚举类

@Getter
@NoArgsConstructor
@AllArgsConstructor
public enum WithDrawEnum {
	SUCCESS(0, "提现成功"),
	FAIL(400, "提现失败,余额小于提现金额"),
	HANDLE_FRE(5000, "操作太频繁"),
	NO_AUTH(5001, "没有该接口权限"),
	AMOUNT_LIMIT(5002, "金额超限"),
	PARAM_ERROR(5003, "参数错误"),
	OPENID_ERROR(5004, "Openid错误"),
	SEND_FAILED(5005, "付款错误"),
	NOTENOUGH(5006, "余额不足"),
	SYSTEMERROR(5007, "系统繁忙,请稍后再试。"),
	NAME_MISMATCH(5008, "姓名校验出错"),
	SIGN_ERROR(5009, "签名错误"),
	XML_ERROR(5010, "发送内容出错"),
	FATAL_ERROR(5011, "两次发送参数不一致"),
	FREQ_LIMIT(5012, "超过频率限制,请稍后再试。"),
	MONEY_LIMIT(5013, "已经达到今日付款总额上限/已达到付款给此用户额度上限"),
	CA_ERROR(5014, "商户证书校验出错"),
	V2_ACCOUNT_SIMPLE_BAN(5015, "无法给未实名用户付款"),
	PARAM_IS_NOT_UTF8(5016, "发送参数中包含不规范字符"),
	SENDNUM_LIMIT(5017, "该用户今日付款次数超过限制, 如有需要请进入【微信支付商户平台-产品中心-付款到零钱-产品设置】进行修改"),
	RECV_ACCOUNT_NOT_ALLOWED(5018, "收款账户不在收款账户列表"),
	PAY_CHANNEL_NOT_ALLOWED(5019, "本商户号未配置此功能"),
	SEND_MONEY_LIMIT(5020, "已达到今日商户付款额度上限"),
	RECEIVED_MONEY_LIMIT(5021, "已达到今日付款给此用户额度上限");
	private int code;
	private String msg;

	public static WithDrawEnum fromText(String text) {
		if (text != null) {
			for (WithDrawEnum b : WithDrawEnum.values()) {
				if (text.equalsIgnoreCase(b.name())) {
					return b;
				}
			}
		}
		return null;
	}
}

最后

微信相关接口的调用,主要还是要耐心看文档的说明,才能实现业务需求。

相关文章