# Client SDK

# Java

Welcome to TripLink SDK for Java, the source repo is on GitHub (opens new window).

TripLink SDK for Java makes it easier for you to connect to the TripLink API, which can automatically help you meeting the non-functional requirements such as parameter verification, signing and verifying signature, data encryption and decryption, sending HTTP requests.

If you encounter any problems while using the TripLink SDK for Java, welcome to submit issue on GitHub.

# Preparation

Befpre using the TripLink SDK for Java, you need to follow the Quick Start to prepare the essential configuration like RSA public key, AES key, merchant account and so on.

# Dependency

It is recommaned to use Maven to manage the project dependencies. You only need to add following dependency in the project pom.xml file:

<dependency>
  <groupId>io.github.ctripcorp</groupId>
  <artifactId>triplink-api</artifactId>
  <version>1.0.0</version>
</dependency>
1
2
3
4
5

# Instruction

TripLink SDK for Java allows you extend for your own Http request and logging.

Http reqeust

TripLink SDK for Java by default uses native Java methods foe Http request. You can implement the HttpClient interface and override the post method.

public class CustomHttpClient implements HttpClient {
    public String post(String requestJson, String url){
        // custom implementation
        return null;
    }    
}
1
2
3
4
5
6

Logging

TripLink SDK for Java follows slf4j logging standard, you can choose the specifia implementation like logback, slf4j-simple, log4j:

For logback, add follwing dependency:

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>${logback.version}</version>
</dependency>
1
2
3
4
5

For slf4j-simple, add follwing dependency:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>${slf4j.version}</version>
</dependency>
1
2
3
4
5

For log4j, add follwing dependency:

<dependency>
    <groupId>log4j</groupId>
    <artifactId>logback-classic</artifactId>
    <version>${log4j.version}</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>${slf4j.version}</version>
</dependency>
1
2
3
4
5
6
7
8
9
10

# Example

The following example shows 5 main steps to use the TripLink SDK for Java

  1. Create HttpClient insatance

  2. Craete TripLinkBiz insatance

  3. Create TripLinkApi instance

  4. Construact request parameters

  5. Proessing the response

import com.ctrip.ccard.creditcard.vcc.api.TripLinkApiImpl;
import com.ctrip.ccard.creditcard.vcc.bean.CreateCardInfo;
import com.ctrip.ccard.creditcard.vcc.bean.CreateRequest;
import com.ctrip.ccard.creditcard.vcc.bean.CreateResponse;
import com.ctrip.ccard.creditcard.vcc.biz.TripLinkBizImpl;
import org.junit.Assert;

import java.math.BigDecimal;

/**
 * Description:
 */
public class TestApi {
    
    private final Logger logger = LoggerFactory.getLogger(TestApi.class);

    private static final String DES = "Wa8Nw*Ei";

    private static final String URL = "https://openpci.fws.ctripqa.com/restful/soa2/18375/json";

    private static final String  PRIVATE_KEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKpQrSAzyJV0MfBN7qKvE8d73phdredNhF3cKm3IWKFlCWg/3alVWI6GE462rPc5A4T+shYcvzqhV5wSOS0QMfj9VfLPqUT+xggFcCQ48mbeX4Jy/N5QZB3RZuQu+YbmQT6f54h2sJvhqLurvE7sgW4qL7r6AaJfDsvYPjKSezYXAgMBAAECgYA32EY8Jd6iarwpMFSMEV4p7craKPVpv3gkkply79tn6EpCXZaf/HUSHpJxHCLw2Uf3JtBcAccOQXMJoMwQo5vOoMVl5nk+EZN//MB8Re8r/7GQV8E+myHdlntMjxOf38PGn9z8Ze0Q020fZwGjA6egBFcU/ld1lCcI0TAj3cZDcQJBANS9UOC3J5njhnuzACjQ1qTTXuv6hr2lbglr2za4Ju9xFJUkKXy2LBAp2LlakXZXDhf7lsqmwZg5BvOBK6DPl18CQQDM8tqqOr4LRJRhq2bqBx398IqtyoZpMshpzBXLr7bdhp7FR2N4AEoAGaa5hS5k3z5SYNLEGKFhRM+sFJBQjnRJAkBnq647I+YffxotM8jTGxpOjlbGhnqc9n4OB0p3evw2WRPfrhStmpUUd2AOy4zxb3EFzOvp66OSC9BQX9Uj86XfAkEAouGbgVDgOupNFvZ2+yWe43Ppc0eS3UZ72wFUjSXgKlzUECu1VOi95yh7xdOf1JFL4YKL30dH8psShUtuimc86QJAeBXASabJBcHAIisPkODvsciiz1pzm1WSuXRUxnuis0TRTRs7+2KEnWE4UV3jxehxkc1RAgteYosWXg5TWQgiUg==";

    public static void main(String[] args) {
        //1:创建HttpClient实例
        TripLinkHttpClient tripLinkHttpClient = new TripLinkHttpClient();
        //2:创建TripLinkBiz实例
        TripLinkBizImpl tripLinkBiz = new TripLinkBizImpl(PRIVATE_KEY,DES,URL,tripLinkHttpClient);
        //3:创建TripLinkApi实例
        TripLinkApiImpl tripLinkApi = new TripLinkApiImpl(tripLinkBiz);
        //4:创建使用Open Api 对应的请求参数
        CreateRequest request = getCreateParams();
        //5:接口请求响应处理
        try{
            CreateResponse response = tripLinkApi.create(request);
            Assert.assertNotNull(response);
        }catch(IllegalArgumentException e){
            //参数异常
            logger.error("参数异常,异常信息",e.getMessage());
        }catch(HttpException e){
             //https请求发生的异常
            //如果使用SDK的HttpClient实现类发送的请求,只要响应码不是200,都会抛 HttpException
            logger.error("接口请求发送异常,异常信息",e.getMessage());
        }
    }

    private static CreateRequest getCreateParams(){
        CreateRequest createRequest = new CreateRequest();
        createRequest.setRequestId("2021000000001");
        createRequest.setRequestTime("20210728102355");
        createRequest.setMerchantName("TEST");
        createRequest.setChannelType("TEST_CFNC_VCC");
        createRequest.setOperator("max");
        createRequest.setRequestSource("order");
        CreateCardInfo createCardInfo = new CreateCardInfo();
        createCardInfo.setLocalCurrency("USD");
        createCardInfo.setBillCurrency("USD");
        createCardInfo.setStartActiveDate("2021-07-28");
        createCardInfo.setEndCloseDate("2023-07-28");
        createCardInfo.setCreditLimitAmt(new BigDecimal(1200.00));
        createCardInfo.setMinAuthAmt(new BigDecimal(0.00));
        createCardInfo.setMaxAuthAmt(new BigDecimal(1200.00));
        createCardInfo.setEnableMutilUse("1");
        createCardInfo.setClosePercentage(100);
        createCardInfo.setEnable3DS("0");
        createCardInfo.setEnaleCVVCheck("0");
        createCardInfo.setEnableCurrencyCheck("0");
        createCardInfo.setMerchantCategoryName("0011");
        createCardInfo.setUserDefineInfo("{\"BookingNumber\":\"205D140D5-01DFFS\"}");
        createRequest.setCardInfo(createCardInfo);
        return createRequest;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72