To use the Alipay SDK, follow these three main steps:
- Initialize the client: Create an instance of
IAopClient (typically DefaultAopClient) with your gateway URL, AppID, private key, and certificate parameters. - Create the request: Instantiate the specific
Request object for the API you want to call and populate its BizModel. - Execute and handle response: Use
client.CertificateExecute(request) to send the request and check the IsError property of the response to handle success or failure.
Note: When using the recommended Certificate Mode, you must provide paths to your application public certificate, Alipay public certificate, and Alipay root certificate via CertParams.
using System;
using Aop.Api;
using Aop.Api.Request;
using Aop.Api.Response;
using Aop.Api.Domain;
namespace SDK.Demo
{
class Program
{
static void Main(string[] args)
{
try
{
// 1. Create IAopClient instance
IAopClient client = new DefaultAopClient(
"https://openapi.alipay.com/gateway.do",
"2019091767145019", // Replace with your AppId
"MIIEowIBAAKCAQ ... ...", // Replace with your PKCS1 format App Private Key
"json", "1.0", "RSA2",
"utf-8", // Replace with your character encoding, recommended: utf-8
false, GetCertParams());
// 2. Create the Request object for the specific Open API
AlipayOpenOperationOpenbizmockBizQueryRequest request = GetRequest();
// 3. Execute request and handle response
AlipayOpenOperationOpenbizmockBizQueryResponse response = client.CertificateExecute(request);
if (!response.IsError)
{
Console.WriteLine("Call successful.");
}
else
{
Console.WriteLine("Call failed. Reason: " + response.Msg + ", " + response.SubMsg);
}
}
catch (Exception e)
{
Console.WriteLine("Exception occurred: " + e.Message);
throw e;
}
}
private static CertParams GetCertParams()
{
return new CertParams
{
// Replace with your application public certificate file path
AlipayPublicCertPath = "/home/foo/alipayCertPublicKey_RSA2.crt",
// Replace with your Alipay public certificate file path
AppCertPath = "/home/foo/appCertPublicKey_2019090366875133.crt",
// Replace with your Alipay root certificate file path
RootCertPath = "/home/foo/alipayRootCert.crt"
};
}
private static AlipayOpenOperationOpenbizmockBizQueryRequest GetRequest()
{
// Initialize Request and populate Model properties.
AlipayOpenOperationOpenbizmockBizQueryRequest request = new AlipayOpenOperationOpenbizmockBizQueryRequest();
AlipayOpenOperationOpenbizmockBizQueryModel model = new AlipayOpenOperationOpenbizmockBizQueryModel
{
BizNo = "123456789"
};
request.SetBizModel(model);
return request;
}
}
}