-
Notifications
You must be signed in to change notification settings - Fork 662
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add createPaymentDetails endpoint (#4941)
- Loading branch information
1 parent
66bea42
commit a2b6760
Showing
12 changed files
with
432 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
payments-core/src/main/java/com/stripe/android/model/ConsumerPaymentDetailsCreateParams.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.stripe.android.model | ||
|
||
import android.os.Parcelable | ||
import kotlinx.parcelize.Parcelize | ||
import kotlinx.parcelize.RawValue | ||
|
||
/** | ||
* Model for Link Payment Method creation parameters, used for 'consumers/payment_details' endpoint. | ||
*/ | ||
sealed class ConsumerPaymentDetailsCreateParams( | ||
internal val type: PaymentMethod.Type | ||
) : StripeParamsModel, Parcelable { | ||
|
||
override fun toParamMap(): Map<String, Any> = | ||
mapOf(PARAM_TYPE to type.code) | ||
|
||
companion object { | ||
private const val PARAM_TYPE = "type" | ||
} | ||
|
||
/** | ||
* Represents a new Card payment method that will be created using the | ||
* [cardPaymentMethodCreateParamsMap] values, converting from the [PaymentMethodCreateParams] | ||
* format to [ConsumerPaymentDetailsCreateParams] format. | ||
*/ | ||
@Parcelize | ||
class Card( | ||
private val cardPaymentMethodCreateParamsMap: Map<String, @RawValue Any> | ||
) : ConsumerPaymentDetailsCreateParams(PaymentMethod.Type.Card) { | ||
override fun toParamMap() = super.toParamMap() | ||
.plus(convertParamsMap()) | ||
|
||
private fun convertParamsMap(): Map<String, Any> { | ||
val params: MutableMap<String, Any> = mutableMapOf() | ||
// card["billing_details"]["address"] becomes card["billing_address"] | ||
( | ||
(cardPaymentMethodCreateParamsMap[PARAM_BILLING_DETAILS] as? Map<*, *>) | ||
?.get(PARAM_ADDRESS) as? Map<*, *> | ||
)?.let { | ||
params[PARAM_BILLING_ADDRESS] = mapOf( | ||
// card["billing_details"]["address"]["country"] | ||
// becomes card["billing_address"]["country_code"] | ||
PARAM_COUNTRY_CODE to it[PARAM_COUNTRY], | ||
PARAM_POSTAL_CODE to it[PARAM_POSTAL_CODE] | ||
) | ||
} | ||
// only card number, exp_month and exp_year are included | ||
(cardPaymentMethodCreateParamsMap[PARAM_CARD] as? Map<*, *>)?.let { | ||
params[PARAM_CARD] = it.toMutableMap().filterKeys { key -> | ||
key in setOf(PARAM_CARD_NUMBER, PARAM_CARD_EXP_MONTH, PARAM_CARD_EXP_YEAR) | ||
} | ||
} | ||
return params | ||
} | ||
|
||
companion object { | ||
private const val PARAM_CARD = "card" | ||
private const val PARAM_CARD_NUMBER = "number" | ||
private const val PARAM_CARD_EXP_MONTH = "exp_month" | ||
private const val PARAM_CARD_EXP_YEAR = "exp_year" | ||
private const val PARAM_BILLING_ADDRESS = "billing_address" | ||
private const val PARAM_COUNTRY_CODE = "country_code" | ||
private const val PARAM_POSTAL_CODE = "postal_code" | ||
|
||
private const val PARAM_BILLING_DETAILS = "billing_details" | ||
private const val PARAM_ADDRESS = "address" | ||
private const val PARAM_COUNTRY = "country" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...nts-core/src/test/java/com/stripe/android/model/ConsumerPaymentDetailsCreateParamsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.stripe.android.model | ||
|
||
import com.google.common.truth.Truth.assertThat | ||
import org.junit.Test | ||
|
||
class ConsumerPaymentDetailsCreateParamsTest { | ||
|
||
@Test | ||
fun createCardParams_generatesCorrectParameters() { | ||
assertThat( | ||
ConsumerPaymentDetailsCreateParams.Card( | ||
mapOf( | ||
"ignored" to "none", | ||
"card" to mapOf( | ||
"number" to "123", | ||
"cvc" to "321", | ||
"brand" to "visa", | ||
"exp_month" to "12", | ||
"exp_year" to "2050" | ||
), | ||
"billing_details" to mapOf<String, Any>( | ||
"address" to mapOf( | ||
"country" to "US", | ||
"postal_code" to "12345", | ||
"extra" to "1" | ||
) | ||
) | ||
) | ||
).toParamMap() | ||
).isEqualTo( | ||
mapOf( | ||
"type" to "card", | ||
"card" to mapOf( | ||
"number" to "123", | ||
"exp_month" to "12", | ||
"exp_year" to "2050" | ||
), | ||
"billing_address" to mapOf( | ||
"country_code" to "US", | ||
"postal_code" to "12345" | ||
) | ||
) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.