Skip to content

Commit 4457d2a

Browse files
Feat: Add code snippets for Flutter and React Native
1 parent 0f8d6c2 commit 4457d2a

File tree

34 files changed

+505
-0
lines changed

34 files changed

+505
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
languages:
2+
- kt
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// other imports
2+
import io.flutter.embedding.engine.FlutterEngine
3+
import io.flutter.plugin.common.MethodChannel
4+
5+
class MainActivity: FlutterActivity() {
6+
private val CHANNEL = "com.example.sample_registration/payment"
7+
// other code snippet
8+
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
9+
super.configureFlutterEngine(flutterEngine)
10+
11+
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
12+
call, result ->
13+
if (call.method == "makePayment") {
14+
val amount = call.argument("amount") ?: 0
15+
makePayment(amount)
16+
17+
result.success(transactionStatus)
18+
} else {
19+
result.notImplemented()
20+
}
21+
}
22+
}
23+
24+
// other code snippet
25+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type: single-lang
2+
languages:
3+
- kt
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
// PaystackIntentResponse.kt
3+
data class PaystackIntentResponse (
4+
val intentKey: String,
5+
val intentResponseCode: Int,
6+
val intentResponse: TerminalResponse
7+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
// TerminalResponse.kt
3+
data class TerminalResponse(
4+
val statusCode: String,
5+
val message: String,
6+
val data: String
7+
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
// TransactionRequest.kt
3+
data class TransactionRequest(
4+
val amount: Int,
5+
val offlineReference: String?,
6+
val supplementaryReceiptData: SupplementaryReceiptData?,
7+
val metadata: Map<String, Any>?
8+
)
9+
10+
data class SupplementaryReceiptData(
11+
val developerSuppliedText: String?,
12+
val developerSuppliedImageUrlPath: String?,
13+
val barcodeOrQrcodeImageText: String?,
14+
val textImageType: TextImageFormat?
15+
)
16+
17+
enum class TextImageFormat {
18+
QR_CODE,
19+
AZTEC_BARCODE
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// TransactionResponse.kt
2+
import com.google.gson.annotations.SerializedName
3+
4+
data class TransactionResponse(
5+
val id: String?,
6+
val amount: Int?,
7+
val reference: String?,
8+
val status: String?,
9+
val currency: String?,
10+
@SerializedName("country_code")
11+
val countryCode: String?,
12+
@SerializedName("paid_at")
13+
val paidAt: String?,
14+
val terminal: String?
15+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
languages:
2+
- kt
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// MainActivity.kt
2+
import android.content.Intent
3+
import android.util.Log
4+
import android.widget.Toast
5+
import com.example.sample_registration.model.CustomField
6+
import com.example.sample_registration.model.PaystackIntentResponse
7+
import com.example.sample_registration.model.TerminalResponse
8+
import com.example.sample_registration.model.TransactionRequest
9+
import com.example.sample_registration.model.TransactionResponse
10+
import com.google.gson.Gson
11+
import io.flutter.embedding.android.FlutterActivity
12+
13+
class MainActivity: FlutterActivity() {
14+
private val gson = Gson()
15+
private var transactionStatus: String? = ""
16+
17+
private val CHANNEL = "com.example.sample_registration/payment"
18+
private val PACKAGE_NAME = "com.paystack.pos"
19+
private val TRANSACTION = "com.paystack.pos.TRANSACT"
20+
private val TRANSACTION_RESULT_CODE = 14
21+
22+
23+
private fun makePayment(amount: Int?) {
24+
val transactionRequest = amount?.let {
25+
TransactionRequest(
26+
amount = it,
27+
offlineReference = null,
28+
supplementaryReceiptData = null,
29+
metadata = mapOf(
30+
"custom_fields" to listOf(
31+
CustomField(
32+
displayName = "App Name",
33+
variableName = "app_name",
34+
value = "Sample Registration"
35+
)
36+
)
37+
)
38+
)
39+
}
40+
41+
val transactionIntent = Intent(Intent.ACTION_VIEW).apply {
42+
setPackage(PACKAGE_NAME)
43+
putExtra(TRANSACTION, gson.toJson(transactionRequest))
44+
}
45+
46+
startActivityForResult(transactionIntent, 1)
47+
}
48+
49+
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
50+
super.onActivityResult(requestCode, resultCode, data)
51+
val paystackIntentResponse: PaystackIntentResponse
52+
53+
if (resultCode == TRANSACTION_RESULT_CODE) {
54+
paystackIntentResponse = gson.fromJson(
55+
data?.getStringExtra(TRANSACTION),
56+
PaystackIntentResponse::class.java
57+
)
58+
59+
processResponse(paystackIntentResponse)
60+
}
61+
else {
62+
// handle invalid result code
63+
}
64+
}
65+
66+
private fun processResponse(response: PaystackIntentResponse) {
67+
68+
val terminalResponse: TerminalResponse = response.intentResponse
69+
val transactionResponse = gson.fromJson(
70+
terminalResponse.data,
71+
TransactionResponse::class.java
72+
)
73+
74+
transactionStatus = transactionResponse.reference
75+
}
76+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
languages:
2+
- dart

0 commit comments

Comments
 (0)