100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 微信企业支付--用户提现到微信钱包余额Java实现

微信企业支付--用户提现到微信钱包余额Java实现

时间:2021-05-13 17:54:40

相关推荐

微信企业支付--用户提现到微信钱包余额Java实现

参考文档:【微信支付】付款开发者文档

1. 实体类(入参,结果类)

import io.swagger.annotations.ApiModel;import io.swagger.annotations.ApiModelProperty;import lombok.Data;import org.hibernate.validator.constraints.Length;/*** 提现到微信钱包入参dto* @author ljchen*/@Data@ApiModel(value = "WithdrawalParamDto", description = "提现入参dto")public class WithdrawalParamDto {@ApiModelProperty(value = "收款用户姓名: re_user_name", required = true)String reUserName;@ApiModelProperty(value = "付款金额,单位为分: amount", required = true)String amount;@ApiModelProperty(value = "付款备注: desc")@Length(max = 100)String desc;}

import io.swagger.annotations.ApiModel;import io.swagger.annotations.ApiModelProperty;import lombok.Data;/*** @author ljchen*/@ApiModel(value = "TransferResultDto")@Datapublic class TransferResultDto {@ApiModelProperty(value = "转账结果:成功或失败(SUCCESS/FAIL)")String resultCode;@ApiModelProperty(value = "商户转账订单号")String partnerTradeNo;@ApiModelProperty(value = "微信订单号")String paymentNo;@ApiModelProperty(value = "微信支付成功时间")String paymentTime;@ApiModelProperty(value = "错误代码")String errorCode;@ApiModelProperty(value = "错误代码描述")String errorCodeDes;}

2. Service 实现:

private String DESC_STR = "提现到微信钱包";private String DEFAULT_IP_ADDR = "127.0.0.1";private String DEFAULT_CHECK_NAME = "FORCE_CHECK";@ResourceWxPayV3Bean wxPayV3Bean;/*** 微信提现* @return* @throws Exception*/public TransferResultDto wxWithdrawalTransferSample(WithdrawalParamDto dto, HttpServletRequest request) throws Exception {TransferResultDto resultDto = new TransferResultDto();//封装提现请求参数mapSortedMap<String, String> packageParams = handleInputParamMap(dto, request);//证书路径"apiclient_cert.p12"String cerPath = wxPayV3Bean.getCertP12Path();//证书密码, 商户号 mchidString cerPass = wxPayV3Bean.getMchId();//调用提现请求接口,返回结果是xml格式String resultXml = WxPayApi.transfers(packageParams, cerPath, cerPass);System.out.println(resultXml);//处理微信提现接口返回结果resultDto = handleTransferResultDto(resultXml);String resultCode = resultDto.getResultCode();if (StringConstants.STRING_SUCCESS.equals(resultCode)) {//todo 提现成功具体业务逻辑}else {//todo 提现失败具体业务逻辑}return resultDto;}private SortedMap<String, String> handleInputParamMap(WithdrawalParamDto dto, HttpServletRequest request) throws Exception {SortedMap<String, String> packageParams = new TreeMap<String, String>();//1.0 拼凑企业支付需要的参数//微信小程序的appidString mchAppId = wxPayV3Bean.getMiniAppId();//商户号String mchId = wxPayV3Bean.getMchId();//生成随机数String nonceStr = WXPayUtil.generateNonceStr();//生成商户订单号String partnerTradeNo = RandomNumber.getRandom();//get current user open idUser user = userService.getUserById(UserUtils.getCurrentUserId());if (user == null) {throw new DealException("当前用户不存在,请联系客服");}String openid = user.getOpenId();//是否验证真实姓名呢:NO_CHECK:不校验真实姓名; FORCE_CHECK:强校验真实姓名String checkName = DEFAULT_CHECK_NAME;String reUserName = dto.getReUserName();verifyStrNull(reUserName, "收款用户真实姓名");//企业付款金额,单位为分String amount = dto.getAmount();int amountInt = NumberUtil.mul(amount, "100").intValue();//企业付款操作说明信息, 默认为提现到微信零钱String desc = dto.getDesc();if (StringUtils.isEmpty(desc)) {desc = DESC_STR;}//如何获取调用接口的机器ipString spbillCreateIp = request.getRemoteHost();if (StringUtils.isEmpty(spbillCreateIp)) {spbillCreateIp = DEFAULT_IP_ADDR;}//2.0 生成map集合//微信小程序的appidpackageParams.put("mch_appid", mchAppId);//商户号packageParams.put("mchid", mchId);//随机生成后数字,保证安全性packageParams.put("nonce_str", nonceStr);//生成商户订单号packageParams.put("partner_trade_no", partnerTradeNo);// 支付给用户openidpackageParams.put("openid", openid);//是否验证真实姓名呢packageParams.put("check_name", checkName);//收款用户姓名packageParams.put("re_user_name", reUserName);//企业付款金额,单位为分packageParams.put("amount", String.valueOf(amountInt));//企业付款操作说明信息。必填。packageParams.put("desc", desc);//调用接口的机器Ip地址packageParams.put("spbill_create_ip", spbillCreateIp);//3.0 生成自己的签名String sign = WXPayUtil.generateSignature(packageParams, wxPayV3Bean.getApiKey());//封装入参mappackageParams.put("sign", sign);return packageParams;}private TransferResultDto handleTransferResultDto(String resultXml) throws Exception {TransferResultDto resultDto = new TransferResultDto();Map<String, String> resultMap = WXPayUtil.xmlToMap(resultXml);if (CollectionUtil.isNotEmpty(resultMap) && StringConstants.STRING_SUCCESS.equals(resultMap.get("result_code"))) {log.info("转账成功");resultDto.setResultCode(resultMap.get("result_code"));// 商户转账订单号resultDto.setPartnerTradeNo(resultMap.get("partner_trade_no"));// 微信订单号resultDto.setPaymentNo(resultMap.get("payment_no"));// 微信支付成功时间resultDto.setPaymentTime(resultMap.get("payment_time"));} else {if (CollectionUtil.isNotEmpty(resultMap)) {log.info("转账失败:" + resultMap.get("err_code") + ":" + resultMap.get("err_code_des"));}resultDto.setResultCode(resultMap.get("result_code"));//错误代码resultDto.setErrorCode(resultMap.get("err_code"));//错误代码描述resultDto.setErrorCodeDes(resultMap.get("err_code_des"));}return resultDto;}

3.WXPayUtil 为微信支付sdk提供的工具类

参考地址:https://pay./wiki/doc/api/download/WxPayAPI_JAVA.zip

4.WxPayV3Bean 类

import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.PropertySource;import org.ponent;@Component@PropertySource("classpath:/wxpay_v3.properties")@ConfigurationProperties(prefix = "v3")public class WxPayV3Bean {private String appId;private String keyPath;private String certPath;private String certP12Path;private String platformCertPath;private String mchId;private String apiKey;private String apiKey3;private String domain;private String miniAppId;public String getAppId() {return appId;}public void setAppId(String appId) {this.appId = appId;}public String getKeyPath() {return keyPath;}public void setKeyPath(String keyPath) {this.keyPath = keyPath;}public String getCertPath() {return certPath;}public void setCertPath(String certPath) {this.certPath = certPath;}public String getCertP12Path() {return certP12Path;}public void setCertP12Path(String certP12Path) {this.certP12Path = certP12Path;}public String getPlatformCertPath() {return platformCertPath;}public void setPlatformCertPath(String platformCertPath) {this.platformCertPath = platformCertPath;}public String getMchId() {return mchId;}public void setMchId(String mchId) {this.mchId = mchId;}public String getApiKey() {return apiKey;}public void setApiKey(String apiKey) {this.apiKey = apiKey;}public String getApiKey3() {return apiKey3;}public void setApiKey3(String apiKey3) {this.apiKey3 = apiKey3;}public String getDomain() {return domain;}public void setDomain(String domain) {this.domain = domain;}@Overridepublic String toString() {return "WxPayV3Bean{" +"keyPath='" + keyPath + '\'' +", certPath='" + certPath + '\'' +", certP12Path='" + certP12Path + '\'' +", platformCertPath='" + platformCertPath + '\'' +", mchId='" + mchId + '\'' +", apiKey='" + apiKey + '\'' +", apiKey3='" + apiKey3 + '\'' +", domain='" + domain + '\'' +'}';}public String getMiniAppId() {return miniAppId;}public void setMiniAppId(String miniAppId) {this.miniAppId = miniAppId;}}

5. 结果:XML

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。