Use the Alipay SDK

Download the SDK

SDKs are available on Alipay open platform to help developers call the open interfaces. JAVA, PHP and .NET are provided. Basic functions such as signature, signature verification, and HTTP request are encapsulated in the SDK to facilitate the merchant's quick integration on the server side. For details about how to use the SDKs, see SDK introduction.

Initialize the SDK

Before using the SDK to call APIs, you must initialize the SDK first.

copy
AlipayClient alipayClient = new DefaultAlipayClient(URL,APP_ID,APP_PRIVATE_KEY,FORMAT,CHARSET,ALIPAY_PUBLIC_KEY, SIGN_TYPE);

Request parameters

Parameter

Description

URL

The Alipay gateway. This field is fixed.

Example:https://openapi.alipay.com/gateway.do

APPID

The unique ID that is assigned by Alipay to identify an app.

Example:2014072300007148

APP_PRIVATE_KEY

The app private key that is generated by the app developers.

FORMAT

The response format. Only JSON is supported.

Example:JSON

CHARSET

The character encoding of the request. UTF-8 and GBK are supported.

Example:UTF-8

ALIPAY_PUBLIC_KEY

The public key that is generated by Alipay.

SIGN_TYPE

The sign method used in the request. The value can be RSA2 or RSA.  RSA2 is preferred.

Example:RSA2

Next, you can use alipayClient to call the specific API. The alipayClient only needs to be initialized once, and the same alipayClient object can be used for subsequent calls to different APIs.

Note: ISVs/Developers can obtain the app auth token through Third-party app authorization, and then ISVs/developers can initiate requests on behalf of the merchant by passing the app auth token as one request parameter.

System flow

The following figure illustrates the SDK system flow.

image.png

Figure 1. The SDK system flow

Example

The following example illustrates how to use the SDK to call Alipay cross-border tax refund API alipay.overseas.tax.order.pay:

copy
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2");
AlipayOverseasTaxOrderPayRequest request = new AlipayOverseasTaxOrderPayRequest();
request.setBizContent("{" +
"\"out_order_no\":\"0005520171122115751\"," +
"\"identify_account_type\":\"barcode\"," +
"\"identify_account_no\":\"286139798480113981\"," +
"\"biz_mode\":\"FOREX_TAX_REFUND\"," +
"\"tax_refund_scene_type\":\"03\"," +
"\"tax_refund_currency\":\"SGD\"," +
"\"tax_refund_amount\":100," +
"\"user_received_currency\":\"CNY\"," +
"\"user_received_amount\":100," +
"\"passport_no\":\"103369874587\"," +
"\"country_code\":\"SG\"," +
"\"passport_name\":\"GONGLEI\"," +
"\"nationality\":\"CHN\"," +
"\"extend_param\":\"{\\\"key\\\":\\\"value\\\"}\"," +
"\"doc_id\":\"12345670001\"," +
"\"sales_date\":\"2017-12-18 17:34:13\"," +
"\"tax_refund_print_date\":\"2017-12-18 17:34:13\"," +
"\"doc_expire_date\":\"2017-12-18 17:34:13\"," +
"\"confirm_date\":\"2017-12-18 17:34:13\"," +
"\"sales_currency\":\"SGD\"," +
"\"sales_amount\":48000," +
"\"departure_point\":\"airport\"," +
"\"company_name\":\"KICC\"," +
"\"available_day\":15" +
"  }");
AlipayOverseasTaxOrderPayResponse response = alipayClient.execute(request);
if(response.isSuccess()){
System.out.println("Success");
} else {
System.out.println("Fail");
}

Notes:

  • out_order_no must be unique in order to avoid repeated refunds. If out_order_no of two orders are different while all other parameters are the same, it will be considered as two different refund orders.
  • When alipay.overseas.tax.order.pay is called and Alipay returns UNKNOWN, retry with the same parameters. If Alipay returns gateway layer errors, contact Alipay Technical Support for help.