Skip to content

Commit

Permalink
Feat: Add code snippets for Flutter and React Native
Browse files Browse the repository at this point in the history
  • Loading branch information
damilola-paystack committed Jan 24, 2024
1 parent 0f8d6c2 commit 4457d2a
Show file tree
Hide file tree
Showing 34 changed files with 505 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
languages:
- kt
25 changes: 25 additions & 0 deletions src/doc/guides/terminal-flutter/create-method-channel/index.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// other imports
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel

class MainActivity: FlutterActivity() {
private val CHANNEL = "com.example.sample_registration/payment"
// other code snippet
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)

MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
call, result ->
if (call.method == "makePayment") {
val amount = call.argument("amount") ?: 0
makePayment(amount)

result.success(transactionStatus)
} else {
result.notImplemented()
}
}
}

// other code snippet
}
3 changes: 3 additions & 0 deletions src/doc/guides/terminal-flutter/create-models/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type: single-lang
languages:
- kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

// PaystackIntentResponse.kt
data class PaystackIntentResponse (
val intentKey: String,
val intentResponseCode: Int,
val intentResponse: TerminalResponse
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

// TerminalResponse.kt
data class TerminalResponse(
val statusCode: String,
val message: String,
val data: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

// TransactionRequest.kt
data class TransactionRequest(
val amount: Int,
val offlineReference: String?,
val supplementaryReceiptData: SupplementaryReceiptData?,
val metadata: Map<String, Any>?
)

data class SupplementaryReceiptData(
val developerSuppliedText: String?,
val developerSuppliedImageUrlPath: String?,
val barcodeOrQrcodeImageText: String?,
val textImageType: TextImageFormat?
)

enum class TextImageFormat {
QR_CODE,
AZTEC_BARCODE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// TransactionResponse.kt
import com.google.gson.annotations.SerializedName

data class TransactionResponse(
val id: String?,
val amount: Int?,
val reference: String?,
val status: String?,
val currency: String?,
@SerializedName("country_code")
val countryCode: String?,
@SerializedName("paid_at")
val paidAt: String?,
val terminal: String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
languages:
- kt
76 changes: 76 additions & 0 deletions src/doc/guides/terminal-flutter/create-payment-intent/index.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// MainActivity.kt
import android.content.Intent
import android.util.Log
import android.widget.Toast
import com.example.sample_registration.model.CustomField
import com.example.sample_registration.model.PaystackIntentResponse
import com.example.sample_registration.model.TerminalResponse
import com.example.sample_registration.model.TransactionRequest
import com.example.sample_registration.model.TransactionResponse
import com.google.gson.Gson
import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
private val gson = Gson()
private var transactionStatus: String? = ""

private val CHANNEL = "com.example.sample_registration/payment"
private val PACKAGE_NAME = "com.paystack.pos"
private val TRANSACTION = "com.paystack.pos.TRANSACT"
private val TRANSACTION_RESULT_CODE = 14


private fun makePayment(amount: Int?) {
val transactionRequest = amount?.let {
TransactionRequest(
amount = it,
offlineReference = null,
supplementaryReceiptData = null,
metadata = mapOf(
"custom_fields" to listOf(
CustomField(
displayName = "App Name",
variableName = "app_name",
value = "Sample Registration"
)
)
)
)
}

val transactionIntent = Intent(Intent.ACTION_VIEW).apply {
setPackage(PACKAGE_NAME)
putExtra(TRANSACTION, gson.toJson(transactionRequest))
}

startActivityForResult(transactionIntent, 1)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
val paystackIntentResponse: PaystackIntentResponse

if (resultCode == TRANSACTION_RESULT_CODE) {
paystackIntentResponse = gson.fromJson(
data?.getStringExtra(TRANSACTION),
PaystackIntentResponse::class.java
)

processResponse(paystackIntentResponse)
}
else {
// handle invalid result code
}
}

private fun processResponse(response: PaystackIntentResponse) {

val terminalResponse: TerminalResponse = response.intentResponse
val transactionResponse = gson.fromJson(
terminalResponse.data,
TransactionResponse::class.java
)

transactionStatus = transactionResponse.reference
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
languages:
- dart
27 changes: 27 additions & 0 deletions src/doc/guides/terminal-flutter/link-method-channel/index.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
static const _methodChannel =
MethodChannel('com.example.sample_registration/payment');

Future<void> makePayment() async {
String reference = '';
try {
var options = {
'amount': 5000,
'supplementaryData': {
'developerSuppliedText': null,
'developerSuppliedImageUrlPath':
"https://assets.paystack.com/assets/img/press/Terminal-x-Bature-x-World-Cup-Receipt.jpg",
'barcodeOrQrcodeImageText': null,
'textImageType': null
}
};
reference = await _methodChannel.invokeMethod('makePayment', options);
print("Reference: $reference");
} on PlatformException catch (e) {
print("Error: $e");
reference = '';
}

setState(() {
_transactionReference = reference;
});
}
3 changes: 3 additions & 0 deletions src/doc/guides/terminal-react-native/create-models/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type: single-lang
languages:
- java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

// PaystackIntentResponse.java
public class PaystackIntentResponse {
private final String intentkey;
private final int intentResponseCode;
private final TerminalResponse intentResponse;

public PaystackIntentResponse(String intentkey, int intentResponseCode, TerminalResponse intentResponse) {
this.intentkey = intentkey;
this.intentResponseCode = intentResponseCode;
this.intentResponse = intentResponse;
}

public String getIntentkey() {
return intentkey;
}

public int getIntentResponseCode() {
return intentResponseCode;
}

public TerminalResponse getIntentResponse() {
return intentResponse;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

// TerminalResponse.java
public class TerminalResponse {
private final String statusCode;
private final String message;
private final String data;

public TerminalResponse(String statusCode, String message, String data) {
this.statusCode = statusCode;
this.message = message;
this.data = data;
}

public String getStatusCode() {
return statusCode;
}

public String getMessage() {
return message;
}

public String getData() {
return data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

// TransactionRequest.java
import java.util.Map;

public class TransactionRequest {
private int amount;
private Map<String, Object> metadata;

public TransactionRequest() {
}

public void setAmount(int amount) {
this.amount = amount;
}

public void setMetadata(Map<String, Object> metadata) {
this.metadata = metadata;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

// TransactionResponse.java
import com.google.gson.annotations.SerializedName;

public class TransactionResponse {
private final String id;
private final int amount;
private final String reference;
private final String status;
private final String currency;
@SerializedName("country_code")
private final String countryCode;
@SerializedName("paid_at")
private final String paidAt;
private final String terminal;

public TransactionResponse(
String id, int amount, String reference, String status,
String currency, String countryCode, String paidAt, String terminal) {
this.id = id;
this.amount = amount;
this.reference = reference;
this.status = status;
this.currency = currency;
this.countryCode = countryCode;
this.paidAt = paidAt;
this.terminal = terminal;
}

public String getId() {
return id;
}

public int getAmount() {
return amount;
}

public String getReference() {
return reference;
}

public String getStatus() {
return status;
}

public String getCurrency() {
return currency;
}

public String getCountryCode() {
return countryCode;
}

public String getPaidAt() {
return paidAt;
}

public String getTerminal() {
return terminal;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
languages:
- java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import android.app.Activity;
import android.content.Intent;

import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.BaseActivityEventListener;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.google.gson.Gson;

public class PaystackModule extends ReactContextBaseJavaModule {

private final Gson gson = new Gson();
private Callback mCallback;

private final ActivityEventListener mActivityEventListener = new BaseActivityEventListener() {
@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
PaystackIntentResponse paystackIntentResponse;
TerminalResponse terminalResponse;

paystackIntentResponse = gson.fromJson(
data != null ? data.getStringExtra("com.paystack.pos.TRANSACT") : null,
PaystackIntentResponse.class);
terminalResponse = paystackIntentResponse.getIntentResponse();
TransactionResponse transactionResponse = gson.fromJson(
terminalResponse.getData(),
TransactionResponse.class);
mCallback.invoke(transactionResponse.getReference());
}
};

// the rest of the code previously added
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
languages:
- java
Loading

0 comments on commit 4457d2a

Please sign in to comment.