基于Springboot+支付宝小程序会员开卡功能的服务端实现总结

简述

因为业务需要,公司项目的支付宝小程序服务端,要能够支持在线会员卡的开卡与发卡操作,自己参考支付宝官方文档实现了功能,但是开发过程中遇到很多问题,索性基本都解决了,现在有时间做一下总结。

官方开发文档:https://opendocs.alipay.com/open/251/cardbasic

1、开发准备

1、支付宝小程序申请、注册、认证
2、支付宝小程序申请卡券(会员卡)功能
3、开发必须获取:
* @param appId  小程序appId
* @param appPrivateKey 用户私钥
* @param alipayPublicKey 支付宝公钥
4、Java SDK使用教程:
https://opendocs.alipay.com/open/54/103419/#s1

2、支付宝会员卡开卡开发基本流程简述

(1)、创建会员卡模板
1、上传图片
alipay.offline.material.image.upload(上传门店照片和视频接口)
2、编写会员卡模板
3、创建会员卡模板
创建完成后,系统返回获取会员卡templateId(模板id,唯一)
(2)、配置支付宝会员卡开卡表单模板(领卡填写的表单)
根据会员卡templateId配置开卡表单模板
(PS:这一步必须要有,配置领卡页面填写数据)
(3)、获取会员卡发卡链接(前端调用接口)
根据会员卡templateId获取发卡链接,
前端调用发卡链接,展示领卡页面
(4)、会员卡发卡(点击页面发卡链接)
前端根据领卡页面,调用发卡服务,进行发卡操作

3、功能开发

(1)、Springboot集成支付宝SDK
<!-- 支付宝SDK依赖-->
   <dependency>
       <groupId>com.alipay.sdk</groupId>
       <artifactId>alipay-sdk-java</artifactId>
       <version>4.10.0.ALL</version>
  </dependency>
(2)、创建会员卡模板(根据需要自己确定)
注:以下代码为样例,可以直接修改使用
/**
* 1、添加支付宝会员卡模板
* @param appId  小程序appId
* @param appPrivateKey 用户私钥
* @param alipayPublicKey 支付宝公钥
 * */
private String gateway = https://openapi.alipay.com/gateway.do;
private String charset = UTF-8;
private String signType = RSA2;
private String format = json;
private String gbkCharset = GBK;

public AlipayMarketingCardTemplateCreateResponse  addMarketingCardTemplate(String appId,String appPrivateKey,String alipayPublicKey) throws AlipayApiException{
        AlipayClient alipayClient = new DefaultAlipayClient(gateway, appId, appPrivateKey, format, gbkCharset, alipayPublicKey, signType);
        AlipayMarketingCardTemplateCreateRequest request = new AlipayMarketingCardTemplateCreateRequest();
        //填充参数
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("request_id", "20210303199"+RundomUtil.getOrderIdByUUId());//请求参数必须唯一
        jsonObject.put("card_type", "OUT_MEMBER_CARD");
        jsonObject.put("biz_no_prefix", "prexupdate");
        jsonObject.put("biz_no_suffix_len", "10");
        jsonObject.put("write_off_type", "none");

        // 新建一个MAP集合.
        Map<String, Object> styleAmap = new HashMap<String, Object>();
        styleAmap.put("card_show_name", "XXXX会员卡");
        //模板logo
        styleAmap.put("logo_id", "XXXXXXXXX");
        styleAmap.put("background_id", "XXXXXXXXX");
        styleAmap.put("bg_color", "rgb(55,112,179)");
        styleAmap.put("front_text_list_enable", true);
        //=====A========模板样式========
        jsonObject.put("template_style_info", styleAmap);
        // 新建一个MAP集合
        Map<String, Object> infoAmap = new HashMap<String, Object>();
        infoAmap.put("code", "BENEFIT_INFO");
        infoAmap.put("title", "xx详情");
        infoAmap.put("operate_type", "openWeb");
        infoAmap.put("value", "");
        Map<String, Object> more_info = new HashMap<String,Object>();
        more_info.put("title","xx详情");
        more_info.put("url","XXXXXXXX");
        infoAmap.put("more_info",more_info);
        // 新建一个List
        ArrayList infoList = new ArrayList();
        infoList.add(infoAmap);

        infoAmap = new HashMap<String, Object>();
        infoAmap.put("code", "BENEFIT_INFO");
        infoAmap.put("title", "xxxx");
        infoAmap.put("value", "");
        infoAmap.put("operate_type", "openWeb");
        more_info = new HashMap<String,Object>();
        more_info.put("title","xxxx");
        more_info.put("url","XXXXX");
        infoAmap.put("more_info",more_info);
        // 新建一个List
        infoList.add(infoAmap);

        infoAmap = new HashMap<String, Object>();
        infoAmap.put("code", "BENEFIT_INFO");
        infoAmap.put("title", "XXX排行榜");
        infoAmap.put("value", "");
        infoAmap.put("operate_type", "openWeb");
        more_info = new HashMap<String,Object>();
        more_info.put("title","XXX排行榜");
        more_info.put("url","xxxx");
        infoAmap.put("more_info",more_info);
        // 新建一个List
        infoList.add(infoAmap);

        //=====B======栏位信息================
        jsonObject.put("column_info_list", infoList);
        // 新建一个MAP集合.
        Map<String, Object> ruleAmap = new HashMap<String, Object>();
        ruleAmap.put("field_name", "ValidDate");
        ruleAmap.put("rule_name", "DATE_IN_FUTURE");
        ruleAmap.put("rule_value", "100m");
        // 新建一个List
        ArrayList ruleList = new ArrayList();
        ruleList.add(ruleAmap);
        jsonObject.put("field_rule_list", ruleList);
        // 新建一个MAP集合.
        Map<String, Object> cardActivemap = new HashMap<String, Object>();
        cardActivemap.put("code", "TO_CLOCK_IN");
        cardActivemap.put("text", "xxxx");
        cardActivemap.put("url_type", "miniAppUrl");
        cardActivemap.put("url", "XXX");
        Map<String, Object> mini_app_url = new HashMap<String, Object>();
        mini_app_url.put("mini_app_id",appId);
        mini_app_url.put("display_on_list","true");
        mini_app_url.put("mini_page_param","XXXX");

        cardActivemap.put("mini_app_url", mini_app_url);
        // 新建一个List
        ArrayList cardActionList = new ArrayList();
        cardActionList.add(cardActivemap);

        Map<String, Object> cardActionmap = new HashMap<String, Object>();
        cardActionmap.put("code", "TO_CLOCK_IN");
        cardActionmap.put("text", "小程序");
        cardActionmap.put("url_type", "miniAppUrl");
        mini_app_url = new HashMap<String, Object>();
        mini_app_url.put("mini_app_id",appId);
        mini_app_url.put("display_on_list","true");
        cardActionmap.put("mini_app_url", mini_app_url);

        cardActionList.add(cardActionmap);
        jsonObject.put("card_action_list", cardActionList);
        request.setBizContent(jsonObject.toString());
        log.info(">>>>>会员卡模板信息:{}",jsonObject.toString());
        AlipayMarketingCardTemplateCreateResponse response = alipayClient.execute(request);
        return response;
    }
(3)、配置会员卡开卡表单模板
public  AlipayMarketingCardFormtemplateSetResponse setMarketingCardTemplate(String appId,String appPrivateKey,String alipayPublicKey,String templateId) throws AlipayApiException{
        AlipayClient alipayClient = new DefaultAlipayClient(gateway, appId, appPrivateKey, format, gbkCharset, alipayPublicKey, signType);
        AlipayMarketingCardFormtemplateSetRequest request = new AlipayMarketingCardFormtemplateSetRequest();
        //填充参数
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("template_id", templateId);
        // 新建一个MAP集合.
        Map<String, Object> ruleAmap = new HashMap<String, Object>();
        // 新建一个MAP集合.
        Map<String, Object> fielAmap = new HashMap<String, Object>();
        String[] ss =new String[]{"OPEN_FORM_FIELD_MOBILE"};
        fielAmap.put("common_fields", ss);
        ruleAmap.put("required", fielAmap);//表单必填字段
        jsonObject.put("fields", ruleAmap);
        request.setBizContent(jsonObject.toString());
        log.info("会员卡开卡表单模板配置参数:{}",jsonObject.toString());
        AlipayMarketingCardFormtemplateSetResponse response = alipayClient.execute(request);
        return response;
    }
(4)、获取会员卡发卡链接
public AlipayMarketingCardActivateurlApplyResponse getAliCardUrl(String appId,String templateId,String privateKey,String publicKey) throws AlipayApiException{
        AlipayClient alipayClient= new DefaultAlipayClient(gateway, appId, privateKey, format, gbkCharset, publicKey, signType);
        AlipayMarketingCardActivateurlApplyRequest request = new AlipayMarketingCardActivateurlApplyRequest();
        //生成参数信息
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("template_id",templateId);//会员卡模板ID
        //用户点击确认领卡成功后的回调地址,此地址需要与应用配置的授权回调地址相同
        jsonObject.put("callback","");//当前没有采用回调形式
        jsonObject.put("out_string","202012393932");
        jsonObject.put("follow_app_id","20150000000000001");
        String bizContent = jsonObject.toJSONString();
        //获取返回对象
        request.setBizContent(bizContent);
        AlipayMarketingCardActivateurlApplyResponse response = alipayClient.execute(request);
        return response;
    }
(5)、会员卡领卡
public  AlipayMarketingCardOpenResponse AliCardActivate(String appId,String privateKey,String publicKey,String userId,String accessToken,String number,String templateId,String time) throws AlipayApiException{
        AlipayClient alipayClient = new DefaultAlipayClient(gateway, appId, privateKey, format, gbkCharset, publicKey, signType);
        AlipayMarketingCardOpenRequest request = new AlipayMarketingCardOpenRequest();
        //填充参数
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("out_serial_no", number);//会员卡编号,自定义保证唯一即可
        jsonObject.put("card_template_id", templateId);//当前模板ID
        // 新建一个MAP集合.
        Map<String, Object> userAmap = new HashMap<String, Object>();
        userAmap.put("user_uni_id", userId);
        userAmap.put("user_uni_id_type", "UID");
        jsonObject.put("card_user_info", userAmap);
        // 新建一个MAP集合.
        Map<String, Object> extAmap = new HashMap<String, Object>();
        extAmap.put("external_card_no", number);
        extAmap.put("open_date", time);
        extAmap.put("valid_date", "2099-10-04 19:45:30");
        extAmap.put("level", "VIP1");
        jsonObject.put("card_ext_info", extAmap);
        request.setBizContent(jsonObject.toString());
        log.info("支付宝会员卡发卡请求参数=[{}]",jsonObject.toString());
        AlipayMarketingCardOpenResponse response = alipayClient.execute(request,accessToken);
        log.info("支付宝会员卡发卡请求完成,返回结果=[]",response.getBody());
        return response;
    }

版权声明:本文为Niezhuxue原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。