服务端 SDK

SDK 获取


开发语言

资源下载

环境要求

JAVA 版资源

Maven项目依赖

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

.NET 版资源

NuGet项目依赖

适用于符合.Net Standard 2.0规范的各类微软框架(比如.Net Framework >= 4.6.1、.Net Core >= 2.0等)。

.Net Framework(3.5 ~ 4.6)的用户可以继续使用AlipaySDKNet

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


普通调用示例


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


普通调用示例(证书)


//构造client
CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
//设置网关地址
certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");
//设置应用Id
certAlipayRequest.setAppId(app_id);
//设置应用私钥
certAlipayRequest.setPrivateKey(privateKey);
//设置请求格式,固定值json
certAlipayRequest.setFormat("json");
//设置字符集
certAlipayRequest.setCharset(charset);
//设置签名类型
certAlipayRequest.setSignType(sign_type);
//设置应用公钥证书路径
certAlipayRequest.setCertPath(app_cert_path);
//设置支付宝公钥证书路径
certAlipayRequest.setAlipayPublicCertPath(alipay_cert_path);
//设置支付宝根证书路径
certAlipayRequest.setRootCertPath(alipay_root_cert_path);
//构造client
AlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
//构造API请求
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
//发送请求
AlipayTradeQueryResponse response = alipayClient.certificateExecute(request);


图片上传接口调用示例


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


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


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


用户授权接口调用示例


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


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

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


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


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


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


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


.Net SDK 集成示例


普通调用示例


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

普通调用示例(证书)


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


图片上传接口调用示例


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

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


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


用户授权接口调用示例


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

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


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


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


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

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


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


PHP SDK 集成示例


普通调用示例


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


普通调用示例(证书)

$c = new AopCertClient;
$appCertPath = "应用证书路径(要确保证书文件可读),例如:/home/admin/cert/appCertPublicKey.crt";
$alipayCertPath = "支付宝公钥证书路径(要确保证书文件可读),例如:/home/admin/cert/alipayCertPublicKey_RSA2.crt";
$rootCertPath = "支付宝根证书路径(要确保证书文件可读),例如:/home/admin/cert/alipayRootCert.crt";

$c->gatewayUrl = "https://openapi.alipay.com/gateway.do";
$c->appId = "app_id";
$c->rsaPrivateKey = '请填写开发者私钥去头去尾去回车,一行字符串' ;
$c->format = "json";
$c->charset= "GBK";
$c->signType= "RSA2";

//调用getPublicKey从支付宝公钥证书中提取公钥
$c->alipayrsaPublicKey = $c->getPublicKey($alipayCertPath);
//是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
$c->isCheckAlipayPublicCert = true;
//调用getCertSN获取证书序列号
$c->appCertSN = $c->getCertSN($appCertPath);
//调用getRootCertSN获取支付宝根证书序列号
$c->alipayRootCertSN = $c->getRootCertSN($rootCertPath);

//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.open.public.template.message.industry.modify
$request = new AlipayOpenPublicTemplateMessageIndustryModifyRequest();
//SDK已经封装掉了公共参数,这里只需要传入业务参数
//此次只是参数展示,未进行字符串转义,实际情况下请转义
$request->setBizContent = "{" .
"    \"primary_industry_name\":\"IT科技/IT软件与服务\"," .
"    \"primary_industry_code\":\"10001/20102\"," .
"    \"secondary_industry_code\":\"10001/20102\"," .
"    \"secondary_industry_name\":\"IT科技/IT软件与服务\"" .
" }";
$response= $c->execute($request);

图片上传接口调用示例


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


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


$c = new AopCertClient;
$appCertPath = "应用证书路径(要确保证书文件可读),例如:/home/admin/cert/appCertPublicKey.crt";
$alipayCertPath = "支付宝公钥证书路径(要确保证书文件可读),例如:/home/admin/cert/alipayCertPublicKey_RSA2.crt";
$rootCertPath = "支付宝根证书路径(要确保证书文件可读),例如:/home/admin/cert/alipayRootCert.crt";

$c->gatewayUrl = "https://openapi.alipay.com/gateway.do";
$c->appId = "app_id";
$c->rsaPrivateKey = '请填写开发者私钥去头去尾去回车,一行字符串' ;
$c->format = "json";
$c->charset = "GBK";
$c->signType= "RSA2";

//调用getPublicKey从支付宝公钥证书中提取公钥
$c->alipayrsaPublicKey = $c->getPublicKey($alipayCertPath);
//是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
$c->isCheckAlipayPublicCert = true;
//调用getCertSN获取证书序列号
$c->appCertSN = $c->getCertSN($appCertPath);
//调用getRootCertSN获取支付宝根证书序列号
$c->alipayRootCertSN = $c->getRootCertSN($rootCertPath);

//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.offline.material.image.upload 
$request = new AlipayOfflineMaterialImageUploadRequest();
$request->setImageName("测试文件");
$request->setImageType("jpg");
//Windows请填写绝对路径,不支持相对路径;Linux支持相对路径
$request->setImageContent("@"."本地文件路径"); 
$response = $c->execute($request);
//获取图片地址
$response->getImageUrl();

用户授权接口调用示例


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


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


$c = new AopCertClient;
$appCertPath = "应用证书路径(要确保证书文件可读),例如:/home/admin/cert/appCertPublicKey.crt";
$alipayCertPath = "支付宝公钥证书路径(要确保证书文件可读),例如:/home/admin/cert/alipayCertPublicKey_RSA2.crt";
$rootCertPath = "支付宝根证书路径(要确保证书文件可读),例如:/home/admin/cert/alipayRootCert.crt";

$c->gatewayUrl = "https://openapi.alipay.com/gateway.do";
$c->appId = "app_id";
$c->rsaPrivateKey = '请填写开发者私钥去头去尾去回车,一行字符串' ;
$c->format = "json";
$c->charset = "GBK";
$c->signType= "RSA2";

//调用getPublicKey从支付宝公钥证书中提取公钥
$c->alipayrsaPublicKey = $c->getPublicKey($alipayCertPath);
//是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
$c->isCheckAlipayPublicCert = true;
//调用getCertSN获取证书序列号
$c->appCertSN = $c->getCertSN($appCertPath);
//调用getRootCertSN获取支付宝根证书序列号
$c->alipayRootCertSN = $c->getRootCertSN($rootCertPath);

//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.user.userinfo.share
$request= new AlipayUserUserinfoShareRequest();
//授权类接口执行API调用时需要带上accessToken
$response= $c->execute($request,"accessToken");



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


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


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


$c = new AopCertClient;
$appCertPath = "应用证书路径(要确保证书文件可读),例如:/home/admin/cert/appCertPublicKey.crt";
$alipayCertPath = "支付宝公钥证书路径(要确保证书文件可读),例如:/home/admin/cert/alipayCertPublicKey_RSA2.crt";
$rootCertPath = "支付宝根证书路径(要确保证书文件可读),例如:/home/admin/cert/alipayRootCert.crt";

$c->gatewayUrl = "https://openapi.alipay.com/gateway.do";
$c->appId = "app_id";
$c->rsaPrivateKey = '请填写开发者私钥去头去尾去回车,一行字符串' ;
$c->format = "json";
$c->charset = "GBK";
$c->signType= "RSA2";

//调用getPublicKey从支付宝公钥证书中提取公钥
$c->alipayrsaPublicKey = $c->getPublicKey($alipayCertPath);
//是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
$c->isCheckAlipayPublicCert = true;
//调用getCertSN获取证书序列号
$c->appCertSN = $c->getCertSN($appCertPath);
//调用getRootCertSN获取支付宝根证书序列号
$c->alipayRootCertSN = $c->getRootCertSN($rootCertPath);

## 实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.open.public.template.message.industry.modify
$request = new AlipayOpenPublicTemplateMessageIndustryModifyRequest();
//SDK已经封装掉了公共参数,这里只需要传入业务参数
//此次只是参数展示,未进行字符串转义,实际情况下请转义
$request->bizContent = "{" .
"    \"primary_industry_name\":\"IT科技/IT软件与服务\"," .
"    \"primary_industry_code\":\"10001/20102\"," .
"    \"secondary_industry_code\":\"10001/20102\"," .
"    \"secondary_industry_name\":\"IT科技/IT软件与服务\"" .
"  }";
//ISV代理商户调用需要传入app_auth_token
$response= $c->execute($request,NULL,"app_auth_token");


Python SDK 集成示例

普通调用示例


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging

from alipay.aop.api.AlipayClientConfig import AlipayClientConfig
from alipay.aop.api.DefaultAlipayClient import DefaultAlipayClient

from alipay.aop.api.domain.AlipayTradeCreateModel import AlipayTradeCreateModel
from alipay.aop.api.request.AlipayTradeCreateRequest import AlipayTradeCreateRequest
from alipay.aop.api.response.AlipayTradeCreateResponse import AlipayTradeCreateResponse

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s %(levelname)s %(message)s',
    filemode='a',)
logger = logging.getLogger('')

if __name__ == '__main__':
    # 实例化客户端
    alipay_client_config = AlipayClientConfig()
    alipay_client_config.server_url = 'https://openapi.alipaydev.com/gateway.do'
    alipay_client_config.app_id = '请填写appi_id'
    alipay_client_config.app_private_key = '请填写开发者私钥去头去尾去回车,单行字符串'
    alipay_client_config.alipay_public_key = '请填写支付宝公钥,单行字符串'
    client = DefaultAlipayClient(alipay_client_config, logger)

    # 构造请求参数对象
    model = AlipayTradeCreateModel()
    model.out_trade_no = "20150320010101001";
    model.total_amount = "88.88";
    model.subject = "Iphone6 16G";
    model.buyer_id = "2088102177846880";
    request = AlipayTradeCreateRequest(biz_model=model)

    # 执行API调用
    try:
        response_content = client.execute(request)
    except Exception as e:
        print(traceback.format_exc())

    if not response_content:
        print("failed execute")
    else:
        # 解析响应结果
        response = AlipayTradeCreateResponse()
        response.parse_response_content(response_content)
        # 响应成功的业务处理
        if response.is_success():
            # 如果业务成功,可以通过response属性获取需要的值
            print("get response trade_no:" + response.trade_no)
        # 响应失败的业务处理
        else:
            # 如果业务失败,可以从错误码中可以得知错误情况,具体错误码信息可以查看接口文档
            print(response.code + "," + response.msg + "," + response.sub_code + "," + response.sub_msg)


图片上传接口调用示例


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging

from alipay.aop.api.AlipayClientConfig import AlipayClientConfig
from alipay.aop.api.DefaultAlipayClient import DefaultAlipayClient

from alipay.aop.api.FileItem import FileItem
from alipay.aop.api.request.AlipayOfflineMaterialImageUploadRequest import AlipayOfflineMaterialImageUploadRequest
from alipay.aop.api.response.AlipayOfflineMaterialImageUploadResponse import AlipayOfflineMaterialImageUploadResponse

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s %(levelname)s %(message)s',
    filemode='a',)
logger = logging.getLogger('')

if __name__ == '__main__':
    # 实例化客户端
    alipay_client_config = AlipayClientConfig()
    alipay_client_config.server_url = 'https://openapi.alipaydev.com/gateway.do'
    alipay_client_config.app_id = '请填写appi_id'
    alipay_client_config.app_private_key = '请填写开发者私钥去头去尾去回车,单行字符串'
    alipay_client_config.alipay_public_key = '请填写支付宝公钥,单行字符串'
    client = DefaultAlipayClient(alipay_client_config, logger)

    # 构造请求参数对象
    request = AlipayOfflineMaterialImageUploadRequest()
    request.image_name = "我的店"
    request.image_type = "jpg"
    file = open("/Users/foo/Downloads/test.jpg", "rb")
    request.image_content = FileItem(file_name="test.jpg", file_content=file.read())
    file.close()

    # 执行API调用
    try:
        response_content = client.execute(request)
    except Exception as e:
        print(traceback.format_exc())

    if not response_content:
        print("failed execute")
    else:
        # 解析响应结果
        response = AlipayOfflineMaterialImageUploadResponse()
        response.parse_response_content(response_content)
        # 响应成功的业务处理
        if response.is_success():
            # 如果业务成功,可以通过response属性获取需要的值
            print("get response image_url:" + response.image_url)
        # 响应失败的业务处理
        else:
            # 如果业务失败,可以从错误码中可以得知错误情况,具体错误码信息可以查看接口文档
            print(response.code + "," + response.msg + "," + response.sub_code + "," + response.sub_msg)


用户授权接口调用示例


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging

from alipay.aop.api.AlipayClientConfig import AlipayClientConfig
from alipay.aop.api.DefaultAlipayClient import DefaultAlipayClient
from alipay.aop.api.constant.ParamConstants import *

from alipay.aop.api.domain.AlipayEbppInvoiceTitleListGetModel import AlipayEbppInvoiceTitleListGetModel
from alipay.aop.api.request.AlipayEbppInvoiceTitleListGetRequest import AlipayEbppInvoiceTitleListGetRequest
from alipay.aop.api.response.AlipayEbppInvoiceTitleListGetResponse import AlipayEbppInvoiceTitleListGetResponse

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s %(levelname)s %(message)s',
    filemode='a',)
logger = logging.getLogger('')

if __name__ == '__main__':
    # 实例化客户端
    alipay_client_config = AlipayClientConfig()
    alipay_client_config.server_url = 'https://openapi.alipaydev.com/gateway.do'
    alipay_client_config.app_id = '请填写appi_id'
    alipay_client_config.app_private_key = '请填写开发者私钥去头去尾去回车,单行字符串'
    alipay_client_config.alipay_public_key = '请填写支付宝公钥,单行字符串'
    client = DefaultAlipayClient(alipay_client_config, logger)

    # 构造请求参数对象
    model = AlipayEbppInvoiceTitleListGetModel()
    model.user_id = "2088102177492087"
    request = AlipayEbppInvoiceTitleListGetRequest(biz_model=model)

    # 添加auth_token
    udf_params = dict()
    udf_params[P_AUTH_TOKEN] = "auth_token,使用时替换成正确的token"
    request.udf_params = udf_params

    # 执行API调用
    try:
        response_content = client.execute(request)
    except Exception as e:
        print(traceback.format_exc())

    if not response_content:
        print("failed execute")
    else:
        # 解析响应结果
        response = AlipayEbppInvoiceTitleListGetResponse()
        response.parse_response_content(response_content)
        # 响应成功的业务处理
        if response.is_success():
            # 如果业务成功,可以通过response属性获取需要的值
            print("get response trade_no:" + response.trade_no)
        # 响应失败的业务处理
        else:
            # 如果业务失败,可以从错误码中可以得知错误情况,具体错误码信息可以查看接口文档
            print(response.code + "," + response.msg + "," + response.sub_code + "," + response.sub_msg)


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


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging

from alipay.aop.api.AlipayClientConfig import AlipayClientConfig
from alipay.aop.api.DefaultAlipayClient import DefaultAlipayClient
from alipay.aop.api.constant.ParamConstants import *

from alipay.aop.api.domain.AlipayTradeCreateModel import AlipayTradeCreateModel
from alipay.aop.api.request.AlipayTradeCreateRequest import AlipayTradeCreateRequest
from alipay.aop.api.response.AlipayTradeCreateResponse import AlipayTradeCreateResponse

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s %(levelname)s %(message)s',
    filemode='a',)
logger = logging.getLogger('')

if __name__ == '__main__':
    # 实例化客户端
    alipay_client_config = AlipayClientConfig()
    alipay_client_config.server_url = 'https://openapi.alipaydev.com/gateway.do'
    alipay_client_config.app_id = '请填写appi_id'
    alipay_client_config.app_private_key = '请填写开发者私钥去头去尾去回车,单行字符串'
    alipay_client_config.alipay_public_key = '请填写支付宝公钥,单行字符串'
    client = DefaultAlipayClient(alipay_client_config, logger)

    # 构造请求参数对象
    model = AlipayTradeCreateModel()
    model.out_trade_no = "20150320010101001";
    model.total_amount = "88.88";
    model.subject = "Iphone6 16G";
    model.buyer_id = "2088102177846880";
    request = AlipayTradeCreateRequest(biz_model=model)

    # 添加app_auth_token
    udf_params = dict()
    udf_params[P_APP_AUTH_TOKEN] = "app_auth_token,使用时替换成正确的token"
    request.udf_params = udf_params

    # 执行API调用
    try:
        response_content = client.execute(request)
    except Exception as e:
        print(traceback.format_exc())

    if not response_content:
        print("failed execute")
    else:
        # 解析响应结果
        response = AlipayTradeCreateResponse()
        response.parse_response_content(response_content)
        # 响应成功的业务处理
        if response.is_success():
            # 如果业务成功,可以通过response属性获取需要的值
            print("get response trade_no:" + response.trade_no)
        # 响应失败的业务处理
        else:
            # 如果业务失败,可以从错误码中可以得知错误情况,具体错误码信息可以查看接口文档
            print(response.code + "," + response.msg + "," + response.sub_code + "," + response.sub_msg)


签名方法


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


/**
@param content 待签名字符串
@param privateKey 加签私钥
@param charset 加签字符集
@param sign_type 签名方式
**/
String AlipaySignature.rsaSign(String content, String privateKey, String charset, String sign_type)


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


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


/**    此方法会去掉sign_type做验签,暂时除生活号(原服务窗)激活开发者模式外都使用V1。
@param params 参数列表(包括待验签参数和签名值sign) key-参数名称 value-参数值
@param publicKey 验签支付宝公钥
@param charset 验签字符集
@param sign_type 验签方式(RSA或RSA2)
**/
boolean AlipaySignature.rsaCheckV1(Map<String, String> params, String publicKey, String charset, String sign_type)

/**    此方法不会去掉sign_type验签,用于生活号(原服务窗)激活开发者模式
@param params 参数列表(包括待验签参数和签名值sign) key-参数名称 value-参数值
@param publicKey 验签支付宝公钥
@param charset 验签字符集
@param sign_type 验签方式(RSA或RSA2)
**/
boolean AlipaySignature.rsaCheckV2(Map<String, String> params, String publicKey, String charset, String sign_type)

/**    此方法会去掉sign_type做验签,暂时除生活号(原服务窗)激活开发者模式外都使用V1。
@param params 参数列表(包括待验签参数和签名值sign) key-参数名称 value-参数值
@param publicKeyCertPath 验签支付宝公钥证书文件路径
@param charset 验签字符集
@param sign_type 验签方式(RSA或RSA2)
**/
boolean AlipaySignature.rsaCertCheckV1(Map<String, String> params, String publicKeyCertPath,
                                         String charset,String signType)


/**    此方法不会去掉sign_type验签,用于生活号(原服务窗)激活开发者模式
@param params 参数列表(包括待验签参数和签名值sign) key-参数名称 value-参数值
@param publicKeyCertPath 验签支付宝公钥证书文件路径
@param charset 验签字符集
@param sign_type 验签方式(RSA或RSA2)
**/
boolean AlipaySignature.rsaCertCheckV2(Map<String, String> params, String alipayPublicCertPath,
                                         String charset,String signType)


注意事项


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

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


onlineServer