-
Notifications
You must be signed in to change notification settings - Fork 7
feat: add confirmPayment on paymentElement #16
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,28 @@ | ||
| @react.component | ||
| let make = ( | ||
| ~id, | ||
| ~options: JSON.t, | ||
| ~onChange, | ||
| ~onReady, | ||
| ~onFocus, | ||
| ~onBlur, | ||
| ~onClick, | ||
| ~onPaymentComplete, | ||
| ~onPaymentButtonClick, | ||
| let make = React.forwardRef(( | ||
| props: { | ||
| "id": string, | ||
| "options": JSON.t, | ||
| "onChange": option<option<JSON.t> => unit>, | ||
| "onReady": option<option<JSON.t> => unit>, | ||
| "onFocus": option<option<JSON.t> => unit>, | ||
| "onBlur": option<option<JSON.t> => unit>, | ||
| "onClick": option<option<JSON.t> => unit>, | ||
| "onPaymentComplete": option<option<JSON.t> => unit>, | ||
| "onPaymentButtonClick": unit => Promise.t<unit>, | ||
| }, | ||
| ref_, | ||
| ) => { | ||
| <PaymentElementsWrapper | ||
| id | ||
| options | ||
| onChange | ||
| onReady | ||
| onFocus | ||
| onBlur | ||
| onClick | ||
| id=props["id"] | ||
| options=props["options"] | ||
| onChange=props["onChange"] | ||
| onReady=props["onReady"] | ||
| onFocus=props["onFocus"] | ||
| onBlur=props["onBlur"] | ||
| onClick=props["onClick"] | ||
| componentType="payment" | ||
| onPaymentComplete | ||
| onPaymentButtonClick | ||
| onPaymentComplete=props["onPaymentComplete"] | ||
| onPaymentButtonClick=props["onPaymentButtonClick"] | ||
| imperativeRef=ref_ | ||
| /> | ||
| } | ||
| }) | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| type paymentElementHandle = {confirmPayment: JSON.t => Promise.t<JSON.t>} | ||
|
|
||
| @react.component | ||
| let make = ( | ||
| ~id="payment-Element", | ||
|
|
@@ -10,13 +12,22 @@ let make = ( | |
| ~onClick, | ||
| ~onPaymentComplete, | ||
| ~onPaymentButtonClick, | ||
| ~imperativeRef: Nullable.t<React.ref<paymentElementHandle>>=Nullable.null, | ||
| ) => { | ||
| let hyperSwitch = React.useContext(Context.switchContext) | ||
| let elementsState = React.useContext(Context.elementsContext) | ||
| let divRef = React.useRef(Nullable.null) | ||
|
|
||
| let paymentElement = elementsState.create(componentType, options) | ||
|
|
||
| React.useImperativeHandle1( | ||
| imperativeRef, | ||
| () => { | ||
| confirmPayment: hyperSwitch.confirmPayment, | ||
| }, | ||
| [hyperSwitch], | ||
| ) | ||
|
|
||
|
Contributor
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. The imperative handle is installed on the first commit, while switchContext is still defaultSwitchContext. Calling ref.current.confirmPayment() before the Hyper promise/element is ready therefore resolves {} and performs no request. Should we gate this on real element readiness (or reject with a stable sdk_not_ready error) instead of exposing the default no-op implementation?
Contributor
Author
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. The handle isn't frozen on mount — useImperativeHandle1 re-runs whenever hyperSwitch changes (the [hyperSwitch] dep array), so once the SDK context resolves the ref updates automatically. |
||
| React.useEffect2(() => { | ||
| let paymentElement = elementsState.create(componentType, options) | ||
| paymentElement.mount(`#orca-elements-payment-element-${id}`) | ||
|
|
||

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.
why these changes were needed?
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.
The change from @react.component let make = (~id, ...) to React.forwardRef((props, ref_) => ...) was required to support ref-forwarding.
Also, the peer dependency is "react": "^17.0.0 || ^18.0.0 || ^19.0.0". This is a library, and its consumers may be on React 17 or 18. Dropping forwardRef in favor of React 19's ref-as-prop would silently break for those consumers.