服务端SDK

SDK获取

开发语言 资源下载 环境要求
JAVA版资源 SDK1.5 Maven项目依赖 适用于Java语言、jdk版本1.5及以上的开发环境
.NET版资源 SDK 适用于Visual studio 2010及以上版本、Framework3.5 SP1及以上版本的开发环境
PHP版资源 SDK 适用于php5.5以上的开发环境
PYTHON版资源(公测) PyPI项目依赖 适用于Python2.7及以上版本的开发环境
NodeJS版资源(公测) NPM项目依赖 适用于Node.js v8.0.0及以上版本的开发环境

SDK集成

  1. SDK已经对加签验签逻辑做了封装,使用SDK可直接调用API。
  2. 确定接口对应的类
    例如接口名:alipay.offline.material.image.upload
    在SDK中对应的类为:每个单词首字母大写,并去掉分隔符(“.”),末尾加上Request(或Response)
    如上接口名对应的类为:
    AlipayOfflineMaterialImageUploadRequest(请求类)
    AlipayOfflineMaterialImageUploadResponse(响应类)
    具体调用方式见下方各语言。

JavaSDK集成示例

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

注意

  • 集成支付宝接口需要引入的文件是:
    alipay-sdk-java*.jar
    commons-logging-1.1.1.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()){
	//.....
}

图片上传接口调用示例

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();
	//.....
}

用户授权接口调用示例

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"); 
//业务处理
//...

应用授权接口调用示例(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()){
	//.....
} 

.netSDK集成示例

普通调用示例

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()){
	//.....
}

图片上传接口调用示例

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.setImageContent(item);
//执行API请求
AlipayOfflineMaterialImageUploadResponse response = alipayClient.execute(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(req,"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()){
	//.....
} 

phpSDK集成示例

普通调用示例

$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软件与服务\"" .
" }";
$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 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");

应用授权接口调用示例(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");

方法说明

加签方法(如果不用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 验签字符集
**/
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 验签字符集
**/
boolean AlipaySignature.rsaCheckV2(Map<String, String> params, String publicKey, String charset, String sign_type)

注意事项

  • AlipayClient的实现类都是线程安全的,所以没有必要每次API请求都新建一个AlipayClient实现类;
  • 创建AlipayClient实现类的实例时,指定format=json,相比xml格式,可以减少数据传输量,提升API请求效率。
onlineServer