messages
component in the JavaScript SDK. You can integrate the messages
component in a variety of ways. The following sections discuss some of the different ways you can configure and use the SDK.
Your options include:
- Configuring the component using JavaScript.
- Using event hooks.
- Using namespace attributes.
- Rendering multiple messages.
- Updating messages without reloading the page.
- Integrating into a single-page app.
Configure using JavaScript
You can configure and display Pay Later messages using JavaScript as an alternative to HTML attributes. The JavaScript SDKmessages
component uses an API that is similar to the API that the buttons
component uses.
The following code sample shows how to create a message object by calling the paypal.Messages
function. The paypal.Messages
function takes a configuration object as its only argument and returns a message object.
The message object has a render()
function that accepts an argument that defines the element in which messages are rendered. In this example, .pp-message
is a CSS selector string, but you also can use an HTMLElement
reference, or an array of HTMLElement
references.
- The amount is set to 500.
- The message is added to the details of a product, so pageType: “product-details” is added to the configuration object.
- The message uses a text layout with a primary logo on top of the message. The SDK renders the message inside any element that contains a class attribute with the value .pp-message.
Note: Both inline attributes and the JavaScript configuration object can be used independently or combined to change the message configuration. If there are conflicting settings, the inline HTML attribute setting overrides the JavaScript configuration object setting.
Use event hooks
Themessages
component of the PayPal JavaScript API lets you to pass in callback functions. Certain events trigger these callback functions. The available events to hook into are onRender
, onClick
, and onApply
.
Event | Description |
---|---|
onRender | Invoked after each message renders into the DOM |
onClick | Invoked after a user selects the message |
on Apply | Invoked after a user selects the Apply button or link in the pop-up modal |
paypal.Messages()
.
The following code sample calls the paypal.Messages
function to create a message object with optional properties for onRender
, onClick
, and onApply
. When any of these events happens, it triggers the associated callback function. The console log includes the following message when this happens: Callback called on render
.
Use namespace attributes
If you’re using a legacy integration for PayPal Checkout, you might be using thedata-namespace
attribute when you load the PayPal JavaScript SDK messages
component.
If you configure messages using JavaScript, the value you set for data-namespace
is the name of the global variable that you use to configure and render messages. This example uses "PayPalSDK"
in data-namespace
.
Render multiple messages
The SDK renders a message into any element with adata-pp-message
attribute or elements that match the target that you defined in the JavaScript configuration with the Messages()
function. If the inline HTML attributes and the JavaScript configuration are different, the SDK uses the inline elements.
You can use both inline attributes and JavaScript. For example, you can use the JavaScript configuration to declare shared or common settings, while you can use inline attributes to customize a specific message.
Update messages without reloading the page
The SDK’smessages
component attaches observers to all attributes that start with data-pp
on DOM elements that it targets with messages.
Changing an attribute can change data-pp-amount
from 100 to 200 to reflect a change, such as when a customer updates an item’s quantity in their cart. Don’t call the render()
function again when this happens, because the SDK automatically detects the change and renders a new message if necessary. However, if new HTML content is dynamically injected to the document, and you want to render a message inside this new HTML, call the render()
function of paypal.Messages
again.
Integrate into a single-page app
The JavaScript SDK should only be loaded once. In many cases, you should add the<script/>
tag into the <head/>
of your main HTML document into which you inject your client-side app. This HTML is often called index.html.
Do not reload the SDK on state changes or router updates. If the state or routes in your SPA change enough to render new content and new messages, invoke paypal.Messages.render()
again, and specify the new targets for the messages.