Skip to content

Commit

Permalink
Merge pull request #14 from wilburt/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Wilberforce Uwadiegwu committed Feb 9, 2020
2 parents daaa935 + 3f68df6 commit 48dc02f
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 10 deletions.
2 changes: 1 addition & 1 deletion example/lib/main.dart
Expand Up @@ -32,7 +32,7 @@ class _HomeWidgetState extends State<HomeWidget> {
var autoValidate = false;
bool acceptCardPayment = true;
bool acceptAccountPayment = true;
bool acceptMpesaPayment = true;
bool acceptMpesaPayment = false;
bool shouldDisplayFee = true;
bool acceptAchPayments = false;
bool acceptGhMMPayments = false;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/common/rave_pay_initializer.dart
Expand Up @@ -79,7 +79,7 @@ class RavePayInitializer {
bool acceptUgMobileMoneyPayments;

/// Whether to request Mobile Money Francophone Africa payment details.
/// `NG` and `XOF` needs to be set as [country] and [currency] respectively
/// `NG` and `XOF` or `XAF` needs to be set as [country] and [currency] respectively
bool acceptMobileMoneyFrancophoneAfricaPayments;

/// Whether to route the payment to Sandbox APIs.
Expand Down
8 changes: 5 additions & 3 deletions lib/src/common/rave_utils.dart
Expand Up @@ -28,10 +28,12 @@ bool get isInDebugMode {
}

putIfNotNull({@required Map map, @required key, @required value}) {
if (value == null || (value is String && value.isEmpty)) {
return;
}
if (value == null || (value is String && value.isEmpty)) return;
map[key] = value;
}

putIfTrue({@required Map map, @required key, @required bool value}) {
if (value == null || !value) return;
map[key] = value;
}

Expand Down
8 changes: 8 additions & 0 deletions lib/src/common/validator_utills.dart
Expand Up @@ -156,6 +156,14 @@ class ValidatorUtils {
if (init.acceptAchPayments == null) return Strings.cannotBeNull('withAch');
if (init.acceptMpesaPayments == null)
return Strings.cannotBeNull('withMpesa');
if (init.acceptMpesaPayments) {
if (init.currency.toUpperCase() != "KES") {
return "currency should be \"KES\" for Mpesa payments but \"${init.currency}\" was passed";
}
if (init.country.toUpperCase() != "KE") {
return "country should be \"KE\" for Mpesa payments but \"${init.country}\" was passed";
}
}
if (init.acceptAccountPayments == null)
return Strings.cannotBeNull('withAccount');
if (init.acceptCardPayments == null)
Expand Down
13 changes: 8 additions & 5 deletions lib/src/dto/payload.dart
Expand Up @@ -107,7 +107,6 @@ class Payload {
"amount": amount,
"email": email,
"txRef": txRef,
"orderRef": orderRef,
"redirect_url": redirectUrl,
};

Expand All @@ -130,15 +129,19 @@ class Payload {
putIfNotNull(map: json, key: "billingstate", value: billingState);
putIfNotNull(map: json, key: "billingcountry", value: billingCountry);
putIfNotNull(map: json, key: "billingzip", value: billingZip);
putIfNotNull(map: json, key: "is_us_bank_charge", value: isUsBankCharge);
putIfNotNull(
putIfTrue(map: json, key: "is_us_bank_charge", value: isUsBankCharge);
putIfTrue(
map: json, key: "is_mobile_money_franco", value: isMobileMoneyFranco);
putIfNotNull(map: json, key: "is_mpesa", value: isMpesa);
putIfNotNull(map: json, key: "is_mpesa_lipa", value: isMpesaLipa);
putIfTrue(map: json, key: "is_mpesa", value: isMpesa);
putIfTrue(map: json, key: "is_mpesa_lipa", value: isMpesaLipa);

putIfNotNull(
map: json, key: "charge_type", value: isPreAuth ? "preauth" : null);

if (isMobileMoneyFranco) {
meta["orderRef"] = orderRef;
}

if (meta == null) meta = {};
meta["sdk"] = "flutter";
json["meta"] = [
Expand Down
32 changes: 32 additions & 0 deletions test/src/common/rave_utils_test.dart
Expand Up @@ -44,4 +44,36 @@ void main() {
});
});
});

group("#putIfNotNull", () {
final map = {};
final cases = [
Case(inp: ["key1", "value1"], out: true),
Case(inp: ["key2", ""], out: false),
Case(inp: ["key3", " value3"], out: true),
Case(inp: ["key4 ", "value4"], out: true),
Case(inp: ["key5 ", null], out: false),
];
cases.forEach((c) {
test("${c.inp} returns ${c.out}", () {
raveUtils.putIfNotNull(map: map, key: c.inp[0], value: c.inp[1]);
expect(c.out, map.containsKey(c.inp[0]));
});
});
});

group("#putIfTrue", () {
final map = {};
final cases = [
Case(inp: ["key1", true], out: true),
Case(inp: ["key2", false], out: false),
Case(inp: ["key3", null], out: false),
];
cases.forEach((c) {
test("${c.inp} returns ${c.out}", () {
raveUtils.putIfTrue(map: map, key: c.inp[0], value: c.inp[1]);
expect(c.out, map.containsKey(c.inp[0]));
});
});
});
}

0 comments on commit 48dc02f

Please sign in to comment.