/
ApiClient.kt
89 lines (79 loc) · 2.97 KB
/
ApiClient.kt
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
package com.example.taptopayandroid
import com.example.taptopayandroid.BuildConfig
import com.example.taptopayandroid.PaymentIntentCreationResponse
import com.stripe.stripeterminal.external.models.ConnectionTokenException
import okhttp3.OkHttpClient
import retrofit2.Callback
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.IOException
/**
* The `ApiClient` is a singleton object used to make calls to our backend and return their results
*/
object ApiClient {
private val client = OkHttpClient.Builder()
.build()
private val retrofit: Retrofit = Retrofit.Builder()
.baseUrl(BuildConfig.EXAMPLE_BACKEND_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build()
private val service: BackendService = retrofit.create(BackendService::class.java)
@Throws(ConnectionTokenException::class)
internal fun createConnectionToken(): String {
try {
val result = service.getConnectionToken().execute()
if (result.isSuccessful && result.body() != null) {
return result.body()!!.secret
} else {
throw ConnectionTokenException("Creating connection token failed")
}
} catch (e: IOException) {
throw ConnectionTokenException("Creating connection token failed", e)
}
}
internal fun createLocation(
displayName: String?,
city: String?,
country: String?,
line1: String?,
line2: String?,
postalCode: String?,
state: String?,
) {
TODO("Call Backend application to create location")
}
internal fun capturePaymentIntent(id: String) {
service.capturePaymentIntent(id).execute()
}
internal fun cancelPaymentIntent(
id: String,
callback: Callback<Void>
) {
service.cancelPaymentIntent(id).enqueue(callback)
}
/**
* This method is calling the example backend (https://github.com/stripe/example-terminal-backend)
* to create paymentIntent for Internet based readers, for example WisePOS E. For your own application, you
* should create paymentIntent in your own merchant backend.
*/
internal fun createPaymentIntent(
amount: Long,
currency: String,
extendedAuth: Boolean,
incrementalAuth: Boolean,
callback: Callback<PaymentIntentCreationResponse>
) {
val createPaymentIntentParams = buildMap<String, String> {
put("amount", amount.toString())
put("currency", currency)
if (extendedAuth) {
put("payment_method_options[card_present[request_extended_authorization]]", "true")
}
if (incrementalAuth) {
put("payment_method_options[card_present[request_incremental_authorization_support]]", "true")
}
}
service.createPaymentIntent(createPaymentIntentParams).enqueue(callback)
}
}