-
Notifications
You must be signed in to change notification settings - Fork 26
feat: add upstream message examples #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nbierdeman
merged 6 commits into
paypal-examples:main
from
merlinpaypal:upstream-message-examples
Jun 16, 2025
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a49b694
feat: add upstream message examples
merlinpaypal ec9607d
Merge branch 'main' of https://github.com/paypal-examples/v6-web-sdk-…
merlinpaypal 037a507
move under components
merlinpaypal b7c4bb1
Change minimal to advanced and update recommended
merlinpaypal d21069c
Run prettier
merlinpaypal b13c392
Update recommended approach
merlinpaypal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Upstream Messages HTML Sample Integration | ||
|
|
||
| This HTML sample integration uses HTML, JavaScript, and CSS. It does not require a build process to transpile the source code. It's just static files that can be served up by any web server. [Vite](https://vite.dev/) is used for the local web server to provide the following functionality: | ||
|
|
||
| 1. Serve up the static HTML and JavaScript files. | ||
| 2. Proxy the API server so both the client and server are running on port 3000. | ||
|
|
||
| ## How to Run Locally | ||
|
|
||
| ```bash | ||
| npm install | ||
| npm start | ||
| ``` | ||
|
|
||
| ### Sample Integrations | ||
|
|
||
| | Sample Integration | Description | | ||
| | ----------------------------------------- | ------------------------------------------------------------------------------------------------ | | ||
| | [Recommended](src/recommended/index.html) | Start with this recommended sample integration. It displays all messages supported by the v6 SDK | | ||
| | [Minimal](src/minimal/index.html) | This is a minimal integration. It only shows how a message can be auto bootstrapped | | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "name": "v6-web-sdk-sample-integration-client-upstream-messages", | ||
| "version": "1.0.0", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "build": "vite build", | ||
| "preview": "vite preview", | ||
| "start": "vite", | ||
| "format": "prettier . --write", | ||
| "format:check": "prettier . --check", | ||
| "lint": "echo \"eslint is not set up\"" | ||
| }, | ||
| "license": "Apache-2.0", | ||
| "devDependencies": { | ||
| "prettier": "^3.5.3", | ||
| "vite": "^6.2.0" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <title>Upstream Messages Sample Integrations - PayPal Web SDK</title> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
| </head> | ||
| <body> | ||
| <h1>Upstream Messages Integrations</h1> | ||
|
|
||
| <ul> | ||
| <li> | ||
| <a href="/recommended/index.html">Recommended integration</a> | ||
| </li> | ||
merlinpaypal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| </ul> | ||
| </body> | ||
| </html> | ||
31 changes: 31 additions & 0 deletions
31
client/components/upstreamMessages/html/src/minimal/app.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| async function onPayPalLoaded() { | ||
| try { | ||
| const clientToken = await getBrowserSafeClientToken(); | ||
| const sdkInstance = await window.paypal.createInstance({ | ||
| clientToken, | ||
| components: ["paypal-messages"], | ||
| }); | ||
| createMessage(sdkInstance); | ||
| } catch (error) { | ||
| console.error(error); | ||
| } | ||
| } | ||
|
|
||
| async function getBrowserSafeClientToken() { | ||
| const response = await fetch("/paypal-api/auth/browser-safe-client-token", { | ||
| method: "GET", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| }, | ||
| }); | ||
| const { accessToken } = await response.json(); | ||
|
|
||
| return accessToken; | ||
| } | ||
|
|
||
| async function createMessage(sdkInstance) { | ||
| sdkInstance.createPayPalMessages({ | ||
| buyerCountry: "US", | ||
| currencyCode: "USD", | ||
| }); | ||
merlinpaypal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
23 changes: 23 additions & 0 deletions
23
client/components/upstreamMessages/html/src/minimal/index.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <title>Upstream Messages - Minimal Integration - PayPal Web SDK</title> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
| </head> | ||
| <body> | ||
| <h1>Upstream Messages Minimal Integration</h1> | ||
|
|
||
| <div id="message-background"> | ||
| <paypal-message auto-bootstrap id="paypal-message"></paypal-message> | ||
| </div> | ||
|
|
||
| <script src="app.js"></script> | ||
|
|
||
| <script | ||
| async | ||
| src="https://www.sandbox.paypal.com/web-sdk/v6/core" | ||
| onload="onPayPalLoaded()" | ||
| ></script> | ||
| </body> | ||
| </html> |
58 changes: 58 additions & 0 deletions
58
client/components/upstreamMessages/html/src/recommended/app.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| async function onPayPalLoaded() { | ||
| try { | ||
| const clientToken = await getBrowserSafeClientToken(); | ||
| const sdkInstance = await window.paypal.createInstance({ | ||
| clientToken, | ||
| components: ["paypal-messages"], | ||
| }); | ||
| const content = await createMessage(sdkInstance); | ||
| addAmountEventListener(content); | ||
merlinpaypal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } catch (error) { | ||
| console.error(error); | ||
| } | ||
| } | ||
|
|
||
| async function getBrowserSafeClientToken() { | ||
| const response = await fetch("/paypal-api/auth/browser-safe-client-token", { | ||
| method: "GET", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| }, | ||
| }); | ||
| const { accessToken } = await response.json(); | ||
|
|
||
| return accessToken; | ||
| } | ||
|
|
||
| async function createMessage(sdkInstance) { | ||
| const messagesInstance = sdkInstance.createPayPalMessages({ | ||
| buyerCountry: "US", | ||
| currencyCode: "USD", | ||
| }); | ||
| const messageElement = document.querySelector("#paypal-message"); | ||
|
|
||
| const content = await messagesInstance.fetchContent({ | ||
| onReady: (content) => { | ||
| messageElement.setContent(content); | ||
| }, | ||
| }); | ||
merlinpaypal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return content; | ||
| } | ||
|
|
||
| // basic example product interaction | ||
| function addAmountEventListener(content) { | ||
| const quantityInput = document.querySelector("#quantity-input"); | ||
| const totalAmount = document.querySelector("#total-amount"); | ||
| const quantity = document.querySelector("#quantity"); | ||
|
|
||
| quantityInput.addEventListener("input", (event) => { | ||
| const quantityValue = event.target.value; | ||
| const calculatedTotalAmount = (50 * quantityValue).toString(); | ||
|
|
||
| quantity.innerHTML = quantityValue; | ||
| totalAmount.innerHTML = calculatedTotalAmount; | ||
|
|
||
| content.update({ amount: calculatedTotalAmount }); | ||
| }); | ||
merlinpaypal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
30 changes: 30 additions & 0 deletions
30
client/components/upstreamMessages/html/src/recommended/index.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <title>Upstream Messages - Recommended Integration - PayPal Web SDK</title> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
| </head> | ||
| <body> | ||
| <h1>Upstream Messages Recommended Integration</h1> | ||
|
|
||
| <div id="message-background"> | ||
| <paypal-message id="paypal-message"></paypal-message> | ||
merlinpaypal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </div> | ||
|
|
||
| <!-- basic example product interaction --> | ||
| <p>Amount: $<span id="total-amount">50</span>, Quantity: <span id="quantity">1</span></p> | ||
| <fieldset> | ||
| <legend>Update the quantity of the items to adjust the amount</legend> | ||
| <input type="number" id="quantity-input" value="1"></input> | ||
| </fieldset> | ||
|
|
||
| <script src="app.js"></script> | ||
|
|
||
| <script | ||
| async | ||
| src="https://www.sandbox.paypal.com/web-sdk/v6/core" | ||
| onload="onPayPalLoaded()" | ||
| ></script> | ||
| </body> | ||
| </html> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { defineConfig } from "vite"; | ||
|
|
||
| // https://vitejs.dev/config/ | ||
| export default defineConfig({ | ||
| plugins: [], | ||
| root: "src", | ||
| server: { | ||
| port: 3000, | ||
| proxy: { | ||
| "/paypal-api": { | ||
| target: "http://localhost:8080", | ||
| changeOrigin: true, | ||
| secure: false, | ||
| }, | ||
| }, | ||
| }, | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.