@@ -7,7 +7,7 @@ import { useForm } from '~/contexts/form';
77import { useEventListener } from '~/hooks/use-event-listener' ;
88import { PayButton , SvgIcon } from './ach.styles' ;
99import { transformPlaidEventName } from './ach.utils' ;
10- import type { AchProps , AchWithChildrenProps , AchWithoutChildrenProps , PlaidLinkOnEventMetadata } from './ach.types' ;
10+ import type { AchProps , } from './ach.types' ;
1111
1212/**
1313 * Renders a ACH button to use in the Square Web Payment SDK, pre-styled to meet
@@ -27,8 +27,6 @@ import type { AchProps, AchWithChildrenProps, AchWithoutChildrenProps, PlaidLink
2727 * }
2828 * ```
2929 */
30- export function Ach ( props : AchWithChildrenProps ) : React . ReactElement ;
31- export function Ach ( props : AchWithoutChildrenProps ) : React . ReactElement ;
3230export function Ach ( {
3331 accountHolderName,
3432 redirectURI,
@@ -40,7 +38,7 @@ export function Ach({
4038} : AchProps ) {
4139 const [ ach , setAch ] = React . useState < Square . ACH | undefined > ( ( ) => undefined ) ;
4240 const [ isSubmitting , setIsSubmitting ] = React . useState < boolean > ( false ) ;
43- const { cardTokenizeResponseReceived, payments } = useForm ( ) ;
41+ const { cardTokenizeResponseReceived, createPaymentRequest , payments } = useForm ( ) ;
4442 const buttonRef = React . useRef < HTMLButtonElement > ( null ) ;
4543
4644 /**
@@ -54,26 +52,29 @@ export function Ach({
5452
5553 if ( ! ach ) {
5654 console . warn ( 'ACH button was clicked, but no ACH instance was found.' ) ;
57-
5855 return ;
5956 }
6057
58+ if ( ! createPaymentRequest ) throw new Error ( '`createPaymentRequest()` is required when using ACH payments' ) ;
59+
6160 setIsSubmitting ( true ) ;
6261
6362 try {
6463 const result = await ach . tokenize ( {
6564 accountHolderName,
65+ intent : 'CHARGE' ,
66+ amount : createPaymentRequest . total . amount ,
67+ currency : createPaymentRequest . currencyCode ,
6668 } ) ;
6769
68- if ( result . status === 'OK' ) {
70+ if ( result ? .status === 'OK' ) {
6971 const tokenizedResult = await cardTokenizeResponseReceived ( result ) ;
7072 return tokenizedResult ;
7173 }
7274
73- let message = `Tokenization failed with status: ${ result . status } ` ;
75+ let message = `Tokenization failed with status: ${ result ? .status ?? '' } ` ;
7476 if ( result ?. errors ) {
7577 message += ` and errors: ${ JSON . stringify ( result ?. errors ) } ` ;
76-
7778 throw new Error ( message ) ;
7879 }
7980
@@ -96,17 +97,26 @@ export function Ach({
9697 transactionId,
9798 } )
9899 . then ( ( res ) => {
99- if ( signal ?. aborted ) {
100- return ;
101- }
102-
100+ if ( signal ?. aborted ) return ;
103101 setAch ( res ) ;
104-
105102 return res ;
106103 } ) ;
107104
108105 if ( signal . aborted ) {
106+ ach ?. removeEventListener ( 'ontokenization' as Square . PlaidEventName , ( ) => { } )
109107 await ach ?. destroy ( ) ;
108+ } else {
109+ ach ?. addEventListener (
110+ 'ontokenization' as Square . PlaidEventName ,
111+ async ( result : Square . SqEvent < Square . TokenizationEvent > ) => {
112+ const { tokenResult, error } = result . detail ;
113+ if ( error ) {
114+ // add code here to handle error
115+ } else if ( tokenResult ?. status == 'OK' ) {
116+ await cardTokenizeResponseReceived ( tokenResult ) ;
117+ }
118+ }
119+ )
110120 }
111121 } ;
112122
@@ -115,17 +125,18 @@ export function Ach({
115125 return ( ) => {
116126 abortController . abort ( ) ;
117127 } ;
118- } , [ payments ] ) ;
128+ } , [ createPaymentRequest , payments ] ) ;
119129
120130 if ( callbacks ) {
121131 for ( const callback of Object . keys ( callbacks ) ) {
122132 ach ?. addEventListener (
123133 transformPlaidEventName ( callback ) ,
124- ( callbacks as Record < string , ( event : Square . SqEvent < PlaidLinkOnEventMetadata > ) => void > ) [ callback ]
134+ ( callbacks as Record < string , ( event : Square . SqEvent < Square . TokenizationEvent > ) => void > ) [ callback ]
125135 ) ;
126136 }
127137 }
128138
139+
129140 useEventListener ( {
130141 listener : handlePayment ,
131142 type : 'click' ,
0 commit comments