Skip to content

Commit 61ff229

Browse files
authored
Merge pull request #114 from weareseeed/fix/Ach
ACH fix.
2 parents 671b85a + d9fc315 commit 61ff229

File tree

8 files changed

+70
-35
lines changed

8 files changed

+70
-35
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-square-web-payments-sdk",
3-
"version": "3.2.3",
3+
"version": "3.2.4-beta.1",
44
"homepage": "https://weareseeed.github.io/react-square-web-payments-sdk/",
55
"bugs": {
66
"url": "https://github.com/weareseeed/react-square-web-payments-sdk/issues"
@@ -59,7 +59,7 @@
5959
"size:why": "size-limit --why"
6060
},
6161
"dependencies": {
62-
"@square/web-sdk": "^2.0.1",
62+
"@square/web-sdk": "^2.1.0",
6363
"@stitches/react": "^1.2.8"
6464
},
6565
"devDependencies": {

src/components/ach/ach.tsx

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { useForm } from '~/contexts/form';
77
import { useEventListener } from '~/hooks/use-event-listener';
88
import { PayButton, SvgIcon } from './ach.styles';
99
import { 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;
3230
export 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',

src/components/ach/ach.types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export type SvgProps = React.ComponentPropsWithRef<'svg'> & {
7575
css?: Stitches.ComponentProps<typeof SvgIcon>['css'];
7676
};
7777

78-
export interface AchBase extends Square.AchOptions, Square.AchTokenOptions {
78+
export interface AchBase extends Square.AchOptions, Square.AchOptions {
7979
callbacks?: {
8080
/** The user has completed the Assets and Bank Income Insights flow. */
8181
bankIncomeInsightsCompleted?(event: Square.SqEvent<PlaidLinkOnEventMetadata>): void;
@@ -189,4 +189,6 @@ export interface AchProps extends AchBase {
189189
buttonProps?: AchPayButtonProps;
190190
/** Props to be passed to the `<svg>` element. */
191191
svgProps?: SvgProps;
192+
/** Props to be passed to the ach.tokenize method */
193+
accountHolderName: string;
192194
}

src/components/ach/ach.utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { PlaidEventName } from "@square/web-sdk";
2+
13
export function transformPlaidEventName(name: string) {
2-
return name.replace(/([A-Z])/g, '_$1').toUpperCase();
4+
return name.replace(/([A-Z])/g, '_$1').toUpperCase() as PlaidEventName;
35
}

src/components/cash-app-pay/cash-app-pay.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ function CashAppPay({
9191
for (const callback of Object.keys(callbacks)) {
9292
cashApp?.addEventListener(
9393
callback.toLowerCase() as 'ontokenization',
94-
(callbacks as Record<string, (event: Square.SqEvent<Square.TokenizationEvent>) => void>)[callback]
94+
(callbacks as Record<string, (event: Square.SqEvent<Square.CashAppPayEventData>) => void>)[callback]
9595
);
9696
}
9797
}

tsconfig.build.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"useDefineForClassFields": true,
55
"lib": ["DOM", "DOM.Iterable", "ESNext"],
66
"allowJs": false,
7-
"skipLibCheck": false,
7+
"skipLibCheck": true,
88
"esModuleInterop": false,
99
"allowSyntheticDefaultImports": true,
1010
"strict": true,

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"useDefineForClassFields": true,
55
"lib": ["DOM", "DOM.Iterable", "ESNext"],
66
"allowJs": false,
7-
"skipLibCheck": false,
7+
"skipLibCheck": true,
88
"esModuleInterop": false,
99
"allowSyntheticDefaultImports": true,
1010
"strict": true,

yarn.lock

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -620,21 +620,21 @@ __metadata:
620620
languageName: node
621621
linkType: hard
622622

623-
"@square/web-payments-sdk-types@npm:^1.1.1":
624-
version: 1.33.0
625-
resolution: "@square/web-payments-sdk-types@npm:1.33.0"
623+
"@square/web-payments-sdk-types@npm:^1.68.1":
624+
version: 1.70.0
625+
resolution: "@square/web-payments-sdk-types@npm:1.70.0"
626626
dependencies:
627-
typescript: "npm:^4.1.0"
628-
checksum: 10c0/55eb90ceffdd2837582db4759f4e5963fd761943c1e6a34153216c708e2903891c492d00c8c297224803b143703e1de35cc55edacdaca2efc83e341151eb9fc1
627+
typescript: "npm:^4.9.5"
628+
checksum: 10c0/5dbf975b968ece3660ebcd21b4882a94ace31880d41204d68e79819b1eee891b583bd1450254ba8d8c3f9f076835b609d625b44f3d948d793e487c6ae1b13178
629629
languageName: node
630630
linkType: hard
631631

632-
"@square/web-sdk@npm:^2.0.1":
633-
version: 2.0.1
634-
resolution: "@square/web-sdk@npm:2.0.1"
632+
"@square/web-sdk@npm:^2.1.0":
633+
version: 2.1.0
634+
resolution: "@square/web-sdk@npm:2.1.0"
635635
dependencies:
636-
"@square/web-payments-sdk-types": "npm:^1.1.1"
637-
checksum: 10c0/8b1015de7ab23da1c84198d1dab7f3050dec239ce6f656fa7261015326733d3d4e0f840ee3a024e6839fd97c66def80f2ac3ce5e96c106cb7a039bbbe6b724ac
636+
"@square/web-payments-sdk-types": "npm:^1.68.1"
637+
checksum: 10c0/5b292f28f2f47e5228ef03b7f419c1627019cdc4ffa46407be085668f1514ebd64ed91ca56e481e38205b5ed4287c572112230e5402a52ecbd922c4d9158143e
638638
languageName: node
639639
linkType: hard
640640

@@ -5294,7 +5294,7 @@ __metadata:
52945294
dependencies:
52955295
"@size-limit/preset-big-lib": "npm:^8.0.0"
52965296
"@size-limit/webpack-why": "npm:^8.0.0"
5297-
"@square/web-sdk": "npm:^2.0.1"
5297+
"@square/web-sdk": "npm:^2.1.0"
52985298
"@stitches/react": "npm:^1.2.8"
52995299
"@testing-library/jest-dom": "npm:^5.16.4"
53005300
"@testing-library/react": "npm:^13.3.0"
@@ -6303,7 +6303,7 @@ __metadata:
63036303
languageName: node
63046304
linkType: hard
63056305

6306-
"typescript@npm:^4.1.0, typescript@npm:^4.7.4":
6306+
"typescript@npm:^4.7.4":
63076307
version: 4.7.4
63086308
resolution: "typescript@npm:4.7.4"
63096309
bin:
@@ -6313,7 +6313,17 @@ __metadata:
63136313
languageName: node
63146314
linkType: hard
63156315

6316-
"typescript@patch:typescript@npm%3A^4.1.0#optional!builtin<compat/typescript>, typescript@patch:typescript@npm%3A^4.7.4#optional!builtin<compat/typescript>":
6316+
"typescript@npm:^4.9.5":
6317+
version: 4.9.5
6318+
resolution: "typescript@npm:4.9.5"
6319+
bin:
6320+
tsc: bin/tsc
6321+
tsserver: bin/tsserver
6322+
checksum: 10c0/5f6cad2e728a8a063521328e612d7876e12f0d8a8390d3b3aaa452a6a65e24e9ac8ea22beb72a924fd96ea0a49ea63bb4e251fb922b12eedfb7f7a26475e5c56
6323+
languageName: node
6324+
linkType: hard
6325+
6326+
"typescript@patch:typescript@npm%3A^4.7.4#optional!builtin<compat/typescript>":
63176327
version: 4.7.4
63186328
resolution: "typescript@patch:typescript@npm%3A4.7.4#optional!builtin<compat/typescript>::version=4.7.4&hash=65a307"
63196329
bin:
@@ -6323,6 +6333,16 @@ __metadata:
63236333
languageName: node
63246334
linkType: hard
63256335

6336+
"typescript@patch:typescript@npm%3A^4.9.5#optional!builtin<compat/typescript>":
6337+
version: 4.9.5
6338+
resolution: "typescript@patch:typescript@npm%3A4.9.5#optional!builtin<compat/typescript>::version=4.9.5&hash=289587"
6339+
bin:
6340+
tsc: bin/tsc
6341+
tsserver: bin/tsserver
6342+
checksum: 10c0/e3333f887c6829dfe0ab6c1dbe0dd1e3e2aeb56c66460cb85c5440c566f900c833d370ca34eb47558c0c69e78ced4bfe09b8f4f98b6de7afed9b84b8d1dd06a1
6343+
languageName: node
6344+
linkType: hard
6345+
63266346
"unbox-primitive@npm:^1.0.2":
63276347
version: 1.0.2
63286348
resolution: "unbox-primitive@npm:1.0.2"

0 commit comments

Comments
 (0)