Skip to content

Commit 56356d6

Browse files
committed
Update kotlin example
1 parent fdf3ed6 commit 56356d6

7 files changed

Lines changed: 552 additions & 37 deletions

File tree

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools">
4-
4+
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
55
<application
66
android:name=".MainApplication"
77
android:allowBackup="true"
@@ -12,33 +12,33 @@
1212
android:supportsRtl="true"
1313
android:theme="@style/AppTheme"
1414
tools:replace="android:fullBackupContent,android:supportsRtl">
15-
16-
<activity android:name=".StartActivity"
15+
<activity
16+
android:name=".StartActivity"
1717
android:exported="true">
1818
<intent-filter>
1919
<action android:name="android.intent.action.MAIN" />
20+
2021
<category android:name="android.intent.category.LAUNCHER" />
2122
</intent-filter>
2223
</activity>
23-
24-
<activity
25-
android:name=".MainActivity" />
26-
24+
<activity android:name=".MainActivity" />
2725
<activity
2826
android:name=".CardReaderActivity"
2927
android:label="Card Reader" />
30-
3128
<activity
3229
android:name=".PayPalQrcActivity"
3330
android:label="PayPal Qrc" />
3431
<activity
3532
android:name=".ManualCardEntryActivity"
3633
android:label="Manual Card Entry" />
34+
<activity
35+
android:name=".SelectTippingStyleActivity"
36+
android:label="Select Tipping Style" />
3737
<activity
3838
android:name="com.izettle.android.auth.OAuthActivity"
39+
android:exported="true"
3940
android:launchMode="singleTask"
40-
android:taskAffinity="@string/oauth_activity_task_affinity"
41-
android:exported="true">
41+
android:taskAffinity="@string/oauth_activity_task_affinity">
4242
<intent-filter>
4343
<data
4444
android:host="@string/redirect_url_host"
@@ -50,7 +50,6 @@
5050
<category android:name="android.intent.category.BROWSABLE" />
5151
</intent-filter>
5252
</activity>
53-
5453
</application>
5554

5655
</manifest>

Examples/Example-Kotlin/app/src/main/java/com/zettle/payments/android/kotlin_example/CardReaderActivity.kt

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,25 @@ package com.zettle.payments.android.kotlin_example
22

33
import android.app.Activity
44
import android.content.Intent
5+
import android.os.Build
56
import android.os.Bundle
7+
import android.os.Parcelable
68
import android.text.SpannableStringBuilder
79
import android.view.ViewGroup
810
import android.widget.Button
911
import android.widget.CheckBox
1012
import android.widget.EditText
13+
import android.widget.TextView
1114
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
1215
import androidx.appcompat.app.AppCompatActivity
1316
import androidx.lifecycle.MutableLiveData
1417
import com.google.android.material.snackbar.Snackbar
15-
import com.zettle.sdk.feature.cardreader.payment.TippingStyle
18+
import com.zettle.sdk.feature.cardreader.payment.PayPalReaderTippingStyle
19+
import com.zettle.sdk.feature.cardreader.payment.TippingConfiguration
20+
import com.zettle.sdk.feature.cardreader.payment.TippingPercentageOptions
1621
import com.zettle.sdk.feature.cardreader.payment.Transaction
1722
import com.zettle.sdk.feature.cardreader.payment.TransactionReference
23+
import com.zettle.sdk.feature.cardreader.payment.ZettleReaderTippingStyle
1824
import com.zettle.sdk.feature.cardreader.payment.refunds.CardPaymentPayload
1925
import com.zettle.sdk.feature.cardreader.payment.refunds.RefundPayload
2026
import com.zettle.sdk.feature.cardreader.ui.CardReaderAction
@@ -38,10 +44,18 @@ class CardReaderActivity : AppCompatActivity() {
3844
private lateinit var refundAmountEditText: EditText
3945
private lateinit var settingsButton: Button
4046
private lateinit var amountEditText: EditText
41-
private lateinit var tippingStyleButton: Button
4247
private lateinit var installmentsCheckBox: CheckBox
4348
private lateinit var lastPaymentTraceId: MutableLiveData<String?>
44-
private var tippingStyle: TippingStyle = TippingStyle.None
49+
50+
private lateinit var selectTippingStyleButton: Button
51+
private lateinit var ztrTippingStyleLabel: TextView
52+
private lateinit var pprTippingStyleLabel: TextView
53+
private lateinit var openPprTippingSettingsButton: Button
54+
55+
private var tippingConfiguration: TippingConfiguration? = TippingConfiguration(
56+
ZettleReaderTippingStyle.None,
57+
PayPalReaderTippingStyle.None
58+
)
4559

4660
override fun onCreate(savedInstanceState: Bundle?) {
4761
super.onCreate(savedInstanceState)
@@ -51,10 +65,16 @@ class CardReaderActivity : AppCompatActivity() {
5165
retrieveButton = findViewById(R.id.retrieve_btn)
5266
settingsButton = findViewById(R.id.settings_btn)
5367
amountEditText = findViewById(R.id.amount_input)
54-
tippingStyleButton = findViewById(R.id.tipping_style_btn)
5568
refundAmountEditText = findViewById(R.id.refund_amount_input)
5669
installmentsCheckBox = findViewById(R.id.installments_check_box)
5770

71+
selectTippingStyleButton = findViewById(R.id.select_tipping_style_btn)
72+
selectTippingStyleButton.setOnClickListener { selectTippingStyle() }
73+
ztrTippingStyleLabel = findViewById(R.id.ztr_tipping_style_label)
74+
pprTippingStyleLabel = findViewById(R.id.ppr_tipping_style_label)
75+
openPprTippingSettingsButton = findViewById(R.id.open_ppr_tipping_settings_btn)
76+
openPprTippingSettingsButton.setOnClickListener { onTippingSettingsClicked() }
77+
5878
lastPaymentTraceId = MutableLiveData()
5979
lastPaymentTraceId.observe(this) { value: String? ->
6080
refundButton.isEnabled = value != null
@@ -65,13 +85,8 @@ class CardReaderActivity : AppCompatActivity() {
6585
refundButton.setOnClickListener { onRefundClicked() }
6686
retrieveButton.setOnClickListener { onRetrieveClicked() }
6787
settingsButton.setOnClickListener { onSettingsClicked() }
68-
tippingStyleButton.setOnClickListener { onTippingStyleClicked() }
6988

7089
setTippingStyleTitle()
71-
supportFragmentManager.setFragmentResultListener(TippingStyleBottomSheet.REQUEST_KEY, this) { _, result ->
72-
tippingStyle = result.getSerializable(TippingStyleBottomSheet.TIPPING_STYLE_KEY) as? TippingStyle ?: TippingStyle.None
73-
setTippingStyleTitle()
74-
}
7590
}
7691

7792
private val paymentLauncher =
@@ -158,9 +173,10 @@ class CardReaderActivity : AppCompatActivity() {
158173
val intent: Intent = CardReaderAction.Payment(
159174
reference = reference,
160175
amount = amount,
161-
tippingStyle = tippingStyle,
176+
tippingConfiguration = tippingConfiguration,
162177
enableInstallments = enableInstallments
163178
).charge(this)
179+
164180
paymentLauncher.launch(intent)
165181
}
166182

@@ -209,13 +225,50 @@ class CardReaderActivity : AppCompatActivity() {
209225
startActivity(CardReaderAction.Settings.show(this))
210226
}
211227

212-
private fun onTippingStyleClicked() {
213-
TippingStyleBottomSheet.newInstance()
214-
.show(supportFragmentManager, TippingStyleBottomSheet::class.java.simpleName)
228+
private val selectTippingStyleLauncher =
229+
registerForActivityResult(StartActivityForResult()) { result ->
230+
if (result.resultCode != Activity.RESULT_OK) {
231+
return@registerForActivityResult
232+
}
233+
234+
val data = result.data ?: return@registerForActivityResult
235+
data.parcelableExtra<TippingConfiguration>(
236+
SelectTippingStyleActivity.TIPPING_CONFIGS_KEY
237+
)?.let {
238+
tippingConfiguration = it
239+
setTippingStyleTitle()
240+
}
241+
}
242+
243+
private fun selectTippingStyle() {
244+
val intent = Intent(this, SelectTippingStyleActivity::class.java)
245+
intent.putExtra(SelectTippingStyleActivity.TIPPING_CONFIGS_KEY, tippingConfiguration)
246+
selectTippingStyleLauncher.launch(intent)
215247
}
216248

217249
private fun setTippingStyleTitle() {
218-
val tippingStyleTitle = "Tipping Style - ${tippingStyle.name}"
219-
tippingStyleButton.text = tippingStyleTitle
250+
tippingConfiguration?.let {
251+
ztrTippingStyleLabel.text = "Zettle Reader: ${it.zettleReaderTippingStyle.name}"
252+
val pprStyle = when (val style = it.payPalReaderTippingStyle) {
253+
is PayPalReaderTippingStyle.PredefinedPercentage -> style.javaClass.simpleName + style.options.displayText()
254+
else -> style.javaClass.simpleName
255+
}
256+
pprTippingStyleLabel.text = "PayPal Reader: $pprStyle"
257+
}
220258
}
259+
260+
private fun onTippingSettingsClicked() {
261+
val intent = CardReaderAction.TippingSettings().show(this)
262+
startActivity(intent)
263+
}
264+
265+
private fun TippingPercentageOptions?.displayText() = when (this) {
266+
null -> "(Options = Null)"
267+
else -> "($option1, $option2, $option3)"
268+
}
269+
}
270+
271+
inline fun <reified T : Parcelable> Intent.parcelableExtra(key: String): T? = when {
272+
Build.VERSION.SDK_INT >= 33 -> getParcelableExtra(key, T::class.java)
273+
else -> @Suppress("DEPRECATION") getParcelableExtra(key) as? T
221274
}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
package com.zettle.payments.android.kotlin_example
2+
3+
import android.content.Intent
4+
import android.os.Bundle
5+
import android.view.View
6+
import android.view.WindowManager
7+
import android.widget.Button
8+
import android.widget.EditText
9+
import android.widget.LinearLayout
10+
import android.widget.RadioGroup
11+
import androidx.activity.OnBackPressedCallback
12+
import androidx.appcompat.app.AppCompatActivity
13+
import com.zettle.sdk.ZettleSDK
14+
import com.zettle.sdk.feature.cardreader.payment.PayPalReaderTippingStyle
15+
import com.zettle.sdk.feature.cardreader.payment.TippingConfiguration
16+
import com.zettle.sdk.feature.cardreader.payment.TippingPercentageOptions
17+
import com.zettle.sdk.feature.cardreader.payment.ZettleReaderTippingStyle
18+
19+
class SelectTippingStyleActivity : AppCompatActivity() {
20+
21+
// Zettle Reader (ztr)
22+
private lateinit var ztrTippingStyleRadioGroup: RadioGroup
23+
24+
// PayPal Reader (ppr)
25+
private lateinit var pprTippingStyleRadioGroup: RadioGroup
26+
private lateinit var pprPercentOptionsRadioGroup: RadioGroup
27+
private lateinit var pprSectionPercentOptions: LinearLayout
28+
29+
private lateinit var pprPercentOption1: EditText
30+
private lateinit var pprPercentOption2: EditText
31+
private lateinit var pprPercentOption3: EditText
32+
33+
override fun onCreate(savedInstanceState: Bundle?) {
34+
super.onCreate(savedInstanceState)
35+
window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
36+
37+
if (!ZettleSDK.isInitialized) {
38+
finish()
39+
}
40+
41+
setContentView(R.layout.activity_tipping_settings_all)
42+
43+
ztrTippingStyleRadioGroup = findViewById(R.id.ztr_tipping_style_group)
44+
pprTippingStyleRadioGroup = findViewById(R.id.ppr_tipping_style_group)
45+
pprSectionPercentOptions = findViewById(R.id.section_ppr_percent_options)
46+
pprPercentOptionsRadioGroup = findViewById(R.id.ppr_percent_options_group)
47+
pprPercentOption1 = findViewById(R.id.ppr_percent_option_1)
48+
pprPercentOption2 = findViewById(R.id.ppr_percent_option_2)
49+
pprPercentOption3 = findViewById(R.id.ppr_percent_option_3)
50+
51+
pprTippingStyleRadioGroup.setOnCheckedChangeListener { _, checkedId ->
52+
if (checkedId == R.id.ppr_tipping_style_percentage) {
53+
pprSectionPercentOptions.visibility = View.VISIBLE
54+
} else {
55+
pprSectionPercentOptions.visibility = View.GONE
56+
}
57+
}
58+
59+
onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
60+
override fun handleOnBackPressed() {
61+
saveAndClose()
62+
}
63+
})
64+
65+
findViewById<Button>(R.id.save_btn).setOnClickListener {
66+
saveAndClose()
67+
}
68+
69+
intent.parcelableExtra<TippingConfiguration>(
70+
TIPPING_CONFIGS_KEY
71+
)?.let {
72+
setZettleReaderTippingStyle(it.zettleReaderTippingStyle)
73+
setPayPalReaderTippingStyle(it.payPalReaderTippingStyle)
74+
}
75+
}
76+
77+
private fun saveAndClose() {
78+
val data = Intent().apply {
79+
putExtra(
80+
TIPPING_CONFIGS_KEY, TippingConfiguration(
81+
zettleReaderTippingStyle = getZettleReaderTippingStyle(),
82+
payPalReaderTippingStyle = getPayPalReaderTippingStyle()
83+
))
84+
}
85+
setResult(RESULT_OK, data)
86+
finish()
87+
}
88+
89+
private fun setZettleReaderTippingStyle(style: ZettleReaderTippingStyle) {
90+
ztrTippingStyleRadioGroup.check(
91+
when (style) {
92+
ZettleReaderTippingStyle.Default -> R.id.ztr_tipping_style_default
93+
ZettleReaderTippingStyle.Amount -> R.id.ztr_tipping_style_amount
94+
ZettleReaderTippingStyle.Percentage -> R.id.ztr_tipping_style_percentage
95+
ZettleReaderTippingStyle.None -> R.id.ztr_tipping_style_none
96+
}
97+
)
98+
}
99+
100+
private fun setPayPalReaderTippingStyle(style: PayPalReaderTippingStyle) {
101+
pprTippingStyleRadioGroup.check(
102+
when (style) {
103+
PayPalReaderTippingStyle.None -> R.id.ppr_tipping_style_none
104+
PayPalReaderTippingStyle.Default -> R.id.ppr_tipping_style_default
105+
PayPalReaderTippingStyle.CustomAmount -> R.id.ppr_tipping_style_amount
106+
is PayPalReaderTippingStyle.PredefinedPercentage -> R.id.ppr_tipping_style_percentage.also {
107+
setPayPalReaderPercentOptions(style.options)
108+
}
109+
PayPalReaderTippingStyle.SDKConfigured -> R.id.ppr_tipping_style_sdk_configured
110+
}
111+
)
112+
}
113+
114+
private fun setPayPalReaderPercentOptions(options: TippingPercentageOptions?) {
115+
pprPercentOptionsRadioGroup.check(
116+
when (options) {
117+
null -> R.id.ppr_percent_options_null
118+
else -> R.id.ppr_percent_options_custom.also {
119+
showPayPalReaderPercentOptions(options)
120+
}
121+
}
122+
)
123+
}
124+
125+
private fun showPayPalReaderPercentOptions(options: TippingPercentageOptions) {
126+
pprPercentOption1.setText(options.option1.toString())
127+
pprPercentOption2.setText(options.option2.toString())
128+
pprPercentOption3.setText(options.option3.toString())
129+
}
130+
131+
private fun getZettleReaderTippingStyle(): ZettleReaderTippingStyle =
132+
when (ztrTippingStyleRadioGroup.checkedRadioButtonId) {
133+
R.id.ztr_tipping_style_none -> ZettleReaderTippingStyle.None
134+
R.id.ztr_tipping_style_default -> ZettleReaderTippingStyle.Default
135+
R.id.ztr_tipping_style_amount -> ZettleReaderTippingStyle.Amount
136+
R.id.ztr_tipping_style_percentage -> ZettleReaderTippingStyle.Percentage
137+
else -> ZettleReaderTippingStyle.None
138+
}
139+
140+
private fun getPayPalReaderTippingStyle(): PayPalReaderTippingStyle =
141+
when (pprTippingStyleRadioGroup.checkedRadioButtonId) {
142+
R.id.ppr_tipping_style_none -> PayPalReaderTippingStyle.None
143+
R.id.ppr_tipping_style_default -> PayPalReaderTippingStyle.Default
144+
R.id.ppr_tipping_style_amount -> PayPalReaderTippingStyle.CustomAmount
145+
R.id.ppr_tipping_style_percentage -> PayPalReaderTippingStyle.PredefinedPercentage(getPayPalReaderPercentOptions())
146+
R.id.ppr_tipping_style_sdk_configured -> PayPalReaderTippingStyle.SDKConfigured
147+
else -> PayPalReaderTippingStyle.None
148+
}
149+
150+
private fun getPayPalReaderPercentOptions(): TippingPercentageOptions? =
151+
when(pprPercentOptionsRadioGroup.checkedRadioButtonId) {
152+
R.id.ppr_percent_options_custom -> TippingPercentageOptions(
153+
option1 = pprPercentOption1.text.toString().toIntOrNull() ?: 1,
154+
option2 = pprPercentOption2.text.toString().toIntOrNull() ?: 1,
155+
option3 = pprPercentOption3.text.toString().toIntOrNull() ?: 1,
156+
)
157+
R.id.ppr_percent_options_null -> null
158+
else -> null
159+
}
160+
161+
companion object {
162+
const val TIPPING_CONFIGS_KEY = "TIPPING_CONFIGS"
163+
}
164+
}

0 commit comments

Comments
 (0)