源码资源网 发表于 2025-4-15 11:58:30

支付宝订阅消息推送

支付宝订阅消息推送
1、前言
[*]在小程序中用到了支付宝模板消息推送,本来支付宝那边有订阅消息的引导文档的,但是不太适合代码的复用,就把封装成了工具类(可能别的地方已经有了),能够直接复用,有需要的可以借鉴一番
2、支付宝官方文档
[*]https://opendocs.alipay.com/mini/introduce/message
[*]支付宝API代码示例
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、消息截图






页: [1]
查看完整版本: 支付宝订阅消息推送

免责声明:
1、本站提供的所有资源仅供参考学习使用,版权归原著所有,禁止下载本站资源参与商业和非法行为,请在24小时之内自行删除!
2、本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,请勿任何商业目的与商业用途。
3、若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。
4、论坛的所有内容都不保证其准确性,完整性,有效性,由于源码具有复制性,一经售出,概不退换。阅读本站内容因误导等因素而造成的损失本站不承担连带责任。
5、用户使用本网站必须遵守适用的法律法规,对于用户违法使用本站非法运营而引起的一切责任,由用户自行承担
6、本站所有资源来自互联网转载,版权归原著所有,用户访问和使用本站的条件是必须接受本站“免责声明”,如果不遵守,请勿访问或使用本网站
7、本站使用者因为违反本声明的规定而触犯中华人民共和国法律的,一切后果自己负责,本站不承担任何责任。
8、凡以任何方式登陆本网站或直接、间接使用本网站资料者,视为自愿接受本网站声明的约束。
9、本站以《2013 中华人民共和国计算机软件保护条例》第二章 “软件著作权” 第十七条为原则:为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。若有学员需要商用本站资源,请务必联系版权方购买正版授权!
10、本网站如无意中侵犯了某个企业或个人的知识产权,请来信【站长信箱312337667@qq.com】告之,本站将立即删除。
郑重声明:
本站所有资源仅供用户本地电脑学习源代码的内含设计思想和原理,禁止任何其他用途!
本站所有资源、教程来自互联网转载,仅供学习交流,不得商业运营资源,不确保资源完整性,图片和资源仅供参考,不提供任何技术服务。
本站资源仅供本地编辑研究学习参考,禁止未经资源商正版授权参与任何商业行为,违法行为!如需商业请购买各资源商正版授权
本站仅收集资源,提供用户自学研究使用,本站不存在私自接受协助用户架设游戏或资源,非法运营资源行为。