# Discount

### Discount

The SDK allows for the host application to retrieve discount related data, which in turn can be used by the host application to fetch data from the Discount system (see documentation for the Discount).

This service will return the current discount and projected discount, alongside some useful discount information. Discount values may not always be available due to insufficient driving data.

## <sup>Fetch Discount</sup>

To fetch discount data, the following API can be used.

#### Concrete example

{% tabs %}
{% tab title="Swift" %}

```swift
let service = DiscountService()
service.fetchDiscount(then: { discount in
    guard !discount.value.isEmpty else {
        // failure
        return
    }
    completionHandler(.success(discount.value))
})
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val discountService = DiscountService()
discountService.fetchDiscount() { result: Result<DiscountMetaData>? ->
     if (result?.value == null || result.throwable != null) {
            // failure
        } else {
            // Success
           val discountMetaData: DiscountMetaData = result?.value
        }

 }
```

{% endtab %}
{% endtabs %}

## <sup>Fetch Discount By Policy</sup>

To fetch discount data for a specific Policy, the following API can be used.

#### Concrete example

{% tabs %}
{% tab title="Swift" %}

```swift
let service = DiscountService()
service.fetchDiscountByPolicyId(policyId: policyId,
            verificationCutOffDate: verificationCutOffDateComponents) { discount in
    guard !discount.value.isEmpty else {
        // failure
        return
    }
    completionHandler(.success(discount.value))
})
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val discountService = DiscountService()

discountService.fetchDiscountByPolicyId(
    policyId = policyId,
    verificationCutOffDate = verificationCutOffDateComponents
) { result: Result<Discount> ->
    if (result.value == null || result.throwable != null) {
        // failure
    } else {
        // Success
        val discount: Discount = result.value
    }
}
```

{% endtab %}
{% endtabs %}

### Discount Metadata

Represents the mapping between scores ranges and discounts. The mappings of scores are represented as Ranges of Doubles to be consistent with the scoring values.

Any valid user on the DriveSync system (identified via the `Identity`set during [SDK initialization](https://sdk.ims.tech/readme/ios/initializing-the-sdk#identity)) should be able to know how discount is calculated (score ranges vs discounts), so that driver knows how his/her driving style affecting his/her discount.

## <sup>Fetch Discount Metadata</sup>

To fetch discount metadata, the following API can be used.

#### Concrete example

{% tabs %}
{% tab title="Swift" %}

```swift
let service = DiscountMetadataService()
service.fetchDiscountMetadata(then: { discount in
    guard !discount.value.isEmpty else {
        // failure
        return
    }
    completionHandler(.success(discount.value))
})
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val discountMetadataService = DiscountMetadataService()
discountMetadataService.fetchMapping() { result: Result<List<DiscountMapping>>? ->
    val discountMappings = result?.value
    val error = result?.throwable
    if (discountMappings != null) {
        // Success
        completionHandler(discountMappings)
    } else if (error != null) {
        // Failure
        errorHandler(error)
    }
}
```

{% endtab %}
{% endtabs %}

## <sup>Fetch Discount Metadata By Policy</sup>

To fetch discount metadata for a specific policy, the following API can be used.

{% tabs %}
{% tab title="Swift" %}

```swift
let service = DiscountMetadataService()
service.fetchByPolicy(policyId: policyId) { discount in
    guard !discount.value.isEmpty else {
        // failure
        return
    }
    completionHandler(.success(discount.value))
})
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val discountMetadataService = DiscountMetadataService()
discountMetadataService.fetchByPolicy(policyId: policyId) { result: Result<List<DiscountMapping>>? ->
   if (result?.value == null || result.throwable != null) {
            // failure
        } else {
            // Success
           val discountMetaData: DiscountMetaData = result?.value
            completionHandler(discountMetaData)
        }
}
```

{% endtab %}
{% endtabs %}

#### Concrete example

### Discount by vehicle

The SDK allows for the host application to retrieve discount related data by vehicle, which in turn can be used by the host application to fetch data from the Discount system (see documentation for the Discount).

This service will return the current discount and projected discount, alongside some useful discount information. Discount values may not always be available due to insufficient driving data.

#### Required information

**vehicleId**: Since each account can be linked to multiple vehicles, vehicle ID is passed.

To fetch discount data by vehicle, the following API can be used.

#### Concrete example

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
val discountService = DiscountService()
discountService.fetchDiscountByVehicle(vehicleId) { result: Result<DiscountMetaData>? ->
     if (result?.value == null || result.throwable != null) {
            // failure
        } else {
            // Success
           val discountMetaData: DiscountMetaData = result?.value
            completionHandler(discountMetaData)
        }

 }
```

{% endtab %}
{% endtabs %}
