import 'dart:ffi'; import 'dart:io'; import 'package:ffi/ffi.dart'; import 'package:path/path.dart' as path; class MidtransTransaction extends Struct { Pointer order_id; @Long() int gross_amount; } class MidtransBanktransfer extends Struct { Pointer bank; Pointer va_number; Pointer bca; Pointer permata; } typedef MidtransStatus = Pointer Function(Pointer); typedef MidtransChargeBanktransfer = Pointer Function(MidtransBanktransfer, MidtransTransaction, Array>); class Midtrans { DynamicLibrary dylib; Midtrans(String apiKey, String pem) { dylib = Platform.isAndroid ? DynamicLibrary.open('libmidtrans.so') : DynamicLibrary.process(); final apiKeyUtf8 = apiKey.toNativeUtf8(); final pemUtf8 = pem.toNativeUtf8(); final midtrans_init = dylib.lookupFunction , Pointer), void Function(Pointer, Pointer)> ('midtrans_init'); midtrans_init(apiKeyUtf8, pemUtf8); calloc.free(apiKeyUtf8); calloc.free(pemUtf8); } String status(String orderID) { final midtrans_status = dylib.lookupFunction('midtrans_status'); final order_id = orderID.toNativeUtf8(); final status = midtrans_status(order_id).toDartString(); calloc.free(order_id); return status; } String chargeBanktransfer(MidtransBanktransfer payment, MidtransTransaction transaction, Array> customFields) { final midtrans_charge = dylib.lookupFunction ('midtrans_charge_banktransfer'); final va_number = midtrans_charge(payment, transaction, customFields).toDartString(); calloc.free(payment.bank); if (payment.va_number != null) { calloc.free(payment.va_number); } if (payment.bca != null) { calloc.free(payment.bca); } if (payment.permata != null) { calloc.free(payment.permata); } calloc.free(transaction.order_id); for (var i = 0; i < 6; i++) { if (customFields[i] != null) { calloc.free(customFields[i]); } } return va_number; } void cleanup() { final midtrans_cleanup = dylib.lookupFunction('midtrans_cleanup'); midtrans_cleanup(); } }