-
Notifications
You must be signed in to change notification settings - Fork 570
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
[ncp] handle create order errors #2320
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 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
This file contains 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 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 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,6 +1,6 @@ | ||
/* @flow */ | ||
|
||
import { request, memoize, popup, supportsPopups } from "@krakenjs/belter/src"; | ||
import { request, memoize, popup } from "@krakenjs/belter/src"; | ||
import { | ||
getSDKHost, | ||
getClientID, | ||
|
@@ -11,6 +11,7 @@ import { FUNDING } from "@paypal/sdk-constants/src"; | |
import { DEFAULT_POPUP_SIZE } from "../zoid/checkout"; | ||
|
||
import type { | ||
BuildOpenPopup, | ||
ButtonVariables, | ||
CreateAccessToken, | ||
CreateOrder, | ||
|
@@ -23,6 +24,7 @@ import type { | |
const entryPoint = "SDK"; | ||
const baseUrl = `https://${getSDKHost()}`; | ||
const apiUrl = baseUrl.replace("www", "api"); | ||
export const popupFallbackClassName = "paypal-popup-fallback"; | ||
|
||
const getHeaders = (accessToken?: string) => ({ | ||
...(accessToken && { Authorization: `Bearer ${accessToken}` }), | ||
|
@@ -75,6 +77,7 @@ export const getHostedButtonDetails: HostedButtonDetailsParams = ({ | |
}, | ||
html: body.html, | ||
htmlScript: body.html_script, | ||
popupFallback: body.popup_fallback, | ||
}; | ||
}); | ||
}; | ||
|
@@ -115,6 +118,7 @@ export const buildHostedButtonCreateOrder = ({ | |
return (data) => { | ||
const userInputs = | ||
window[`__pp_form_fields_${hostedButtonId}`]?.getUserInputs?.() || {}; | ||
const onError = window[`__pp_form_fields_${hostedButtonId}`]?.onError; | ||
return createAccessToken(getClientID()).then((accessToken) => { | ||
return request({ | ||
url: `${apiUrl}/v1/checkout/links/${hostedButtonId}/create-context`, | ||
|
@@ -126,16 +130,37 @@ export const buildHostedButtonCreateOrder = ({ | |
merchant_id: merchantId, | ||
...userInputs, | ||
}), | ||
}).then(({ body }) => { | ||
return body.context_id; | ||
}); | ||
}) | ||
.then(({ body }) => body.context_id || onError(body.name)) | ||
.catch(() => onError("REQUEST_FAILED")); | ||
}); | ||
}; | ||
}; | ||
|
||
export const buildOpenPopup: BuildOpenPopup = ({ popupFallback, selector }) => { | ||
return (url) => { | ||
try { | ||
popup(url, { | ||
width: DEFAULT_POPUP_SIZE.WIDTH, | ||
height: DEFAULT_POPUP_SIZE.HEIGHT, | ||
}); | ||
} catch (e) { | ||
const div = document.createElement("div"); | ||
div.classList.add(popupFallbackClassName); | ||
div.innerHTML = popupFallback.replace("#", url); | ||
const container = | ||
typeof selector === "string" | ||
? document.querySelector(selector) | ||
: selector; | ||
container?.appendChild(div); | ||
} | ||
}; | ||
}; | ||
|
||
export const buildHostedButtonOnApprove = ({ | ||
hostedButtonId, | ||
merchantId, | ||
openPopup, | ||
}: GetCallbackProps): OnApprove => { | ||
return (data) => { | ||
return createAccessToken(getClientID()).then((accessToken) => { | ||
|
@@ -149,19 +174,14 @@ export const buildHostedButtonOnApprove = ({ | |
context_id: data.orderID, | ||
}), | ||
}).then((response) => { | ||
// remove the popup fallback message, if present | ||
document.querySelector(`.${popupFallbackClassName}`)?.remove(); | ||
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. this is another weird thing in this PR. I was really hoping to have all the dom manipulation contained within the popup handling code, but we need this here to handle a specific use case:
|
||
// The "Debit or Credit Card" button does not open a popup | ||
// so we need to open a new popup for buyers who complete | ||
// a checkout via "Debit or Credit Card". | ||
if (data.paymentSource === FUNDING.CARD) { | ||
const url = `${baseUrl}/ncp/payment/${hostedButtonId}/${data.orderID}`; | ||
if (supportsPopups()) { | ||
popup(url, { | ||
width: DEFAULT_POPUP_SIZE.WIDTH, | ||
height: DEFAULT_POPUP_SIZE.HEIGHT, | ||
}); | ||
} else { | ||
window.location = url; | ||
} | ||
openPopup?.(url); | ||
} | ||
return response; | ||
}); | ||
|
This file contains 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
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.
I'm not sure I like this implementation. I did it so that I didn't have to pass
popupFallback
andselector
down intoonApprove
. e.g. the alternative to this would be:and then inside of
buildHostedButtonOnApprove
would be the contents of this function. I did it this way so that I could easily unit-test it without managing passing around props but now that I'm looking at it in the GitHub diff it looks a little confusing. wdyt?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.
Ease of testing is a good reason in my book for going this route. Thanks for explaining this!