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 GooglePayResult #1861

Merged
merged 1 commit into from
Nov 22, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.stripe.android.model

import com.stripe.android.model.StripeJsonUtils.optString
import org.json.JSONException
import org.json.JSONObject

/**
* Result of a successful Google Pay Payment Data Request
*/
data class GooglePayResult constructor(
val token: Token? = null,
val address: Address? = null,
val name: String? = null,
val email: String? = null,
val phoneNumber: String? = null
) {
companion object {
/**
* @param paymentDataJson a JSONObject representation of a
* [PaymentData](https://developers.google.com/pay/api/android/reference/response-objects#PaymentData)
* response
*/
@Throws(JSONException::class)
@JvmStatic
fun fromJson(paymentDataJson: JSONObject): GooglePayResult {
val paymentMethodData = paymentDataJson
.getJSONObject("paymentMethodData")
val paymentToken = paymentMethodData
.getJSONObject("tokenizationData")
.getString("token")
val stripeToken = Token.fromJson(JSONObject(paymentToken))

val googlePayBillingAddress = paymentMethodData
.getJSONObject("info")
.optJSONObject("billingAddress")

val address = googlePayBillingAddress?.let {
Address(
line1 = optString(it, "address1"),
line2 = optString(it, "address2"),
city = optString(it, "locality"),
state = optString(it, "administrativeArea"),
country = optString(it, "countryCode"),
postalCode = optString(it, "postalCode")
)
}

val name = optString(googlePayBillingAddress, "name")
val email = optString(paymentDataJson, "email")
val phone = optString(googlePayBillingAddress, "phoneNumber")

return GooglePayResult(
token = stripeToken,
address = address,
name = name,
email = email,
phoneNumber = phone
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -312,41 +312,17 @@ data class PaymentMethodCreateParams internal constructor(
@Throws(JSONException::class)
@JvmStatic
fun createFromGooglePay(googlePayPaymentData: JSONObject): PaymentMethodCreateParams {
val paymentMethodData = googlePayPaymentData
.getJSONObject("paymentMethodData")
val googlePayBillingAddress = paymentMethodData
.getJSONObject("info")
.optJSONObject("billingAddress")
val paymentToken = paymentMethodData
.getJSONObject("tokenizationData")
.getString("token")
val stripeToken = Token.fromJson(JSONObject(paymentToken))
val stripeTokenId = requireNotNull(stripeToken).id

val email = googlePayPaymentData.optString("email")
val billingDetails = if (googlePayBillingAddress != null) {
PaymentMethod.BillingDetails.Builder()
.setAddress(Address.Builder()
.setLine1(googlePayBillingAddress.optString("address1"))
.setLine2(googlePayBillingAddress.optString("address2"))
.setCity(googlePayBillingAddress.optString("locality"))
.setState(googlePayBillingAddress.optString("administrativeArea"))
.setCountry(googlePayBillingAddress.optString("countryCode"))
.setPostalCode(googlePayBillingAddress.optString("postalCode"))
.build())
.setName(googlePayBillingAddress.optString("name"))
.setEmail(email)
.setPhone(googlePayBillingAddress.optString("phoneNumber"))
.build()
} else {
PaymentMethod.BillingDetails.Builder()
.setEmail(email)
.build()
}
val googlePayResult = GooglePayResult.fromJson(googlePayPaymentData)
val tokenId = requireNotNull(googlePayResult.token?.id)

return create(
Card.create(stripeTokenId),
billingDetails
Card.create(tokenId),
PaymentMethod.BillingDetails.Builder()
.setAddress(googlePayResult.address)
.setName(googlePayResult.name)
.setEmail(googlePayResult.email)
.setPhone(googlePayResult.phoneNumber)
.build()
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.stripe.android.model
import org.json.JSONObject

internal object GooglePayFixtures {
@JvmField
val GOOGLE_PAY_RESULT_WITH_FULL_BILLING_ADDRESS = JSONObject(
"""
{
Expand Down Expand Up @@ -38,7 +37,6 @@ internal object GooglePayFixtures {
""".trimIndent()
)

@JvmField
val GOOGLE_PAY_RESULT_WITH_NO_BILLING_ADDRESS = JSONObject(
"""
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.stripe.android.model

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

class GooglePayResultTest {

@Test
fun fromJson_withFullBillingAddress() {
val result = GooglePayResult.fromJson(
GooglePayFixtures.GOOGLE_PAY_RESULT_WITH_FULL_BILLING_ADDRESS
)
val expectedAddress = Address(
line1 = "510 Townsend St",
city = "San Francisco",
state = "CA",
country = "US",
postalCode = "94103"
)

assertEquals("tok_1F4VSjBbvEcIpqUbSsbEtBap", result.token?.id)
assertEquals("card", result.token?.type)

assertEquals(expectedAddress, result.address)

assertEquals("Stripe Johnson", result.name)
assertEquals("stripe@example.com", result.email)
assertEquals("1-888-555-1234", result.phoneNumber)
}

@Test
fun fromJson_withNoBillingAddress() {
val result = GooglePayResult.fromJson(
GooglePayFixtures.GOOGLE_PAY_RESULT_WITH_NO_BILLING_ADDRESS
)

assertEquals("tok_1F4ACMCRMbs6FrXf6fPqLnN7", result.token?.id)
assertEquals("card", result.token?.type)
assertNull(result.address)
assertNull(result.name)
assertNull(result.email)
assertNull(result.phoneNumber)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class PaymentMethodCreateParamsTest {
val expectedParams = PaymentMethodCreateParams.create(
PaymentMethodCreateParams.Card.create("tok_1F4ACMCRMbs6FrXf6fPqLnN7"),
PaymentMethod.BillingDetails.Builder()
.setEmail("")
.build()
)
assertEquals(expectedParams, createdParams)
Expand All @@ -43,7 +42,6 @@ class PaymentMethodCreateParamsTest {
.setName("Stripe Johnson")
.setAddress(Address.Builder()
.setLine1("510 Townsend St")
.setLine2("")
.setCity("San Francisco")
.setState("CA")
.setPostalCode("94103")
Expand Down