import 'dart:ffi'; import 'dart:io'; import 'package:ffi/ffi.dart'; class MidtransTransaction extends Struct { Pointer order_id; @Long() int gross_amount; } class MidtransBanktransfer extends Struct { Pointer bank; Pointer va_number; Pointer bca; Pointer permata; } class MidtransEchannel extends Struct { Pointer bill_info1; Pointer bill_info2; } typedef MidtransStatus = Pointer Function(Pointer); typedef MidtransBanktransferNew = MidtransBanktransfer Function(Pointer); typedef MidtransEchannelNew = MidtransEchannel Function(Pointer, Pointer); typedef MidtransChargeBanktransfer = Pointer Function(MidtransBanktransfer, MidtransTransaction); typedef MidtransChargeEchannel = Pointer Function(MidtransEchannel, MidtransTransaction); class Midtrans { final dylib = Platform.isAndroid ? DynamicLibrary.open('libmidtrans.so') : DynamicLibrary.process(); Midtrans(String apiKey, String pem) { final apiKeyUtf8 = apiKey.toNativeUtf8(); final pemUtf8 = pem.toNativeUtf8(); dylib.lookupFunction , Pointer), void Function(Pointer, Pointer)> ('midtrans_init')(apiKeyUtf8, pemUtf8); calloc.free(apiKeyUtf8); calloc.free(pemUtf8); } String chargeBanktransfer(String bank, String orderID, int grossAmount) { final bankUtf8 = bank.toNativeUtf8(); final order_id = orderID.toNativeUtf8(); final vaNumber = dylib.lookupFunction ('midtrans_charge_banktransfer') (dylib.lookupFunction ('midtrans_banktransfer_new')(bankUtf8), dylib.lookupFunction , Long), MidtransTransaction Function (Pointer, int)> ('midtrans_transaction_new')(order_id, grossAmount)) .toDartString(); calloc.free(bankUtf8); calloc.free(order_id); return vaNumber; } String chargeEchannel(String billInfo1, String billInfo2, String orderID, int grossAmount) { final bill_info1 = billInfo1.toNativeUtf8(); final bill_info2 = billInfo2.toNativeUtf8(); final order_id = orderID.toNativeUtf8(); final billKey = dylib.lookupFunction ('midtrans_charge_echannel') (dylib.lookupFunction ('midtrans_echannel_new')(bill_info1, bill_info2), dylib.lookupFunction , Long), MidtransTransaction Function (Pointer, int)> ('midtrans_transaction_new')(order_id, grossAmount)) .toDartString(); calloc.free(bill_info1); calloc.free(bill_info2); calloc.free(order_id); return billKey; } String status(String orderID) { final order_id = orderID.toNativeUtf8(); final status = dylib.lookupFunction ('midtrans_status')(order_id).toDartString(); calloc.free(order_id); return status; } void cleanup() { dylib.lookupFunction ('midtrans_cleanup')(); } }