官方接口文档:[url]https://docs.open.alipay.com/251/105668/[/url],我这里写的是开卡组件前两步的操作流程,仅供参考
1.调用接口前的准备工作(创建应用等)参考该文档:[url]https://docs.open.alipay.com/251/105731/[/url]
2.核心代码,这里要说明一下,目前测试发现这个接口的model原型有问题,推荐使用拼接字符串或者使用我下面的这个代码来编写项目,后面这个会将其优化model
示例代码
//会员卡开卡表单模板配置(alipay.marketing.card.formtemplate.set) public static void CardForm_templateSet(){ AlipayClient alipayClient=alipayclient(); AlipayMarketingCardFormtemplateSetRequest request = new AlipayMarketingCardFormtemplateSetRequest(); String templateId = "20180131000000000782698000300330"; JSONObject bizParams = new JSONObject(); bizParams.put("template_id", templateId); ArrayList
requiredFields = new ArrayList
(); requiredFields.add("OPEN_FORM_FIELD_NAME"); requiredFields.add("OPEN_FORM_FIELD_MOBILE"); requiredFields.add("OPEN_FORM_FIELD_IDCARD"); requiredFields.add("OPEN_FORM_FIELD_GENDER"); ArrayList
optionalFields = new ArrayList
(); optionalFields.add("OPEN_FORM_FIELD_EMAIL"); optionalFields.add("OPEN_FORM_FIELD_BIRTHDAY"); JSONObject fields = new JSONObject(); JSONObject requiredField = new JSONObject(); requiredField.put("common_fields", requiredFields); JSONObject optionalField = new JSONObject(); optionalField.put("common_fields", optionalFields); fields.put("required", requiredField); fields.put("optional", optionalField); bizParams.put("fields", fields); request.setBizContent(bizParams.toString()); AlipayMarketingCardFormtemplateSetResponse response = null; try { response = alipayClient.execute(request); System.out.println(response.getBody()); } catch (AlipayApiException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(response.isSuccess()){ System.out.println("调用成功"); } else { System.out.println("调用失败"); } }
接口返回信息示例:
{"alipay_marketing_card_formtemplate_set_response":{"code":"10000","msg":"Success"},"sign":"PK9mGX8uy3xfwRKygPxPCuf62YQU5JrdY4yDSn5dRhRcjtcTlzm8Gzv1yhnNXOhyITDzY6O8+rwPpSUqrQXT3VvS6dkMvEnoXsT5uKefRKX9PuvwbttN5Vc7Me57aPX9dReiBji4HgBVMI22gcugZ8mBTuNIecYNAAxiVcZjUFQ="}
注意点:
1.template_id为会员卡模板ID,【创建会员卡模板】将会返回该id
然后开始操作第二步的流程,调用获取会员卡领卡投放链接(alipay.marketing.card.activateurl.apply),其核心代码
注意点:
1.template_id为会员卡模板ID,创建会员卡模板将会返回该id
2.callback为会员卡领卡表单页面成功回调地址,此地址需要与应用配置的授权回调地址相同。(否则报错ERR010)
3.out_string参数由商户自定义设置,确认领卡成功后将作为url参数拼接在callback回调地址之后原样带回
4.follow_app_id,需要关注的生活号AppId。若需要在领卡页面展示“关注生活号”提示,可设置此参数为待关注的生活AppId
获取会员卡领卡投放链接示例代码
//alipay.marketing.card.activateurl.apply(获取会员卡领卡投放链接) public static void CardActivateurl_Apply(){ AlipayClient alipayClient=alipayclient(); AlipayMarketingCardActivateurlApplyRequest request = new AlipayMarketingCardActivateurlApplyRequest(); request.setBizContent("{" + "\"template_id\":\"20180131000000000782698000300330\"," + "\"out_string\":\"test_callback\"," + "\"callback\":\"https://www.baidu.com\"," + "\"follow_app_id\":\"2014100900013222\"" + " }"); AlipayMarketingCardActivateurlApplyResponse response = null; try { response = alipayClient.execute(request); System.out.println(response.getBody()); } catch (AlipayApiException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(response.isSuccess()){ System.out.println("调用成功"); } else { System.out.println("调用失败"); } } }
接口返回信息:
{"alipay_marketing_card_activateurl_apply_response":{"code":"10000","msg":"Success","apply_card_url":"https%3A%2F%2Fmemberprod.alipay.com%2Faccount%2Fopenform%2Factivecard.htm%3Fapp_id%3D2014100900013222%26template_id%3D20180131000000000782698000300330%26__webview_options__%3DcanPullDown%253dNO%2526transparentTitle%253dauto%26out_string%3Dtest_callback%26follow_id%3D2014100900013222%26callback%3Dhttps%3A%2F%2Fwww.baidu.com"},"sign":"mcie0QYbGM8cs/9KXUtFHEqoG5j1+N9N2pAqMKigR2iWmtKHKAlc5HjutFqGEhJ/MN6gGOlqRv8xDYD9FDGqlDhtz80gKwU0AKfIaOMTURANmcL7YrGuymfatLTsoF14jkJf6NqPuoNleJZDY9kXQmHx7MBWlJY8EUPS+RRqnOk="}
将apply_card_url的值使用urldecode解码得到的值,使用二维码生成器生成,然后使用支付宝扫码可以预览到领卡界面,如果字段申请都得到通过的话,这些必填字段都会获取到的,如要修改信息,点击修改按钮即可
- 点击确认领取,会跳转callback地址,然后会得到类似这样的信息
[url]https://www.baidu.com/?app_id=2014100900013222&auth_code=bec331907d9945a88b4c0be07ecbXXXE&state=bWNhcmQ%3D&scope=auth_base%2Cauth_user%2Cauth_ecard&template_id=20180131000000000782698000300330&request_id=20180131018043460230270561446&out_string=test_callback[/url]
- 然后通过code去换取accessToken去开卡的,如何换取请参考:获取会员信息([url]https://openclub.alipay.com/read.php?tid=14599&fid=68[/url])
调用这个接口会员卡开卡(alipay.marketing.card.open):[url]https://openclub.alipay.com/read.php?tid=1628&fid=71[/url]