Checkout page remains logout after first load.

Checkout page remains logout after first load.

abhikldh6929
Shopify Partner
3 0 0

I am developing mobile app using shopify android buy sdk and checkout sdk . When i am opening checkout then for first time checkout page remaing logged in with user detail but when it is openend second time then it does not loads the logged in user data. 

 

Below is my code. 

 

 

val accessToken = SharedPrefs.getInstance(requireContext()).getAccessToken()
        val cartId = SharedPrefs.getInstance(requireContext()).getCartId()
        val email = SharedPrefs.getInstance(requireContext()).getEmail()
        CoroutineScope(Dispatchers.Main).launch {
            if (cartId.isNullOrEmpty()) {
                val cartCreateInput = CartInput()
                if (!accessToken.isNullOrEmpty()) {
                    val buyerIdentity = Storefront.CartBuyerIdentityInput()
                        .setCustomerAccessToken(accessToken)
                        .setEmail(email)
                    cartCreateInput.setBuyerIdentity(buyerIdentity)
                }
                cartCreateInput.setLines(mutableListOf(Storefront.CartLineInput(ID(selectedVariant))))
                val mutation = Storefront.mutation { mutation ->
                    mutation.cartCreate({ arg -> arg.input(cartCreateInput) }) { createPayload ->
                        createPayload.cart { cart ->
                            cart.createdAt()
                            cart.updatedAt()
                            cart.checkoutUrl()
                        }
                            .userErrors { error ->
                                error
                                    .field()
                                    .message()
                            }
                    }
                }
                val call = ShopifyClient.client.mutateGraph(mutation)

                call.enqueue { result ->

                    when (result) {

                        is GraphCallResult.Success -> {
                            val response = result.response
                            val data = response.data?.cartCreate
                            if (data?.cart?.id != null) {
                                SharedPrefs.getInstance(requireContext()).saveCartId(data.cart.id.toString())
                                if (withBuyNow) {
                                    if (data.cart?.checkoutUrl != null) {
                                        Handler(Looper.getMainLooper()).post {
                                            initiateCheckout(requireContext(), requireActivity(), data.cart?.checkoutUrl.toString())
                                        }
                                    }
                                } else {
                                    showSnackBar(binding.root, "Product added to cart.")
                                }
                            }
                            toggleCartLoader(false)
                        }

                        is GraphCallResult.Failure -> {
                            val error = result.error
                            Log.e("Customer Login", " $error")
                            toggleCartLoader(false)
                        }

                    }

                }

            } else {
                val buyerIdentity = Storefront.CartBuyerIdentityInput()
                if (!accessToken.isNullOrEmpty()) {
                    buyerIdentity.setEmailInput(Input.value(email))
                    buyerIdentity.setCustomerAccessToken(accessToken)
                    val defAdd = fetchDefaultAddress(accessToken)
                    val shippingAddressInput = Storefront.MailingAddressInput().apply {
                        defAdd?.let {
                            firstName = it.firstName
                            lastName = it.lastName
                            address1 = it.address1
                            city = it.city
                            country = it.country
                            province = it.province
                            zip = it.zip
                            phone = it.phone
                        }
                    }
                    buyerIdentity.setDeliveryAddressPreferences(listOf(Storefront.DeliveryAddressInput().setDeliveryAddress(shippingAddressInput)))

                }
                val cartCreateInput = Storefront.CartLineInput(ID(selectedVariant))
                val mutation = Storefront.mutation { mutation ->
                    mutation.cartBuyerIdentityUpdate(ID(cartId), buyerIdentity) {
                        it.cart {
                            it.buyerIdentity {
                                it.email()
                            }
                        }
                    }.cartLinesAdd(mutableListOf(cartCreateInput), ID(cartId)) {
                        it.cart {
                            it.updatedAt()
                            it.checkoutUrl()
                        }
                    }
                }

                val call = ShopifyClient.client.mutateGraph(mutation)
                call.enqueue { result ->

                    when (result) {

                        is GraphCallResult.Success -> {
                            val response = result.response
                            val data = response.data?.cartLinesAdd

                            if (data?.cart?.id != null) {
                                Log.e("Cart 0", " ${data?.cart?.buyerIdentity?.email}")
                                if (withBuyNow) {
                                    if (data.cart?.checkoutUrl != null) {
                                        Handler(Looper.getMainLooper()).post {
                                            initiateCheckout(requireContext(), requireActivity(), data.cart?.checkoutUrl.toString())
                                        }
                                    }
                                } else {
                                    showSnackBar(binding.root, "Product added to cart.")
                                }
                            }

                            Log.e("Cart 1", " ${response.data?.cartBuyerIdentityUpdate?.cart?.buyerIdentity?.email}")
                            toggleCartLoader(false)
                        }

                        is GraphCallResult.Failure -> {
                            val error = result.error
                            toggleCartLoader(false)
                            Log.e("Cart", " $error")
                        }

                    }

                }

            }
        }
Replies 0 (0)