# Fraud Detection

## Introduction

Every transaction needs to go through risk control. Integrating Fraud Detection can prevent and reduce fraud rates, significantly improving your transaction success rate.

## Using with Checkout Component

If you are using our checkout component, you don't need to manually import the SDK. The component itself has already integrated the SDK, and you **don't need to do any additional configuration** as the component has already configured the environment for you.

> For checkout component integration, refer to: [PayKKa Checkout UI Component Documentation](/payments/docs/developer-resources/checkout-ui-component).


## Integrating with API Payment

When integrating with API payment, you need to manually import the SDK.

### Import SDK

Import the Fraud Detection SDK separately via CDN.

The SDK import methods for each environment are as follows:

Europe
```html html
<script src="https://checkout.eu.paykka.com/cp/fraud-detection.js"></script>
```

Hong Kong
```html html
<script src="https://checkout.aq.paykka.com/cp/fraud-detection.js"></script>
```

Sandbox
```html html
<script src="https://checkout-sandbox.aq.paykka.com/cp/fraud-detection.js"></script>
```

### Environment Configuration

When initializing Fraud Detection, you need to configure the corresponding environment based on your merchant's region.

Currently supported environments are:

| Environment | Description |
|  --- | --- |
| **sandbox** | Available for sandbox testing |
| **eu** | Default value, available for European merchants |
| **hk** | Available for Hong Kong merchants |
| **us** | Available for US merchants, currently not open |


After successfully importing and loading the SDK, the variable `PayKKaFraudDetection` will be exposed. When calling it, you need to pass the following parameters:

- `env`: Environment parameters, optional values: `sandbox` | `eu` | `hk`
- `clientKey`: Your client key


```typescript
const paykkaFraudDetection = PayKKaFraudDetection({
  env: 'sandbox',
  clientKey: 'xxx',
})
```

PayKKaFraudDetection is a singleton, so multiple calls return the same instance. You don't need to worry about duplicate requests.

### Usage

#### createFraudDetection()

You need to call `paykkaFraudDetection.createFraudDetection()` to create Fraud Detection. At this time, it will start initializing and requesting the fraudDetectionID. This method caches the requested result, so multiple calls won't update it.
It is recommended to call this method immediately after the SDK is loaded to get the fraudDetectionID faster.

#### getFraudDetectionId()

You can get the `fraudDetectionID` by calling `paykkaFraudDetection.getFraudDetectionId()`.
`getFraudDetectionId` is an async method. It takes time to obtain the `fraudDetectionID` from the channel, so It is recommended to submit the payment before execution..
When getting fraudDetectionID, it is recommended to pass the user-entered 6-digit card bin **to reduce the probability of 3DS challenges or failures**:

```javascript javascript
const fraudDetectionID = await paykkaFraudDetection.getFraudDetectionId({ bin: '400000' })
```

#### Complete Example

Here is a complete integration example (using Sandbox environment):

```html html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <!-- Import Fraud Detection SDK -->
    <script src="https://checkout-sandbox.aq.paykka.com/cp/fraud-detection.js"></script>
    <!-- Europe -->
    <!-- <script src="https://checkout.eu.paykka.com/cp/fraud-detection.js"></script> -->
    <!-- Hong Kong -->
    <!-- <script src="https://checkout.aq.paykka.com/cp/fraud-detection.js"></script> -->
    <title>Fraud Detection Demo</title>
  </head>
  <body>
    <div class="form">
      <!-- Form -->
    </div>
    <!-- Payment button -->
    <button onclick="handleClickPay()">Pay</button>

    <div>fraudDetectionID: <span id="fraudDetectionID"></span></div>

    <script>
      let paykkaFraudDetection

      // Call API payment
      const paymentWithAPI = () => {}

      /** Triggered when payment button is clicked */
      const handleClickPay = async () => {
        // Get fraudDetectionID, it's recommended to pass a 6-digit card bin
        const fraudDetectionID = await paykkaFraudDetection.getFraudDetectionId({ bin: '400000' })
        document.getElementById('fraudDetectionID').textContent = fraudDetectionID
        const paymentParams = {
          browser: {
            // Pass the obtained fraudDetectionID
            fraud_detection_id: fraudDetectionID,
            // ...otherBrowserInfo
          },
          //...otherPaymentParams
        }

        paymentWithAPI()
      }

      window.onload = () => {
        // Create Fraud Detection instance, pass environment parameters: sandbox | eu | hk, and client key
        paykkaFraudDetection = PayKKaFraudDetection({
          env: 'sandbox',
          clientKey: 'xxx',
        })

        // After creating the Fraud Detection instance, you need to call the createFraudDetection method
        paykkaFraudDetection.createFraudDetection()
      }
    </script>
  </body>
</html>
```