文档中心 > 当面付-英文版

Integrate with SDK provided by Alipay

更新时间:2016/04/12 访问次数:6020

1. Acquiring SDK

  • OpenAPI SDK supports JAVA(SDK1.5SDK1.4),.NET, and PHP.

  • Please refer to SDK details for more information.

2. Configuration of Public/Private Key and Common Parameters

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;
    }
}

3. Examples for API calls

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

3.1 Barcode Payment

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”.

3.2 QR Code Payment

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);
}

3.3 Query API

If the payment response is an unsuccessful status, the caller needs to poll the result with Query API call.

  • It is recommended that the time interval should be 5 second between two query calls. The max of query should be 5 times.
  • If it is still an unsuccessful status after the 5 tries, the merchant should be call the cancel API to close the order and restart the payment
    alipay.trade.query 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 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);
}

3.4 Cancel

  • If a payment request returns uncertain result (such as system error or network exception), the merchant can call this API to cancel the payment and refund the user.
  • After the cancel call, if the user fails to complete the payment, Alipay will close the payment transaction. If the payment is completed, Alipay will refund the user.
  • 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);
}

3.5 Refund

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);
}

FAQ

关于此文档暂时还没有FAQ
返回
顶部