From 94baa97bdbaf9d0accbb3a1162f902062d30c2e1 Mon Sep 17 00:00:00 2001 From: Jason Atwood Date: Fri, 27 Sep 2024 15:26:46 -0400 Subject: [PATCH] Update CashAppV3SampleActivity to align with documentation --- docs/getting-started/checkout-v3.md | 8 ++-- docs/getting-started/v3-with-cash-app-pay.md | 4 +- .../com/example/CashAppV3SampleActivity.kt | 41 ++++++++++++------- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/docs/getting-started/checkout-v3.md b/docs/getting-started/checkout-v3.md index fe292645..2b9070f6 100644 --- a/docs/getting-started/checkout-v3.md +++ b/docs/getting-started/checkout-v3.md @@ -20,7 +20,7 @@ nav_order: 5 {: .alert } > Checkout V3 is currently available in the following region(s): US, UK, Australia and Canada -> + Checkout V3 generates a one-time payment card for every Afterpay order and provides the card number to insert into your credit card checkout. This allows for a front-end-only integration. Unlike V1 and V2, with V3 your server does not directly interact with Afterpay APIs. The one-time payment card is Visa for the US, UK, and Canada. In Australia the one-time payment card is Mastercard. ## How it works @@ -88,14 +88,12 @@ registerForActivityResult( } ``` -## Step 4: Receive one-time use card details and pass back to your server for processing +## Step 4: Receive one-time use card details and process -The success result will contain card details, tokens, and a valid-until time. Pass these back to your own server and process them through your normal card processing infrastructure. +The success result will contain card details, tokens, and a valid-until time. Pass these back to your own server and process them through your normal card processing infrastructure, or pass them on to another mobile card processing SDK . ```kotlin Afterpay.parseCheckoutSuccessResponseV3(intent)?.let { - // pass card details and token to your server for processing with - // your existing card processing network it.cardDetails it.tokens it.cardValidUntil diff --git a/docs/getting-started/v3-with-cash-app-pay.md b/docs/getting-started/v3-with-cash-app-pay.md index 1564aea1..ff6c27e8 100644 --- a/docs/getting-started/v3-with-cash-app-pay.md +++ b/docs/getting-started/v3-with-cash-app-pay.md @@ -301,9 +301,9 @@ fun confirmCheckoutWithAfterpay( } ``` -## Step 11: Receive one-time use card details and pass back to your server for processing +## Step 11: Receive one-time use card details and process -The success result will contain card details, tokens, and a valid-until time. Pass these back to your own server and process them through your normal card processing infrastructure. +The success result will contain card details, tokens, and a valid-until time. Pass these back to your own server and process them through your normal card processing infrastructure, or pass them on to another mobile card processing SDK . ```kotlin result.onSuccess { diff --git a/sample/src/main/java/com/example/CashAppV3SampleActivity.kt b/sample/src/main/java/com/example/CashAppV3SampleActivity.kt index 1d3a5d2a..e5f022e0 100644 --- a/sample/src/main/java/com/example/CashAppV3SampleActivity.kt +++ b/sample/src/main/java/com/example/CashAppV3SampleActivity.kt @@ -54,9 +54,7 @@ import kotlinx.coroutines.launch class CashAppV3SampleActivity : AppCompatActivity() { private lateinit var bindings: CashAppV3LayoutBinding - - private val cashAppPay: CashAppPay = - CashAppPayFactory.createSandbox(AFTERPAY_ENVIRONMENT.payKitClientId) + private lateinit var cashAppPay: CashAppPay private var checkoutV3CashAppPay: CheckoutV3CashAppPay? = null @@ -67,7 +65,7 @@ class CashAppV3SampleActivity : AppCompatActivity() { when (newState) { is ReadyToAuthorize -> { /** - * Step 5: Cash App Pay SDK will response when an authorization attempt is ready + * Step 6: Cash App Pay SDK will response when an authorization attempt is ready * We can now enable the button to let customer proceed with checkout */ lifecycleScope.launch { // jump back to UI thread to update UI @@ -77,14 +75,14 @@ class CashAppV3SampleActivity : AppCompatActivity() { Authorizing -> { /** - * Step 7: Disable button while auth in process + * Step 8: Disable button while auth in process */ bindings.cashappPayButton.isEnabled = false } is Approved -> { /** - * Step 8: After successful approval, confirm checkout with Afterpay + * Step 9: After successful approval, confirm checkout with Afterpay */ Log.d(tag, newState.responseData.toString()) @@ -161,7 +159,6 @@ class CashAppV3SampleActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - cashAppPay.registerForStateUpdates(cashAppPayListener) /** * The SDK is agnostic to the UI library of your choice: inflate XML, view binding, Compose, etc. * Here we use view binding to keep it simple. @@ -171,7 +168,7 @@ class CashAppV3SampleActivity : AppCompatActivity() { isEnabled = false setOnClickListener { _ -> /** - * Step 6: When customer clicks Cash App Pay button + * Step 7: When customer clicks Cash App Pay button * begin authorization. This will begin UI flow into Cash App */ cashAppPay.authorizeCustomerRequest() @@ -209,6 +206,7 @@ class CashAppV3SampleActivity : AppCompatActivity() { */ Log.d(tag, "Fetched merchant configs") Afterpay.setConfigurationV3(merchantConfiguration) + initializeCashAppSDK() beginCheckout() } @@ -221,9 +219,18 @@ class CashAppV3SampleActivity : AppCompatActivity() { } } + private fun initializeCashAppSDK() { + /** + * Step 2: create and register a state listener with Cash APp Pay SDK + */ + Log.d(tag, "Initializing Cash App Pay SDK") + cashAppPay = CashAppPayFactory.createSandbox(AFTERPAY_ENVIRONMENT.payKitClientId) + cashAppPay.registerForStateUpdates(cashAppPayListener) + } + private suspend fun beginCheckout() { /** - * Step 2: Begin checkout process by requesting data from Afterpay, to be used later + * Step 3: Begin checkout process by requesting data from Afterpay, to be used later * with Cash App Pay SDK */ Afterpay.beginCheckoutV3WithCashAppPay( @@ -234,7 +241,7 @@ class CashAppV3SampleActivity : AppCompatActivity() { ).let { result: Result -> result.onSuccess { /** - * Step 3: Store [CheckoutV3CashAppPay] for use later and create Customer Request + * Step 4: Store [CheckoutV3CashAppPay] for use later and create Customer Request */ checkoutV3CashAppPay = it createCashAppPayCustomerRequest(it) @@ -250,7 +257,7 @@ class CashAppV3SampleActivity : AppCompatActivity() { private fun createCashAppPayCustomerRequest(checkoutV3CashAppPay: CheckoutV3CashAppPay) { /** - * Step 4: Create customer request with Cash App Pay SDK + * Step 5: Create customer request with Cash App Pay SDK * * [redirectUri] defines where customer returns to after completing Cash App flow * You are not required to use [CheckoutV3CashAppPay.redirectUri] . You can set your own @@ -262,8 +269,10 @@ class CashAppV3SampleActivity : AppCompatActivity() { val action = CashAppPayPaymentAction.OneTimeAction( currency = USD, amount = checkoutV3CashAppPay.amount.toInt(), - // This is not the same merchant ID you started with. - // This is a specific for use with Cash App Pay SDK. + /** + * This is not the same merchant ID you set in [CheckoutV3Configuration]. + * This is a specific for use with Cash App Pay SDK. + */ scopeId = checkoutV3CashAppPay.merchantId, ) @@ -279,6 +288,10 @@ class CashAppV3SampleActivity : AppCompatActivity() { ) { requireNotNull(checkoutV3CashAppPay) checkoutV3CashAppPay?.let { it -> + /** + * Step 10: confirm checkout with Afterpay by combining newly received [grantId] and + * [customerId] with previously-saved [CheckoutV3CashAppPay]. + */ Afterpay.confirmCheckoutV3WithCashAppPay( grantId = grantId, customerId = customerId, @@ -290,7 +303,7 @@ class CashAppV3SampleActivity : AppCompatActivity() { result.onSuccess { /** - * Step 9: Receive single-use card details. Pass these back to your server + * Step 11: Receive single-use card details. Pass these back to your server * and on to your payment processor: */ Log.d(tag, "Received card details, etc ${it.cardDetails}")