summaryrefslogtreecommitdiff
path: root/midtrans.dart
blob: b1bc830b6a7575c8897e056a5ed4daed7a6932e9 (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
73
74
75
76
77
78
79
import 'dart:ffi';
import 'dart:io';

import 'package:ffi/ffi.dart';
import 'package:path/path.dart' as path;

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;
}

class Midtrans {
	DynamicLibrary dylib;

	Midtrans(String apiKey, String caInfo) {
		dylib = Platform.isAndroid
			? DynamicLibrary.open('libmidtrans.so')
			: DynamicLibrary.process();

		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);
	}

	void chargeBanktransfer(Pointer<MidtransBanktransfer> payment,
			Pointer<MidtransTransaction> transaction,
			Array<Pointer<Utf8>> customFields) {
		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 cleanup() {
		final midtrans_cleanup = dylib.lookupFunction<Void Function(),
		      void Function()>('midtrans_cleanup');
		midtrans_cleanup();
	}
}