-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Add code snippets for Flutter and React Native
- Loading branch information
1 parent
0f8d6c2
commit 4457d2a
Showing
34 changed files
with
505 additions
and
0 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
src/doc/guides/terminal-flutter/create-method-channel/config.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
languages: | ||
- kt |
25 changes: 25 additions & 0 deletions
25
src/doc/guides/terminal-flutter/create-method-channel/index.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
type: single-lang | ||
languages: | ||
- kt |
7 changes: 7 additions & 0 deletions
7
src/doc/guides/terminal-flutter/create-models/paystack-intent-response.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
7 changes: 7 additions & 0 deletions
7
src/doc/guides/terminal-flutter/create-models/terminal-response.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
20 changes: 20 additions & 0 deletions
20
src/doc/guides/terminal-flutter/create-models/transaction-request.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
15 changes: 15 additions & 0 deletions
15
src/doc/guides/terminal-flutter/create-models/transaction-response.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? | ||
) |
2 changes: 2 additions & 0 deletions
2
src/doc/guides/terminal-flutter/create-payment-intent/config.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
languages: | ||
- kt |
76 changes: 76 additions & 0 deletions
76
src/doc/guides/terminal-flutter/create-payment-intent/index.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/doc/guides/terminal-flutter/link-method-channel/config.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
languages: | ||
- dart |
27 changes: 27 additions & 0 deletions
27
src/doc/guides/terminal-flutter/link-method-channel/index.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
3
src/doc/guides/terminal-react-native/create-models/config.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
type: single-lang | ||
languages: | ||
- java |
25 changes: 25 additions & 0 deletions
25
src/doc/guides/terminal-react-native/create-models/paystack-intent-response.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/doc/guides/terminal-react-native/create-models/terminal-response.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/doc/guides/terminal-react-native/create-models/transaction-request.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/doc/guides/terminal-react-native/create-models/transaction-response.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/doc/guides/terminal-react-native/create-module/paystack-module-callback/config.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
languages: | ||
- java |
34 changes: 34 additions & 0 deletions
34
src/doc/guides/terminal-react-native/create-module/paystack-module-callback/index.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
2 changes: 2 additions & 0 deletions
2
src/doc/guides/terminal-react-native/create-module/paystack-module-final/config.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
languages: | ||
- java |
Oops, something went wrong.