Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create SourceOrderParams #1837

Merged
merged 1 commit into from
Nov 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 172 additions & 0 deletions stripe/src/main/java/com/stripe/android/model/SourceOrderParams.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package com.stripe.android.model

/**
* Information about the items and shipping associated with the source. Required for transactional
* credit (for example Klarna) sources before you can charge it.
*
* [API reference](https://stripe.com/docs/api/sources/create#create_source-source_order)
*/
data class SourceOrderParams @JvmOverloads constructor(
/**
* List of items constituting the order.
*/
val items: List<Item>? = null,

/**
* Shipping address for the order. Required if any of the SKUs are for products that have
* `shippable` set to true.
*/
val shipping: Shipping? = null
) : StripeParamsModel {
override fun toParamMap(): Map<String, Any> {
return emptyMap<String, Any>()
.plus(
items?.let {
mapOf(PARAM_ITEMS to it.map { item -> item.toParamMap() })
}.orEmpty()
).plus(
shipping?.let { mapOf(PARAM_SHIPPING to it.toParamMap()) }.orEmpty()
)
}

/**
* List of items constituting the order.
*
* [API reference](https://stripe.com/docs/api/sources/create#create_source-source_order-items)
*/
data class Item(
/**
* Optional. The type of this order item.
* Must be [Type.Sku], [Type.Tax], or [Type.Shipping].
*/
val type: Type? = null,

/**
* Optional. The amount (price) for this order item.
*/
val amount: Int? = null,

/**
* Optional. This currency of this order item. Required when amount is present.
*/
val currency: String? = null,

/**
* Optional. Human-readable description for this order item.
*/
val description: String? = null,

/**
* Optional. The ID of the SKU being ordered.
*/
val parent: String? = null,

/**
* Optional. The quantity of this order item. When type is [Type.Sku], this is the number of
* instances of the SKU to be ordered.
*/
val quantity: Int? = null
) : StripeParamsModel {

override fun toParamMap(): Map<String, Any> {
return emptyMap<String, Any>()
.plus(
amount?.let { mapOf(PARAM_AMOUNT to it) }.orEmpty()
)
.plus(
currency?.let { mapOf(PARAM_CURRENCY to it) }.orEmpty()
)
.plus(
description?.let { mapOf(PARAM_DESCRIPTION to it) }.orEmpty()
)
.plus(
parent?.let { mapOf(PARAM_PARENT to it) }.orEmpty()
)
.plus(
quantity?.let { mapOf(PARAM_QUANTITY to it) }.orEmpty()
)
.plus(
type?.let { mapOf(PARAM_TYPE to it.code) }.orEmpty()
)
}

enum class Type(internal val code: String) {
Sku("sku"),
Tax("tax"),
Shipping("shipping")
}

private companion object {
private const val PARAM_AMOUNT = "amount"
private const val PARAM_CURRENCY = "currency"
private const val PARAM_DESCRIPTION = "description"
private const val PARAM_PARENT = "parent"
private const val PARAM_QUANTITY = "quantity"
private const val PARAM_TYPE = "type"
}
}

/**
* Shipping address for the order.
* Required if any of the SKUs are for products that have `shippable` set to true.
*
* [API reference](https://stripe.com/docs/api/sources/create#create_source-source_order-shipping)
*/
data class Shipping(
/**
* Required. Shipping address.
*/
val address: Address,

/**
* Optional. The delivery service that shipped a physical product,
* such as Fedex, UPS, USPS, etc.
*/
val carrier: String? = null,

/**
* Optional. Recipient name.
*/
val name: String? = null,

/**
* Optional. Recipient phone (including extension).
*/
val phone: String? = null,

/**
* Optional. The tracking number for a physical product, obtained from the delivery service.
* If multiple tracking numbers were generated for this purchase, please separate
* them with commas.
*/
val trackingNumber: String? = null
) : StripeParamsModel {

override fun toParamMap(): Map<String, Any> {
return mapOf(
PARAM_ADDRESS to address.toParamMap()
).plus(
carrier?.let { mapOf(PARAM_CARRIER to it) }.orEmpty()
).plus(
name?.let { mapOf(PARAM_NAME to it) }.orEmpty()
).plus(
phone?.let { mapOf(PARAM_PHONE to it) }.orEmpty()
).plus(
trackingNumber?.let { mapOf(PARAM_TRACKING_NUMBER to it) }.orEmpty()
)
}

private companion object {
private const val PARAM_ADDRESS = "address"
private const val PARAM_CARRIER = "carrier"
private const val PARAM_NAME = "name"
private const val PARAM_PHONE = "phone"
private const val PARAM_TRACKING_NUMBER = "tracking_number"
}
}

private companion object {
private const val PARAM_ITEMS = "items"
private const val PARAM_SHIPPING = "shipping"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.stripe.android.model

import kotlin.test.Test
import kotlin.test.assertEquals

class SourceOrderParamsTest {

@Test
fun toParamMap() {
val params = SourceOrderParams(
items = listOf(
SourceOrderParams.Item(
type = SourceOrderParams.Item.Type.Sku,
amount = 1000,
currency = "eur",
description = "shoes",
quantity = 1
)
),
shipping = SourceOrderParams.Shipping(
address = requireNotNull(Address.fromJson(AddressFixtures.ADDRESS_JSON)),
carrier = "UPS",
name = "Jenny Rosen"
)
)

val expectedParamsMap = mapOf(
"items" to listOf(
mapOf(
"type" to "sku",
"amount" to 1000,
"currency" to "eur",
"description" to "shoes",
"quantity" to 1
)
),
"shipping" to mapOf(
"address" to mapOf(
"city" to "San Francisco",
"country" to "US",
"line1" to "123 Market St",
"line2" to "#345",
"postal_code" to "94107",
"state" to "CA"
),
"carrier" to "UPS",
"name" to "Jenny Rosen"
)
)

assertEquals(expectedParamsMap, params.toParamMap())
}
}