Skip to content

Commit 21d27f3

Browse files
authored
Merge pull request #937 from commercelayer/async-captures
Add helper to identify async captures and remove them from timeline
2 parents 499c44f + b74b151 commit 21d27f3

File tree

6 files changed

+116
-2
lines changed

6 files changed

+116
-2
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import type { Authorization, Capture } from '@commercelayer/sdk'
2+
import { orderTransactionIsAnAsyncCapture } from './transactions'
3+
4+
const capture: Capture = {
5+
id: 'capture-id',
6+
type: 'captures',
7+
created_at: '2023-01-01T00:00:00Z',
8+
updated_at: '2023-01-01T00:00:00Z',
9+
succeeded: false,
10+
message: '',
11+
error_code: '',
12+
amount_cents: 1000,
13+
currency_code: 'USD',
14+
amount_float: 10,
15+
formatted_amount: '$10.00',
16+
number: '123456'
17+
}
18+
19+
describe('orderTransactionIsAnAsyncCapture', () => {
20+
it("returns false if transaction type is not 'captures'", () => {
21+
const transaction: Authorization = {
22+
type: 'authorizations',
23+
succeeded: false,
24+
message: '',
25+
error_code: '',
26+
amount_cents: 1000,
27+
currency_code: 'USD',
28+
created_at: '2023-01-01T00:00:00Z',
29+
updated_at: '2023-01-01T00:00:00Z',
30+
id: 'capture-id',
31+
amount_float: 10,
32+
formatted_amount: '$10.00',
33+
number: '123456'
34+
}
35+
expect(orderTransactionIsAnAsyncCapture(transaction)).toBe(false)
36+
})
37+
38+
it('returns false if transaction.succeeded is true', () => {
39+
const transaction: Capture = {
40+
...capture,
41+
succeeded: true,
42+
message: '',
43+
error_code: ''
44+
}
45+
expect(orderTransactionIsAnAsyncCapture(transaction)).toBe(false)
46+
})
47+
48+
it('returns false if transaction.message is not empty', () => {
49+
const transaction: Capture = {
50+
...capture,
51+
succeeded: false,
52+
message: 'Some message',
53+
error_code: ''
54+
}
55+
expect(orderTransactionIsAnAsyncCapture(transaction)).toBe(false)
56+
})
57+
58+
it('returns false if transaction.error_code is not empty', () => {
59+
const transaction: Capture = {
60+
...capture,
61+
succeeded: false,
62+
message: '',
63+
error_code: 'ERR123'
64+
}
65+
expect(orderTransactionIsAnAsyncCapture(transaction)).toBe(false)
66+
})
67+
68+
it('returns true for async pending capture (not succeeded, empty message and error_code)', () => {
69+
const transaction: Capture = {
70+
...capture,
71+
succeeded: false,
72+
message: '',
73+
error_code: ''
74+
}
75+
expect(orderTransactionIsAnAsyncCapture(transaction)).toBe(true)
76+
})
77+
78+
it('returns true for async pending capture (not succeeded, undefined message and error_code)', () => {
79+
const transaction: Capture = {
80+
...capture,
81+
succeeded: false,
82+
message: undefined,
83+
error_code: undefined
84+
}
85+
expect(orderTransactionIsAnAsyncCapture(transaction)).toBe(true)
86+
})
87+
})
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { Authorization, Capture, Refund, Void } from '@commercelayer/sdk'
2+
import isEmpty from 'lodash-es/isEmpty'
3+
4+
/**
5+
* Check if the transaction is an async capture
6+
* We assume that async pending captures are those
7+
* that are not succeeded and have no message or error code.
8+
*/
9+
export function orderTransactionIsAnAsyncCapture(
10+
transaction: Authorization | Void | Capture | Refund
11+
): boolean {
12+
return (
13+
transaction.type === 'captures' &&
14+
!transaction.succeeded &&
15+
isEmpty(transaction.message) &&
16+
isEmpty(transaction.error_code)
17+
)
18+
}

packages/app-elements/src/locales/en.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,8 @@ const en = {
508508
payment_authorization: 'Payment authorization',
509509
payment_capture: 'Payment capture',
510510
payment_refund: 'Refund',
511-
payment_void: 'Void'
511+
payment_void: 'Void',
512+
waiting_for_successful_capture: 'Waiting for successful capture'
512513
},
513514
form: {
514515
language: 'Language',

packages/app-elements/src/locales/it.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,8 @@ const it: typeof en = {
495495
payment_authorization: 'Autorizzazione pagamento',
496496
payment_capture: 'Cattura pagamento',
497497
payment_refund: 'Rimborso',
498-
payment_void: 'Annulla'
498+
payment_void: 'Annulla',
499+
waiting_for_successful_capture: 'In attesa di catturare il pagamento'
499500
},
500501
form: {
501502
language: 'Lingua',

packages/app-elements/src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export {
3939
type Rate,
4040
type TrackingDetail
4141
} from '#helpers/tracking'
42+
export { orderTransactionIsAnAsyncCapture } from '#helpers/transactions'
4243
export {
4344
getUnitOfWeightName,
4445
getUnitsOfWeightForSelect,

packages/app-elements/src/ui/resources/ResourceOrderTimeline.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getOrderTransactionName } from '#dictionaries/orders'
22
import { navigateTo } from '#helpers/appsNavigation'
33
import { isAttachmentValidNote, referenceOrigins } from '#helpers/attachments'
4+
import { orderTransactionIsAnAsyncCapture } from '#helpers/transactions'
45
import { useCoreApi, useCoreSdkProvider } from '#providers/CoreSdkProvider'
56
import { t } from '#providers/I18NProvider'
67
import { useTokenProvider } from '#providers/TokenProvider'
@@ -351,6 +352,11 @@ const useTimelineReducer = (order: Order) => {
351352
const isFailedAuthorization =
352353
transaction.type === 'authorizations' && !transaction.succeeded
353354

355+
if (orderTransactionIsAnAsyncCapture(transaction)) {
356+
// skipping timeline event when the capture is pending async
357+
return
358+
}
359+
354360
dispatch({
355361
type: 'add',
356362
payload: {

0 commit comments

Comments
 (0)