# 客户端SDK

欢迎使用 TripLink SDK,源代码位于 GitHub (opens new window)

TripLink SDK 让您更轻松方便地对接 TripLink API,自动进行:加密与解密数据,生成与验证签名,校验基本参数以及发送 HTTPS 请求。

在使用 TripLink SDK 之前,您需要参考 快速接入 完成准备工作,包括准备AES密钥、RSA公钥等。

# Java SDK

# 安装

最低版本要求:Java 6

推荐通过 Maven 来管理项目依赖,您只需在项目的pom.xml文件中声明依赖 Vcc SDK Java (opens new window)(请保持版本为最新版):

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

# 功能

TripLink Java SDK 支持您扩展自己的HTTPS请求和日志实现。

HTTPS 请求

TripLink Java SDK 的 HTTPS 请求默认使用 Java 原生方法。您也可以实现HttpClient接口,重写post方法。

public class CustomHttpClient implements HttpClient<CallHttpResponse> {
    @Override
    public CallHttpResponse post(String requestJson, String url, Map<String, String> header) {
        // 自定义实现
    }
}
1
2
3
4
5
6

日志实现

TripLink Java SDK For 遵循slf4j日志标准,您可以根据自己应用选择具体的实现:

Logback
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
</dependency>
1
2
3
4
SLF4J Simple
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
</dependency>
1
2
3
4
Log4j
<dependency>
    <groupId>log4j</groupId>
    <artifactId>logback-classic</artifactId>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
</dependency>
1
2
3
4
5
6
7
8

# 示例

API文档 里每个接口的SDK示例下,都有可直接运行的测试方法提供。

package com.ctrip.ccard.creditcard.vcc.api.demo;

import com.ctrip.ccard.creditcard.vcc.api.V2.TripLinkApiImplV2;
import com.ctrip.ccard.creditcard.vcc.bean.CallHttpResponse;
import com.ctrip.ccard.creditcard.vcc.bean.V2.CardCreateRequest;
import com.ctrip.ccard.creditcard.vcc.bean.V2.CardCreateResponse;
import com.ctrip.ccard.creditcard.vcc.biz.V2.TripLinkBizImplV2;
import com.ctrip.ccard.creditcard.vcc.exception.BusinessException;
import com.ctrip.ccard.creditcard.vcc.exception.HttpException;
import com.ctrip.ccard.creditcard.vcc.util.HttpClient;
import com.ctrip.ccard.creditcard.vcc.util.TripLinkHttpClient;
import org.junit.Test;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class TripLinkSDKTest {
    // API地址
    private static final String BASE_URL = "https://vcc-compass-fat.ctripqa.com/compass/api";
    // 客户ID
    private static final String CUSTOMER_ID = "CUSTOMER_ID";
    // AES密钥
    private static final String AES_KEY = "AES_KEY";
    // 客户RSA私钥
    private static final String CUSTOMER_PRIVATE_KEY = "CUSTOMER_PRIVATE_KEY";
    // TripLink RSA公钥
    private static final String TRIPLINK_PUBLIC_KEY = "TRIPLINK_PUBLIC_KEY";

    @Test
    public void testCreateCard() {
        // 初始化TripLinkApi实例
        HttpClient<CallHttpResponse> httpClient = new TripLinkHttpClient();
        TripLinkBizImplV2 tripLinkCore = new TripLinkBizImplV2(CUSTOMER_PRIVATE_KEY, TRIPLINK_PUBLIC_KEY, AES_KEY, BASE_URL, httpClient);
        TripLinkApiImplV2 tripLinkApi = new TripLinkApiImplV2(tripLinkCore);

        // 拼接请求参数
        CardCreateRequest request = new CardCreateRequest();
        request.setRequestId(UUID.randomUUID().toString());
        request.setCustomerId(CUSTOMER_ID);
        request.setCardCurrencyCode("840");
        request.setSettlementCurrencyCode("840");
        request.setActiveDate("2022-01-01");
        request.setInactiveDate("2024-06-01");
        request.setCardLimit(BigDecimal.valueOf(1000.12));
        request.setMinAuthAmount(BigDecimal.valueOf(3.45));
        request.setMaxAuthAmount(BigDecimal.valueOf(500.67));
        request.setMaxAuthTimes(-1);
        request.setCardCloseUsage(40);
        request.setSupportedMccGroup("ecom");
        request.setMultipleCurrencyCard(true);
        request.setCvv2ForceCheck(true);
        request.setCardBin("522981");
        request.setCardType("GWTTP");
        request.setCardLabel("MasterCard");
        Map<String, String> userReference = new HashMap<>();
        userReference.put("useRef1Txt", "anything");
        request.setUserReferenceMap(userReference);

        try {
            // 发送请求
            CardCreateResponse response = tripLinkApi.create(request);
        } catch (HttpException e) {
            // 处理HTTPS请求异常
        } catch (BusinessException e) {
            // 处理业务异常
        }
    }
}
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

# PHP SDK

# 安装

最低版本要求:PHP 7.3

# 功能

TripLink Java SDK 支持您扩展自己的HTTPS请求

HTTPS 请求

TripLink PHP SDK 的 HTTPS 请求默认使用Guzzle。您也可以自己实现HttpClient接口,重写post方法。

class CustomHttpClient implements HttpClient {
    public function post(string $url, array $headers, string $body): HttpResponse {
        // 自定义实现
    }
}
1
2
3
4
5

# 示例

API文档 里每个接口的SDK示例下,都有可直接运行的测试方法提供。

<?php declare(strict_types=1);

namespace v2\demo;

use PHPUnit\Framework\TestCase;
use v2\agent\support\SimpleTripLinkAgent;
use v2\client\support\GuzzleHttpClient;
use v2\config\Customer;
use v2\exception\TripLinkException;
use v2\model\core\CreateCardRequest;
use v2\model\core\UserReference;

// API地址
const BASE_URL = 'https://vcc-compass-fat.ctripqa.com/compass/api';
// 客户ID
const CUSTOMER_ID = 'CUSTOMER_ID';
// AES密钥
const AES_KEY = 'AES_KEY';
// 客户RSA私钥
const CUSTOMER_PRIVATE_KEY = 'CUSTOMER_PRIVATE_KEY';
// TripLink RSA公钥
const TRIPLINK_PUBLIC_KEY = 'TRIPLINK_PUBLIC_KEY';

class TripLinkSDKTest extends TestCase {

    public function testCreateCard(): void {
        // 初始化TripLinkAgent实例
        $httpClient = new GuzzleHttpClient();
        $customer = new Customer(CUSTOMER_ID, AES_KEY, CUSTOMER_PRIVATE_KEY, TRIPLINK_PUBLIC_KEY);
        $tripLinkAgent = new SimpleTripLinkAgent(BASE_URL, $customer, $httpClient);

        // 拼接请求参数
        $request = new CreateCardRequest(uniqid(), CUSTOMER_ID);
        $request->setCardCurrencyCode('840');
        $request->setSettlementCurrencyCode('840');
        $request->setActiveDate('2022-01-01');
        $request->setInactiveDate('2024-06-01');
        $request->setCardLimit(1000.12);
        $request->setMinAuthAmount(3.45);
        $request->setMaxAuthAmount(500.67);
        $request->setMaxAuthTimes(-1);
        $request->setCardCloseUsage(40);
        $request->setSupportedMccGroup('ecom');
        $request->setMultipleCurrencyCard(true);
        $request->setCvv2ForceCheck(true);
        $request->setCardBin('522981');
        $request->setCardType('GWTTP');
        $request->setCardLabel('MasterCard');
        $userReference = new UserReference();
        $userReference->setUseRef1Txt('anything');
        $request->setUserReferenceMap($userReference);

        try {
            // 发送请求
            $response = $tripLinkAgent->createCard($request);
        } catch (TripLinkException $e) {
            // 处理业务异常
        }
    }
}
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