Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions client/components/upstreamMessages/html/README.md
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 |
19 changes: 19 additions & 0 deletions client/components/upstreamMessages/html/package.json
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"
}
}
17 changes: 17 additions & 0 deletions client/components/upstreamMessages/html/src/index.html
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>
</ul>
</body>
</html>
31 changes: 31 additions & 0 deletions client/components/upstreamMessages/html/src/minimal/app.js
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",
});
}
23 changes: 23 additions & 0 deletions client/components/upstreamMessages/html/src/minimal/index.html
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 client/components/upstreamMessages/html/src/recommended/app.js
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);
} 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);
},
});

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 });
});
}
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>
</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>
17 changes: 17 additions & 0 deletions client/components/upstreamMessages/html/vite.config.js
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,
},
},
},
});