-
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.
Merge pull request #56 from PaystackOSS/feat-terminal
Feat: Add code snippets for Flutter and React Native
- Loading branch information
Showing
49 changed files
with
1,018 additions
and
4 deletions.
There are no files selected for viewing
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 @@ | ||
const kt = `// 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 | ||
}` | ||
|
||
export {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,54 @@ | ||
const paystack_intent_response = ` | ||
// PaystackIntentResponse.kt | ||
data class PaystackIntentResponse ( | ||
val intentKey: String, | ||
val intentResponseCode: Int, | ||
val intentResponse: TerminalResponse | ||
)` | ||
|
||
const terminal_response = ` | ||
// TerminalResponse.kt | ||
data class TerminalResponse( | ||
val statusCode: String, | ||
val message: String, | ||
val data: String | ||
)` | ||
|
||
const transaction_request = ` | ||
// 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 | ||
}` | ||
|
||
const transaction_response = `// 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? | ||
)` | ||
|
||
export {paystack_intent_response, terminal_response, transaction_request, transaction_response} |
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,78 @@ | ||
const kt = `// 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 | ||
} | ||
}` | ||
|
||
export {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,29 @@ | ||
const dart = `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; | ||
}); | ||
}` | ||
|
||
export {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,135 @@ | ||
const paystack_intent_response = ` | ||
// 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; | ||
} | ||
}` | ||
|
||
const terminal_response = ` | ||
// 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; | ||
} | ||
}` | ||
|
||
const transaction_request = ` | ||
// 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; | ||
} | ||
}` | ||
|
||
const transaction_response = ` | ||
// 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; | ||
} | ||
}` | ||
|
||
export {paystack_intent_response, terminal_response, transaction_request, transaction_response} |
36 changes: 36 additions & 0 deletions
36
dist/doc/guides/terminal-react-native/create-module/paystack-module-callback.js
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,36 @@ | ||
const java = `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 | ||
}` | ||
|
||
export {java} |
Oops, something went wrong.