## PHP SDK 集成示例
### 普通调用示例
~~~php
$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);
~~~
### 普通调用示例(证书)
~~~php
$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);
~~~
### 图片上传接口调用示例
~~~php
$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();
~~~
### 图片上传接口调用示例(证书)
~~~php
$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();
~~~
### 用户授权接口调用示例
~~~php
$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");
~~~
### 用户授权接口调用示例(证书)
~~~php
$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 代理商户调用)
~~~php
$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 代理商户调用)(证书)
~~~php
$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 集成示例
### 普通调用示例
~~~php
#!/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)
~~~
### 图片上传接口调用示例
~~~php
#!/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)
~~~
### 用户授权接口调用示例
~~~php
#!/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 代理商户调用)
~~~php
#!/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 调用,推荐用该方法加签)
~~~java
/**
@param content 待签名字符串
@param privateKey 加签私钥
@param charset 加签字符集
@param signType 签名方式
**/
String AlipaySignature.rsaSign(String content, String privateKey, String charset, String signType);
~~~
### 验签方法(例如异步通知的时候,用户需要用到验签方法)
验签方法,根据接口不同,验签时会去掉 sign\_type(rsaCheckV1) 或者保留 sign\_type(rsaCheckV2)。
~~~java
/** 此方法会去掉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 请求效率。