#1309 增加发送和查询企业微信红包的接口

This commit is contained in:
酱油99号 2019-12-03 17:09:31 +08:00 committed by Binary Wang
parent c1c801c420
commit d4d830fe3a
9 changed files with 608 additions and 39 deletions

View File

@ -0,0 +1,33 @@
package com.github.binarywang.wxpay.bean.entpay;
import com.github.binarywang.wxpay.bean.request.BaseWxPayRequest;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import lombok.*;
/**
* 红包发送记录查询请求
* @author wuyong
* @date 2019-12-01 17:19
*/
@Data
@EqualsAndHashCode(callSuper = true)
@Builder(builderMethodName = "newBuilder")
@NoArgsConstructor
@AllArgsConstructor
@XStreamAlias("xml")
public class EntPayRedpackQueryRequest extends BaseWxPayRequest {
/**
* 商户订单号
*/
@XStreamAlias("mch_billno")
private String mchBillNo;
@Override
protected void checkConstraints() throws WxPayException {
}
}

View File

@ -0,0 +1,133 @@
package com.github.binarywang.wxpay.bean.entpay;
import com.github.binarywang.wxpay.bean.result.BaseWxPayResult;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
/**
* 红包发送记录查询返回
*
* @author wuyong
* @date 2019-12-01 17:23
*/
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@XStreamAlias("xml")
public class EntPayRedpackQueryResult extends BaseWxPayResult {
/**
* 商户订单号
* 商户使用查询API填写的商户单号的原路返回
*/
@XStreamAlias("mch_billno")
protected String mchBillNo;
/**
* 红包单号
* 使用API发放现金红包时返回的红包单号
*/
@XStreamAlias("detailId")
private String detailId;
/**
* 红包状态
* SENDING:发放
* SENT:
* 已发放待领取
* FAILED发放失败
* RECEIVED:已领取
* RFUND_ING:退款中 REFUND:已退款
*/
@XStreamAlias("status")
private String status;
/**
* 发放类型
* API:通过API接口发放
*/
@XStreamAlias("send_type")
private String sendType;
/**
* 红包金额
* 红包总金额单位分
*/
@XStreamAlias("total_amount")
private Integer totalAmount;
/**
* 失败原因
* 发送失败原因
*/
@XStreamAlias("reason")
private Integer reason;
/**
* 红包发送时间
*/
@XStreamAlias("send_time")
private String sendTime;
/**
* 红包的退款时间
*/
@XStreamAlias("refund_time")
private String refundTime;
/**
* 红包退款金额
*/
@XStreamAlias("refund_amount")
private Integer refundAmount;
/**
* 祝福语
*/
@XStreamAlias("wishing")
private String wishing;
/**
* 备注
*/
@XStreamAlias("remark")
private String remark;
/**
* 活动名称
*/
@XStreamAlias("act_name")
private String actName;
/**
* 领取红包的Openid
*/
@XStreamAlias("openid")
private String openid;
/**
* 金额
*/
@XStreamAlias("amount")
private Integer amount;
/**
* 接收时间
*/
@XStreamAlias("rcv_time")
private Integer rcvTime;
/**
* 发送者名称
*/
@XStreamAlias("sender_name")
private Integer senderName;
/**
* 发送者头像
* 通过企业微信开放接口上传获取
*/
@XStreamAlias("sender_header_media_id")
private Integer senderHeaderMediaId;
}

View File

@ -0,0 +1,147 @@
package com.github.binarywang.wxpay.bean.entpay;
import com.github.binarywang.wxpay.bean.request.BaseWxPayRequest;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import lombok.*;
import me.chanjar.weixin.common.annotation.Required;
/**
* 发送企业红包
* @author wuyong
* @date 2019-12-1
*/
@Data
@EqualsAndHashCode(callSuper = true)
@Builder(builderMethodName = "newBuilder")
@NoArgsConstructor
@AllArgsConstructor
@XStreamAlias("xml")
public class EntPayRedpackRequest extends BaseWxPayRequest {
private static final long serialVersionUID = 1L;
@Override
protected void checkConstraints() throws WxPayException {
}
/**
* 商户订单号每个订单号必须唯一
* 组成mch_id+yyyymmdd+10位一天内不能重复的数字 接口根据商户订单号支持重入如出现超时可再调用
* 必填
*/
@Required
@XStreamAlias("mch_billno")
private String mchBillNo;
/**
* 微信分配的公众账号ID企业微信corpid即为此appId
* 必填
*/
@Required
@XStreamAlias("wxappid")
private String wxAppId;
/**
* 发送者名称
* 以个人名义发红包红包发送者名称(需要utf-8格式)与agentid互斥二者只能填一个
* 必填
*/
@XStreamAlias("sender_name")
private String senderName;
/**
* 发送红包的应用id
* 以企业应用的名义发红包企业应用id整型可在企业微信管理端应用的设置页面查看与sender_name互斥二者只能填一个
* 必填
*/
@XStreamAlias("agentid")
private String agentId;
/**
* 发送者头像
* 发送者头像素材id通过企业微信开放上传素材接口获取
* 必填
*/
@XStreamAlias("sender_header_media_id")
private String senderHeaderMediaId;
/**
* 用户openid
* 接受红包的用户.用户在wxappid下的openid
* 必填
*/
@Required
@XStreamAlias("re_openid")
private String reOpenid;
/**
* 金额
* 单位分单笔最小金额默认为1元
* 必填
*/
@Required
@XStreamAlias("total_amount")
private Integer totalAmount;
/**
* 红包祝福语
* 必填
*/
@Required
@XStreamAlias("wishing")
private String wishing;
/**
* 项目名称
* 必填
*/
@Required
@XStreamAlias("act_name")
private String actName;
/**
* 备注
* 必填
*/
@Required
@XStreamAlias("remark")
private String remark;
/**
* 场景
* 发放红包使用场景红包金额大于200时必传
* PRODUCT_1:商品促销
* PRODUCT_2:抽奖
* PRODUCT_3:虚拟物品兑奖
* PRODUCT_4:企业内部福利
* PRODUCT_5:渠道分润
* PRODUCT_6:保险回馈
* PRODUCT_7:彩票派奖
* PRODUCT_8:税务刮奖
*/
@XStreamAlias("scene_id")
private String sceneId;
@Override
protected boolean ignoreAppid() {
return true;
}
@Override
protected boolean ignoreSubAppId() {
return true;
}
@Override
protected boolean ignoreSubMchId() {
return true;
}
@Override
protected boolean isWxWorkSign() {
return true;
}
}

View File

@ -0,0 +1,80 @@
package com.github.binarywang.wxpay.bean.entpay;
import com.github.binarywang.wxpay.bean.result.BaseWxPayResult;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* 企业微信红包返回
* @author wuyong
* @date 2019-12-01 11:31
*/
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@XStreamAlias("xml")
public class EntPayRedpackResult extends BaseWxPayResult implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 商户订单号
* 商户订单号每个订单号必须唯一组成mch_id+yyyymmdd+10位一天内不能重复的数字
*/
@XStreamAlias("mch_billno")
private String mchBillNo;
/**
* 商户号
* 微信支付分配的商户号
*/
@XStreamAlias("mch_id")
private String mchId;
/**
* 公众账号appid
* 商户appid接口传入的所有appid应该为公众号的appid不能为APP的appid
*/
@XStreamAlias("wxappid")
private String wxAppId;
/**
* 用户openid
* 接受收红包的用户在wxappid下的openid
*/
@XStreamAlias("re_openid")
private String reOpenid;
/**
* 付款金额
* 付款金额单位分
*/
@XStreamAlias("totalAmount")
private String totalAmount;
/**
* 微信单号
* 红包订单的微信单号
*/
@XStreamAlias("sendListid")
private String sendListId;
/**
* 发送者名称
* 红包发送者名称(需要utf-8格式)
*/
@XStreamAlias("sender_name")
private String senderName;
/**
* 发送者头像
* 发送者头像素材id通过企业微信开放上传素材接口获取
*/
@XStreamAlias("sender_header_media_id")
private String senderHeaderMediaId;
}

View File

@ -1,21 +1,20 @@
package com.github.binarywang.wxpay.bean.request;
import java.io.Serializable;
import java.math.BigDecimal;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.util.SignUtils;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import lombok.Data;
import lombok.experimental.Accessors;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.BeanUtils;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
import org.apache.commons.lang3.StringUtils;
import java.io.Serializable;
import java.math.BigDecimal;
import static com.github.binarywang.wxpay.constant.WxPayConstants.SignType.ALL_SIGN_TYPES;
@ -118,6 +117,21 @@ public abstract class BaseWxPayRequest implements Serializable {
@XStreamAlias("sign_type")
private String signType;
/**
* 企业微信签名
*/
@XStreamAlias("workwx_sign")
private String workWxSign;
public String getWorkWxSign() {
return workWxSign;
}
public void setWorkWxSign(String workWxSign) {
this.workWxSign = workWxSign;
}
/**
* 将单位为元转换为单位为分.
*
@ -205,6 +219,26 @@ public abstract class BaseWxPayRequest implements Serializable {
return false;
}
/**
* 签名时是否忽略sub_appid.
*
* @return the boolean
*/
protected boolean ignoreSubAppId() {
return false;
}
protected boolean ignoreSubMchId(){
return false;
}
/**
* 是否是企业微信字段
*/
protected boolean isWxWorkSign(){
return false;
}
/**
* 签名时忽略的参数.
*
@ -238,12 +272,16 @@ public abstract class BaseWxPayRequest implements Serializable {
this.setMchId(config.getMchId());
}
if (StringUtils.isBlank(getSubAppId())) {
this.setSubAppId(config.getSubAppId());
if (!ignoreSubAppId()) {
if (StringUtils.isBlank(getSubAppId())) {
this.setSubAppId(config.getSubAppId());
}
}
if (StringUtils.isBlank(getSubMchId())) {
this.setSubMchId(config.getSubMchId());
if (!ignoreSubMchId()) {
if (StringUtils.isBlank(getSubMchId())) {
this.setSubMchId(config.getSubMchId());
}
}
if (StringUtils.isBlank(getSignType())) {

View File

@ -119,4 +119,28 @@ public interface EntPayService {
* @throws WxPayException the wx pay exception
*/
EntPayBankQueryResult queryPayBank(EntPayBankQueryRequest request) throws WxPayException;
/**
* 企业发送微信红包给个人用户
* <pre>
* 文档地址https://work.weixin.qq.com/api/doc
* 接口地址 https://api.mch.weixin.qq.com/mmpaymkttransfers/sendworkwxredpack
* </pre>
* @param request 请求对象
* @return the wx pay send redpack result
* @throws WxPayException the wx pay exception
*/
EntPayRedpackResult sendEnterpriseRedpack(EntPayRedpackRequest request) throws WxPayException;
/**
* 企业发送微信红包查询
* <pre>
* 文档地址https://work.weixin.qq.com/api/doc
* 接口地址 https://api.mch.weixin.qq.com/mmpaymkttransfers/queryworkwxredpack
* </pre>
* @param request 请求对象
* @return the wx pay send redpack result
* @throws WxPayException the wx pay exception
*/
EntPayRedpackQueryResult queryEnterpriseRedpack(EntPayRedpackQueryRequest request) throws WxPayException;
}

View File

@ -1,35 +1,26 @@
package com.github.binarywang.wxpay.service.impl;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.PublicKey;
import java.security.Security;
import javax.crypto.Cipher;
import com.github.binarywang.wxpay.bean.entpay.*;
import com.github.binarywang.wxpay.bean.request.WxPayDefaultRequest;
import com.github.binarywang.wxpay.bean.result.BaseWxPayResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.EntPayService;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.util.SignUtils;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import com.github.binarywang.wxpay.bean.entpay.EntPayBankQueryRequest;
import com.github.binarywang.wxpay.bean.entpay.EntPayBankQueryResult;
import com.github.binarywang.wxpay.bean.entpay.EntPayBankRequest;
import com.github.binarywang.wxpay.bean.entpay.EntPayBankResult;
import com.github.binarywang.wxpay.bean.entpay.EntPayQueryRequest;
import com.github.binarywang.wxpay.bean.entpay.EntPayQueryResult;
import com.github.binarywang.wxpay.bean.entpay.EntPayRequest;
import com.github.binarywang.wxpay.bean.entpay.EntPayResult;
import com.github.binarywang.wxpay.bean.entpay.GetPublicKeyResult;
import com.github.binarywang.wxpay.bean.request.WxPayDefaultRequest;
import com.github.binarywang.wxpay.bean.result.BaseWxPayResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.EntPayService;
import com.github.binarywang.wxpay.service.WxPayService;
import javax.crypto.Cipher;
import java.io.File;
import java.io.FileReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.PublicKey;
import java.security.Security;
/**
* <pre>
@ -140,6 +131,32 @@ public class EntPayServiceImpl implements EntPayService {
return result;
}
@Override
public EntPayRedpackResult sendEnterpriseRedpack(EntPayRedpackRequest request) throws WxPayException {
//企业微信签名,需要在请求签名之前
request.setNonceStr(String.valueOf(System.currentTimeMillis()));
request.setWorkWxSign(SignUtils.createEntSign(request.getActName(),request.getMchBillNo(),request.getMchId(),request.getNonceStr(),request.getReOpenid(),request.getTotalAmount(),request.getWxAppId(),"Hcf-X_dzLeaTIyK33okGmODK8sLzc7kLrgkWXOAoMbE","MD5"));
request.checkAndSign(this.payService.getConfig());
String url = this.payService.getPayBaseUrl() + "/mmpaymkttransfers/sendworkwxredpack";
String responseContent = this.payService.post(url, request.toXML(), true);
final EntPayRedpackResult result = BaseWxPayResult.fromXML(responseContent, EntPayRedpackResult.class);
result.checkResult(this.payService, request.getSignType(), true);
return result;
}
@Override
public EntPayRedpackQueryResult queryEnterpriseRedpack(EntPayRedpackQueryRequest request) throws WxPayException {
request.checkAndSign(this.payService.getConfig());
String url = this.payService.getPayBaseUrl() + "/mmpaymkttransfers/queryworkwxredpack";
String responseContent = this.payService.post(url, request.toXML(), true);
final EntPayRedpackQueryResult result = BaseWxPayResult.fromXML(responseContent, EntPayRedpackQueryResult.class);
result.checkResult(this.payService, request.getSignType(), true);
return result;
}
private String encryptRSA(File publicKeyFile, String srcString) throws WxPayException {
try {
Security.addProvider(new BouncyCastleProvider());

View File

@ -91,7 +91,7 @@ public class SignUtils {
if (shouldSign) {
toSign.append(key).append("=").append(value).append("&");
}
}
}
toSign.append("key=").append(signKey);
@ -102,6 +102,49 @@ public class SignUtils {
}
}
/**
* 企业微信签名
* @param signType md5 目前接口要求使用的加密类型
*/
public static String createEntSign(String actName,String mchBillNo,String mchId,String nonceStr,
String reOpenid,Integer totalAmount,String wxAppId,String signKey,
String signType){
Map<String, String> sortedMap = new HashMap<>();
sortedMap.put("act_name",actName);
sortedMap.put("mch_billno",mchBillNo);
sortedMap.put("mch_id",mchId);
sortedMap.put("nonce_str",nonceStr);
sortedMap.put("re_openid",reOpenid);
sortedMap.put("total_amount", totalAmount + "");
sortedMap.put("wxappid",wxAppId);
Map<String, String> sortParams = new TreeMap<>(sortedMap);
Set<Map.Entry<String, String>> entries = sortParams.entrySet();
Iterator<Map.Entry<String, String>> iterator = entries.iterator();
StringBuilder toSign = new StringBuilder();
while(iterator.hasNext()){
Map.Entry entry = iterator.next();
String key = String.valueOf(entry.getKey());
String value = String.valueOf(entry.getValue());
boolean shouldSign = false;
if (StringUtils.isNotEmpty(value)) {
shouldSign = true;
}
if (shouldSign) {
toSign.append(key).append("=").append(value).append("&");
}
}
//企业微信这里字段名不一样
toSign.append("secret=").append(signKey);
if (SignType.HMAC_SHA256.equals(signType)) {
return me.chanjar.weixin.common.util.SignUtils.createHmacSha256Sign(toSign.toString(), signKey);
} else {
return DigestUtils.md5Hex(toSign.toString()).toUpperCase();
}
}
/**
* 校验签名是否正确.
*

View File

@ -1,8 +1,6 @@
package com.github.binarywang.wxpay.service.impl;
import com.github.binarywang.wxpay.bean.entpay.EntPayBankRequest;
import com.github.binarywang.wxpay.bean.entpay.EntPayBankResult;
import com.github.binarywang.wxpay.bean.entpay.EntPayRequest;
import com.github.binarywang.wxpay.bean.entpay.*;
import com.github.binarywang.wxpay.constant.WxPayConstants.CheckNameOption;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
@ -10,7 +8,10 @@ import com.github.binarywang.wxpay.testbase.ApiTestModule;
import com.google.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.*;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
/**
* <pre>
@ -94,4 +95,57 @@ public class EntPayServiceImplTest {
public void testQueryPayBank() throws Exception {
this.logger.info(this.payService.getEntPayService().queryPayBank("123").toString());
}
/**
* 发送企业红包
* @throws Exception the exception
*/
@Test
public void testSendEnterpriseRedpack() {
EntPayRedpackRequest request = new EntPayRedpackRequest();
request.setMchId("1");
//商户单号
request.setMchBillNo(request.getMchId()+"20191202"+"1");
//企业微信corpid即为此appId
request.setWxAppId("1");
// request.setSenderName("1");
// request.setSenderHeaderMediaId("2");
request.setAgentId("1");
request.setReOpenid("1");
//目前企业微信api红包最低1块钱
request.setTotalAmount(1000);
request.setWishing("1");
request.setActName("1");
request.setRemark("1");
EntPayRedpackResult redpackResult = null;
try {
redpackResult = this.payService.getEntPayService().sendEnterpriseRedpack(request);
} catch (WxPayException e) {
}
this.logger.info(redpackResult.toString());
}
/**
* 查询企业红包
* @throws Exception
*/
@Test
public void testQueryEnterpriseRedpack() throws Exception {
while (true) {
EntPayRedpackQueryRequest request = new EntPayRedpackQueryRequest();
request.setAppid("1");
request.setMchId("1");
request.setMchBillNo("1");
try {
EntPayRedpackQueryResult result = this.payService.getEntPayService().queryEnterpriseRedpack(request);
this.logger.info(result.toString());
} catch (Exception e) {
}
TimeUnit.SECONDS.sleep(3);
}
}
}