-
Notifications
You must be signed in to change notification settings - Fork 516
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1bd325e
Refactor the standard integration guide
gregjopa e91ab1e
chore: pr feedback - read port from env var
gregjopa 91d78cb
chore(docs): recommend at least Node 16
gregjopa 7dfa4da
chore: update deps and server startup message
gregjopa 6e801b8
fix: update the codespaces path for the standard integration
gregjopa 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
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
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 |
---|---|---|
@@ -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" |
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
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,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¤cy=USD"></script> | ||
<script> | ||
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', | ||
}, | ||
gregjopa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
], | ||
}), | ||
}); | ||
|
||
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> |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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.
one thing i would love to see is all of our examples using
paypal-js
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.
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?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.
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
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.
I think with the former, we have to start talking about build tools and that always sucks
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.
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.