支付寶訂閱消息推送


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、消息截圖

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM