-
Notifications
You must be signed in to change notification settings - Fork 10
feat: add Card Fields example #67
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
Open
mchoun
wants to merge
4
commits into
main
Choose a base branch
from
feature/add-card-fields-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
19 changes: 19 additions & 0 deletions
19
client/components/cardFields/oneTimePayment/html/README.md
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 @@ | ||
# One-Time Payments 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 buttons supported by the v6 SDK and includes eligibility logic. It uses the "auto" presentation mode which attempts to launch a popup window and then automatically falls back to the modal presentation mode when the browser does not support popups. | | ||
19 changes: 19 additions & 0 deletions
19
client/components/cardFields/oneTimePayment/html/package.json
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-html", | ||
"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.6.2", | ||
"vite": "^7.0.4" | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
client/components/cardFields/oneTimePayment/html/src/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,19 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>One-Time Payment Sample Integrations - PayPal Web SDK</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
</head> | ||
<body> | ||
<h1>One-Time Payment Integrations</h1> | ||
|
||
<ul> | ||
<li> | ||
<a href="/recommended/index.html" | ||
>Recommended integration with PayPal Card Fields | ||
</a> | ||
</li> | ||
</ul> | ||
</body> | ||
</html> |
125 changes: 125 additions & 0 deletions
125
client/components/cardFields/oneTimePayment/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,125 @@ | ||||||
async function onPayPalWebSdkLoaded() { | ||||||
try { | ||||||
const clientToken = await getBrowserSafeClientToken(); | ||||||
const sdkInstance = await window.paypal.createInstance({ | ||||||
clientToken, | ||||||
components: ["card-fields"], | ||||||
pageType: "checkout", | ||||||
}); | ||||||
|
||||||
// const paymentMethods = await sdkInstance.findEligibleMethods({ | ||||||
// currencyCode: "USD", | ||||||
// }); | ||||||
|
||||||
// NOTE: Eligibility does not currently return card | ||||||
// if (paymentMethods.isEligible("card")) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we enable this eligibility check?
Suggested change
|
||||||
// setupCardFields(sdkInstance); | ||||||
// } | ||||||
setupCardFields(sdkInstance); | ||||||
} catch (error) { | ||||||
console.error(error); | ||||||
} | ||||||
} | ||||||
|
||||||
async function setupCardFields(sdkInstance) { | ||||||
const cardFieldsInstance = | ||||||
sdkInstance.createCardFieldsOneTimePaymentSession(); | ||||||
|
||||||
const numberField = cardFieldsInstance.createCardFieldsComponent({ | ||||||
type: "number", | ||||||
placeholder: "Enter a number:", | ||||||
ariaDescription: "card-number-field", | ||||||
ariaLabel: "some-values", | ||||||
style: { | ||||||
input: { | ||||||
color: "green", | ||||||
}, | ||||||
}, | ||||||
// SDK to take care of aria-invalid when the value is invalid on Change | ||||||
}); | ||||||
const cvvField = cardFieldsInstance.createCardFieldsComponent({ | ||||||
type: "cvv", | ||||||
placeholder: "Enter CVV:", | ||||||
}); | ||||||
const expiryField = cardFieldsInstance.createCardFieldsComponent({ | ||||||
type: "expiry", | ||||||
placeholder: "Enter Expiry:", | ||||||
}); | ||||||
|
||||||
document.querySelector("#paypal-card-fields-number").appendChild(numberField); | ||||||
document.querySelector("#paypal-card-fields-cvv").appendChild(cvvField); | ||||||
document.querySelector("#paypal-card-fields-expiry").appendChild(expiryField); | ||||||
|
||||||
const payButton = document.querySelector("#save-payment-method-button"); | ||||||
payButton.addEventListener("click", async () => { | ||||||
try { | ||||||
const orderId = await createOrder(); | ||||||
const { data, state } = await cardFieldsInstance.submit(orderId, { | ||||||
billingAddress: { | ||||||
postalCode: "95131", | ||||||
}, | ||||||
}); | ||||||
|
||||||
switch (state) { | ||||||
case "succeeded": { | ||||||
const { orderId, ...liabilityShift } = data; | ||||||
const orderData = await captureOrder({ | ||||||
orderId: data.orderId, | ||||||
headers: { "X-CSRF-TOKEN": "<%= csrfToken %>" }, | ||||||
}); | ||||||
} | ||||||
case "canceled": { | ||||||
// specifically for if buyer cancels 3DS modal | ||||||
const { orderId } = data; | ||||||
} | ||||||
case "failed": { | ||||||
const { orderId, message } = data; | ||||||
} | ||||||
} | ||||||
} 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 createOrder() { | ||||||
const response = await fetch( | ||||||
"/paypal-api/checkout/orders/create-with-sample-data", | ||||||
{ | ||||||
method: "POST", | ||||||
headers: { | ||||||
"Content-Type": "application/json", | ||||||
}, | ||||||
}, | ||||||
); | ||||||
const { id } = await response.json(); | ||||||
|
||||||
return { orderId: id }; | ||||||
} | ||||||
|
||||||
async function captureOrder({ orderId }) { | ||||||
const response = await fetch( | ||||||
`/paypal-api/checkout/orders/${orderId}/capture`, | ||||||
{ | ||||||
method: "POST", | ||||||
headers: { | ||||||
"Content-Type": "application/json", | ||||||
}, | ||||||
}, | ||||||
); | ||||||
const data = await response.json(); | ||||||
|
||||||
return data; | ||||||
} |
32 changes: 32 additions & 0 deletions
32
client/components/cardFields/oneTimePayment/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,32 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>One-Time Payment - Recommended Integration - PayPal Web SDK</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<style> | ||
.card-fields-container { | ||
display: flex; | ||
flex-direction: column; | ||
gap: 12px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h1>One-Time Payment Recommended Integration - Card Fields</h1> | ||
|
||
<div class="card-fields-container"> | ||
<div class="card-field" id="paypal-card-fields-number"></div> | ||
<div class="card-field" id="paypal-card-fields-cvv"></div> | ||
<div class="card-field" id="paypal-card-fields-expiry"></div> | ||
</div> | ||
<button id="pay-button" class="pay-button">Pay</button> | ||
<script src="app.js"></script> | ||
|
||
<script | ||
async | ||
src="https://www.sandbox.paypal.com/web-sdk/v6/core" | ||
onload="onPayPalWebSdkLoaded()" | ||
></script> | ||
</body> | ||
</html> |
17 changes: 17 additions & 0 deletions
17
client/components/cardFields/oneTimePayment/html/vite.config.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,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, | ||
}, | ||
}, | ||
}, | ||
}); |
19 changes: 19 additions & 0 deletions
19
client/components/cardFields/savePayment/html/package.json
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", | ||
"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.6.2", | ||
"vite": "^7.0.4" | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
client/components/cardFields/savePayment/html/src/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,118 @@ | ||
async function onPayPalWebSdkLoaded() { | ||
try { | ||
const clientToken = await getBrowserSafeClientToken(); | ||
const sdkInstance = await window.paypal.createInstance({ | ||
clientToken, | ||
components: ["card-fields"], | ||
pageType: "checkout", | ||
}); | ||
|
||
// const paymentMethods = await sdkInstance.findEligibleMethods({ | ||
// currencyCode: "USD", | ||
// paymentFlow: "VAULT_WITHOUT_PAYMENT", | ||
// }); | ||
|
||
// NOTE: Eligibility does not currently return card | ||
// if (paymentMethods.isEligible("card")) { | ||
// setupCardFields(sdkInstance); | ||
// } | ||
setupCardFields(sdkInstance); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
async function setupCardFields(sdkInstance) { | ||
const cardFieldsInstance = sdkInstance.createCardFieldsSavePaymentSession(); | ||
|
||
const numberField = cardFieldsInstance.createCardFieldsComponent({ | ||
type: "number", | ||
placeholder: "Enter a number:", | ||
ariaDescription: "card-number-field", | ||
ariaLabel: "some-values", | ||
style: { | ||
input: { | ||
color: "green", | ||
}, | ||
}, | ||
}); | ||
const cvvField = cardFieldsInstance.createCardFieldsComponent({ | ||
type: "cvv", | ||
placeholder: "Enter CVV:", | ||
}); | ||
const expiryField = cardFieldsInstance.createCardFieldsComponent({ | ||
type: "expiry", | ||
placeholder: "Enter Expiry:", | ||
}); | ||
|
||
document.querySelector("#paypal-card-fields-number").appendChild(numberField); | ||
document.querySelector("#paypal-card-fields-cvv").appendChild(cvvField); | ||
document.querySelector("#paypal-card-fields-expiry").appendChild(expiryField); | ||
|
||
const payButton = document.querySelector("#pay-button"); | ||
payButton.addEventListener("click", async () => { | ||
try { | ||
const vaultSetupToken = await createVaultSetupToken(); | ||
const { data, state } = await cardFieldsInstance.submit(vaultSetupToken, { | ||
billingAddress: { | ||
postalCode: "95131", | ||
}, | ||
}); | ||
|
||
switch (state) { | ||
case "succeeded": { | ||
const { vaultSetupToken, ...liabilityShift } = data; | ||
const paymentToken = await createPaymentToken({ | ||
vaultSetupToken: data.vaultSetupToken, | ||
headers: { "X-CSRF-TOKEN": "<%= csrfToken %>" }, | ||
}); | ||
} | ||
case "canceled": { | ||
// specifically for if buyer cancels 3DS modal | ||
const { orderId } = data; | ||
} | ||
case "failed": { | ||
const { orderId, message } = data; | ||
} | ||
} | ||
} 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 createVaultSetupToken() { | ||
const response = await fetch("/paypal-api/vault/setup-token/create", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
const { id } = await response.json(); | ||
|
||
return { vaultSetupToken: id }; | ||
} | ||
|
||
async function createPaymentToken({ vaultSetupToken }) { | ||
const response = await fetch(`/paypal-api/vault/payment-token/create`, { | ||
body: JSON.stringify({ vaultSetupToken }), | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
const data = await response.json(); | ||
|
||
return data; | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to be updated?