要在您的应用中使用支付宝开放产品的接口能力,您需要先去蚂蚁金服开放平台(open.alipay.com),在管理中心中创建登记您的应用,并提交审核,审核通过后会为您生成应用唯一标识(APPID),并且可以申请开通开放产品使用权限,通过APPID您的应用才能调用开放产品的接口能力。需要详细了解开放平台创建应用步骤请参考《开放平台应用创建指南》。
开发者调用接口前需要先生成RSA密钥,RSA密钥包含应用私钥(APP_PRIVATE_KEY)、应用公钥(APP_PUBLIC_KEY)。生成密钥后在开放平台管理中心进行密钥配置,配置完成后可以获取支付宝公钥(ALIPAY_PUBLIC_KEY)。详情请参考《配置应用环境》。
1. 下载服务端SDK
为了帮助开发者调用开放接口,我们提供了开放平台服务端SDK,包含JAVA、PHP和.NET三个语言版本,封装了签名&验签、HTTP接口请求等基础功能。请先下载对应语言版本的SDK并引入您的开发工程。
各语言版本服务端SDK详细使用说明,请参考《服务端SDK说明》。
2. 接口调用配置
在SDK调用前需要进行初始化,以Java代码为示例如下:
AlipayClient alipayClient = new DefaultAlipayClient(URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
关键参数说明:
配置参数 | 示例值解释 | 获取方式/示例值 |
---|---|---|
URL | 支付宝网关(固定) | https://openapi.alipay.com/gateway.do |
APPID | APPID 即创建应用后生成 | 获取见上面创建应用并获取APPID |
APP_PRIVATE_KEY | 开发者私钥,由开发者自己生成 | 获取详见上面配置密钥 |
FORMAT | 参数返回格式,只支持json | json(固定) |
CHARSET | 编码集,支持GBK/UTF-8 | 开发者根据实际工程编码配置 |
ALIPAY_PUBLIC_KEY | 支付宝公钥,由支付宝生成 | 获取详见上面配置密钥 |
SIGN_TYPE | 商户生成签名字符串所使用的签名算法类型,目前支持RSA2和RSA,推荐使用RSA2 | RSA2 |
1)口碑业务授权查询接口(koubei.member.data.oauth.query )
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2"); KoubeiMemberDataOauthQueryRequest request = new KoubeiMemberDataOauthQueryRequest(); request.setBizContent("{" + " \"auth_type\":\"pay_member\"," + " \"code\":\"4b203fe6c11548bcabd8da5bb087a83b\"," + " \"ext_info\":\"{\\\"key\\\":\\\"value\\\"}\"" + " }"); KoubeiMemberDataOauthQueryResponse response = alipayClient.execute(request); if(response.isSuccess()){ System.out.println("调用成功"); } else { System.out.println("调用失败"); }
2)图片上传接口(alipay.offline.material.image.upload)
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2"); AlipayOfflineMaterialImageUploadRequest request = new AlipayOfflineMaterialImageUploadRequest(); request.setImageType("jpg或mp4"); request.setImageName("海底捞"); FileItem ImageContent = new FileItem(C:/Downloads/ooopic_963991_7eea1f5426105f9e6069/16365_1271139700.jpg); request.setImageContent(ImageContent); request.setImagePid("2088021822217233"); AlipayOfflineMaterialImageUploadResponse response = alipayClient.execute(request); if(response.isSuccess()){ System.out.println("调用成功"); } else { System.out.println("调用失败"); }
3)创建手艺人接口(koubei.craftsman.data.provider.create)
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2"); KoubeiCraftsmanDataProviderCreateRequest request = new KoubeiCraftsmanDataProviderCreateRequest(); request.setBizContent("{" + " \"out_craftsman_id\":\"208801020306\"," + " \"name\":\"张三\"," + " \"account\":\"zhangsan\"," + " \"nick_name\":\"阿诺,KK.\"," + " \"avatar\":\"TxFyuus3RUW8wAeYfubM7gAAACMAAQED\"," + " \"careers\":[" + " \"发型师\"" + " ]," + " \"title\":\"资深发型总监\"," + " \"tel_num\":\"18018801880\"," + " \"career_begin\":\"2016-01-01\"," + " \"specialities\":[" + " \"烫发\",\"瑜伽\",\"有氧\"" + " ]," + " \"introduction\":\"中国好声音御用造型师,2010年获得沙宣美发大赛一等奖,得过沙宣美发学校的证书。\"," + " \"shop_relations\":[{" + " \"shop_id\":\"2016102100077000000003441154\"," + " \"recommend_weight\":0" + " }]," + " \"auth_code\":\"6a8713a414da4a2dafdb7c24e597TX80\"" + " }"); KoubeiCraftsmanDataProviderCreateResponse response = alipayClient.execute(request); if(response.isSuccess()){ System.out.println("调用成功"); } else { System.out.println("调用失败"); }
4)修改手艺人信息接口(koubei.craftsman.data.provider.modify)
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2"); KoubeiCraftsmanDataProviderModifyRequest request = new KoubeiCraftsmanDataProviderModifyRequest(); request.setBizContent("{" + " \"craftsman_id\":\"886038\"," + " \"out_craftsman_id\":\"12345\"," + " \"nick_name\":\"阿诺,KK.\"," + " \"avatar\":\"TxFyuus3RUW8wAeYfubM7gAAACMAAQED\"," + " \"careers\":[" + " \"发型师\"" + " ]," + " \"title\":\"资深发型总监\"," + " \"tel_num\":\"18018801880\"," + " \"career_begin\":\"2016-01-01\"," + " \"specialities\":[" + " \"瑜伽\",\"有氧\"" + " ]," + " \"introduction\":\"中国好声音御用造型师,2010年获得沙宣美发大赛一等奖,得过沙宣美发学校的证书。\"," + " \"shop_relations\":[{" + " \"shop_id\":\"2016102100077000000003441154\"," + " \"recommend_weight\":0" + " }]," + " \"auth_code\":\"6a8713a414da4a2dafdb7c24e597TX80\"" + " }"); KoubeiCraftsmanDataProviderModifyResponse response = alipayClient.execute(request); if(response.isSuccess()){ System.out.println("调用成功"); } else { System.out.println("调用失败"); }
5)门店摘要信息批量查询接口(alipay.offline.market.shop.summary.batchquery)
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2"); AlipayOfflineMarketShopSummaryBatchqueryRequest request = new AlipayOfflineMarketShopSummaryBatchqueryRequest(); request.setBizContent("{" + " \"op_role\":\"ISV、PROVIDER\"," + " \"query_type\":\"BRAND_RELATION\"," + " \"related_partner_id\":\"2088001969784501\"," + " \"shop_id\":\"2015062100077000000000120773\"," + " \"shop_status\":\"PAUSED\"," + " \"page_no\":1," + " \"page_size\":20" + " }"); AlipayOfflineMarketShopSummaryBatchqueryResponse response = alipayClient.execute(request); if(response.isSuccess()){ System.out.println("调用成功"); } else { System.out.println("调用失败"); }
6)批量查询手艺人信息接口(koubei.craftsman.data.provider.batchquery)
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2"); KoubeiCraftsmanDataProviderBatchqueryRequest request = new KoubeiCraftsmanDataProviderBatchqueryRequest(); request.setBizContent("{" + " \"craftsman_ids\":[" + " \"210937\"" + " ]," + " \"out_craftsman_ids\":[" + " \"20161227\"" + " ]," + " \"shop_id\":\"2015123000077000000002414370\"," + " \"recommend\":true," + " \"page_no\":\"1\"," + " \"page_size\":\"20\"," + " \"qr_code_shop_id\":\"2016102100077000000003441154\"," + " \"auth_code\":\"6a8713a414da4a2dafdb7c24e597TX80\"" + " }"); KoubeiCraftsmanDataProviderBatchqueryResponse response = alipayClient.execute(request); if(response.isSuccess()){ System.out.println("调用成功"); } else { System.out.println("调用失败"); }
7)创建手艺人作品接口(koubei.craftsman.data.work.create )
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2"); KoubeiCraftsmanDataWorkCreateRequest request = new KoubeiCraftsmanDataWorkCreateRequest(); request.setBizContent("{" + " \"craftsman_id\":\"886038\"," + " \"shop_ids\":[" + " \"2016102100077000000003441154\",\"2016102100077000000003441152\"" + " ]," + " \"works\":[{" + " \"out_work_id\":\"12345\"," + " \"title\":\"长卷发\"," + " \"media_type\":\"PICTURE\"," + " \"media_id\":\"Qt5XB8R7SMizIC2CZ_qLXAAAACMAAQED|XXwcvckbS_WcT5-mYXtY1QAAACMAAQED\"," + " \"duration\":150" + " }]," + " \"auth_code\":\"6a8713a414da4a2dafdb7c24e597TX80\"" + " }"); KoubeiCraftsmanDataWorkCreateResponse response = alipayClient.execute(request); if(response.isSuccess()){ System.out.println("调用成功"); } else { System.out.println("调用失败"); }
8)修改手艺人作品接口(koubei.craftsman.data.work.modify)
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2"); KoubeiCraftsmanDataWorkModifyRequest request = new KoubeiCraftsmanDataWorkModifyRequest(); request.setBizContent("{" + " \"work_id\":\"2016122603545559\"," + " \"title\":\"波波头造型\"," + " \"auth_code\":\"6a8713a414da4a2dafdb7c24e597TX80\"" + " }"); KoubeiCraftsmanDataWorkModifyResponse response = alipayClient.execute(request); if(response.isSuccess()){ System.out.println("调用成功"); } else { System.out.println("调用失败"); }
9)删除手艺人作品信息接口(koubei.craftsman.data.work.delete)
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2"); KoubeiCraftsmanDataWorkDeleteRequest request = new KoubeiCraftsmanDataWorkDeleteRequest(); request.setBizContent("{" + " \"craftsman_id\":\"886038\"," + " \"work_ids\":[" + " \"2016122603545559\"" + " ]," + " \"auth_code\":\"6a8713a414da4a2dafdb7c24e597TX80\"" + " }"); KoubeiCraftsmanDataWorkDeleteResponse response = alipayClient.execute(request); if(response.isSuccess()){ System.out.println("调用成功"); } else { System.out.println("调用失败"); }
10)批量查询手艺人作品接口(koubei.craftsman.data.work.batchquery)
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2"); KoubeiCraftsmanDataWorkBatchqueryRequest request = new KoubeiCraftsmanDataWorkBatchqueryRequest(); request.setBizContent("{" + " \"work_ids\":[" + " \"2016100703485664\"" + " ]," + " \"craftsman_id\":\"210937\"," + " \"page_no\":\"1\"," + " \"page_size\":\"100\"," + " \"auth_code\":\"6a8713a414da4a2dafdb7c24e597TX80\"" + " }"); KoubeiCraftsmanDataWorkBatchqueryResponse response = alipayClient.execute(request); if(response.isSuccess()){ System.out.println("调用成功"); } else { System.out.println("调用失败"); }
11)批量查询评价接口(koubei.content.comment.data.batchquery)
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2"); KoubeiContentCommentDataBatchqueryRequest request = new KoubeiContentCommentDataBatchqueryRequest(); request.setBizContent("{" + " \"comment_id\":\"a8f963a718aa40f43291675706010000\"," + " \"shop_id\":\"2015123000077000000002414370\"," + " \"craftsman_id\":\"210937\"," + " \"last_comment_id\":\"a8f963a718aa40f43291675706010000\"," + " \"page_size\":20" + " }"); KoubeiContentCommentDataBatchqueryResponse response = alipayClient.execute(request); if(response.isSuccess()){ System.out.println("调用成功"); } else { System.out.println("调用失败"); }
12)评价回复接口(koubei.content.comment.reply.create)
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2"); KoubeiContentCommentReplyCreateRequest request = new KoubeiContentCommentReplyCreateRequest(); request.setBizContent("{" + " \"comment_id\":\"1be9a6e1e11c42ca8855049006010000\"," + " \"content\":\"感谢一直以来对我的支持!\"," + " \"auth_code\":\"6a8713a414da4a2dafdb7c24e597TX80\"" + " }"); KoubeiContentCommentReplyCreateResponse response = alipayClient.execute(request); if(response.isSuccess()){ System.out.println("调用成功"); } else { System.out.println("调用失败"); }
13)一键营销商家中心PUSH消息接口(koubei.marketing.data.message.deliver)
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2"); KoubeiMarketingDataMessageDeliverRequest request = new KoubeiMarketingDataMessageDeliverRequest(); request.setBizContent("{" + " \"msg_type\":\"PROMO_RECOMMEND\"," + " \"content\":\"{\\\"KEY1\\\":\\\"val1\\\",\\\"KEY2\\\":\\\"val2\\\",\\\"KEY3\\\":\\\"val3\\\"}\"," + " \"ext_info\":\"{\\\"REDIRECT_URL\\\":\\\"https://e.alipay.com\\\",\\\"CHANNEL\\\":\\\"MSGBOX\\\"}\"" + " }"); KoubeiMarketingDataMessageDeliverResponse response = alipayClient.execute(request); if(response.isSuccess()){ System.out.println("调用成功"); } else { System.out.println("调用失败"); }
14)isv回传的用户操作行为信息调用接口(alipay.offline.provider.useraction.record)
该接口如何调用请参见上传用户泛行业订单API详细说明。
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2"); AlipayOfflineProviderUseractionRecordRequest request = new AlipayOfflineProviderUseractionRecordRequest(); request.setBizContent("{" + " \"action_type\":\"order_dishes\"," + " \"mobile\":\"15657153919\"," + " \"platform_user_id\":\"2014323100009\"," + " \"source\":\"koubei.com\"," + " \"industry\":\"REPAST\"," + " \"user_id\":\"2088502948051150\"," + " \"date_time\":\"2015-12-03 12:34:23\"," + " \"alipay_app_id\":\"dish-order-app-plug\"," + " \"action_detail\":\"{\\\"outer_dish_id\\\":\\\"20\\\",\\\"name\\\":\\\"红烧猪蹄\\\",\\\"price\\\":4500,\\\"quantity\\\":222}\"," + " \"outer_shop_do\":{" + " \"shop_id\":\"2016062900077000000016003316\"," + " \"outer_id\":\"2323445656fe234abc33\"," + " \"type\":\"_2dFire\"" + " }," + " \"entity\":\"user\"," + " \"action_outer_id\":\"gade3331b\"" + " }"); AlipayOfflineProviderUseractionRecordResponse response = alipayClient.execute(request); if(response.isSuccess()){ System.out.println("调用成功"); } else { System.out.println("调用失败"); }