-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathComposeButtonsFragment.kt
More file actions
191 lines (173 loc) · 7.27 KB
/
ComposeButtonsFragment.kt
File metadata and controls
191 lines (173 loc) · 7.27 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.braintreepayments.demo
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.SegmentedButton
import androidx.compose.material3.SegmentedButtonDefaults
import androidx.compose.material3.SingleChoiceSegmentedButtonRow
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.core.net.toUri
import androidx.navigation.fragment.NavHostFragment
import com.braintreepayments.api.core.PaymentMethodNonce
import com.braintreepayments.api.paypal.PayPalResult
import com.braintreepayments.api.paypal.PayPalTokenizeCallback
import com.braintreepayments.api.uicomponents.PayPalButtonColor
import com.braintreepayments.api.uicomponents.VenmoButtonColor
import com.braintreepayments.api.uicomponents.compose.PayPalSmartButton
import com.braintreepayments.api.uicomponents.compose.VenmoSmartButton
import com.braintreepayments.api.venmo.VenmoPaymentMethodUsage
import com.braintreepayments.api.venmo.VenmoRequest
import com.braintreepayments.api.venmo.VenmoResult
import com.braintreepayments.api.venmo.VenmoTokenizeCallback
class ComposeButtonsFragment : BaseFragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
super.onCreateView(inflater, container, savedInstanceState)
val paypalRequest = paypalRequest(requireContext())
return ComposeView(requireContext()).apply {
setContent {
var paypalStyle: PayPalButtonColor by remember { mutableStateOf(PayPalButtonColor.Blue) }
var venmoStyle: VenmoButtonColor by remember { mutableStateOf(VenmoButtonColor.Blue) }
Column {
SingleChoiceSegmentedButton(onClick = { selectedIndex ->
paypalStyle = when (selectedIndex) {
0 -> PayPalButtonColor.Blue
1 -> PayPalButtonColor.Black
2 -> PayPalButtonColor.White
else -> PayPalButtonColor.Blue
}
})
PayPalSmartButton(
style = paypalStyle,
payPalRequest = paypalRequest,
authorization = authStringArg,
appLinkReturnUrl =
"https://mobile-sdk-demo-site-838cead5d3ab.herokuapp.com/braintree-payments".toUri(),
deepLinkFallbackUrlScheme = "com.braintreepayments.demo.braintree",
paypalTokenizeCallback = paypalTokenizeCallback
)
SingleChoiceSegmentedButton(onClick = { selectedIndex ->
venmoStyle = when (selectedIndex) {
0 -> VenmoButtonColor.Blue
1 -> VenmoButtonColor.Black
2 -> VenmoButtonColor.White
else -> VenmoButtonColor.Blue
}
})
VenmoSmartButton(
style = venmoStyle,
venmoRequest = venmoRequest,
authorization = authStringArg,
appLinkReturnUrl =
"https://mobile-sdk-demo-site-838cead5d3ab.herokuapp.com/braintree-payments".toUri(),
deepLinkFallbackUrlScheme = "com.braintreepayments.demo.braintree",
venmoTokenizeCallback = venmoTokenizeCallback
)
}
}
}
}
@Composable
fun SingleChoiceSegmentedButton(modifier: Modifier = Modifier, onClick: (Int) -> Unit = {}) {
var selectedIndex by remember { mutableIntStateOf(0) }
val options = listOf("Blue", "Black", "White")
SingleChoiceSegmentedButtonRow {
options.forEachIndexed { index, label ->
SegmentedButton(
shape = SegmentedButtonDefaults.itemShape(
index = index,
count = options.size
),
onClick = {
selectedIndex = index
onClick(selectedIndex)
},
selected = index == selectedIndex,
label = { Text(label) }
)
}
}
}
private fun handlePayPalResult(paymentMethodNonce: PaymentMethodNonce?) {
if (paymentMethodNonce != null) {
super.onPaymentMethodNonceCreated(paymentMethodNonce)
val action =
ComposeButtonsFragmentDirections.actionComposePaymentButtonsFragmentToDisplayNonceFragment(
paymentMethodNonce
)
NavHostFragment.findNavController(this).navigate(action)
}
}
private fun handleVenmoResult(paymentMethodNonce: PaymentMethodNonce?) {
if (paymentMethodNonce != null) {
super.onPaymentMethodNonceCreated(paymentMethodNonce)
val action =
ComposeButtonsFragmentDirections.actionComposePaymentButtonsFragmentToDisplayNonceFragment(
paymentMethodNonce
)
NavHostFragment.findNavController(this).navigate(action)
}
}
private fun paypalRequest(context: Context) = PayPalRequestFactory.createPayPalCheckoutRequest(
context,
"10.0",
null,
null,
null,
false,
null,
false,
false,
false
)
private val paypalTokenizeCallback = PayPalTokenizeCallback { payPalResult ->
when (payPalResult) {
is PayPalResult.Success -> {
handlePayPalResult(payPalResult.nonce)
}
is PayPalResult.Cancel -> {
handleError(Exception("User did not complete PayPal payment flow"))
}
is PayPalResult.Failure -> {
handleError(payPalResult.error)
}
}
}
private val venmoRequest = VenmoRequest(VenmoPaymentMethodUsage.SINGLE_USE).apply {
profileId = null
shouldVault = shouldVault
collectCustomerBillingAddress = true
collectCustomerShippingAddress = true
totalAmount = "0.20"
subTotalAmount = "0.18"
taxAmount = "0.01"
shippingAmount = "0.01"
}
private val venmoTokenizeCallback = VenmoTokenizeCallback { venmoResult ->
when (venmoResult) {
is VenmoResult.Success -> {
handleVenmoResult(venmoResult.nonce)
}
is VenmoResult.Cancel -> {
handleError(Exception("User did not complete Venmo payment flow"))
}
is VenmoResult.Failure -> {
handleError(venmoResult.error)
}
}
}
}