Payment And Order

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 payWithPayPal method is triggered.
    • For PayPal Pay Later: The payWithPayPalPayLater method is triggered.

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, the PlatformException with code 'payment_cancelled' is captured, and a BraintreeUserCancelled exception is thrown:
    void _handlePaymentCancelledError(dynamic error) {
      throw BraintreeUserCancelled(message: 'User cancelled the payment process.');
    }
    
  • Transaction Failure (payment_failed): In case of a failed transaction, the PlatformException with code 'payment_failed' is captured, and a BraintreePaymentFailed exception 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_authorized event through the event channel.
  • The plugin parses this event and emits it via the paymentAuthorised stream:
    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

ErrorCauseResolution
payment_cancelledUser exits PayPal flow without completing payment.Notify the user that payment was cancelled.
payment_failedTransaction failed due to technical issues or insufficient funds.Inform the user and allow retrying the transaction.

Summary

  1. User selects PayPal or PayPal Pay Later.
  2. App invokes respective Braintree methods (payWithPayPal or payWithPayPalPayLater).
  3. The plugin fetches a client token if required and invokes the native method via MethodChannel.
  4. Native layer interacts with PayPal via the Braintree SDK.
  5. Upon success, the native layer sends an event back to the plugin.
  6. Plugin emits a success event to the Flutter app.
  7. On failure, the plugin handles the error and throws appropriate exceptions.

Copyright © 2026