11import React , { useEffect , useState } from 'react'
22import './MoneroPaymentMethod.scss' ;
33import { LoadingSpinner } from '@bigcommerce/checkout/ui' ;
4- import { createCheckoutService } from '@bigcommerce/checkout-sdk' ;
4+ import { Order } from '@bigcommerce/checkout-sdk' ;
55import QRCode from 'react-qr-code' ;
66
7+ enum PaymentState {
8+ unpaid ,
9+ pending ,
10+ paid ,
11+ withdrawn ,
12+ refunded ,
13+ expired ,
14+ deleted
15+ }
16+
717interface CreatePaymentRequest {
8- checkoutId : string ;
9- amount : number ;
10- currency : string ;
11- email : string ;
18+ orderId : number ;
1219}
1320
1421interface CreatePaymentResponse {
15- paymentId : string ;
22+ orderId : number ;
1623 xmrAmount : number ;
1724 address : string ;
18- walletURI : string ;
19- qrLink : string ;
25+ paymentState : PaymentState ;
26+ }
27+
28+ interface MoneroPaymentMethodProps {
29+ order : Order ;
2030}
2131
22- export function MoneroPaymentMethod ( props : any ) {
32+ export function MoneroPaymentMethod ( { order } : MoneroPaymentMethodProps ) {
2333 const [ loading , setLoading ] = useState ( false ) ;
2434 const [ paymentResponse , setPaymentResponse ] = useState < CreatePaymentResponse | null > ( null ) ;
2535 const [ paymentLink , setPaymentLink ] = useState ( "" ) ;
36+ const [ error , setError ] = useState ( "" ) ;
2637
27- console . log ( props )
38+ useEffect ( ( ) => {
39+ ( async ( ) => {
40+ await getPaymentDetails ( ) ;
41+ } ) ( ) ;
42+ } , [ ] ) ;
2843
2944 useEffect ( ( ) => {
30- // Try to fetch existing payment by checkoutId
31- } , [ ] )
45+ const interval = setInterval ( getPaymentDetails , 120000 ) ;
46+ return ( ) => { clearInterval ( interval ) ; }
47+ } , [ paymentResponse ] ) ;
3248
33- const createPaymentAddress = async ( ) => {
49+ const getPaymentDetails = async ( ) => {
3450 setLoading ( true ) ;
3551
3652 try {
37- const service = createCheckoutService ( ) ;
38- // const state = await service.loadCheckout(checkoutId);
39-
40- const checkoutSelectors = await service . loadCheckout ( service . getState ( ) . data . getCheckout ( ) ?. id )
41-
42- const checkout = checkoutSelectors . data . getCheckout ( ) ;
43-
44- if ( ! checkout ) return ;
45-
46- console . log ( checkout ?. grandTotal )
47- console . log ( checkout ?. id )
48- console . log ( checkout ?. cart . currency . code )
49- console . log ( checkout ?. customer . email )
50-
5153 const createPaymentRequest : CreatePaymentRequest = {
52- amount : checkout . grandTotal ,
53- checkoutId : checkout . id ,
54- email : checkout . customer . email ,
55- currency : checkout . cart . currency . code
54+ orderId : order . orderId ,
5655 }
5756
57+ // TODO Configable
5858 const response = await fetch ( "http://localhost:5025/api" + "/payments" , {
5959 method : "POST" ,
6060 body : JSON . stringify ( createPaymentRequest ) ,
@@ -65,7 +65,7 @@ export function MoneroPaymentMethod(props: any) {
6565 } ) ;
6666
6767 if ( ! response . ok ) {
68- // setError(await response.json());
68+ setError ( await response . json ( ) ) ;
6969 return ;
7070 }
7171
@@ -85,19 +85,28 @@ export function MoneroPaymentMethod(props: any) {
8585
8686 return (
8787 < div className = 'main' >
88- < button type = 'button' onClick = { createPaymentAddress } className = 'button' > Create a one-time payment address </ button >
89- < LoadingSpinner isLoading = { loading } > </ LoadingSpinner >
88+ < p className = 'monero-text' > Pay with Monero </ p >
89+ < LoadingSpinner isLoading = { loading } / >
9090 {
9191 paymentResponse &&
9292 < div >
9393 < label htmlFor = 'address' > Address</ label >
9494 < textarea readOnly rows = { 2 } name = 'address' id = 'address' className = 'form-input optimizedCheckout-form-input address' value = { paymentResponse . address } />
9595 < label htmlFor = 'amount' > Amount</ label >
9696 < input readOnly name = 'amount' id = 'amount' className = 'form-input optimizedCheckout-form-input amount' value = { paymentResponse . xmrAmount } />
97-
98- < QRCode className = "QR" value = { paymentLink } > </ QRCode >
97+ < label htmlFor = 'state' > Status</ label >
98+ < input readOnly name = 'state' id = 'state' className = 'form-input optimizedCheckout-form-input state' value = { PaymentState [ paymentResponse . paymentState ] } />
99+
100+ < QRCode className = "QR" value = { paymentLink } />
101+ < a className = "openwallet" href = { paymentLink } > Open Monero wallet</ a >
99102 </ div >
100103 }
104+ {
105+ error !== "" ?
106+ < > </ >
107+ :
108+ < > </ >
109+ }
101110 </ div >
102111 ) ;
103112
0 commit comments