@@ -14,6 +14,10 @@ enum PaymentState {
1414 deleted
1515}
1616
17+ interface MoneroApiUrlResponse {
18+ url : string ;
19+ }
20+
1721interface CreatePaymentRequest {
1822 orderId : number ;
1923}
@@ -48,14 +52,33 @@ export function MoneroPaymentMethod({ order }: MoneroPaymentMethodProps) {
4852
4953 const getPaymentDetails = async ( ) => {
5054 setLoading ( true ) ;
55+ setError ( "" ) ;
56+ setPaymentResponse ( null ) ;
5157
5258 try {
59+
60+ let moneroApiUrlResponse : MoneroApiUrlResponse ;
61+
62+ if ( process . env . NODE_ENV === 'development' ) {
63+ const apiUrlResponse = await fetch ( "http://localhost:3000/stencil/00000000-0000-0000-0000-000000000001/dist/content/moneroapiurl.json" ) ;
64+ moneroApiUrlResponse = await apiUrlResponse . json ( ) as MoneroApiUrlResponse ;
65+ }
66+ else {
67+ const apiUrlResponse = await fetch ( window . location . origin + "/content/moneroapiurl.json" , {
68+ method : "GET" ,
69+ mode : "no-cors" ,
70+ headers : {
71+ "Accept" : "application/json" ,
72+ }
73+ } ) ;
74+ moneroApiUrlResponse = await apiUrlResponse . json ( ) as MoneroApiUrlResponse ;
75+ }
76+
5377 const createPaymentRequest : CreatePaymentRequest = {
5478 orderId : order . orderId ,
5579 }
5680
57- // TODO Configable
58- const response = await fetch ( "http://localhost:5025/api" + "/payments" , {
81+ const response = await fetch ( moneroApiUrlResponse . url + "/payments" , {
5982 method : "POST" ,
6083 body : JSON . stringify ( createPaymentRequest ) ,
6184 mode : "cors" ,
@@ -70,13 +93,12 @@ export function MoneroPaymentMethod({ order }: MoneroPaymentMethodProps) {
7093 }
7194
7295 const createPaymentResponse : CreatePaymentResponse = await response . json ( ) ;
73- console . log ( createPaymentResponse )
7496
7597 setPaymentResponse ( createPaymentResponse ) ;
7698 setPaymentLink ( `monero:${ createPaymentResponse . address } ?tx_amount=${ createPaymentResponse . xmrAmount ?? 0 } ` ) ;
7799 }
78100 catch {
79-
101+ setError ( "There was an error getting payment details." ) ;
80102 }
81103 finally {
82104 setLoading ( false ) ;
@@ -103,7 +125,10 @@ export function MoneroPaymentMethod({ order }: MoneroPaymentMethodProps) {
103125 }
104126 {
105127 error !== "" ?
106- < > </ >
128+ < div >
129+ < p > { error } </ p >
130+ < button type = 'button' onClick = { getPaymentDetails } className = 'button' > Try again</ button >
131+ </ div >
107132 :
108133 < > </ >
109134 }
0 commit comments