<iframe>
integration provides enhanced security by isolating payment processing code within a secure <iframe>
wrapper while maintaining seamless communication with the parent merchant page through the postMessage
API. In this case, the term “sandboxed” refers to the isolation of payment processing in an <iframe>
wrapper, not the sandbox testing environment that PayPal provides.
In a sandboxed <iframe>
integration, the parent page is a merchant page. Within the merchant page, an <iframe>
wrapper uses the PayPal JavaScript SDK v6 and communicates with a server that accesses the PayPal server SDK. The merchant page and <iframe>
use postMessage
to communicate about the state of a payment. Using the PayPal SDK inside an <iframe>
wrapper prevents having to request the SDK script directly from the merchant page.
This type of integration is recommended for high-security uses, such as banking and financial services or integrations with strict compliance requirements, such as PCI DSS, or SOC 2. However, it requires more complex setup and maintenance than other PayPal integrations.
This implementation consists of two separate applications:
- A merchant page at
localhost:3001
for your main website and app - A PayPal
<iframe>
atlocalhost:3000
that provides an isolated payment processing environment with secure cross-frame communication through thepostMessage
API
Prerequisites
Before you integrate:- Create a PayPal developer account if you don’t have one.
- Get your client ID and client secret from PayPal’s Developer Dashboard. For more information about your client ID and client secret, see Get client ID and client secret.
- Ensure that your PayPal server provides the necessary endpoints.
Environment configuration
To work with these examples, create an .env file with your PayPal sandbox credentials to enable proper authentication and API communication.Merchant page HTML structure
Set up yor merchant page HTML before setting up your<iframe>
wrapper. This example shows how to set up the main merchant page with proper <iframe>
sandbox attributes, overlay containers for modal presentation, and debug information displays.
Merchant page JavaScript implementation
This example implements page-state management, secure postMessage listeners with origin validation, and handlers for different presentation modes (popup
, modal
, payment-handler
).
PayPal <iframe>
HTML structure
This example creates the <iframe>
content with configuration options for the presentation mode, the PayPal button element, and proper script loading for the v6 SDK.
PayPal <iframe>
JavaScript implementation
This example implements secure communication with the parent window, v6 SDK initialization, payment session creation, and event handlers for payment flow states.
Package.json configuration
This example configuresnpm
scripts to run the merchant page and the PayPal <iframe>
servers concurrently using the Vite build tool with proper port assignments. For more information about Vite, see Vite configuration files.
Vite configuration files
This example sets up separate Vite configurations for the merchant page (port 3001) and PayPal iFrame (port 3000) with proper proxy settings for PayPal API endpoints.<iframe>
sandbox security configuration
This example configures <iframe>
sandbox attributes to enable necessary permissions while maintaining security isolation with allow-scripts
, allow-same-origin
, allow-popups
, and allow-forms
.
PostMessage origin validation
This example implements critical security measures, such as validating message origins to prevent XSS attacks and ensuring that only trusted domains can communicate with your application.Content security policy header configuration
Content Security Policy (CSP) headers control resource loading and prevent unauthorized script execution while allowing necessary PayPal SDK and<iframe>
communication in this example. You learn more about security headers in Production security headers.
Communication protocol messages
Define standardized message formats for iframe-to-parent communication, including changes to the presentation mode, payment flow events, and error handling, as shown in this example.Advanced event handling
You can implement a comprehensive event-handling system with custom logic for payment start, success, cancellation, and error scenarios. This example also includes analytics tracking and user feedback.Dynamic <iframe>
management
The IframeManager
class can handle message queuing, ready-state management, and cleanup operations for better control over <iframe>
lifecycle, as shown in this example.
Error recovery and retry logic
Implement payment retry management with configurable retry limits, user confirmation dialogs, and graceful fallback handling for failed payment attempts, as shown in this example.Production security headers
In a production environment, configureExpress.js
security headers for production deployment, including X-Frame-Options
, content security policy (CSP), and MIME-type protection. For more information about CSP, see Content security policy header configuration.
Troubleshooting PostMessage
To troubleshoot common postMessage communication issues, check origin validation and<iframe>
loading status. Also, confirm that you configured the proper sandbox attributes.
See also
You can see an end-to-end (E2E) example of an<iframe>
integration with PayPal in the public examples repository on GitHub.