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
80
81
82
83
84
85
|
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 MidtransChargeBanktransfer
= Pointer<Utf8> Function(MidtransBanktransfer, MidtransTransaction,
Array<Pointer<Utf8>>);
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
<Void Function(Pointer<Utf8>, Pointer<Utf8>),
void Function(Pointer<Utf8>, Pointer<Utf8>)>
('midtrans_init');
midtrans_init(apiKeyUtf8, pemUtf8);
calloc.free(apiKeyUtf8);
calloc.free(pemUtf8);
}
String status(String orderID) {
final midtrans_status = dylib.lookupFunction<MidtransStatus,
MidtransStatus>('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<Pointer<Utf8>> customFields) {
final midtrans_charge = dylib.lookupFunction
<MidtransChargeBanktransfer, MidtransChargeBanktransfer>
('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<Void Function(),
void Function()>('midtrans_cleanup');
midtrans_cleanup();
}
}
|