Skip to content

Commit a1dea54

Browse files
committed
chore: updated the checkout function
1 parent 0816188 commit a1dea54

File tree

3 files changed

+92
-68
lines changed

3 files changed

+92
-68
lines changed

example/lib/main.dart

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class _HomePageState extends State<HomePage> {
152152
primaryColorLight: Colors.white,
153153
primaryColorDark: navyBlue,
154154
textTheme: Theme.of(context).textTheme.copyWith(
155-
bodyText2: TextStyle(
155+
bodyText2: TextStyle(
156156
color: lightBlue,
157157
),
158158
),
@@ -311,40 +311,24 @@ class _HomePageState extends State<HomePage> {
311311
}
312312
}
313313

314-
_chargeCard(Charge charge) {
315-
// This is called only before requesting OTP
316-
// Save reference so you may send to server if error occurs with OTP
317-
handleBeforeValidate(Transaction transaction) {
318-
_updateStatus(transaction.reference, 'validating...');
319-
}
314+
_chargeCard(Charge charge) async {
315+
final response = await PaystackPlugin.chargeCard(context, charge: charge);
320316

321-
handleOnError(Object e, Transaction transaction) {
322-
// If an access code has expired, simply ask your server for a new one
323-
// and restart the charge instead of displaying error
324-
if (e is ExpiredAccessCodeException) {
325-
_startAfreshCharge();
326-
_chargeCard(charge);
327-
return;
328-
}
329-
330-
if (transaction.reference != null) {
331-
_verifyOnServer(transaction.reference);
332-
} else {
333-
setState(() => _inProgress = false);
334-
_updateStatus(transaction.reference, e.toString());
335-
}
336-
}
317+
final reference = response.reference;
337318

338-
// This is called only after transaction is successful
339-
handleOnSuccess(Transaction transaction) {
340-
_verifyOnServer(transaction.reference);
319+
// Checking if the transaction is successful
320+
if (response.status) {
321+
_verifyOnServer(reference);
322+
return;
341323
}
342324

343-
PaystackPlugin.chargeCard(context,
344-
charge: charge,
345-
beforeValidate: (transaction) => handleBeforeValidate(transaction),
346-
onSuccess: (transaction) => handleOnSuccess(transaction),
347-
onError: (error, transaction) => handleOnError(error, transaction));
325+
// The transaction failed. Checking if we should verify the transaction
326+
if (response.verify) {
327+
_verifyOnServer(reference);
328+
} else {
329+
setState(() => _inProgress = false);
330+
_updateStatus(reference, response.message);
331+
}
348332
}
349333

350334
String _getReference() {

lib/src/common/paystack.dart

Lines changed: 73 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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
176189
class _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(

lib/src/models/checkout_response.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ class CheckoutResponse {
2727
/// The means of payment. It may return [CheckoutMethod.bank] or [CheckoutMethod.card]
2828
CheckoutMethod method;
2929

30-
/// If the transaction should be verified. See https://developers.paystack.co/v2.0/reference#verify-transaction
31-
/// This might return true regardless whether a transaction fails or not.
30+
/// If the transaction should be verified. See https://developers.paystack.co/v2.0/reference#verify-transaction.
31+
/// This is usually false for transactions that didn't reach Paystack before terminating
32+
///
33+
/// It might return true regardless whether a transaction fails or not.
3234
bool verify;
3335

3436
CheckoutResponse.defaults() {

0 commit comments

Comments
 (0)