-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCartActivity.kt
More file actions
81 lines (70 loc) · 2.62 KB
/
CartActivity.kt
File metadata and controls
81 lines (70 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.shopify.example.storefront
// [START complete-tutorial.present-checkout]
// [START integrate.present-checkout]
// [START integrate.install]
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.shopify.checkoutsheetkit.*
import com.shopify.checkoutsheetkit.lifecycleevents.CheckoutCompletedEvent
import kotlinx.coroutines.launch
// [END integrate.install]
class CartActivity : AppCompatActivity() {
private var checkoutUrl: String? = null
private val storefrontClient = StorefrontClient(
shopDomain = "{shop}.myshopify.com",
accessToken = "your-storefront-access-token"
)
private val checkoutEventProcessor = object : DefaultCheckoutEventProcessor(this) {
override fun onCheckoutCompleted(event: CheckoutCompletedEvent) {
val orderId = event.orderDetails.id
Log.d("Checkout", "Order completed: $orderId")
checkoutUrl = null
}
override fun onCheckoutCanceled() {
Log.d("Checkout", "Checkout canceled")
}
override fun onCheckoutFailed(error: CheckoutException) {
Log.e("Checkout", "Checkout failed: ${error.message}")
Toast.makeText(
this@CartActivity,
"Checkout error: ${error.message}",
Toast.LENGTH_LONG
).show()
}
override fun onCheckoutLinkClicked(uri: android.net.Uri) {
val intent = android.content.Intent(android.content.Intent.ACTION_VIEW, uri)
startActivity(intent)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_cart)
findViewById<Button>(R.id.checkout_button).setOnClickListener {
presentCheckout()
}
}
fun addToCart(variantId: String) {
lifecycleScope.launch {
try {
val cart = storefrontClient.createCart(variantId)
checkoutUrl = cart.checkoutUrl
} catch (e: Exception) {
Log.e("Cart", "Failed to create cart: ${e.message}")
}
}
}
private fun presentCheckout() {
val url = checkoutUrl
if (url == null) {
Toast.makeText(this, "No checkout URL available", Toast.LENGTH_SHORT).show()
return
}
ShopifyCheckoutSheetKit.present(url, this, checkoutEventProcessor)
}
}
// [END integrate.present-checkout]
// [END complete-tutorial.present-checkout]