服务端 SDK

SDK 获取


开发语言

资源下载

环境要求

JAVA 版资源

Maven项目依赖

适用于 Java 语言、JDK 版本 1.6 及以上的开发环境

.NET 版资源

NuGet项目依赖
SDK源码下载

适用于 Visual Studio 2010 及以上版本、Framework 3.5 SP1 及以上版本的开发环境

PHP 版资源

SDK源码下载

适用于 PHP 5.5 以上的开发环境

PYTHON 版资源

PyPI项目依赖

适用于 Python 2.7 及以上版本的开发环境

NodeJS 版资源

NPM项目依赖

适用于 Node.js v8.0.0 及以上版本的开发环境


SDK 集成


  1. 注意:由于实例化SDK客户端时需要指定应用的私钥信息,请务必注意不要将私钥信息配置在源码中(比如配置为常量或储存在配置文件的某个字段中等),因为私钥的保密等级往往比源码高得多,将会增加私钥泄露的风险。推荐将私钥信息储存在专用的私钥文件中,将私钥文件通过安全的流程分发到服务器的安全储存区域上,仅供自己的应用运行时读取。
  2. SDK 已经对加签验签逻辑做了封装,使用 SDK 可直接调用 API。
  3. 确定接口对应的类。
    例如,接口名:alipay.offline.material.image.upload
    在 SDK 中对应的类为:每个单词首字母大写,并去掉分隔符(“.”),末尾加上Request(或Response)
    如上接口名对应的类为:
    AlipayOfflineMaterialImageUploadRequest(请求类)
    AlipayOfflineMaterialImageUploadResponse(响应类)
    具体调用方式见下方各语言。


Java SDK 集成示例


SDK 包说明


  • alipay-sdk-java*.jar—————————支付宝SDK编译文件jar

  • alipay-sdk-java*-source.jar——————支付宝SDK源码文件jar

  • commons-logging-1.1.1.jar——————SDK依赖的日志jar

  • commons-logging-1.1.1-sources.jar———SDK依赖的日志源码jar
  • bcprov-jdk15on-1.62.jar——————SDK依赖的证书工具jar


注意


  • 集成支付宝接口需要引入的文件是:
    alipay-sdk-java*.jar
    commons-logging-1.1.1.jar

bcprov-jdk15on-1.62.jar(证书签名方式 接入需依赖)

  • 若进一步了解代码实现请引入文件:
    alipay-sdk-java*-source.jar
    commons-logging-1.1.1-sources.jar


普通调用示例


 1//实例化客户端
 2AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2");
 3//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.open.public.template.message.industry.modify 
 4AlipayOpenPublicTemplateMessageIndustryModifyRequest request = new AlipayOpenPublicTemplateMessageIndustryModifyRequest();
 5//SDK已经封装掉了公共参数,这里只需要传入业务参数
 6//此次只是参数展示,未进行字符串转义,实际情况下请转义
 7request.setBizContent("  {" +
 8"    \"primary_industry_name\":\"IT科技/IT软件与服务\"," +
 9"    \"primary_industry_code\":\"10001/20102\"," +
10"    \"secondary_industry_code\":\"10001/20102\"," +
11"    \"secondary_industry_name\":\"IT科技/IT软件与服务\"" +
12" }");
13AlipayOpenPublicTemplateMessageIndustryModifyResponse response = alipayClient.execute(request); 
14//调用成功,则处理业务逻辑
15if(response.isSuccess()){
16    //.....
17}


普通调用示例(证书)


 1//构造client
 2CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
 3//设置网关地址
 4certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");
 5//设置应用Id
 6certAlipayRequest.setAppId(app_id);
 7//设置应用私钥
 8certAlipayRequest.setPrivateKey(privateKey);
 9//设置请求格式,固定值json
10certAlipayRequest.setFormat("json");
11//设置字符集
12certAlipayRequest.setCharset(charset);
13//设置签名类型
14certAlipayRequest.setSignType(sign_type);
15//设置应用公钥证书路径
16certAlipayRequest.setCertPath(app_cert_path);
17//设置支付宝公钥证书路径
18certAlipayRequest.setAlipayPublicCertPath(alipay_cert_path);
19//设置支付宝根证书路径
20certAlipayRequest.setRootCertPath(alipay_root_cert_path);
21//构造client
22AlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
23//构造API请求
24AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
25//发送请求
26AlipayTradeQueryResponse response = alipayClient.certificateExecute(request);


图片上传接口调用示例


 1AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2");
 2// 实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.offline.material.image.upload 
 3AlipayOfflineMaterialImageUploadRequest request = new AlipayOfflineMaterialImageUploadRequest();
 4request.setImageName("test");
 5//Windows请填写绝对路径,不支持相对路径;Linux支持相对路径
 6FileItem item = new FileItem("C:/Downloads/ooopic_963991_7eea1f5426105f9e6069/16365_1271139700.jpg");
 7request.setImageType("JPG");
 8request.setImageContent(item);
 9//执行API请求
10AlipayOfflineMaterialImageUploadResponse response = alipayClient.execute(request);
11//调用成功,则处理业务逻辑
12if(response.isSuccess()){
13     //获取图片访问地址
14    String imageUrl = response.getImageUrl();
15    //.....
16}


图片上传接口调用示例(证书)


 1//构造client
 2CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
 3DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
 4// 实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.offline.material.image.upload 
 5AlipayOfflineMaterialImageUploadRequest request = new AlipayOfflineMaterialImageUploadRequest();
 6request.setImageName("test");
 7//Windows请填写绝对路径,不支持相对路径;Linux支持相对路径
 8FileItem item = new FileItem("C:/Downloads/ooopic_963991_7eea1f5426105f9e6069/16365_1271139700.jpg");
 9request.setImageType("JPG");
10request.setImageContent(item);
11//执行API请求
12AlipayOfflineMaterialImageUploadResponse response = alipayClient.execute(request);
13//调用成功,则处理业务逻辑
14if(response.isSuccess()){
15     //获取图片访问地址
16    String imageUrl = response.getImageUrl();
17    //.....
18}


用户授权接口调用示例


1AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2");
2//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.user.userinfo.share
3AlipayUserUserinfoShareRequest request = new AlipayUserUserinfoShareRequest();
4//授权类接口执行API调用时需要带上accessToken
5AlipayUserUserinfoShareResponse response= alipayClient.execute(request,"accessToken"); 
6//业务处理
7//...


用户授权接口调用示例(证书)

1//构造client
2CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
3DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
4//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.user.userinfo.share
5AlipayUserUserinfoShareRequest request = new AlipayUserUserinfoShareRequest();
6//授权类接口执行API调用时需要带上accessToken
7AlipayUserUserinfoShareResponse response = alipayClient.certificateExecute(request, "access_token");


应用授权接口调用示例(ISV代理商户调用)


 1AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2");
 2//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.open.public.template.message.industry.modify 
 3AlipayOpenPublicTemplateMessageIndustryModifyRequest request = new AlipayOpenPublicTemplateMessageIndustryModifyRequest();
 4//SDK已经封装掉了公共参数,这里只需要传入业务参数
 5//此次只是参数展示,未进行字符串转义,实际情况下请转义
 6request.setBizContent("  { " +
 7"    \"primary_industry_name\":\"IT科技/IT软件与服务\"," +
 8"    \"primary_industry_code\":\"10001/20102\"," + 
 9"    \"secondary_industry_code\":\"10001/20102\"," +
10"    \"secondary_industry_name\":\"IT科技/IT软件与服务\"" +
11"  }");
12//ISV代理商户调用需要传入app_auth_token
13request.putOtherTextParam("app_auth_token", "201511BBaaa6464f271f49e482f2e9fe63ca5F05");
14AlipayOpenPublicTemplateMessageIndustryModifyResponse response = alipayClient.execute(request); 
15//调用成功,则处理业务逻辑
16if(response.isSuccess()){
17    //.....
18}


应用授权接口调用示例(ISV代理商户调用)(证书)


 1//构造client
 2CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
 3AlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
 4//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.open.public.template.message.industry.modify 
 5AlipayOpenPublicTemplateMessageIndustryModifyRequest request = new AlipayOpenPublicTemplateMessageIndustryModifyRequest();
 6//SDK已经封装掉了公共参数,这里只需要传入业务参数
 7//此次只是参数展示,未进行字符串转义,实际情况下请转义
 8request.setBizContent("  { " +
 9"    \"primary_industry_name\":\"IT科技/IT软件与服务\"," +
10"    \"primary_industry_code\":\"10001/20102\"," + 
11"    \"secondary_industry_code\":\"10001/20102\"," +
12"    \"secondary_industry_name\":\"IT科技/IT软件与服务\"" +
13"  }");
14//ISV代理商户调用需要传入app_auth_token
15request.putOtherTextParam("app_auth_token", "201511BBaaa6464f271f49e482f2e9fe63ca5F05");
16//发送应用授权请求
17AlipayTradeQueryResponse response = alipayClient.certificateExecute(request);


.Net SDK 集成示例


普通调用示例


 1IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", APPID, APP_PRIVATE_KEY, "json", "1.0", "RSA2", ALIPAY_PUBLIC_KEY, CHARSET, false);
 2//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称如:alipay.open.public.template.message.industry.modify 
 3AlipayOpenPublicTemplateMessageIndustryModifyRequest request = new AlipayOpenPublicTemplateMessageIndustryModifyRequest();
 4//SDK已经封装掉了公共参数,这里只需要传入业务参数
 5//此次只是参数展示,未进行字符串转义,实际情况下请转义
 6request.BizContent="{" +
 7"    \"primary_industry_name\":\"IT科技/IT软件与服务\"," +
 8"    \"primary_industry_code\":\"10001/20102\"," +
 9"    \"secondary_industry_code\":\"10001/20102\"," +
10"    \"secondary_industry_name\":\"IT科技/IT软件与服务\"" +
11"  }";
12AlipayOpenPublicTemplateMessageIndustryModifyResponse response = client.Execute(request); 
13//调用成功,则处理业务逻辑
14if(response.isSuccess()){
15    //.....
16}

普通调用示例(证书)


 1//设置证书相关参数
 2CertParams certParams = new CertParams
 3{
 4    AlipayPublicCertPath = "支付宝公钥证书路径",
 5    AppCertPath = "商户应用证书路径",
 6    RootCertPath = "支付宝根证书路径"
 7};
 8IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", APPID, APP_PRIVATE_KEY, "json", "1.0", "RSA2", "utf-8", false, certParams);
 9//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称如:alipay.open.public.template.message.industry.modify 
10AlipayOpenPublicTemplateMessageIndustryModifyRequest request = new AlipayOpenPublicTemplateMessageIndustryModifyRequest();
11//SDK已经封装掉了公共参数,这里只需要传入业务参数
12//此次只是参数展示,未进行字符串转义,实际情况下请转义
13request.BizContent="{" +
14"    \"primary_industry_name\":\"IT科技/IT软件与服务\"," +
15"    \"primary_industry_code\":\"10001/20102\"," +
16"    \"secondary_industry_code\":\"10001/20102\"," +
17"    \"secondary_industry_name\":\"IT科技/IT软件与服务\"" +
18"  }";
19AlipayOpenPublicTemplateMessageIndustryModifyResponse response = client.CertificateExecute(request); 
20//调用成功,则处理业务逻辑
21if(response.isSuccess()){
22    //.....
23}


图片上传接口调用示例


 1IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", APPID, APP_PRIVATE_KEY, "json", "1.0", "RSA2", ALIPAY_PUBLIC_KEY, CHARSET, false);
 2// 实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.offline.material.image.upload 
 3AlipayOfflineMaterialImageUploadRequest request = new AlipayOfflineMaterialImageUploadRequest();
 4request.setImageName("test");
 5//Windows请填写绝对路径,不支持相对路径;Linux支持相对路径
 6FileItem item = new FileItem("C:/Downloads/ooopic_963991_7eea1f5426105f9e6069/16365_1271139700.jpg");
 7request.setImageType("JPG");
 8request.ImageContent = item;
 9//执行API请求
10AlipayOfflineMaterialImageUploadResponse response = alipayClient.Execute(request);
11//调用成功,则处理业务逻辑
12if(response.isSuccess()){
13     //获取图片访问地址
14    String imageUrl = response.getImageUrl();
15    //.....
16}

图片上传接口调用示例(证书)


 1//设置证书相关参数
 2CertParams certParams = new CertParams
 3{
 4    AlipayPublicCertPath = "支付宝公钥证书路径",
 5    AppCertPath = "商户应用证书路径",
 6    RootCertPath = "支付宝根证书路径"
 7};
 8IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", APPID, APP_PRIVATE_KEY, "json", "1.0", "RSA2", "utf-8", false, certParams);
 9// 实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.offline.material.image.upload 
10AlipayOfflineMaterialImageUploadRequest request = new AlipayOfflineMaterialImageUploadRequest();
11request.setImageName("test");
12//Windows请填写绝对路径,不支持相对路径;Linux支持相对路径
13FileItem item = new FileItem("C:/Downloads/ooopic_963991_7eea1f5426105f9e6069/16365_1271139700.jpg");
14request.setImageType("JPG");
15request.ImageContent = item;
16//执行API请求
17AlipayOfflineMaterialImageUploadResponse response = alipayClient.CertificateExecute(request);
18//调用成功,则处理业务逻辑
19if(response.isSuccess()){
20     //获取图片访问地址
21    String imageUrl = response.getImageUrl();
22    //.....
23}


用户授权接口调用示例


1IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", APPID, APP_PRIVATE_KEY, "json", "1.0", "RSA2", ALIPAY_PUBLIC_KEY, CHARSET, false);
2//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.user.userinfo.share
3AlipayUserUserinfoShareRequest request = new AlipayUserUserinfoShareRequest();
4//授权类接口执行API调用时需要带上accessToken
5AlipayUserUserinfoShareResponse response= client.Execute(request, "accessToken"); 
6//业务处理
7//...

用户授权接口调用示例(证书)


 1//设置证书相关参数
 2CertParams certParams = new CertParams
 3{
 4    AlipayPublicCertPath = "支付宝公钥证书路径",
 5    AppCertPath = "商户应用证书路径",
 6    RootCertPath = "支付宝根证书路径"
 7};
 8IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", APPID, APP_PRIVATE_KEY, "json", "1.0", "RSA2", "utf-8", false, certParams);
 9//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.user.userinfo.share
10AlipayUserUserinfoShareRequest request = new AlipayUserUserinfoShareRequest();
11//授权类接口执行API调用时需要带上accessToken
12AlipayUserUserinfoShareResponse response= client.CertificateExecute(request, "accessToken"); 
13//业务处理
14//...


应用授权接口调用示例(ISV 代理商户调用)


 1IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", APPID, APP_PRIVATE_KEY, "json", "1.0", "RSA2", ALIPAY_PUBLIC_KEY, CHARSET, false);
 2//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.open.public.template.message.industry.modify 
 3AlipayOpenPublicTemplateMessageIndustryModifyRequest request = new AlipayOpenPublicTemplateMessageIndustryModifyRequest();
 4//SDK已经封装掉了公共参数,这里只需要传入业务参数
 5//此次只是参数展示,未进行字符串转义,实际情况下请转义
 6request.setBizContent("  {" +
 7"    \"primary_industry_name\":\"IT科技/IT软件与服务\"," +
 8"    \"primary_industry_code\":\"10001/20102\"," +
 9"    \"secondary_industry_code\":\"10001/20102\"," +
10"    \"secondary_industry_name\":\"IT科技/IT软件与服务\"" +
11"  }");
12AlipayOpenPublicTemplateMessageIndustryModifyResponse response = client.Execute(request, null, "app_auth_token"); 
13//调用成功,则处理业务逻辑
14if(response.isSuccess()){
15    //.....
16}

应用授权接口调用示例(ISV 代理商户调用)(证书)


 1//设置证书相关参数
 2CertParams certParams = new CertParams
 3{
 4    AlipayPublicCertPath = "支付宝公钥证书路径",
 5    AppCertPath = "商户应用证书路径",
 6    RootCertPath = "支付宝根证书路径"
 7};
 8IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", APPID, APP_PRIVATE_KEY, "json", "1.0", "RSA2", "utf-8", false, certParams);
 9//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.open.public.template.message.industry.modify 
10AlipayOpenPublicTemplateMessageIndustryModifyRequest request = new AlipayOpenPublicTemplateMessageIndustryModifyRequest();
11//SDK已经封装掉了公共参数,这里只需要传入业务参数
12//此次只是参数展示,未进行字符串转义,实际情况下请转义
13request.setBizContent("  {" +
14"    \"primary_industry_name\":\"IT科技/IT软件与服务\"," +
15"    \"primary_industry_code\":\"10001/20102\"," +
16"    \"secondary_industry_code\":\"10001/20102\"," +
17"    \"secondary_industry_name\":\"IT科技/IT软件与服务\"" +
18"  }");
19AlipayOpenPublicTemplateMessageIndustryModifyResponse response = client.CertificateExecute(request, null, "app_auth_token"); 
20//调用成功,则处理业务逻辑
21if(response.isSuccess()){
22    //.....
23}


PHP SDK 集成示例


普通调用示例


 1$c = new AopClient;
 2$c->gatewayUrl = "https://openapi.alipay.com/gateway.do";
 3$c->appId = "app_id";
 4$c->rsaPrivateKey = '请填写开发者私钥去头去尾去回车,一行字符串' ;
 5$c->format = "json";
 6$c->charset= "GBK";
 7$c->signType= "RSA2";
 8$c->alipayrsaPublicKey = '请填写支付宝公钥,一行字符串';
 9//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.open.public.template.message.industry.modify
10$request = new AlipayOpenPublicTemplateMessageIndustryModifyRequest();
11//SDK已经封装掉了公共参数,这里只需要传入业务参数
12//此次只是参数展示,未进行字符串转义,实际情况下请转义
13$request->setBizContent = "{" .
14"    \"primary_industry_name\":\"IT科技/IT软件与服务\"," .
15"    \"primary_industry_code\":\"10001/20102\"," .
16"    \"secondary_industry_code\":\"10001/20102\"," .
17"    \"secondary_industry_name\":\"IT科技/IT软件与服务\"" .
18" }";
19$response= $c->execute($request);


普通调用示例(证书)

 1$c = new AopCertClient;
 2$appCertPath = "应用证书路径(要确保证书文件可读),例如:/home/admin/cert/appCertPublicKey.crt";
 3$alipayCertPath = "支付宝公钥证书路径(要确保证书文件可读),例如:/home/admin/cert/alipayCertPublicKey_RSA2.crt";
 4$rootCertPath = "支付宝根证书路径(要确保证书文件可读),例如:/home/admin/cert/alipayRootCert.crt";
 5
 6$c->gatewayUrl = "https://openapi.alipay.com/gateway.do";
 7$c->appId = "app_id";
 8$c->rsaPrivateKey = '请填写开发者私钥去头去尾去回车,一行字符串' ;
 9$c->format = "json";
10$c->charset= "GBK";
11$c->signType= "RSA2";
12
13//调用getPublicKey从支付宝公钥证书中提取公钥
14$c->alipayrsaPublicKey = $c->getPublicKey($alipayCertPath);
15//是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
16$c->isCheckAlipayPublicCert = true;
17//调用getCertSN获取证书序列号
18$c->appCertSN = $c->getCertSN($appCertPath);
19//调用getRootCertSN获取支付宝根证书序列号
20$c->alipayRootCertSN = $c->getRootCertSN($rootCertPath);
21
22//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.open.public.template.message.industry.modify
23$request = new AlipayOpenPublicTemplateMessageIndustryModifyRequest();
24//SDK已经封装掉了公共参数,这里只需要传入业务参数
25//此次只是参数展示,未进行字符串转义,实际情况下请转义
26$request->setBizContent = "{" .
27"    \"primary_industry_name\":\"IT科技/IT软件与服务\"," .
28"    \"primary_industry_code\":\"10001/20102\"," .
29"    \"secondary_industry_code\":\"10001/20102\"," .
30"    \"secondary_industry_name\":\"IT科技/IT软件与服务\"" .
31" }";
32$response= $c->execute($request);

图片上传接口调用示例


 1$c = new AopClient;
 2$c->gatewayUrl = "https://openapi.alipay.com/gateway.do";
 3$c->appId = "app_id";
 4$c->rsaPrivateKey = '请填写开发者私钥去头去尾去回车,一行字符串' ;
 5$c->format = "json";
 6$c->charset = "GBK";
 7$c->signType= "RSA2";
 8$c->alipayrsaPublicKey = '请填写支付宝公钥,一行字符串';
 9//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.offline.material.image.upload 
10$request = new AlipayOfflineMaterialImageUploadRequest();
11$request->setImageName("测试文件");
12$request->setImageType("jpg");
13//Windows请填写绝对路径,不支持相对路径;Linux支持相对路径
14$request->setImageContent("@"."本地文件路径"); 
15$response = $c->execute($request);
16//获取图片地址
17$response->getImageUrl();


图片上传接口调用示例(证书)


 1$c = new AopCertClient;
 2$appCertPath = "应用证书路径(要确保证书文件可读),例如:/home/admin/cert/appCertPublicKey.crt";
 3$alipayCertPath = "支付宝公钥证书路径(要确保证书文件可读),例如:/home/admin/cert/alipayCertPublicKey_RSA2.crt";
 4$rootCertPath = "支付宝根证书路径(要确保证书文件可读),例如:/home/admin/cert/alipayRootCert.crt";
 5
 6$c->gatewayUrl = "https://openapi.alipay.com/gateway.do";
 7$c->appId = "app_id";
 8$c->rsaPrivateKey = '请填写开发者私钥去头去尾去回车,一行字符串' ;
 9$c->format = "json";
10$c->charset = "GBK";
11$c->signType= "RSA2";
12
13//调用getPublicKey从支付宝公钥证书中提取公钥
14$c->alipayrsaPublicKey = $c->getPublicKey($alipayCertPath);
15//是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
16$c->isCheckAlipayPublicCert = true;
17//调用getCertSN获取证书序列号
18$c->appCertSN = $c->getCertSN($appCertPath);
19//调用getRootCertSN获取支付宝根证书序列号
20$c->alipayRootCertSN = $c->getRootCertSN($rootCertPath);
21
22//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.offline.material.image.upload 
23$request = new AlipayOfflineMaterialImageUploadRequest();
24$request->setImageName("测试文件");
25$request->setImageType("jpg");
26//Windows请填写绝对路径,不支持相对路径;Linux支持相对路径
27$request->setImageContent("@"."本地文件路径"); 
28$response = $c->execute($request);
29//获取图片地址
30$response->getImageUrl();

用户授权接口调用示例


 1$c = new AopClient;
 2$c->gatewayUrl = "https://openapi.alipay.com/gateway.do";
 3$c->appId = "app_id";
 4$c->rsaPrivateKey = '请填写开发者私钥去头去尾去回车,一行字符串' ;
 5$c->format = "json";
 6$c->charset = "GBK";
 7$c->signType= "RSA2";
 8$c->alipayrsaPublicKey = '请填写支付宝公钥,一行字符串';
 9//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.user.userinfo.share
10$request= new AlipayUserUserinfoShareRequest();
11//授权类接口执行API调用时需要带上accessToken
12$response= $c->execute($request,"accessToken");


用户授权接口调用示例(证书)


 1$c = new AopCertClient;
 2$appCertPath = "应用证书路径(要确保证书文件可读),例如:/home/admin/cert/appCertPublicKey.crt";
 3$alipayCertPath = "支付宝公钥证书路径(要确保证书文件可读),例如:/home/admin/cert/alipayCertPublicKey_RSA2.crt";
 4$rootCertPath = "支付宝根证书路径(要确保证书文件可读),例如:/home/admin/cert/alipayRootCert.crt";
 5
 6$c->gatewayUrl = "https://openapi.alipay.com/gateway.do";
 7$c->appId = "app_id";
 8$c->rsaPrivateKey = '请填写开发者私钥去头去尾去回车,一行字符串' ;
 9$c->format = "json";
10$c->charset = "GBK";
11$c->signType= "RSA2";
12
13//调用getPublicKey从支付宝公钥证书中提取公钥
14$c->alipayrsaPublicKey = $c->getPublicKey($alipayCertPath);
15//是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
16$c->isCheckAlipayPublicCert = true;
17//调用getCertSN获取证书序列号
18$c->appCertSN = $c->getCertSN($appCertPath);
19//调用getRootCertSN获取支付宝根证书序列号
20$c->alipayRootCertSN = $c->getRootCertSN($rootCertPath);
21
22//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.user.userinfo.share
23$request= new AlipayUserUserinfoShareRequest();
24//授权类接口执行API调用时需要带上accessToken
25$response= $c->execute($request,"accessToken");



应用授权接口调用示例(ISV 代理商户调用)


 1$c = new AopClient;
 2$c->gatewayUrl = "https://openapi.alipay.com/gateway.do";
 3$c->appId = "app_id";
 4$c->rsaPrivateKey = '请填写开发者私钥去头去尾去回车,一行字符串' ;
 5$c->format = "json";
 6$c->charset = "GBK";
 7$c->signType= "RSA2";
 8$c->alipayrsaPublicKey = '请填写支付宝公钥,一行字符串';
 9## 实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.open.public.template.message.industry.modify
10$request = new AlipayOpenPublicTemplateMessageIndustryModifyRequest();
11//SDK已经封装掉了公共参数,这里只需要传入业务参数
12//此次只是参数展示,未进行字符串转义,实际情况下请转义
13$request->bizContent = "{" .
14"    \"primary_industry_name\":\"IT科技/IT软件与服务\"," .
15"    \"primary_industry_code\":\"10001/20102\"," .
16"    \"secondary_industry_code\":\"10001/20102\"," .
17"    \"secondary_industry_name\":\"IT科技/IT软件与服务\"" .
18"  }";
19//ISV代理商户调用需要传入app_auth_token
20$response= $c->execute($request,NULL,"app_auth_token");


应用授权接口调用示例(ISV 代理商户调用)(证书)


 1$c = new AopCertClient;
 2$appCertPath = "应用证书路径(要确保证书文件可读),例如:/home/admin/cert/appCertPublicKey.crt";
 3$alipayCertPath = "支付宝公钥证书路径(要确保证书文件可读),例如:/home/admin/cert/alipayCertPublicKey_RSA2.crt";
 4$rootCertPath = "支付宝根证书路径(要确保证书文件可读),例如:/home/admin/cert/alipayRootCert.crt";
 5
 6$c->gatewayUrl = "https://openapi.alipay.com/gateway.do";
 7$c->appId = "app_id";
 8$c->rsaPrivateKey = '请填写开发者私钥去头去尾去回车,一行字符串' ;
 9$c->format = "json";
10$c->charset = "GBK";
11$c->signType= "RSA2";
12
13//调用getPublicKey从支付宝公钥证书中提取公钥
14$c->alipayrsaPublicKey = $c->getPublicKey($alipayCertPath);
15//是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
16$c->isCheckAlipayPublicCert = true;
17//调用getCertSN获取证书序列号
18$c->appCertSN = $c->getCertSN($appCertPath);
19//调用getRootCertSN获取支付宝根证书序列号
20$c->alipayRootCertSN = $c->getRootCertSN($rootCertPath);
21
22## 实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.open.public.template.message.industry.modify
23$request = new AlipayOpenPublicTemplateMessageIndustryModifyRequest();
24//SDK已经封装掉了公共参数,这里只需要传入业务参数
25//此次只是参数展示,未进行字符串转义,实际情况下请转义
26$request->bizContent = "{" .
27"    \"primary_industry_name\":\"IT科技/IT软件与服务\"," .
28"    \"primary_industry_code\":\"10001/20102\"," .
29"    \"secondary_industry_code\":\"10001/20102\"," .
30"    \"secondary_industry_name\":\"IT科技/IT软件与服务\"" .
31"  }";
32//ISV代理商户调用需要传入app_auth_token
33$response= $c->execute($request,NULL,"app_auth_token");


Python SDK 集成示例

普通调用示例


 1#!/usr/bin/env python
 2# -*- coding: utf-8 -*-
 3
 4import logging
 5
 6from alipay.aop.api.AlipayClientConfig import AlipayClientConfig
 7from alipay.aop.api.DefaultAlipayClient import DefaultAlipayClient
 8
 9from alipay.aop.api.domain.AlipayTradeCreateModel import AlipayTradeCreateModel
10from alipay.aop.api.request.AlipayTradeCreateRequest import AlipayTradeCreateRequest
11from alipay.aop.api.response.AlipayTradeCreateResponse import AlipayTradeCreateResponse
12
13logging.basicConfig(
14    level=logging.INFO,
15    format='%(asctime)s %(levelname)s %(message)s',
16    filemode='a',)
17logger = logging.getLogger('')
18
19if __name__ == '__main__':
20    # 实例化客户端
21    alipay_client_config = AlipayClientConfig()
22    alipay_client_config.server_url = 'https://openapi.alipaydev.com/gateway.do'
23    alipay_client_config.app_id = '请填写appi_id'
24    alipay_client_config.app_private_key = '请填写开发者私钥去头去尾去回车,单行字符串'
25    alipay_client_config.alipay_public_key = '请填写支付宝公钥,单行字符串'
26    client = DefaultAlipayClient(alipay_client_config, logger)
27
28    # 构造请求参数对象
29    model = AlipayTradeCreateModel()
30    model.out_trade_no = "20150320010101001";
31    model.total_amount = "88.88";
32    model.subject = "Iphone6 16G";
33    model.buyer_id = "2088102177846880";
34    request = AlipayTradeCreateRequest(biz_model=model)
35
36    # 执行API调用
37    try:
38        response_content = client.execute(request)
39    except Exception as e:
40        print(traceback.format_exc())
41
42    if not response_content:
43        print("failed execute")
44    else:
45        # 解析响应结果
46        response = AlipayTradeCreateResponse()
47        response.parse_response_content(response_content)
48        # 响应成功的业务处理
49        if response.is_success():
50            # 如果业务成功,可以通过response属性获取需要的值
51            print("get response trade_no:" + response.trade_no)
52        # 响应失败的业务处理
53        else:
54            # 如果业务失败,可以从错误码中可以得知错误情况,具体错误码信息可以查看接口文档
55            print(response.code + "," + response.msg + "," + response.sub_code + "," + response.sub_msg)


图片上传接口调用示例


 1#!/usr/bin/env python
 2# -*- coding: utf-8 -*-
 3
 4import logging
 5
 6from alipay.aop.api.AlipayClientConfig import AlipayClientConfig
 7from alipay.aop.api.DefaultAlipayClient import DefaultAlipayClient
 8
 9from alipay.aop.api.FileItem import FileItem
10from alipay.aop.api.request.AlipayOfflineMaterialImageUploadRequest import AlipayOfflineMaterialImageUploadRequest
11from alipay.aop.api.response.AlipayOfflineMaterialImageUploadResponse import AlipayOfflineMaterialImageUploadResponse
12
13logging.basicConfig(
14    level=logging.INFO,
15    format='%(asctime)s %(levelname)s %(message)s',
16    filemode='a',)
17logger = logging.getLogger('')
18
19if __name__ == '__main__':
20    # 实例化客户端
21    alipay_client_config = AlipayClientConfig()
22    alipay_client_config.server_url = 'https://openapi.alipaydev.com/gateway.do'
23    alipay_client_config.app_id = '请填写appi_id'
24    alipay_client_config.app_private_key = '请填写开发者私钥去头去尾去回车,单行字符串'
25    alipay_client_config.alipay_public_key = '请填写支付宝公钥,单行字符串'
26    client = DefaultAlipayClient(alipay_client_config, logger)
27
28    # 构造请求参数对象
29    request = AlipayOfflineMaterialImageUploadRequest()
30    request.image_name = "我的店"
31    request.image_type = "jpg"
32    file = open("/Users/foo/Downloads/test.jpg", "rb")
33    request.image_content = FileItem(file_name="test.jpg", file_content=file.read())
34    file.close()
35
36    # 执行API调用
37    try:
38        response_content = client.execute(request)
39    except Exception as e:
40        print(traceback.format_exc())
41
42    if not response_content:
43        print("failed execute")
44    else:
45        # 解析响应结果
46        response = AlipayOfflineMaterialImageUploadResponse()
47        response.parse_response_content(response_content)
48        # 响应成功的业务处理
49        if response.is_success():
50            # 如果业务成功,可以通过response属性获取需要的值
51            print("get response image_url:" + response.image_url)
52        # 响应失败的业务处理
53        else:
54            # 如果业务失败,可以从错误码中可以得知错误情况,具体错误码信息可以查看接口文档
55            print(response.code + "," + response.msg + "," + response.sub_code + "," + response.sub_msg)
56


用户授权接口调用示例


 1#!/usr/bin/env python
 2# -*- coding: utf-8 -*-
 3
 4import logging
 5
 6from alipay.aop.api.AlipayClientConfig import AlipayClientConfig
 7from alipay.aop.api.DefaultAlipayClient import DefaultAlipayClient
 8from alipay.aop.api.constant.ParamConstants import *
 9
10from alipay.aop.api.domain.AlipayEbppInvoiceTitleListGetModel import AlipayEbppInvoiceTitleListGetModel
11from alipay.aop.api.request.AlipayEbppInvoiceTitleListGetRequest import AlipayEbppInvoiceTitleListGetRequest
12from alipay.aop.api.response.AlipayEbppInvoiceTitleListGetResponse import AlipayEbppInvoiceTitleListGetResponse
13
14logging.basicConfig(
15    level=logging.INFO,
16    format='%(asctime)s %(levelname)s %(message)s',
17    filemode='a',)
18logger = logging.getLogger('')
19
20if __name__ == '__main__':
21    # 实例化客户端
22    alipay_client_config = AlipayClientConfig()
23    alipay_client_config.server_url = 'https://openapi.alipaydev.com/gateway.do'
24    alipay_client_config.app_id = '请填写appi_id'
25    alipay_client_config.app_private_key = '请填写开发者私钥去头去尾去回车,单行字符串'
26    alipay_client_config.alipay_public_key = '请填写支付宝公钥,单行字符串'
27    client = DefaultAlipayClient(alipay_client_config, logger)
28
29    # 构造请求参数对象
30    model = AlipayEbppInvoiceTitleListGetModel()
31    model.user_id = "2088102177492087"
32    request = AlipayEbppInvoiceTitleListGetRequest(biz_model=model)
33
34    # 添加auth_token
35    udf_params = dict()
36    udf_params[P_AUTH_TOKEN] = "auth_token,使用时替换成正确的token"
37    request.udf_params = udf_params
38
39    # 执行API调用
40    try:
41        response_content = client.execute(request)
42    except Exception as e:
43        print(traceback.format_exc())
44
45    if not response_content:
46        print("failed execute")
47    else:
48        # 解析响应结果
49        response = AlipayEbppInvoiceTitleListGetResponse()
50        response.parse_response_content(response_content)
51        # 响应成功的业务处理
52        if response.is_success():
53            # 如果业务成功,可以通过response属性获取需要的值
54            print("get response trade_no:" + response.trade_no)
55        # 响应失败的业务处理
56        else:
57            # 如果业务失败,可以从错误码中可以得知错误情况,具体错误码信息可以查看接口文档
58            print(response.code + "," + response.msg + "," + response.sub_code + "," + response.sub_msg)
59


应用授权接口调用示例(ISV 代理商户调用)


 1#!/usr/bin/env python
 2# -*- coding: utf-8 -*-
 3
 4import logging
 5
 6from alipay.aop.api.AlipayClientConfig import AlipayClientConfig
 7from alipay.aop.api.DefaultAlipayClient import DefaultAlipayClient
 8from alipay.aop.api.constant.ParamConstants import *
 9
10from alipay.aop.api.domain.AlipayTradeCreateModel import AlipayTradeCreateModel
11from alipay.aop.api.request.AlipayTradeCreateRequest import AlipayTradeCreateRequest
12from alipay.aop.api.response.AlipayTradeCreateResponse import AlipayTradeCreateResponse
13
14logging.basicConfig(
15    level=logging.INFO,
16    format='%(asctime)s %(levelname)s %(message)s',
17    filemode='a',)
18logger = logging.getLogger('')
19
20if __name__ == '__main__':
21    # 实例化客户端
22    alipay_client_config = AlipayClientConfig()
23    alipay_client_config.server_url = 'https://openapi.alipaydev.com/gateway.do'
24    alipay_client_config.app_id = '请填写appi_id'
25    alipay_client_config.app_private_key = '请填写开发者私钥去头去尾去回车,单行字符串'
26    alipay_client_config.alipay_public_key = '请填写支付宝公钥,单行字符串'
27    client = DefaultAlipayClient(alipay_client_config, logger)
28
29    # 构造请求参数对象
30    model = AlipayTradeCreateModel()
31    model.out_trade_no = "20150320010101001";
32    model.total_amount = "88.88";
33    model.subject = "Iphone6 16G";
34    model.buyer_id = "2088102177846880";
35    request = AlipayTradeCreateRequest(biz_model=model)
36
37    # 添加app_auth_token
38    udf_params = dict()
39    udf_params[P_APP_AUTH_TOKEN] = "app_auth_token,使用时替换成正确的token"
40    request.udf_params = udf_params
41
42    # 执行API调用
43    try:
44        response_content = client.execute(request)
45    except Exception as e:
46        print(traceback.format_exc())
47
48    if not response_content:
49        print("failed execute")
50    else:
51        # 解析响应结果
52        response = AlipayTradeCreateResponse()
53        response.parse_response_content(response_content)
54        # 响应成功的业务处理
55        if response.is_success():
56            # 如果业务成功,可以通过response属性获取需要的值
57            print("get response trade_no:" + response.trade_no)
58        # 响应失败的业务处理
59        else:
60            # 如果业务失败,可以从错误码中可以得知错误情况,具体错误码信息可以查看接口文档
61            print(response.code + "," + response.msg + "," + response.sub_code + "," + response.sub_msg)
62


签名方法


加签方法(如果不用 SDK 调用,推荐用该方法加签)


1/**
2@param content 待签名字符串
3@param privateKey 加签私钥
4@param charset 加签字符集
5@param sign_type 签名方式
6**/
7String AlipaySignature.rsaSign(String content, String privateKey, String charset, String sign_type)


验签方法(例如异步通知的时候,用户需要用到验签方法)


验签方法,根据接口不同,验签时会去掉sign_type(rsaCheckV1)或者保留sign_type(rsaCheckV2)。


 1/**    此方法会去掉sign_type做验签,暂时除生活号(原服务窗)激活开发者模式外都使用V1。
 2@param params 参数列表(包括待验签参数和签名值sign) key-参数名称 value-参数值
 3@param publicKey 验签支付宝公钥
 4@param charset 验签字符集
 5@param sign_type 验签方式(RSA或RSA2)
 6**/
 7boolean AlipaySignature.rsaCheckV1(Map<String, String> params, String publicKey, String charset, String sign_type)
 8
 9/**    此方法不会去掉sign_type验签,用于生活号(原服务窗)激活开发者模式
10@param params 参数列表(包括待验签参数和签名值sign) key-参数名称 value-参数值
11@param publicKey 验签支付宝公钥
12@param charset 验签字符集
13@param sign_type 验签方式(RSA或RSA2)
14**/
15boolean AlipaySignature.rsaCheckV2(Map<String, String> params, String publicKey, String charset, String sign_type)
16
17/**    此方法会去掉sign_type做验签,暂时除生活号(原服务窗)激活开发者模式外都使用V1。
18@param params 参数列表(包括待验签参数和签名值sign) key-参数名称 value-参数值
19@param publicKeyCertPath 验签支付宝公钥证书文件路径
20@param charset 验签字符集
21@param sign_type 验签方式(RSA或RSA2)
22**/
23boolean AlipaySignature.rsaCertCheckV1(Map<String, String> params, String publicKeyCertPath,
24                                         String charset,String signType)
25
26
27/**    此方法不会去掉sign_type验签,用于生活号(原服务窗)激活开发者模式
28@param params 参数列表(包括待验签参数和签名值sign) key-参数名称 value-参数值
29@param publicKeyCertPath 验签支付宝公钥证书文件路径
30@param charset 验签字符集
31@param sign_type 验签方式(RSA或RSA2)
32**/
33boolean AlipaySignature.rsaCertCheckV2(Map<String, String> params, String alipayPublicCertPath,
34                                         String charset,String signType)


注意事项


  • AlipayClient 的实现类都是线程安全的,所以没有必要每次 API 请求都新建一个 AlipayClient 实现类;

  • 创建 AlipayClient 实现类的实例时,指定 format=json,相比 xml 格式,可以减少数据传输量,提升 API 请求效率。


onlineServer