Skip to content

Commit

Permalink
Add SourceParams creation method for CardParams (#2674)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshafrir-stripe authored Jul 31, 2020
1 parent 8dbe6fd commit e4bb229
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 64 deletions.
28 changes: 28 additions & 0 deletions stripe/src/main/java/com/stripe/android/model/SourceParams.kt
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,34 @@ class SourceParams private constructor(
.setMetaData(card.metadata)
}

/**
* Create Card Source params.
*
* @param cardParams A [CardParams] object containing the details necessary for the source.
* @return a [SourceParams] object that can be used to create a card source.
*
* @see [Card Payments with Sources](https://stripe.com/docs/sources/cards)
*/
@JvmStatic
internal fun createCardParams(cardParams: CardParams): SourceParams {
return SourceParams(SourceType.CARD, cardParams.attribution)
.setApiParameterMap(
mapOf(
PARAM_NUMBER to cardParams.number,
PARAM_EXP_MONTH to cardParams.expMonth,
PARAM_EXP_YEAR to cardParams.expYear,
PARAM_CVC to cardParams.cvc
)
)
.setOwner(
OwnerParams(
address = cardParams.address,
name = cardParams.name
)
)
.setMetaData(cardParams.metadata)
}

/**
* @param googlePayPaymentData a [JSONObject] derived from Google Pay's
* [PaymentData#toJson()](https://developers.google.com/pay/api/android/reference/client#tojson)
Expand Down
27 changes: 27 additions & 0 deletions stripe/src/test/java/com/stripe/android/StripeEndToEndTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ import com.nhaarman.mockitokotlin2.verify
import com.stripe.android.exception.InvalidRequestException
import com.stripe.android.model.AccountParams
import com.stripe.android.model.AddressFixtures
import com.stripe.android.model.CardBrand
import com.stripe.android.model.CardFunding
import com.stripe.android.model.CardParamsFixture
import com.stripe.android.model.PaymentIntent
import com.stripe.android.model.PaymentMethod
import com.stripe.android.model.PaymentMethodCreateParams
import com.stripe.android.model.PaymentMethodCreateParamsFixtures
import com.stripe.android.model.SetupIntent
import com.stripe.android.model.SourceParams
import com.stripe.android.model.SourceTypeModel
import com.stripe.android.model.Token
import kotlin.test.Test
import kotlin.test.assertEquals
Expand Down Expand Up @@ -124,6 +129,28 @@ class StripeEndToEndTest {
)
}

@Test
fun `create Source using CardParams should return object with expected data`() {
val source = defaultStripe.createSourceSynchronous(
SourceParams.createCardParams(CardParamsFixture.DEFAULT)
)
assertThat(source?.sourceTypeModel)
.isEqualTo(
SourceTypeModel.Card(
addressLine1Check = "unchecked",
addressZipCheck = "unchecked",
brand = CardBrand.Visa,
country = "US",
cvcCheck = "unchecked",
expiryMonth = 12,
expiryYear = 2025,
last4 = "4242",
funding = CardFunding.Credit,
threeDSecureStatus = SourceTypeModel.Card.ThreeDSecureStatus.Optional
)
)
}

private fun createStripeWithTestScope(
publishableKey: String = ApiKeyFixtures.DEFAULT_PUBLISHABLE_KEY
): Stripe {
Expand Down
16 changes: 16 additions & 0 deletions stripe/src/test/java/com/stripe/android/model/CardParamsFixture.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.stripe.android.model

import com.stripe.android.CardNumberFixtures

internal object CardParamsFixture {
val DEFAULT = CardParams(
number = CardNumberFixtures.VISA_NO_SPACES,
expMonth = 12,
expYear = 2025,
cvc = "123",
name = "Jenny Rosen",
address = AddressFixtures.ADDRESS,
currency = "usd",
metadata = mapOf("fruit" to "orange")
)
}
16 changes: 2 additions & 14 deletions stripe/src/test/java/com/stripe/android/model/CardParamsTest.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
package com.stripe.android.model

import com.google.common.truth.Truth.assertThat
import com.stripe.android.CardNumberFixtures
import kotlin.test.Test

class CardParamsTest {

@Test
fun toParamMap_shouldCreateExpectedMap() {
val actualParams = CardParams(
number = CardNumberFixtures.VISA_NO_SPACES,
expMonth = 12,
expYear = 2025,
cvc = "123",
name = "Jenny Rosen",
address = AddressFixtures.ADDRESS,
currency = "usd",
metadata = mapOf("fruit" to "orange")
).toParamMap()

assertThat(actualParams)
fun `toParamMap() should create expected map`() {
assertThat(CardParamsFixture.DEFAULT.toParamMap())
.isEqualTo(
mapOf("card" to
mapOf(
Expand Down
101 changes: 51 additions & 50 deletions stripe/src/test/java/com/stripe/android/model/SourceParamsTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.stripe.android.model

import com.google.common.truth.Truth.assertThat
import com.stripe.android.CardNumberFixtures.VISA_NO_SPACES
import kotlin.test.Test
import kotlin.test.assertEquals
Expand Down Expand Up @@ -170,59 +171,59 @@ class SourceParamsTest {
}

@Test
fun createCardParams_hasBothExpectedMaps() {
val params = SourceParams.createCardParams(FULL_FIELDS_VISA_CARD)

val apiMap = params.apiParameterMap
requireNotNull(apiMap)
assertEquals(VISA_NO_SPACES, apiMap["number"])
assertEquals(12, apiMap["exp_month"])
assertEquals(2050, apiMap["exp_year"])
assertEquals("123", apiMap["cvc"])

val owner = requireNotNull(params.owner)
assertEquals("Captain Cardholder", owner.name)
assertNull(owner.email)
assertNull(owner.phone)

val addressMap = owner.address?.toParamMap().orEmpty()
assertEquals("1 ABC Street", addressMap["line1"])
assertEquals("Apt. 123", addressMap["line2"])
assertEquals("San Francisco", addressMap["city"])
assertEquals("CA", addressMap["state"])
assertEquals("94107", addressMap["postal_code"])
assertEquals("US", addressMap["country"])

assertEquals(METADATA, params.metaData)
fun `create with CardParams object should return expected map`() {
assertThat(SourceParams.createCardParams(CardParamsFixture.DEFAULT).toParamMap())
.isEqualTo(
mapOf(
"type" to "card",
"card" to mapOf(
"number" to VISA_NO_SPACES,
"exp_month" to 12,
"exp_year" to 2025,
"cvc" to "123"
),
"owner" to mapOf(
"address" to mapOf(
"line1" to "123 Market St",
"line2" to "#345",
"city" to "San Francisco",
"state" to "CA",
"postal_code" to "94107",
"country" to "US"
),
"name" to "Jenny Rosen"
),
"metadata" to mapOf("fruit" to "orange")
)
)
}

@Test
fun createCardParams_toParamMap_createsExpectedMap() {
val params = SourceParams.createCardParams(FULL_FIELDS_VISA_CARD)

val totalExpectedMap = mapOf(
"type" to "card",
"card" to mapOf(
"number" to VISA_NO_SPACES,
"exp_month" to 12,
"exp_year" to 2050,
"cvc" to "123"
),
"owner" to mapOf(
"address" to mapOf(
"line1" to "1 ABC Street",
"line2" to "Apt. 123",
"city" to "San Francisco",
"state" to "CA",
"postal_code" to "94107",
"country" to "US"
),
"name" to "Captain Cardholder"
),
"metadata" to METADATA
)

assertEquals(totalExpectedMap, params.toParamMap())
fun `create with Card object should return expected map`() {
assertThat(SourceParams.createCardParams(FULL_FIELDS_VISA_CARD).toParamMap())
.isEqualTo(
mapOf(
"type" to "card",
"card" to mapOf(
"number" to VISA_NO_SPACES,
"exp_month" to 12,
"exp_year" to 2050,
"cvc" to "123"
),
"owner" to mapOf(
"address" to mapOf(
"line1" to "1 ABC Street",
"line2" to "Apt. 123",
"city" to "San Francisco",
"state" to "CA",
"postal_code" to "94107",
"country" to "US"
),
"name" to "Captain Cardholder"
),
"metadata" to METADATA
)
)
}

@Test
Expand Down

0 comments on commit e4bb229

Please sign in to comment.