# Java

## Description

Only registered partners can use this feature. Unregistered partners may test on the testnet using the secret key provided in the sample code.

This is a feature required to build the point payment system. Please create a wallet to be used for payments, and provide its address to the loyalty system operations team.

Please create a wallet to be used with this feature and provide the wallet address to the loyalty system operations team.

* For the testnet, the wallet's secret key to be used is:\
  0xa0dcffca22f13363ab5d109f3a51ca99754cff4ce4c71dccc0c5df7f6492beee
* For the mainnet, create a wallet independently and deliver only its public address to the operations team.

***

## Development and Operation

Follow the steps below to proceed:

### Development and Testing:

1. First, install the user app and set the network to testnet.
2. Review the test code and implement the necessary features.\
   When creating PaymentClient, use the wallet's private key:\
   0xa0dcffca22f13363ab5d109f3a51ca99754cff4ce4c71dccc0c5df7f6492beee (for testnet).
3. Note: Testnet store IDs begin with “0x0003”.

### Switching to Mainnet:

1. Generate a new wallet and use its private key to create PaymentClient. You may use a wallet generated using MetaMask.
2. Send the wallet address created in step 1 to the KIOS operations team.

### Cautions:

1. Both testnet and mainnet are always active, so ensure the system can handle both networks in the future.
2. If the store ID of the POS or KIOSK starts with "0x0003", use testnet information for PaymentClient. If the store ID starts with "0x0004", use mainnet information.
3. SMS functionality is available only in Korea.

***

## SDK

{% embed url="<https://github.com/kios-coin/kios-service-sdk>" %}

## 샘플코드

{% embed url="<https://github.com/kios-coin/kios-service-sdk/blob/v0.x.x/java/ver21/sample/src/main/java/org/example/Main.java>" %}

### &#x20;1. 네트워크별로 지갑의 개인키를 정의합니다.

테스트넷은 이미 등록된 값을 사용하시고, 메인넷에서 사용할 키는 직접 생성하신 후 주소만 KIOS운영팀에 전달해 주세요

```java
// 구매정보 저장을 위해 필요한 키
// 네트워크 별로 가지고 있어야 한다
//---------------------------------------------------------------------------------------
Map<NetWorkType, String> keysOfPayment = new HashMap<>();
keysOfPayment.put(NetWorkType.kios_testnet, "0xa0dcffca22f13363ab5d109f3a51ca99754cff4ce4c71dccc0c5df7f6492beee");
keysOfPayment.put(NetWorkType.acc_testnet, "0x8acceea5937a8e4bb07abc93a1374264dd9bd2fc384c979717936efe63367276");
//---------------------------------------------------------------------------------------
```

### 2. 네트워크의 종류를 결정하기

네트워크의 종류는 상점아이디에 의하여 결정됩니다.

```java
//---------------------------------------------------------------------------------------
// 키오스크의 상점 아이디
String shopId = "0x0003be96d74202df38fd21462ffcef10dfe0fcbd7caa3947689a3903e8b6b874";

// 상점아이디에 따른 네트워크를 선택한다
NetWorkType network = CommonUtils.getNetWorkType(shopId);
```

### 3.  결제를 위한 클라이언트 생성하기&#x20;

```java
PaymentClient client = new PaymentClient(network, keysOfPayment.get(network));
```

### 4. PaymentEventListener를 구현하기

```java
class TaskEventListener implements ITaskEventListener {
    public void onNewPaymentEvent(
            String type,
            int code,
            String message,
            long sequence,
            PaymentTaskItem paymentTaskItem
    ) {
        System.out.printf("  -> onNewPaymentEvent %s - %d - %s - %d\n", type, code, message, sequence);
    }

    public void onNewShopEvent(
            String type,
            int code,
            String message,
            long sequence,
            ShopTaskItem shopTaskItem
    ) {
        System.out.printf("  -> onNewShopEvent %s - %d - %s - %d\n", type, code, message, sequence);
    }
}
```

### 5. TaskEventCollector 를 생성하기

```java
TaskEventListener listener = new TaskEventListener();
TaskEventCollector collector = new TaskEventCollector(client, listener);
```

### 6. Event Collector 를 시작하기

```java
collector.start();
```

### 7. 신규 결제를 시작하기

```java
PaymentTaskItem paymentItem = client.openNewPayment(
    CommonUtils.getSamplePurchaseId(), // 구매 아이디
    temporaryAccount,                  // 키오스로 부터 전달받은 사용자의 임시주소
    Amount.make("1_000").getValue(),   // 결제할 금액
    "krw",                             // 결제할 금액의 환률 심벌 
    shopClient.getShopId(),            // 상점 아이디
    terminalID                         // 키오스크의 아이디 (공백이어도 무관)
);
```

### 8. 신규 결제를 종료하기

```java
PaymentTaskItem paymentItem = client.closeNewPayment(paymentItem.paymentId, true);
```

### 9. 취소 결제를 시작하기

```java
PaymentTaskItem paymentItem = client.openCancelPayment(paymentItem.paymentId, terminalID);
```

### 10. 취소 결제를 종료하기

```java
PaymentTaskItem paymentItem = client.closeCancelPayment(paymentItem.paymentId, true);
```

### 11. 이벤트 수집기를 종료하기

```java
collector.Stop();
```
