Paypal
This document provides a detailed explanation of the PayPal and PayPal Pay Later flows implemented via the BraintreePlugin. It covers user payment selection, token generation, native method calls, error handling, and success flows.
This document provides a detailed explanation of the PayPal and PayPal Pay Later flows implemented via the BraintreePlugin. It covers user payment selection, token generation, native method calls, error handling, and success flows.
Overview
The integration leverages Braintree as the payment gateway for processing PayPal and PayPal Pay Later transactions. The BraintreePlugin abstracts communication between the Flutter application and the native Braintree SDKs (Android/iOS).
PayPal Flow
1. User Payment Selection
- The user selects PayPal or PayPal Pay Later as their preferred payment method on the UI.
- Based on this selection:
- For PayPal: The
payWithPayPalmethod is triggered. - For PayPal Pay Later: The
payWithPayPalPayLatermethod is triggered.
- For PayPal: The
2. Token Generation
- The plugin requires a client token to authenticate and initiate PayPal transactions:
final braintree = BraintreePlugin(() async { // Fetch client token from the server return await fetchClientToken(); }); - The client token must be securely fetched from the backend, which communicates with the Braintree server.
3. Native Calling Methods
PayPal Transaction
- Method:
payWithPayPal - Parameters:
amount(String): The total transaction amount.currencyCode(String): The currency for the transaction (e.g., "USD").
- Native Invocation:
The plugin invokes the following method on the native side:
await _braintreePlugin.invokeMethod<String>('payWithPayPal', { 'amount': amount, 'currency_code': currencyCode, });
PayPal Pay Later Transaction
- Method:
payWithPayPalPayLater - Parameters:
amount(String): The total transaction amount.currencyCode(String): The currency for the transaction (e.g., "USD").
- Native Invocation:
await _braintreePlugin.invokeMethod<String>('payWithPayPalPayLater', { 'amount': amount, 'currency_code': currencyCode, });
4. Error Handling
Errors are managed centrally in the plugin using specific error handlers:
- User Cancellation (
payment_cancelled): If the user cancels the payment, thePlatformExceptionwith code'payment_cancelled'is captured, and aBraintreeUserCancelledexception is thrown:void _handlePaymentCancelledError(dynamic error) { throw BraintreeUserCancelled(message: 'User cancelled the payment process.'); } - Transaction Failure (
payment_failed): In case of a failed transaction, thePlatformExceptionwith code'payment_failed'is captured, and aBraintreePaymentFailedexception is thrown:void _handlePaymentFailedError(dynamic error) { throw BraintreePaymentFailed(message: 'Payment failed. Please try again.'); }
5. Success Flow
Upon successful completion:
- The native layer sends a
payment_authorizedevent through the event channel. - The plugin parses this event and emits it via the
paymentAuthorisedstream:Stream<AuthorisedPaymentResult> get paymentAuthorised => eventStream // .where((event) => event['type'] == 'payment') .map(AuthorisedPaymentResult.fromJson); - The app can listen for this stream to display a success message or update the order status:
braintree.paymentAuthorised.listen((result) { // Handle success, e.g., display success UI or proceed to order confirmation print('Payment Successful: ${result.transactionId}'); });
Error Scenarios
| Error | Cause | Resolution |
|---|---|---|
payment_cancelled | User exits PayPal flow without completing payment. | Notify the user that payment was cancelled. |
payment_failed | Transaction failed due to technical issues or insufficient funds. | Inform the user and allow retrying the transaction. |
Summary
- User selects PayPal or PayPal Pay Later.
- App invokes respective Braintree methods (
payWithPayPalorpayWithPayPalPayLater). - The plugin fetches a client token if required and invokes the native method via
MethodChannel. - Native layer interacts with PayPal via the Braintree SDK.
- Upon success, the native layer sends an event back to the plugin.
- Plugin emits a success event to the Flutter app.
- On failure, the plugin handles the error and throws appropriate exceptions.