-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstandard_request.dart
More file actions
82 lines (75 loc) · 2.54 KB
/
standard_request.dart
File metadata and controls
82 lines (75 loc) · 2.54 KB
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
import 'dart:convert';
import 'dart:io';
import 'package:flutterwave_standard/models/TransactionError.dart';
import 'package:flutterwave_standard/models/requests/customer.dart';
import 'package:flutterwave_standard/models/requests/customizations.dart';
import 'package:flutterwave_standard/models/responses/standard_response.dart';
import 'package:flutterwave_standard/models/subaccount.dart';
import 'package:flutterwave_standard/utils.dart';
import 'package:http/http.dart';
class StandardRequest {
String txRef;
String amount;
Customization customization;
Customer customer;
bool isTestMode;
String publicKey;
String paymentOptions;
String redirectUrl;
String? currency;
String? paymentPlanId;
List<SubAccount>? subAccounts;
Map<dynamic, dynamic>? meta;
StandardRequest(
{required this.txRef,
required this.amount,
required this.customer,
required this.paymentOptions,
required this.customization,
required this.isTestMode,
required this.publicKey,
required this.redirectUrl,
this.currency,
this.paymentPlanId,
this.subAccounts,
this.meta});
String toString() => jsonEncode(this._toJson());
/// Converts this instance to json
Map<String, dynamic> _toJson() {
final request = {
"tx_ref": this.txRef,
"publicKey": this.publicKey,
"amount": this.amount,
"currency": this.currency,
"payment_options": this.paymentOptions,
"payment_plan": this.paymentPlanId,
"redirect_url": this.redirectUrl,
"customer": this.customer.toJson(),
"subaccounts": this.subAccounts?.map((e) => e.toJson()).toList(),
"meta": this.meta,
"customizations": customization.toJson()
};
print(this.customer.toJson());
return Utils.removeKeysWithEmptyValues(request);
}
/// Executes network call to initiate transactions
Future<StandardResponse> execute(Client client) async {
final url = Utils.getBaseUrl(this.isTestMode) + Utils.STANDARD_PAYMENT;
final uri = Uri.parse(url);
try {
final response = await client.post(uri,
headers: {
HttpHeaders.authorizationHeader: this.publicKey,
HttpHeaders.contentTypeHeader: 'application/json'
},
body: json.encode(this._toJson()));
final responseBody = json.decode(response.body);
if (responseBody["status"] == "error") {
throw TransactionError(TransactionError.formatError(responseBody));
}
return StandardResponse.fromJson(responseBody);
} catch (error) {
throw (error);
}
}
}