# 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](/readme/ios/initializing-the-sdk.md#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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sdk.ims.tech/drivesync-portal/discount.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
