Skip to content

Commit 72fba90

Browse files
Merge pull request #56 from PaystackOSS/feat-terminal
Feat: Add code snippets for Flutter and React Native
2 parents 0f8d6c2 + f71ee79 commit 72fba90

File tree

49 files changed

+1018
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1018
-4
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const kt = `// 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+
}`
26+
27+
export {kt}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const paystack_intent_response = `
2+
// PaystackIntentResponse.kt
3+
data class PaystackIntentResponse (
4+
val intentKey: String,
5+
val intentResponseCode: Int,
6+
val intentResponse: TerminalResponse
7+
)`
8+
9+
const terminal_response = `
10+
// TerminalResponse.kt
11+
data class TerminalResponse(
12+
val statusCode: String,
13+
val message: String,
14+
val data: String
15+
)`
16+
17+
const transaction_request = `
18+
// TransactionRequest.kt
19+
data class TransactionRequest(
20+
val amount: Int,
21+
val offlineReference: String?,
22+
val supplementaryReceiptData: SupplementaryReceiptData?,
23+
val metadata: Map<String, Any>?
24+
)
25+
26+
data class SupplementaryReceiptData(
27+
val developerSuppliedText: String?,
28+
val developerSuppliedImageUrlPath: String?,
29+
val barcodeOrQrcodeImageText: String?,
30+
val textImageType: TextImageFormat?
31+
)
32+
33+
enum class TextImageFormat {
34+
QR_CODE,
35+
AZTEC_BARCODE
36+
}`
37+
38+
const transaction_response = `// TransactionResponse.kt
39+
import com.google.gson.annotations.SerializedName
40+
41+
data class TransactionResponse(
42+
val id: String?,
43+
val amount: Int?,
44+
val reference: String?,
45+
val status: String?,
46+
val currency: String?,
47+
@SerializedName("country_code")
48+
val countryCode: String?,
49+
@SerializedName("paid_at")
50+
val paidAt: String?,
51+
val terminal: String?
52+
)`
53+
54+
export {paystack_intent_response, terminal_response, transaction_request, transaction_response}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
const kt = `// 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+
}`
77+
78+
export {kt}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const dart = `static const _methodChannel =
2+
MethodChannel('com.example.sample_registration/payment');
3+
4+
Future<void> makePayment() async {
5+
String reference = '';
6+
try {
7+
var options = {
8+
'amount': 5000,
9+
'supplementaryData': {
10+
'developerSuppliedText': null,
11+
'developerSuppliedImageUrlPath':
12+
"https://assets.paystack.com/assets/img/press/Terminal-x-Bature-x-World-Cup-Receipt.jpg",
13+
'barcodeOrQrcodeImageText': null,
14+
'textImageType': null
15+
}
16+
};
17+
reference = await _methodChannel.invokeMethod('makePayment', options);
18+
print("Reference: $reference");
19+
} on PlatformException catch (e) {
20+
print("Error: $e");
21+
reference = '';
22+
}
23+
24+
setState(() {
25+
_transactionReference = reference;
26+
});
27+
}`
28+
29+
export {dart}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
const paystack_intent_response = `
2+
// PaystackIntentResponse.java
3+
public class PaystackIntentResponse {
4+
private final String intentkey;
5+
private final int intentResponseCode;
6+
private final TerminalResponse intentResponse;
7+
8+
public PaystackIntentResponse(String intentkey, int intentResponseCode, TerminalResponse intentResponse) {
9+
this.intentkey = intentkey;
10+
this.intentResponseCode = intentResponseCode;
11+
this.intentResponse = intentResponse;
12+
}
13+
14+
public String getIntentkey() {
15+
return intentkey;
16+
}
17+
18+
public int getIntentResponseCode() {
19+
return intentResponseCode;
20+
}
21+
22+
public TerminalResponse getIntentResponse() {
23+
return intentResponse;
24+
}
25+
}`
26+
27+
const terminal_response = `
28+
// TerminalResponse.java
29+
public class TerminalResponse {
30+
private final String statusCode;
31+
private final String message;
32+
private final String data;
33+
34+
public TerminalResponse(String statusCode, String message, String data) {
35+
this.statusCode = statusCode;
36+
this.message = message;
37+
this.data = data;
38+
}
39+
40+
public String getStatusCode() {
41+
return statusCode;
42+
}
43+
44+
public String getMessage() {
45+
return message;
46+
}
47+
48+
public String getData() {
49+
return data;
50+
}
51+
}`
52+
53+
const transaction_request = `
54+
// TransactionRequest.java
55+
import java.util.Map;
56+
57+
public class TransactionRequest {
58+
private int amount;
59+
private Map<String, Object> metadata;
60+
61+
public TransactionRequest() {
62+
}
63+
64+
public void setAmount(int amount) {
65+
this.amount = amount;
66+
}
67+
68+
public void setMetadata(Map<String, Object> metadata) {
69+
this.metadata = metadata;
70+
}
71+
}`
72+
73+
const transaction_response = `
74+
// TransactionResponse.java
75+
import com.google.gson.annotations.SerializedName;
76+
77+
public class TransactionResponse {
78+
private final String id;
79+
private final int amount;
80+
private final String reference;
81+
private final String status;
82+
private final String currency;
83+
@SerializedName("country_code")
84+
private final String countryCode;
85+
@SerializedName("paid_at")
86+
private final String paidAt;
87+
private final String terminal;
88+
89+
public TransactionResponse(
90+
String id, int amount, String reference, String status,
91+
String currency, String countryCode, String paidAt, String terminal) {
92+
this.id = id;
93+
this.amount = amount;
94+
this.reference = reference;
95+
this.status = status;
96+
this.currency = currency;
97+
this.countryCode = countryCode;
98+
this.paidAt = paidAt;
99+
this.terminal = terminal;
100+
}
101+
102+
public String getId() {
103+
return id;
104+
}
105+
106+
public int getAmount() {
107+
return amount;
108+
}
109+
110+
public String getReference() {
111+
return reference;
112+
}
113+
114+
public String getStatus() {
115+
return status;
116+
}
117+
118+
public String getCurrency() {
119+
return currency;
120+
}
121+
122+
public String getCountryCode() {
123+
return countryCode;
124+
}
125+
126+
public String getPaidAt() {
127+
return paidAt;
128+
}
129+
130+
public String getTerminal() {
131+
return terminal;
132+
}
133+
}`
134+
135+
export {paystack_intent_response, terminal_response, transaction_request, transaction_response}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const java = `import android.app.Activity;
2+
import android.content.Intent;
3+
4+
import com.facebook.react.bridge.ActivityEventListener;
5+
import com.facebook.react.bridge.BaseActivityEventListener;
6+
import com.facebook.react.bridge.Callback;
7+
import com.facebook.react.bridge.ReactApplicationContext;
8+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
9+
import com.google.gson.Gson;
10+
11+
public class PaystackModule extends ReactContextBaseJavaModule {
12+
13+
private final Gson gson = new Gson();
14+
private Callback mCallback;
15+
16+
private final ActivityEventListener mActivityEventListener = new BaseActivityEventListener() {
17+
@Override
18+
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
19+
PaystackIntentResponse paystackIntentResponse;
20+
TerminalResponse terminalResponse;
21+
22+
paystackIntentResponse = gson.fromJson(
23+
data != null ? data.getStringExtra("com.paystack.pos.TRANSACT") : null,
24+
PaystackIntentResponse.class);
25+
terminalResponse = paystackIntentResponse.getIntentResponse();
26+
TransactionResponse transactionResponse = gson.fromJson(
27+
terminalResponse.getData(),
28+
TransactionResponse.class);
29+
mCallback.invoke(transactionResponse.getReference());
30+
}
31+
};
32+
33+
// the rest of the code previously added
34+
}`
35+
36+
export {java}

0 commit comments

Comments
 (0)