Please refer to SDK details for more information.
Code of Config sample:
public class Config { // developer’s private key. Java with PKCS8 format,PHP/.Net use the raw format in rsa_private_key.pem file. public static final String RSA_RRIVATE_KEY = "详见密钥生成"; // API gateway. Fixed value for payment, query, cancel, and refund API public static final String URL = "https://openapi.alipay.com/gateway.do"; // The hmerchant APPID. If your application includes the API and the API is open, the application’s APPID could be used. The developer could check it on OpenAPI portal - Management Center – Application. public static final String APPID = "2015********8324"; // encoding. Default: utf-8 public static final String CHARSET = "utf-8"; // response format. Dfault: json public static final String FORMAT = "json"; //Alipay public key used to verify messages sent by Alipay. public static final String ALIPAY_PUBLIC_KEY = "开发者登录开放平台-管理中心-进入应用后查看"; }
AlipayClient is a public class for request methods.
AlipayClientFactory is the static factory class of AlipayClient:
import com.alipay.api.AlipayClient; import com.alipay.api.DefaultAlipayClient; public class AlipayClientFactory { // SDK public request class. It includes the common request parameters, signature signing and verification. The developer would not need to worry about the digital signature portion. private static final AlipayClient client = new DefaultAlipayClient(Config.URL, Config.APPID, Config.RSA_RRIVATE_KEY, Config.FORMAT, Config.CHARSET, Config.ALIPAY_PUBLIC_KEY); public static AlipayClient getAlipayClientInstance() { return client; } }
API calls normally have three steps:
1) Construct business parameters
2) Send the result to Alipay server (OpenAPI)
3) Get the response and process it
alipay.trade.pay sample:
/** * BarcodeBarcode Payment, sound wave payment * @param appAuthToken, if it is called by third-part vendor, the value should be app_auth_token acquired after the authorization. If it is called by the merchant, it will be set as null. * @param bizContent = "{\"out_trade_no\":\"123213445324532\",\"scene\":\"bar_code\",\"auth_code\":\"用户钱包付款码(数字)\",\"total_amount\":\"0.01\",\"subject\":\"测试\"}"; * @return * @throws AlipayApiException */ public AlipayTradePayResponse pay(String appAuthToken, String bizContent) throws AlipayApiException { AlipayTradePayRequest request = new AlipayTradePayRequest(); request.putOtherTextParam("app_auth_token", appAuthToken); request.setBizContent(bizContent); return AlipayClientFactory.getAlipayClientInstance().execute(request); }
Notes:
How to acquire app_auth_token, please refer to “third-party application authorization”.
alipay.trade.precreate sample:
/** * * @param appAuthToken, if it is called by third-part vendor, the value should be app_auth_token acquired after the authorization. If it is called by the merchant, it will be set as null. * @param bizContent = "{\"out_trade_no\":\"123213445324538\",\"total_amount\":\"0.01\",\"subject\":\"测试\"}"; * @return * @throws AlipayApiException */ public AlipayTradePrecreateResponse precreate(String appAuthToken, String bizContent) throws AlipayApiException { AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest(); request.putOtherTextParam("app_auth_token", appAuthToken); request.setBizContent(bizContent); return AlipayClientFactory.getAlipayClientInstance().execute(request); }
If the payment response is an unsuccessful status, the caller needs to poll the result with Query API call.
/** * @param appAuthToken, if it is called by third-part vendor, the value should be app_auth_token acquired after the authorization. If it is called by the merchant, it will be set as null. * @param bizContent = "{\"out_trade_no\":\"123213445324538\"}"; * @return * @throws AlipayApiException */ public AlipayTradeQueryResponse query(String appAuthToken, String bizContent) throws AlipayApiException { AlipayTradeQueryRequest request = new AlipayTradeQueryRequest(); request.putOtherTextParam("app_auth_token", appAuthToken); request.setBizContent(bizContent); return AlipayClientFactory.getAlipayClientInstance().execute(request); }
The cancel API can only cancel the payment within 24 hours. After that, the merchant can call refund API.
Notes:
Cancel API should be used only for uncertain payment result such as system timeout or unknown payment result. Please use the refund API if the payment is successful.
alipay.trade.cancel sample:
/** * * @param appAuthToken, if it is called by third-part vendor, the value should be app_auth_token acquired after the authorization. If it is called by the merchant, it will be set as null. * @param bizContent = "{\"out_trade_no\":\"123213445324538\"}"; * @return * @throws AlipayApiException */ public AlipayTradeCancelResponse cancel(String appAuthToken, String bizContent) throws AlipayApiException { AlipayTradeCancelRequest request = new AlipayTradeCancelRequest(); request.putOtherTextParam("app_auth_token", appAuthToken); request.setBizContent(bizContent); return AlipayClientFactory.getAlipayClientInstance().execute(request); }
Within three months (or the time frame specified in the contract), if the merchant decides to refund, it can call Refund API. Once Alipay receives the request, it will validate the request and refund the buyer to the original funding source.
alipay.trade.refund sample:
/** * * * @param appAuthToken, if it is called by third-part vendor, the value should be app_auth_token acquired after the authorization. If it is called by the merchant, it will be set as null. * @param bizContent = "{\"trade_no\":\"20160303363475784957483739744\",\"refund_amount\":\"0.01\"}"; * @return * @throws AlipayApiException */ public AlipayTradeRefundResponse refund(String appAuthToken, String bizContent) throws AlipayApiException { AlipayTradeRefundRequest request = new AlipayTradeRefundRequest(); request.putOtherTextParam("app_auth_token", appAuthToken); request.setBizContent(bizContent); return AlipayClientFactory.getAlipayClientInstance().execute(request); }