@@ -80,6 +80,12 @@ class PaystackPlugin {
80
80
static void _performChecks () {
81
81
//validate that sdk has been initialized
82
82
Utils .validateSdkInitialized ();
83
+ //check for null value, and length and starts with pk_
84
+ if (_publicKey == null ||
85
+ _publicKey.isEmpty ||
86
+ ! _publicKey.startsWith ("pk_" )) {
87
+ throw new AuthenticationException (Utils .getKeyErrorMsg ('public' ));
88
+ }
83
89
}
84
90
85
91
/// Make payment by charging the user's card
@@ -93,16 +99,22 @@ class PaystackPlugin {
93
99
/// [onSuccess] - Called when the payment is completes successfully
94
100
///
95
101
/// [onError] - Called when the payment completes with an unrecoverable error
96
- static chargeCard (BuildContext context,
97
- {@required Charge charge,
98
- @required OnTransactionChange <Transaction > beforeValidate,
99
- @required OnTransactionChange <Transaction > onSuccess,
100
- @required OnTransactionError <Object , Transaction > onError}) {
101
- assert (context != null , 'context must not be null' );
102
+ ///
102
103
104
+ static Future <CheckoutResponse > chargeCard (
105
+ BuildContext context,
106
+ {@required
107
+ Charge charge,
108
+ @Deprecated ("Use the CheckoutResponse from this function instead. Will be removed in 1.1.0" )
109
+ OnTransactionChange <Transaction > beforeValidate,
110
+ @Deprecated ("Use the CheckoutResponse from this function instead. Will be removed in 1.1.0" )
111
+ OnTransactionChange <Transaction > onSuccess,
112
+ @Deprecated ("Use the CheckoutResponse from this function instead. Will be removed in 1.1.0" )
113
+ OnTransactionError <Object , Transaction > onError}) {
114
+ assert (context != null , 'context must not be null' );
103
115
_performChecks ();
104
116
105
- _Paystack (publicKey ).chargeCard (
117
+ return _Paystack ().chargeCard (
106
118
context: context,
107
119
charge: charge,
108
120
beforeValidate: beforeValidate,
@@ -158,10 +170,10 @@ class PaystackPlugin {
158
170
method != null ,
159
171
'method must not be null. You can pass CheckoutMethod.selectable if you want '
160
172
'the user to select the checkout option' );
161
- assert (fullscreen != null , 'fillscreen must not be null' );
173
+ assert (fullscreen != null , 'fullscreen must not be null' );
162
174
assert (hideAmount != null , 'hideAmount must not be null' );
163
175
assert (hideEmail != null , 'hideEmail must not be null' );
164
- return _Paystack (publicKey ).checkout (
176
+ return _Paystack ().checkout (
165
177
context,
166
178
charge: charge,
167
179
method: method,
@@ -173,40 +185,66 @@ class PaystackPlugin {
173
185
}
174
186
}
175
187
188
+ // TODO: Remove beforeValidate, onSuccess, and onError in v1.1.0
176
189
class _Paystack {
177
- String _publicKey;
178
-
179
- _Paystack (this ._publicKey);
180
-
181
- chargeCard (
190
+ Future <CheckoutResponse > chargeCard (
182
191
{@required BuildContext context,
183
192
@required Charge charge,
184
- @required OnTransactionChange <Transaction > beforeValidate,
185
- @required OnTransactionChange <Transaction > onSuccess,
186
- @required OnTransactionError <Object , Transaction > onError}) {
193
+ OnTransactionChange <Transaction > beforeValidate,
194
+ OnTransactionChange <Transaction > onSuccess,
195
+ OnTransactionError <Object , Transaction > onError}) {
196
+ final completer = Completer <CheckoutResponse >();
187
197
try {
188
- //check for null value, and length and starts with pk_
189
- if (_publicKey == null ||
190
- _publicKey.isEmpty ||
191
- ! _publicKey.startsWith ("pk_" )) {
192
- throw new AuthenticationException (Utils .getKeyErrorMsg ('public' ));
193
- }
198
+ final manager = new CardTransactionManager (
199
+ service: CardService (),
200
+ charge: charge,
201
+ context: context,
202
+ beforeValidate: (t) {
203
+ if (beforeValidate != null ) beforeValidate (t);
204
+ },
205
+ onSuccess: (t) {
206
+ completer.complete (CheckoutResponse (
207
+ message: t.message,
208
+ reference: t.reference,
209
+ status: true ,
210
+ card: charge.card..nullifyNumber (),
211
+ method: CheckoutMethod .card,
212
+ verify: true ));
213
+
214
+ if (onSuccess != null ) onSuccess (t);
215
+ t? .message;
216
+ },
217
+ onError: (o, t) {
218
+ completer.complete (CheckoutResponse (
219
+ message: o.toString (),
220
+ reference: t.reference,
221
+ status: false ,
222
+ card: charge.card..nullifyNumber (),
223
+ method: CheckoutMethod .card,
224
+ verify: ! (o is PaystackException )));
225
+
226
+ if (onError != null ) onError (o, t);
227
+ });
194
228
195
- new CardTransactionManager (
196
- service: CardService (),
197
- charge: charge,
198
- context: context,
199
- beforeValidate: beforeValidate,
200
- onSuccess: onSuccess,
201
- onError: onError)
202
- .chargeCard ();
229
+ manager.chargeCard ();
203
230
} catch (e) {
204
- if (e is AuthenticationException ) {
205
- rethrow ;
231
+ final message = e is PaystackException ? e.message : Strings .sthWentWrong;
232
+ completer.complete (CheckoutResponse (
233
+ message: message,
234
+ reference: charge.reference,
235
+ status: false ,
236
+ card: charge.card..nullifyNumber (),
237
+ method: CheckoutMethod .card,
238
+ verify: ! (e is PaystackException )));
239
+
240
+ if (onError != null ) {
241
+ if (e is AuthenticationException ) {
242
+ rethrow ;
243
+ }
244
+ onError (e, null );
206
245
}
207
- assert (onError != null );
208
- onError (e, null );
209
246
}
247
+ return completer.future;
210
248
}
211
249
212
250
Future <CheckoutResponse > checkout (
0 commit comments