支付宝订阅消息推送

1、前言

  • 在小程序中用到了支付宝模板消息推送,本来支付宝那边有订阅消息的引导文档的,但是不太适合代码的复用,就把封装成了工具类(可能别的地方已经有了),能够直接复用,有需要的可以借鉴一番

2、支付宝官方文档

AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2");
AlipayOpenAppMiniTemplatemessageSendRequest request = new AlipayOpenAppMiniTemplatemessageSendRequest();
request.setBizContent("{" +
"\"to_user_id\":\"2088102122458832\"," +
"\"form_id\":\"2017010100000000580012345678\"," +
"\"user_template_id\":\"MDI4YzIxMDE2M2I5YTQzYjUxNWE4MjA4NmU1MTIyYmM=\"," +
"\"page\":\"page/component/index\"," +
"\"data\":\"{\\\"keyword1\\\": {\\\"value\\\" : \\\"12:00\\\"},\\\"keyword2\\\": {\\\"value\\\" : \\\"20180808\\\"},\\\"keyword3\\\": {\\\"value\\\" : \\\"支付宝\\\"}}\"" +
"  }");
AlipayOpenAppMiniTemplatemessageSendResponse response = alipayClient.execute(request);
if(response.isSuccess()){
System.out.println("调用成功");
} else {
System.out.println("调用失败");
}

  

  • 可以看出,除了支付宝相关配置,最主要的是bizContent的设置,所以这边也是围绕bizContent去做封装

3、工具类封装

3.1、MessageData

  • 主要用于封装消息体(关键字)
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

/**
 * @author cxq
 * @version : MessageData.java, v 0.1 2020年03月24日 6:57 下午 cxq Exp $
 */
@Data
public class MessageData {
    private Keyword keyword1;
    private Keyword keyword2;
    private Keyword keyword3;
    private Keyword keyword4;
    private Keyword keyword5;
    private Keyword keyword6;
    private Keyword keyword7;
    private Keyword keyword8;
    private Keyword keyword9;
    private Keyword keyword10;

    public void setKeyword1Value(String value) {
        if (StringUtils.isNotBlank(value)) {
            this.keyword1 = new Keyword(value);
        }
    }

    public void setKeyword2Value(String value) {
        if (StringUtils.isNotBlank(value)) {
            this.keyword2 = new Keyword(value);
        }
    }

    public void setKeyword3Value(String value) {
        if (StringUtils.isNotBlank(value)) {
            this.keyword3 = new Keyword(value);
        }
    }

    public void setKeyword4Value(String value) {
        if (StringUtils.isNotBlank(value)) {
            this.keyword4 = new Keyword(value);
        }
    }

    public void setKeyword5Value(String value) {
        if (StringUtils.isNotBlank(value)) {
            this.keyword4 = new Keyword(value);
        }
    }

    public void setKeyword6Value(String value) {
        if (StringUtils.isNotBlank(value)) {
            this.keyword5 = new Keyword(value);
        }
    }

    public void setKeyword7Value(String value) {
        if (StringUtils.isNotBlank(value)) {
            this.keyword7 = new Keyword(value);
        }
    }

    public void setKeyword8Value(String value) {
        if (StringUtils.isNotBlank(value)) {
            this.keyword8 = new Keyword(value);
        }
    }

    public void setKeyword9Value(String value) {
        if (StringUtils.isNotBlank(value)) {
            this.keyword9 = new Keyword(value);
        }
    }

    public void setKeyword10Value(String value) {
        if (StringUtils.isNotBlank(value)) {
            this.keyword10 = new Keyword(value);
        }
    }

    // 格式为{keyword1:xxxx, keyword2:xxxx}
    public MessageData(String jsonStr) {
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        this.setKeyword1Value((String) jsonObject.get("keyword1"));
        this.setKeyword2Value((String) jsonObject.get("keyword2"));
        this.setKeyword3Value((String) jsonObject.get("keyword3"));
        this.setKeyword4Value((String) jsonObject.get("keyword4"));
        this.setKeyword5Value((String) jsonObject.get("keyword5"));
        this.setKeyword6Value((String) jsonObject.get("keyword6"));
        this.setKeyword7Value((String) jsonObject.get("keyword7"));
        this.setKeyword8Value((String) jsonObject.get("keyword8"));
        this.setKeyword9Value((String) jsonObject.get("keyword9"));
        this.setKeyword10Value((String) jsonObject.get("keyword10"));
    }

    public MessageData() {
    }

    @Data
    public static class Keyword {
        public Keyword(String value) {
            this.value = value;
        }

        private String value;
    }

}

 

3.2、获取订阅消息相关数据

  • 小程序相关信息
@Override
    public AlipayOpenAppMiniTemplatemessageSendResponse appMiniTemplateMsgPush(String clubCode, String alipayUid,
                                                                               String userTemplateId, String page, MessageData data) {

        AlipayConfig alipayConfig = this.getAlipayConfigByClub(clubCode);
        MiniTemplateMessage miniTemplateMessage = new MiniTemplateMessage();
        miniTemplateMessage.setTemplateId(userTemplateId);
        miniTemplateMessage.setUid(alipayUid);
        miniTemplateMessage.setPage(page);
        miniTemplateMessage.setData(data);

        return alipayOpenApiClient.appMiniTemplateMsgPush(miniTemplateMessage, alipayConfig);
    }

 

3.3、支付宝API工具类

  • 可封装到支付宝工具类中
/**
     * 模板消息推送
     *
     * @param miniTemplateMessage 推送内容
     * @param alipayInfo
     * @return
     */
    public AlipayOpenAppMiniTemplatemessageSendResponse appMiniTemplateMsgPush(MiniTemplateMessage miniTemplateMessage,
                                                                               AlipayConfig alipayInfo) {
        AlipayOpenAppMiniTemplatemessageSendRequest request = new AlipayOpenAppMiniTemplatemessageSendRequest();
        String bizContent = JSON.toJSONString(miniTemplateMessage);
        request.setBizContent(bizContent);
        return executeRequest(request, alipayInfo);
    }


    private <T extends AlipayResponse> T executeRequest(AlipayRequest request, AlipayConfig alipayInfo) throws AlipayOpenApiException {
        return executeRequest(request, null, alipayInfo);
    }

    private <T extends AlipayResponse> T executeRequest(AlipayRequest request,
                                                        String accessToken, AlipayConfig alipayInfo) throws AlipayOpenApiException {
        AlipayClient alipayClient = alipayClientFactory.getAlipayClient(alipayInfo);

        AlipayResponse response;
        try {
            response = null == accessToken ? alipayClient.execute(request) : alipayClient.execute(request, accessToken);
            if (!response.isSuccess()) {
                throw new AlipayOpenApiException(response, String.format("请求接口(%s),执行失败(%s)",
                        request.getApiMethodName(), response.getMsg()));
            }
            return (T) response;
        } catch (AlipayApiException e) {
            log.error(String.format("请求接口(%s),出现错误!", request.getApiMethodName()), e);
            throw new AlipayOpenApiException(null, String.format("请求接口(%s),出现错误!", request.getApiMethodName()));
        }
    }

 

3.4、测试

@PostMapping("")
    public Boolean testMsgPush(@RequestBody Parameter parameter) {
        log.info("parameter is {}", parameter);
        // 推送内容:活动名称、中奖号、开奖时间
        MessageData data = new MessageData();
        data.setKeyword1Value(parameter.getKeyword1());
        data.setKeyword2Value(parameter.getKeyword2());
        data.setKeyword3Value(parameter.getKeyword3());
        data.setKeyword4Value(parameter.getKeyword4());

        AlipayOpenAppMiniTemplatemessageSendResponse response
                = alipayService.appMiniTemplateMsgPush("manchester", parameter.getAlipayUid(),
                "xxx", "xxx", data);
        log.info("response is {}", response.getSubMsg());

        return true;
    }


@Data
@ToString
class Parameter {
    private String keyword1;
    private String keyword2;
    private String keyword3;
    private String keyword4;
    private String alipayUid;
}

 

3.5、消息截图

 

 

 

 

posted @ 2021-06-30 14:22  想念泡凤爪的味道  阅读(1110)  评论(0编辑  收藏  举报