- A user clicks the PayPal button in a merchant’s app or website.
- The server API creates an order.
- A payment session starts with the
direct-app-switch
mode. - The user is redirected to the PayPal app (if available) or website to make a payment.
- When the payment is approved, the user returns to the merchant app or website to complete the transaction.
- The payment session resumes and captures the order.
directAppSwitch
example in the public examples repository on GitHub.
The integration consists of 2 main files:
index.html
: HTML structure with the PayPal button and an auto-redirect toggleapp.js
: JavaScript implementation that handles the payment flow
Prerequisites
- PayPal SDK v6
- Server-side PayPal integration for order creation and capture, which uses these endpoints:
POST /paypal-api/checkout/orders/create
creates the PayPal order.POST /paypal-api/checkout/orders/{orderId}/capture
captures completed order.
- Client token generation endpoint, which uses the
GET /paypal-api/auth/browser-safe-client-token
endpoint.
Integration
Completing the integration involves the steps in this section.1. HTML structure
In this example, you’ll find these key features:- An auto-redirect checkbox to control redirect behavior
- A hidden PayPal button that becomes visible after SDK initialization
- The JavaScript SDK v6 core script with an
onload
callback
2. SDK initialization
In this example, you’ll find these key features:- It creates an SDK instance with a client token.
- It initializes a one-time payment session with PayPal.
- It handles the return from an external app using
hasReturned()
. For more information about return URL handling, see Return URL handling. - It resumes a session or sets up the button accordingly.
3. Payment session configuration
This example uses the following callbacks:onApprove
: Triggered when payment is approved to capture the orderonCancel
: Handles payment cancellationonError
: Handles payment errors
4. Button setup and payment initiation
In this example, note the Direct App Switch configuration, which includes these features:presentationMode: "direct-app-switch"
enables the Direct App Switch flowautoRedirect.enabled
controls automatic redirection behaviorredirectURL
redirects to the PayPal app or site
5. Server integration functions
These examples show how to set up client token retrieval, order creation, and order capture. Select a tab to see and use the corresponding example. In this order creation example, you see these settings:shippingPreference: "NO_SHIPPING"
means that no shipping is required.userAction: "CONTINUE"
means that the user continues after approval.returnUrl
andcancelUrl
use the URL of the current page.
- Tab Title
- Tab Title
- Tab Title
Client token retrieval
Return URL handling
To understand return URL handing, keep these points in mind:- Both
returnUrl
andcancelUrl
point towindow.location.href
. - The
hasReturned()
method detects when user returns from external flow - The
resume()
method continues the payment process.
Best practices
These examples demonstrate some best practices. Specifically, best practices for Direct App Switch integrations include:- Always wrap payment operations in
try
-catch
blocks for better error handling. - To detect returning users, use
hasReturned()
. - To continue or return to a payment session, call
resume()
. - Use responsive design to ensure a device-optimized experience for users.
Testing
- Web-fallback behavior: While it might be obvious to test on devices that have the PayPal app, also test your app or website on devices that don’t have the PayPal app installed.
- Auto-redirect: Test auto-redirect functionality to ensure that it provides the experience that you expect for users.
- Payment cancellation: Be sure to test what happens when payment fails or is cancelled.
- Order capture: Validate that captured orders accurately reflect the intended purchase for a successful payment.