Skip to content

Commit c5452df

Browse files
feat: add support to metadata (#20)
* feat: add support to metadata into the sdk init payment method * feat: serialize metadata object * chore: fix tests --------- Co-authored-by: OlegSyng <olegsingaevskii9@gmail.com>
1 parent 3ed40ed commit c5452df

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

flizpaysdk/src/androidTest/kotlin/flizpay2/flizpaysdk/FlizpaySDKTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class FlizpaySDKTest {
3030
mockkConstructor(TransactionService::class)
3131
every {
3232
anyConstructed<TransactionService>().fetchTransactionInfo(
33+
any(),
3334
any(),
3435
any(),
3536
captureLambda()
@@ -62,6 +63,7 @@ class FlizpaySDKTest {
6263
// Override default success mock with failure
6364
every {
6465
anyConstructed<TransactionService>().fetchTransactionInfo(
66+
any(),
6567
any(),
6668
any(),
6769
captureLambda()

flizpaysdk/src/main/kotlin/flizpay2/flizpaysdk/FlizpaySDK.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@ object FlizpaySDK {
1515
* @param context The Context from which to launch the payment WebView.
1616
* @param token The JWT token fetched by the host app.
1717
* @param amount The transaction amount.
18+
* @param metadata The metadata object.
1819
* @param onFailure Optional callback to handle errors (e.g., show alerts).
1920
*/
2021
fun initiatePayment(
2122
context: Context,
2223
token: String,
2324
amount: String,
25+
metadata: Map<String, Any?>? = null,
2426
onFailure: ((Throwable) -> Unit)? = null,
2527
) {
2628
val transactionService = TransactionService()
2729

28-
transactionService.fetchTransactionInfo(token, amount) { result ->
30+
transactionService.fetchTransactionInfo(token, amount, metadata) { result ->
2931
if(result.isSuccess) {
3032
val redirectUrl = result.getOrNull() ?: Constants.BASE_URL
3133

flizpaysdk/src/main/kotlin/flizpay2/flizpaysdk/lib/TransactionService.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,32 @@ import flizpay2.flizpaysdk.Constants
77
import okhttp3.MediaType.Companion.toMediaTypeOrNull
88
import okhttp3.RequestBody.Companion.toRequestBody
99
import org.json.JSONObject
10+
import org.json.JSONArray
1011

1112

1213
class TransactionService {
1314
private val client = OkHttpClient()
1415

16+
private fun Map<String, Any?>.toJsonObject(): JSONObject =
17+
JSONObject().apply {
18+
forEach { (k, v) -> put(k, JSONObject.wrap(v)) }
19+
}
20+
1521
/**
1622
* Calls the /transactions endpoint using the provided token and amount.
1723
*/
1824
fun fetchTransactionInfo(
1925
token: String,
2026
amount: String,
27+
metadata: Map<String, Any?>? = null,
2128
completion: (Result<String>) -> Unit
2229
) {
2330
val url = "${Constants.API_URL}/transactions"
2431
val requestBody = JSONObject()
2532
.put("amount", amount)
2633
.put("currency", "EUR")
2734
.put("source", "sdk_integration")
35+
.put("metadata", metadata?.toJsonObject() ?: JSONObject())
2836
.toString()
2937
.toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull())
3038

0 commit comments

Comments
 (0)