@@ -9,24 +9,79 @@ import {
99 PaymentWidget as PaymentWidgetClass ,
1010 type PaymentWidgetCompleteEvent ,
1111 type PaymentWidgetErrorEvent ,
12- type PaymentWidgetFlow
12+ type PaymentWidgetFlow ,
13+ type PaymentWidgetOptions
1314} from '@uphold/enterprise-payment-widget-web-sdk' ;
1415import { useCreatePaymentSession } from '../../shared/react/payment-widget-session' ;
1516import { useEffect , useMemo , useState } from 'react' ;
1617import { useFlowData } from '../../shared/react/payment-widget-session/use-flow-data' ;
1718
19+ /**
20+ * Flow button configs.
21+ *
22+ */
23+
24+ type WidgetOptions = PaymentWidgetOptions & Record < string , unknown > ;
25+
26+ type FlowButton = {
27+ label : string ;
28+ flow : PaymentWidgetFlow ;
29+ options ?: WidgetOptions ;
30+ } ;
31+
32+ const DEFAULT_WIDGET_OPTIONS : WidgetOptions = {
33+ debug : true
34+ } ;
35+
36+ const FLOW_BUTTONS : FlowButton [ ] = [
37+ { flow : 'select-for-deposit' , label : 'Select for Deposit' } ,
38+ { flow : 'select-for-withdrawal' , label : 'Select for Withdrawal' } ,
39+ {
40+ // WIP
41+ flow : 'e2e-dev' ,
42+ label : 'E2E ' ,
43+ options : {
44+ e2e : {
45+ type : 'deposit'
46+ }
47+ }
48+ } ,
49+ {
50+ // WIP
51+ flow : 'e2e-dev' ,
52+ label : 'E2E (card only)' ,
53+ options : {
54+ e2e : {
55+ type : 'deposit'
56+ } ,
57+ paymentMethods : [ { type : 'card' } ]
58+ }
59+ } ,
60+ {
61+ flow : 'authorize' ,
62+ label : 'Authorize' ,
63+ options : {
64+ authorize : {
65+ mode : 'default'
66+ }
67+ }
68+ }
69+ ] ;
70+
1871/**
1972 * Export component.
2073 */
2174
2275export default function PaymentWidget ( ) {
2376 const [ createPaymentSessionData , setCreatePaymentSessionData ] = useState < CreatePaymentSessionData > ( ) ;
77+ const [ selectedOptions , setSelectedOptions ] = useState < WidgetOptions > ( ) ;
2478 const { error : loadFlowDataError , isLoading : isLoadingFlowData , loadFlowData } = useFlowData ( ) ;
2579
26- const onFlowButtonClick = ( flow : PaymentWidgetFlow ) => {
80+ const onFlowButtonClick = ( flow : PaymentWidgetFlow , options ?: WidgetOptions ) => {
2781 const load = async ( ) => {
2882 const data = await loadFlowData ( flow ) ;
2983
84+ setSelectedOptions ( options ) ;
3085 setCreatePaymentSessionData ( {
3186 data,
3287 flow
@@ -47,7 +102,16 @@ export default function PaymentWidget() {
47102
48103 const widget = useMemo ( ( ) => {
49104 if ( paymentSession ) {
50- const widget = new PaymentWidgetClass ( paymentSession , { debug : true } ) ;
105+ const options : WidgetOptions = {
106+ ...DEFAULT_WIDGET_OPTIONS ,
107+ ...selectedOptions
108+ } ;
109+
110+ const widget = new PaymentWidgetClass (
111+ paymentSession ,
112+
113+ options
114+ ) ;
51115
52116 const errorHandler = ( e : PaymentWidgetErrorEvent ) => {
53117 setMessage ( `[PWSDK] 'error' event raised with error: ${ JSON . stringify ( e . detail . error ) } ` ) ;
@@ -75,7 +139,7 @@ export default function PaymentWidget() {
75139
76140 return widget ;
77141 }
78- } , [ paymentSession ] ) ;
142+ } , [ paymentSession , selectedOptions ] ) ;
79143
80144 useEffect ( ( ) => {
81145 return ( ) => {
@@ -88,34 +152,40 @@ export default function PaymentWidget() {
88152 < h1 > Payment Widget Web SDK Test Page</ h1 >
89153 { isLoading && < div className = "loading" > Loading...</ div > }
90154 { error && (
91- < div >
92- < div className = "error" >
155+ < details className = "error-details" >
156+ < summary className = "error" >
93157 < span className = "error-icon" > ⚠️</ span >
94158 < span className = "error-message" > An error occurred. Please try again later.</ span >
95- </ div >
159+ </ summary >
96160 < div >
97161 < br />
98162 < span > { error . toString ( ) } </ span >
99163 </ div >
100- </ div >
164+ </ details >
101165 ) }
102166 { message && (
103- < div id = "message" className = "message" >
104- { message }
105- </ div >
167+ < details className = "message-details" >
168+ < summary className = "message" id = "message" >
169+ Message
170+ </ summary >
171+ < div >
172+ < br />
173+ < span > { message } </ span >
174+ </ div >
175+ </ details >
106176 ) }
107177 { ! createPaymentSessionData && ! isLoading && ! error && (
108178 < div className = "button-container" >
109179 < p className = "select-flow-text" > Select flow:</ p >
110- < button className = "action-button" onClick = { ( ) => onFlowButtonClick ( 'select-for-deposit' ) } >
111- Select for Deposit
112- </ button >
113- < button className = "action-button" onClick = { ( ) => onFlowButtonClick ( 'select-for-withdrawal' ) } >
114- Select for Withdrawal
115- </ button >
116- < button className = "action-button" onClick = { ( ) => onFlowButtonClick ( 'authorize' ) } >
117- Authorize
118- </ button >
180+ { FLOW_BUTTONS . map ( ( button , index ) => (
181+ < button
182+ key = { ` ${ button . flow } - ${ index } ` }
183+ className = "action-button"
184+ onClick = { ( ) => onFlowButtonClick ( button . flow , button . options ) }
185+ >
186+ { button . label }
187+ </ button >
188+ ) ) }
119189 </ div >
120190 ) }
121191
0 commit comments