summaryrefslogtreecommitdiff
path: root/midtrans.dart
blob: 4c220d4b3a20ac80b65a5de7f742bf528551aaaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import 'dart:ffi';
import 'dart:io';
import 'package:ffi/ffi.dart';

class MidtransTransaction extends Struct {
	Pointer<Utf8> order_id;

	@Long()
	int gross_amount;
}

class MidtransBanktransfer extends Struct {
	Pointer<Utf8> bank;
	Pointer<Utf8> va_number;
	Pointer<Utf8> bca;
	Pointer<Utf8> permata;
}

typedef MidtransStatus = Pointer<Utf8> Function(Pointer<Utf8>);
typedef MidtransBanktransferNew = MidtransBanktransfer Function(Pointer<Utf8>);
typedef MidtransChargeBanktransfer
= Pointer<Utf8> Function(MidtransBanktransfer, 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
			<Void Function(Pointer<Utf8>, Pointer<Utf8>),
			void Function(Pointer<Utf8>, Pointer<Utf8>)>
				('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
			<MidtransChargeBanktransfer, MidtransChargeBanktransfer>
			('midtrans_charge_banktransfer')
			(dylib.lookupFunction
			 <MidtransBanktransferNew, MidtransBanktransferNew>
			 ('midtrans_banktransfer_new')(bankUtf8),
			 dylib.lookupFunction
			 <MidtransTransaction Function (Pointer<Utf8>, Long),
			 MidtransTransaction Function (Pointer<Utf8>, int)>
			 ('midtrans_transaction_new')(order_id, grossAmount))
			.toDartString();
		calloc.free(bankUtf8);
		calloc.free(order_id);
		return vaNumber;
	}

	String status(String orderID) {
		final order_id = orderID.toNativeUtf8();
		final status = dylib.lookupFunction
			<MidtransStatus, MidtransStatus>
			('midtrans_status')(order_id).toDartString();
		calloc.free(order_id);
		return status;
	}

	void cleanup() {
		dylib.lookupFunction<Void Function(), void Function()>
			('midtrans_cleanup')();
	}
}