@@ -80,6 +80,12 @@ class PaystackPlugin {
8080 static void _performChecks () {
8181 //validate that sdk has been initialized
8282 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+ }
8389 }
8490
8591 /// Make payment by charging the user's card
@@ -93,16 +99,22 @@ class PaystackPlugin {
9399 /// [onSuccess] - Called when the payment is completes successfully
94100 ///
95101 /// [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+ ///
102103
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' );
103115 _performChecks ();
104116
105- _Paystack (publicKey ).chargeCard (
117+ return _Paystack ().chargeCard (
106118 context: context,
107119 charge: charge,
108120 beforeValidate: beforeValidate,
@@ -158,10 +170,10 @@ class PaystackPlugin {
158170 method != null ,
159171 'method must not be null. You can pass CheckoutMethod.selectable if you want '
160172 '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' );
162174 assert (hideAmount != null , 'hideAmount must not be null' );
163175 assert (hideEmail != null , 'hideEmail must not be null' );
164- return _Paystack (publicKey ).checkout (
176+ return _Paystack ().checkout (
165177 context,
166178 charge: charge,
167179 method: method,
@@ -173,40 +185,66 @@ class PaystackPlugin {
173185 }
174186}
175187
188+ // TODO: Remove beforeValidate, onSuccess, and onError in v1.1.0
176189class _Paystack {
177- String _publicKey;
178-
179- _Paystack (this ._publicKey);
180-
181- chargeCard (
190+ Future <CheckoutResponse > chargeCard (
182191 {@required BuildContext context,
183192 @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 >();
187197 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+ });
194228
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 ();
203230 } 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 );
206245 }
207- assert (onError != null );
208- onError (e, null );
209246 }
247+ return completer.future;
210248 }
211249
212250 Future <CheckoutResponse > checkout (
0 commit comments