diff options
-rw-r--r-- | midtrans.dart | 80 |
1 files changed, 70 insertions, 10 deletions
diff --git a/midtrans.dart b/midtrans.dart index 1133d78..c70c55e 100644 --- a/midtrans.dart +++ b/midtrans.dart @@ -1,3 +1,9 @@ +import 'dart:ffi'; +import 'dart:io'; + +import 'package:ffi/ffi.dart'; +import 'package:path/path.dart' as path; + class MidtransTransaction extends Struct { external Pointer<Utf8> order_id; @@ -5,17 +11,71 @@ class MidtransTransaction extends Struct { external long gross_amount; } -typedef MidtransInitNative = Void Function(Pointer<Utf8> apiKey, - Pointer<Utf8> caInfo) -typedef MidtransInit = void Function(Pointer<Utf8> apiKey, Pointer<Utf8> caInfo) +class MidtransBanktransfer extends Struct { + external Pointer<Utf8> bank; + external Pointer<Utf8> va_number; + external Pointer<Utf8> bca; + external Pointer<Utf8> permata; +} + +class Midtrans { + final DynamicLibrary dylib; + + Midtrans(String apiKey, String caInfo) { + dylib = Platform.isAndroid + ? DynamicLibrary.open('libmidtrans.so') + : DynamicLibrary.process(); -typedef MidtransChargeNative = Void Function(Int32 type, Pointer<Void>, - Pointer<MidtransTransaction>, Array<Pointer<Utf8>>) -typedef MidtransCharge = void Function(int type, Pointer<Void>, - Pointer<MidtransTransaction>, Array<Pointer<Utf8>>) + final apiKeyUtf8 = apiKey.toNativeUtf8(); + final caInfoUtf8 = caInfo.toNativeUtf8(); + final midtrans_init = dylib.lookupFunction + <Void Function(Pointer<Utf8>, Pointer<Utf8>), + void Function(Pointer<Utf8>, Pointer<Utf8>)> + ('midtrans_init'); + midtrans_init(apiKeyUtf8, caInfoUtf8); + calloc.free(apiKeyUtf8); + calloc.free(caInfoUtf8); + } -typedef MidtransCleanupNative = Void Function() -typedef MidtransCleanup = void Function() + void charge<T extends Struct>(Pointer<T> payment, + Pointer<MidtransTransaction> transaction, + Array<Pointer<Utf8>> customFields) { + if (payment is MidtransBanktransfer) { + final midtrans_charge = dylib.lookupFunction + <Void Function(Pointer<MidtransBanktransfer>, + Pointer<MidtransTransaction>, + Array<Pointer<Utf8>>), + void Function(Pointer<MidtransBanktransfer>, + Pointer<MidtransTransaction>, + Array<Pointer<Utf8>>)> + ('midtrans_charge_banktransfer'); + midtrans_charge(payment, transaction, customFields); + calloc.free((payment as MidtransBanktransfer).bank); + if ((payment as MidtransBanktransfer).va_number + != null) { + calloc.free((payment as MidtransBanktransfer) + .va_number); + } + if ((payment as MidtransBanktransfer).bca != null) { + calloc.free((payment as MidtransBanktransfer) + .bca); + } + if ((payment as MidtransBanktransfer).permata != null) { + calloc.free((payment as MidtransBanktransfer) + .permata); + } + } + calloc.free(transaction.order_id); + for (var i = 0; i < 6; i++) { + if (customFields[i] != null) { + calloc.free(customFields[i]); + } + } + } -void main() { + void cleanup() { + final midtrans_cleanup = dylib.lookupFunction<Void Function(), + void Function()>('midtrans_cleanup'); + midtrans_cleanup(); + } } |