Skip to content

Commit de0f777

Browse files
authored
feat(types): add types for onCancel callback (#793)
1 parent 99b63b3 commit de0f777

File tree

7 files changed

+44
-5
lines changed

7 files changed

+44
-5
lines changed

.changeset/nine-tables-tell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@paypal/paypal-js": minor
3+
---
4+
5+
Add v6 types for onCancel() callback

packages/paypal-js/types/v6/components/base-component.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export type OnApproveDataOneTimePayments = {
1111
billingToken?: string;
1212
};
1313

14+
export type OnCancelDataOneTimePayments = {
15+
orderId?: string;
16+
};
17+
1418
export type OnCompleteData = {
1519
paymentSessionState: "approved" | "canceled" | "error";
1620
};
@@ -34,7 +38,7 @@ export type OnErrorData = PayPalError;
3438
*/
3539
export type BasePaymentSessionOptions = {
3640
onApprove: (data: OnApproveDataOneTimePayments) => Promise<void>;
37-
onCancel?: () => void;
41+
onCancel?: (data: OnCancelDataOneTimePayments) => void;
3842
onComplete?: (data: OnCompleteData) => void;
3943
onError?: (data: OnErrorData) => void;
4044
testBuyerCountry?: string;

packages/paypal-js/types/v6/components/paypal-legacy-billing-agreements.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@ export type OnApproveDataBillingAgreements = {
1515
payerId?: string;
1616
};
1717

18+
export type OnCancelDataBillingAgreements = {
19+
billingToken?: string;
20+
};
21+
1822
export type PayPalLegacyBillingAgreementsSessionOptions = Omit<
1923
BasePaymentSessionOptions,
20-
"onApprove"
24+
"onApprove" | "onCancel"
2125
> & {
2226
billingToken?: string;
2327
onApprove: (data: OnApproveDataBillingAgreements) => Promise<void>;
28+
onCancel?: (data: OnCancelDataBillingAgreements) => void;
2429
};
2530

2631
export type PayPalLegacyBillingPresentationModeOptions =

packages/paypal-js/types/v6/components/paypal-payments.d.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ export type OnApproveDataSavePayments = {
7474
payerId?: string;
7575
};
7676

77+
export type OnCancelDataSavePayments = {
78+
vaultSetupToken?: string;
79+
};
80+
7781
export type PayPalOneTimePaymentSessionOptions = BasePaymentSessionOptions & {
7882
orderId?: string;
7983
onApprove?: (data: OnApproveDataOneTimePayments) => Promise<void>;
@@ -87,12 +91,13 @@ export type PayPalOneTimePaymentSessionOptions = BasePaymentSessionOptions & {
8791

8892
export type SavePaymentSessionOptions = Omit<
8993
BasePaymentSessionOptions,
90-
"onApprove"
94+
"onApprove" | "onCancel"
9195
> & {
9296
clientMetadataId?: string;
9397
orderId?: never;
9498
vaultSetupToken?: string;
95-
onApprove?: (data?: OnApproveDataSavePayments) => void;
99+
onApprove?: (data: OnApproveDataSavePayments) => Promise<void>;
100+
onCancel?: (data: OnCancelDataSavePayments) => void;
96101
};
97102

98103
export type PayPalPresentationModeOptions =

packages/paypal-js/types/v6/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ export {
214214
CreateOrderCallback,
215215
CreateOrderPromise,
216216
OnApproveDataOneTimePayments,
217+
OnCancelDataOneTimePayments,
217218
OnCompleteData,
218219
OnErrorData,
219220
} from "./components/base-component";

packages/paypal-js/types/v6/tests/paypal-legacy-billing-agreements.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { loadCoreSdkScript } from "../../../src/v6";
22
import type {
33
OnApproveDataBillingAgreements,
4+
OnCancelDataBillingAgreements,
45
PayPalV6Namespace,
56
} from "../index";
67

@@ -49,9 +50,16 @@ async function main() {
4950
return Promise.resolve();
5051
}
5152

53+
function onCancelCallback({ billingToken }: OnCancelDataBillingAgreements) {
54+
console.log({
55+
billingToken,
56+
});
57+
}
58+
5259
const paypalPaymentSession =
5360
sdkInstance.createPayPalBillingAgreementWithoutPurchase({
5461
onApprove: onApproveCallback,
62+
onCancel: onCancelCallback,
5563
});
5664

5765
const createBillingToken = () =>

packages/paypal-js/types/v6/tests/paypal-payments.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { loadCoreSdkScript } from "../../../src/v6";
2-
import type { OnApproveDataOneTimePayments, PayPalV6Namespace } from "../index";
2+
import type {
3+
OnApproveDataOneTimePayments,
4+
OnCancelDataOneTimePayments,
5+
PayPalV6Namespace,
6+
} from "../index";
37

48
// eslint-disable-next-line @typescript-eslint/no-unused-vars
59
async function main() {
@@ -46,8 +50,15 @@ async function main() {
4650
return Promise.resolve();
4751
}
4852

53+
function onCancelCallback({ orderId }: OnCancelDataOneTimePayments) {
54+
console.log({
55+
orderId,
56+
});
57+
}
58+
4959
const paypalPaymentSession = sdkInstance.createPayPalOneTimePaymentSession({
5060
onApprove: onApproveCallback,
61+
onCancel: onCancelCallback,
5162
});
5263

5364
const createOrder = () => Promise.resolve({ orderId: "ABC123" });

0 commit comments

Comments
 (0)