Skip to content

Refactor the standard integration guide #61

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
merged 5 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions .devcontainer/standard-integration/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
"name": "PayPal Standard Integration",
"image": "mcr.microsoft.com/devcontainers/universal:2",
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}/advanced-integration",
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}/standard-integration",

// Use 'onCreateCommand' to run commands when creating the container.
"onCreateCommand": "bash ../.devcontainer/standard-integration/welcome-message.sh",
Expand All @@ -21,17 +21,17 @@
],
"portsAttributes": {
"8888": {
"label": "Preview of Advanced Checkout Flow",
"label": "Preview of Standard Checkout Flow",
"onAutoForward": "openBrowserOnce"
}
},

"secrets": {
"CLIENT_ID": {
"PAYPAL_CLIENT_ID": {
"description": "Sandbox client ID of the application.",
"documentationUrl": "https://developer.paypal.com/dashboard/applications/sandbox"
},
"APP_SECRET": {
"PAYPAL_CLIENT_SECRET": {
"description": "Sandbox secret of the application.",
"documentationUrl": "https://developer.paypal.com/dashboard/applications/sandbox"
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ Once you've setup a PayPal account, you'll need to obtain a **Client ID** and **

These examples will ask you to run commands like `npm install` and `npm start`.

You'll need a version of node >= 14 which can be downloaded from the [Node.js website](https://nodejs.org/en/download/).
You'll need a version of node >= 16 which can be downloaded from the [Node.js website](https://nodejs.org/en/download/).
4 changes: 2 additions & 2 deletions standard-integration/.env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Create an application to obtain credentials at
# https://developer.paypal.com/dashboard/applications/sandbox

CLIENT_ID="YOUR_CLIENT_ID_GOES_HERE"
APP_SECRET="YOUR_SECRET_GOES_HERE"
PAYPAL_CLIENT_ID="YOUR_CLIENT_ID_GOES_HERE"
PAYPAL_CLIENT_SECRET="YOUR_SECRET_GOES_HERE"
2 changes: 1 addition & 1 deletion standard-integration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This folder contains example code for a standard PayPal integration using both t
## Instructions

1. [Create an application](https://developer.paypal.com/dashboard/applications/sandbox/create)
3. Rename `.env.example` to `.env` and update `CLIENT_ID` and `APP_SECRET`
3. Rename `.env.example` to `.env` and update `PAYPAL_CLIENT_ID` and `PAYPAL_CLIENT_SECRET`
2. Replace `test` in `public/index.html` with your app's client-id
4. Run `npm install`
5. Run `npm start`
Expand Down
119 changes: 119 additions & 0 deletions standard-integration/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PayPal JS SDK Standard Integration</title>
</head>
<body>
<div id="paypal-button-container"></div>
<!-- Replace the "test" client-id value with your client-id -->
<script src="https://www.paypal.com/sdk/js?client-id=test&currency=USD"></script>
<script>
Comment on lines +11 to +12

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one thing i would love to see is all of our examples using paypal-js

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be open to that. How do you think they should use paypal-js in a repo like this? Should we recommend a separate client-side js file that imports in paypal-js and then serve up a bundle via rollup or webpack? Or host it from paypalobjects.com and have folks download it from there?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the later. I think ideally we'd have something that was as easy as stripe's: https://stripe.com/docs/libraries/stripejs-esmodule#web-stripejs-html

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think with the former, we have to start talking about build tools and that always sucks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hear ya on the burden of teaching build tools. I like offering both the CDN option and the ES Module like Stripe does. I'll take this paypal-js loader suggestion back to the team to get more feedback.

paypal
.Buttons({
createOrder: async (data) => {
try {
const response = await fetch('/api/orders', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
// use the "body" param to optionally pass additional order information
// like product ids and quantities
body: JSON.stringify({
cart: [
{
id: 'YOUR_PRODUCT_ID',
quantity: 'YOUR_PRODUCT_QUANTITY',
},
],
}),
});

const orderData = await response.json();

if (orderData.id) {
return orderData.id;
} else {
const errorDetail = orderData?.details?.[0];
const errorMessage = errorDetail
? `${errorDetail.issue} ${errorDetail.description} (${orderData.debug_id})`
: JSON.stringify(orderData);

throw new Error(errorMessage);
}
} catch (error) {
console.error(error);
resultMessage(
`Could not initiate PayPal Checkout...<br><br>${error}`,
);
}
},
onApprove: async (data, actions) => {
try {
const response = await fetch(
`/api/orders/${data.orderID}/capture`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
},
);

const orderData = await response.json();
// Three cases to handle:
// (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart()
// (2) Other non-recoverable errors -> Show a failure message
// (3) Successful transaction -> Show confirmation or thank you message

const errorDetail = orderData?.details?.[0];

if (errorDetail?.issue === 'INSTRUMENT_DECLINED') {
// (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart()
// recoverable state, per https://developer.paypal.com/docs/checkout/standard/customize/handle-funding-failures/
return actions.restart();
} else if (errorDetail) {
// (2) Other non-recoverable errors -> Show a failure message
throw new Error(
`${errorDetail.description} (${orderData.debug_id})`,
);
} else if (!orderData.purchase_units) {
throw new Error(JSON.stringify(orderData));
}
else {
// (3) Successful transaction -> Show confirmation or thank you message
// Or go to another URL: actions.redirect('thank_you.html');
const transaction =
orderData.purchase_units[0].payments.captures[0];
resultMessage(
`<h3>Transaction ${transaction.status}: ${transaction.id}<br><br>See console for all available details</h3>`,
);
console.log(
'Capture result',
orderData,
JSON.stringify(orderData, null, 2),
);
}
} catch (error) {
console.error(error);
resultMessage(
`Sorry, your transaction could not be processed...<br><br>${error}`,
);
}
},
})
.render('#paypal-button-container');

// Example function to show a result to the user. Your site's UI library can be used instead,
// however alert() should not be used as it will interrupt the JS SDK window
function resultMessage(message) {
const container = document.getElementById('paypal-button-container');
const p = document.createElement('p');
p.innerHTML = message;
container.parentNode.appendChild(p);
}
</script>
</body>
</html>
6 changes: 3 additions & 3 deletions standard-integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"license": "Apache-2.0",
"description": "",
"dependencies": {
"dotenv": "^16.0.0",
"express": "^4.17.3",
"node-fetch": "^3.2.1"
"dotenv": "^16.3.1",
"express": "^4.18.2",
"node-fetch": "^3.3.2"
}
}
78 changes: 0 additions & 78 deletions standard-integration/paypal-api.js

This file was deleted.

66 changes: 0 additions & 66 deletions standard-integration/public/index.html

This file was deleted.

Loading