Skip to content

Commit 64e073c

Browse files
committed
Exposed PaystackException and removed
1 parent e027c2d commit 64e073c

16 files changed

+137
-143
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.5.1
2+
3+
* Exposed Paystack Exception
4+
* Properly formatted .dart files
5+
* Removed deprecated APIs
6+
17
## 0.5.0
28

3-
* Initial release.
9+
* Initial beta release.

lib/flutter_paystack.dart

Lines changed: 1 addition & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,6 @@
1-
import 'dart:async';
2-
3-
import 'package:flutter/foundation.dart';
4-
import 'package:flutter/material.dart';
5-
import 'package:flutter_paystack/src/model/charge.dart';
6-
import 'package:flutter_paystack/src/paystack.dart';
7-
import 'package:flutter_paystack/src/transaction.dart';
8-
import 'package:flutter_paystack/src/utils/utils.dart';
9-
import 'package:flutter_paystack/src/platform_info.dart';
10-
import 'package:flutter/services.dart';
11-
import 'package:flutter_paystack/src/exceptions.dart';
12-
131
// Expose the following files
142
export 'package:flutter_paystack/src/model/card.dart';
153
export 'package:flutter_paystack/src/paystack.dart' hide Paystack;
164
export 'package:flutter_paystack/src/model/charge.dart';
175
export 'package:flutter_paystack/src/transaction.dart';
18-
export 'package:flutter_paystack/src/exceptions.dart' hide PaystackException;
19-
20-
class PaystackPlugin {
21-
static bool _sdkInitialized = false;
22-
static String _publicKey;
23-
24-
PaystackPlugin._();
25-
26-
static Future<PaystackPlugin> initialize({@required String publicKey}) async {
27-
assert(() {
28-
if (publicKey == null || publicKey.isEmpty) {
29-
throw new PaystackException('publicKey cannot be null or empty');
30-
}
31-
return true;
32-
}());
33-
//do all the init work here
34-
35-
var completer = Completer<PaystackPlugin>();
36-
37-
//check if sdk is actually initialized
38-
if (sdkInitialized) {
39-
completer.complete(PaystackPlugin._());
40-
} else {
41-
_publicKey = publicKey;
42-
43-
// Using cascade notation to build the platform specific info
44-
try {
45-
String userAgent = await Utils.channel.invokeMethod('getUserAgent');
46-
String paystackBuild =
47-
await Utils.channel.invokeMethod('getVersionCode');
48-
String deviceId = await Utils.channel.invokeMethod('getDeviceId');
49-
PlatformInfo()
50-
..userAgent = userAgent
51-
..paystackBuild = paystackBuild
52-
..deviceId = deviceId;
53-
54-
_sdkInitialized = true;
55-
completer.complete(PaystackPlugin._());
56-
} on PlatformException catch (e) {
57-
58-
completer.completeError(e);
59-
}
60-
}
61-
return completer.future;
62-
}
63-
64-
static bool get sdkInitialized => _sdkInitialized;
65-
66-
static String get publicKey {
67-
// Validate that the sdk has been initialized
68-
Utils.validateSdkInitialized();
69-
return _publicKey;
70-
}
71-
72-
static void _performChecks() {
73-
//validate that sdk has been initialized
74-
Utils.validateSdkInitialized();
75-
76-
//validate public keys
77-
Utils.hasPublicKey();
78-
}
79-
80-
static chargeCard(BuildContext context,
81-
{@required Charge charge,
82-
@required OnTransactionChange<Transaction> beforeValidate,
83-
@required OnTransactionChange<Transaction> onSuccess,
84-
@required OnTransactionError<Object, Transaction> onError}) {
85-
assert(context != null, 'context must not be null');
86-
87-
_performChecks();
88-
89-
// Construct new paystack object
90-
Paystack paystack = Paystack.withPublicKey(publicKey);
91-
92-
// Create token
93-
paystack.chargeCard(context, charge,
94-
beforeValidate: beforeValidate, onSuccess: onSuccess, onError: onError);
95-
}
96-
}
6+
export 'package:flutter_paystack/src/exceptions.dart';
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
class ApiResponse {
22
String status;
33
String message;
4-
}
4+
}

lib/src/api/model/transaction_api_response.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'package:flutter_paystack/src/api/model/api_response.dart';
2-
import 'package:flutter_paystack/src/utils/string_utils.dart';
32

43
class TransactionApiResponse extends ApiResponse {
54
String reference;

lib/src/api/request/base_request_body.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import 'package:flutter_paystack/src/platform_info.dart';
22

33
abstract class BaseRequestBody {
4-
54
final fieldDevice = 'device';
65
String _device;
76

87
Map<String, String> paramsMap();
98

10-
119
String get device => _device;
1210

1311
setDeviceId() {

lib/src/api/request/charge_request_body.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ class ChargeRequestBody extends BaseRequestBody {
4848
this._reference = charge.reference;
4949
this._subAccount = charge.subAccount;
5050
this._transactionCharge =
51-
charge.transactionCharge != null && charge.transactionCharge > 0
52-
? charge.transactionCharge.toString()
53-
: null;
51+
charge.transactionCharge != null && charge.transactionCharge > 0
52+
? charge.transactionCharge.toString()
53+
: null;
5454
this._bearer = charge.bearer != null ? _getBearer(charge.bearer) : null;
5555
this._metadata = charge.metadata;
5656
this._plan = charge.plan;
@@ -60,8 +60,9 @@ class ChargeRequestBody extends BaseRequestBody {
6060
}
6161

6262
static Future<ChargeRequestBody> getChargeRequestBody(Charge charge) async {
63-
return Crypto.encrypt(CardUtils.concatenateCardFields(charge.card)).then(
64-
(clientData) => ChargeRequestBody._(charge, clientData));
63+
return Crypto
64+
.encrypt(CardUtils.concatenateCardFields(charge.card))
65+
.then((clientData) => ChargeRequestBody._(charge, clientData));
6566
}
6667

6768
addPin(String pin) async {

lib/src/exceptions.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
import 'package:flutter_paystack/src/my_strings.dart';
2+
13
class PaystackException implements Exception {
24
String message;
35

46
PaystackException(this.message);
57

68
@override
79
String toString() {
8-
if (message == null) return "Exception";
9-
return "Exception: $message";
10+
if (message == null) return Strings.unKnownError;
11+
return message;
1012
}
1113
}
1214

@@ -49,4 +51,3 @@ class ProcessingException extends ChargeException {
4951
: super(
5052
'A transaction is currently processing, please wait till it concludes before attempting a new charge.');
5153
}
52-

lib/src/model/card.dart

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,17 @@ class PaymentCard {
103103

104104
PaymentCard(
105105
{@required String number,
106-
@required this.cvc,
107-
@required this.expiryMonth,
108-
@required this.expiryYear,
109-
String name,
110-
String addressLine1,
111-
String addressLine2,
112-
String addressLine3,
113-
String addressLine4,
114-
String addressPostCode,
115-
String addressCountry,
116-
String country}) {
106+
@required this.cvc,
107+
@required this.expiryMonth,
108+
@required this.expiryYear,
109+
String name,
110+
String addressLine1,
111+
String addressLine2,
112+
String addressLine3,
113+
String addressLine4,
114+
String addressPostCode,
115+
String addressCountry,
116+
String country}) {
117117
this.number = CardUtils.getCleanedNumber(number);
118118
this.name = StringUtils.nullify(name);
119119
this.addressLine1 = StringUtils.nullify(addressLine1);
@@ -149,9 +149,9 @@ class PaymentCard {
149149

150150
var cvcValue = cardCvc.trim();
151151
bool validLength =
152-
((_type == null && cvcValue.length >= 3 && cvcValue.length <= 4) ||
153-
(CardType.americanExpress == _type && cvcValue.length == 4) ||
154-
(CardType.americanExpress != _type && cvcValue.length == 3));
152+
((_type == null && cvcValue.length >= 3 && cvcValue.length <= 4) ||
153+
(CardType.americanExpress == _type && cvcValue.length == 4) ||
154+
(CardType.americanExpress != _type && cvcValue.length == 3));
155155
return !(!CardUtils.isWholeNumberPositive(cvcValue) || !validLength);
156156
}
157157

@@ -164,7 +164,8 @@ class PaymentCard {
164164
if (StringUtils.isEmpty(cardNumber)) return false;
165165

166166
// Remove all non digits
167-
var formattedNumber = cardNumber.trim().replaceAll(new RegExp(r'[^0-9]'), '');
167+
var formattedNumber =
168+
cardNumber.trim().replaceAll(new RegExp(r'[^0-9]'), '');
168169

169170
// Verve card needs no other validation except it matched pattern
170171
if (CardType.fullPatternVerve.hasMatch(formattedNumber)) {
@@ -242,13 +243,12 @@ abstract class CardType {
242243
r'^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$');
243244
static final fullPatternAmericanExpress = RegExp(r'^3[47][0-9]{13}$');
244245
static final fullPatternDinersClub = RegExp(r'^3(?:0[0-5]|[68][0-9])'
245-
r'[0-9]{11}$');
246+
r'[0-9]{11}$');
246247
static final fullPatternDiscover = RegExp(r'^6(?:011|5[0-9]{2})[0-9]{12}$');
247248
static final fullPatternJCB = RegExp(r'^(?:2131|1800|35[0-9]{3})'
248-
r'[0-9]{11}$');
249+
r'[0-9]{11}$');
249250
static final fullPatternVerve =
250-
RegExp(r'^((506(0|1))|(507(8|9))|(6500))[0-9]{12,15}$');
251-
251+
RegExp(r'^((506(0|1))|(507(8|9))|(6500))[0-9]{12,15}$');
252252

253253
// Regular expression to match starting characters (aka issuer
254254
// identification number (IIN)) of the card
@@ -257,14 +257,12 @@ abstract class CardType {
257257
static final startingPatternMasterCard = RegExp(
258258
r'((5[1-5])|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720))');
259259
static final startingPatternAmericanExpress = RegExp(r'((34)|(37))');
260-
static final startingPatternDinersClub = RegExp(
261-
r'((30[0-5])|(3[89])|(36)|(3095))');
260+
static final startingPatternDinersClub =
261+
RegExp(r'((30[0-5])|(3[89])|(36)|(3095))');
262262
static final startingPatternJCB = RegExp(r'(352[89]|35[3-8][0-9])');
263-
static final startingPatternVerve = RegExp(
264-
r'((506(0|1))|(507(8|9))|(6500))');
263+
static final startingPatternVerve = RegExp(r'((506(0|1))|(507(8|9))|(6500))');
265264
static final startingPatternDiscover = RegExp(r'((6[45])|(6011))');
266265

267-
268266
bool hasFullMatch(String cardNumber);
269267

270268
bool hasStartingMatch(String cardNumber);

lib/src/my_strings.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ class Strings {
44
static const String numberIsInvalid = 'Card is invalid';
55
static const String continue_ = 'Continue';
66
static const String cancel = 'Cancel';
7-
}
7+
static const String unKnownError = 'Unknown Error';
8+
}

lib/src/paystack.dart

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,92 @@
1+
import 'dart:async';
2+
import 'package:flutter/foundation.dart';
3+
import 'package:flutter/services.dart';
4+
15
import 'package:flutter_paystack/src/model/charge.dart';
6+
import 'package:flutter_paystack/src/platform_info.dart';
27
import 'package:flutter_paystack/src/transaction.dart';
38
import 'package:flutter/material.dart';
49
import 'package:flutter_paystack/src/exceptions.dart';
510
import 'package:flutter_paystack/src/transaction_manager.dart';
611
import 'package:flutter_paystack/src/utils/utils.dart';
712

13+
class PaystackPlugin {
14+
static bool _sdkInitialized = false;
15+
static String _publicKey;
16+
17+
PaystackPlugin._();
18+
19+
static Future<PaystackPlugin> initialize({@required String publicKey}) async {
20+
assert(() {
21+
if (publicKey == null || publicKey.isEmpty) {
22+
throw new PaystackException('publicKey cannot be null or empty');
23+
}
24+
return true;
25+
}());
26+
//do all the init work here
27+
28+
var completer = Completer<PaystackPlugin>();
29+
30+
//check if sdk is actually initialized
31+
if (sdkInitialized) {
32+
completer.complete(PaystackPlugin._());
33+
} else {
34+
_publicKey = publicKey;
35+
36+
// Using cascade notation to build the platform specific info
37+
try {
38+
String userAgent = await Utils.channel.invokeMethod('getUserAgent');
39+
String paystackBuild =
40+
await Utils.channel.invokeMethod('getVersionCode');
41+
String deviceId = await Utils.channel.invokeMethod('getDeviceId');
42+
PlatformInfo()
43+
..userAgent = userAgent
44+
..paystackBuild = paystackBuild
45+
..deviceId = deviceId;
46+
47+
_sdkInitialized = true;
48+
completer.complete(PaystackPlugin._());
49+
} on PlatformException catch (e) {
50+
completer.completeError(e);
51+
}
52+
}
53+
return completer.future;
54+
}
55+
56+
static bool get sdkInitialized => _sdkInitialized;
57+
58+
static String get publicKey {
59+
// Validate that the sdk has been initialized
60+
Utils.validateSdkInitialized();
61+
return _publicKey;
62+
}
63+
64+
static void _performChecks() {
65+
//validate that sdk has been initialized
66+
Utils.validateSdkInitialized();
67+
68+
//validate public keys
69+
Utils.hasPublicKey();
70+
}
71+
72+
static chargeCard(BuildContext context,
73+
{@required Charge charge,
74+
@required OnTransactionChange<Transaction> beforeValidate,
75+
@required OnTransactionChange<Transaction> onSuccess,
76+
@required OnTransactionError<Object, Transaction> onError}) {
77+
assert(context != null, 'context must not be null');
78+
79+
_performChecks();
80+
81+
// Construct new paystack object
82+
Paystack paystack = Paystack.withPublicKey(publicKey);
83+
84+
// Create token
85+
paystack.chargeCard(context, charge,
86+
beforeValidate: beforeValidate, onSuccess: onSuccess, onError: onError);
87+
}
88+
}
89+
890
class Paystack {
991
String _publicKey;
1092

@@ -60,13 +142,12 @@ class Paystack {
60142

61143
transactionManager.chargeCard();
62144
} catch (e) {
63-
64145
assert(onError != null);
65146
onError(e, null);
66147
}
67148
}
68149
}
69150

70151
typedef void OnTransactionChange<Transaction>(Transaction transaction);
71-
typedef void OnTransactionError<Object, Transaction>(
152+
typedef void OnTransactionError<Object, Transaction>(
72153
Object e, Transaction transaction);

0 commit comments

Comments
 (0)