@paykit-sdk/paypal 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/schema.ts DELETED
@@ -1,230 +0,0 @@
1
- import {
2
- lazy,
3
- object,
4
- Schema,
5
- number,
6
- string,
7
- stringEnum,
8
- boolean,
9
- } from '@paypal/paypal-server-sdk/dist/types/schema';
10
-
11
- export interface ShippingAmount {
12
- /**
13
- * The currency code.
14
- */
15
- currency_code: string;
16
-
17
- /**
18
- * The value which might be an integer for currencies like JPY that are not typically fractional.
19
- */
20
- value: string;
21
- }
22
-
23
- export const shippingAmountSchema: Schema<ShippingAmount> = object({
24
- currency_code: ['currency_code', string()],
25
- value: ['value', string()],
26
- });
27
-
28
- export interface CreateSubscriptionSchema {
29
- /**
30
- * The ID of the plan, maps to `item_id` in the Paykit SDK.
31
- */
32
- plan_id: string;
33
-
34
- /**
35
- * The quantity of the plan.
36
- */
37
- quantity: number;
38
-
39
- /**
40
- * The custom ID of the subscription.
41
- */
42
- custom_id: string;
43
-
44
- /**
45
- * The start time of the subscription.
46
- */
47
- start_time: string;
48
-
49
- /**
50
- * The subscriber of the subscription.
51
- */
52
- subscriber: {
53
- email_address: string;
54
- name: { given_name: string; surname: string };
55
- phone: { phone_number: string };
56
- };
57
- }
58
-
59
- export const createSubscriptionApticSchema: Schema<CreateSubscriptionSchema> = object({
60
- plan_id: ['plan_id', string()],
61
- quantity: ['quantity', number()],
62
- custom_id: ['custom_id', string()],
63
- start_time: ['start_time', string()],
64
- subscriber: [
65
- 'subscriber',
66
- lazy(() =>
67
- object({
68
- email_address: ['email_address', string()],
69
- name: [
70
- 'name',
71
- lazy(() =>
72
- object({
73
- given_name: ['given_name', string()],
74
- surname: ['surname', string()],
75
- }),
76
- ),
77
- ],
78
- phone: [
79
- 'phone',
80
- lazy(() => object({ phone_number: ['phone_number', string()] })),
81
- ],
82
- }),
83
- ),
84
- ],
85
- });
86
-
87
- enum SubscriptionStatus {
88
- APPROVAL_PENDING = 'APPROVAL_PENDING',
89
- APPROVED = 'APPROVED',
90
- ACTIVE = 'ACTIVE',
91
- SUSPENDED = 'SUSPENDED',
92
- CANCELLED = 'CANCELLED',
93
- EXPIRED = 'EXPIRED',
94
- }
95
-
96
- export interface Subscription {
97
- /**
98
- * The ID of the subscription.
99
- */
100
- id: string;
101
-
102
- /**
103
- * The ID of the plan.
104
- */
105
- plan_id: string;
106
-
107
- /**
108
- * The quantity of the subscription.
109
- */
110
- quantity: number;
111
-
112
- /**
113
- * The status of the subscription.
114
- */
115
- status: SubscriptionStatus;
116
-
117
- /**
118
- * The time the status was updated.
119
- */
120
- status_update_time: string;
121
-
122
- /**
123
- * The note for the status change.
124
- */
125
- status_change_note: string;
126
-
127
- /**
128
- * The time the subscription started.
129
- */
130
- start_time: string;
131
-
132
- /**
133
- * The subscriber of the subscription.
134
- */
135
- subscriber: {
136
- /**
137
- * The email address of the subscriber.
138
- */
139
- email_address: string;
140
-
141
- /**
142
- * The name of the subscriber.
143
- */
144
- name: {
145
- /**
146
- * The given name of the subscriber.
147
- */
148
- given_name: string;
149
- /**
150
- * The surname of the subscriber.
151
- */
152
- surname: string;
153
- };
154
-
155
- /**
156
- * The ID of the payer.
157
- */
158
- payer_id: string;
159
- };
160
-
161
- /**
162
- * Whether the plan was overridden.
163
- */
164
- plan_overridden: boolean;
165
-
166
- /**
167
- * The custom ID of the subscription.
168
- */
169
- custom_id: string;
170
- }
171
-
172
- export const subscriptionApticSchema: Schema<Subscription> = object({
173
- id: ['id', string()],
174
- plan_id: ['plan_id', string()],
175
- quantity: ['quantity', number()],
176
- custom_id: ['custom_id', string()],
177
- plan_overridden: ['plan_overridden', boolean()],
178
- start_time: ['start_time', string()],
179
- subscriber: [
180
- 'subscriber',
181
- lazy(() =>
182
- object({
183
- email_address: ['email_address', string()],
184
- name: [
185
- 'name',
186
- lazy(() =>
187
- object({
188
- given_name: ['given_name', string()],
189
- surname: ['surname', string()],
190
- }),
191
- ),
192
- ],
193
- payer_id: ['payer_id', string()],
194
- }),
195
- ),
196
- ],
197
- status: ['status', stringEnum(SubscriptionStatus)],
198
- status_change_note: ['status_change_note', string()],
199
- status_update_time: ['status_update_time', string()],
200
- });
201
-
202
- interface ResumeSubscriptionSchema {
203
- /**
204
- * The reason for resuming the subscription.
205
- */
206
- reason: string;
207
- }
208
-
209
- export const resumeSubscriptionApticSchemaRequest: Schema<ResumeSubscriptionSchema> =
210
- object({
211
- reason: ['reason', string()],
212
- });
213
-
214
- // WEBHOOK
215
-
216
- export enum VerifyWebhookStatus {
217
- SUCCESS = 'SUCCESS',
218
- FAILURE = 'FAILURE',
219
- }
220
-
221
- export interface VerifyWebhookSchema {
222
- /**
223
- * The status of the verification.
224
- */
225
- verification_status: VerifyWebhookStatus;
226
- }
227
-
228
- export const verifyWebhookSchema: Schema<VerifyWebhookSchema> = object({
229
- verification_status: ['verification_status', stringEnum(VerifyWebhookStatus)],
230
- });
package/src/types.ts DELETED
@@ -1,126 +0,0 @@
1
- export type SubscriptionStatus =
2
- | 'APPROVAL_PENDING'
3
- | 'APPROVED'
4
- | 'ACTIVE'
5
- | 'SUSPENDED'
6
- | 'CANCELLED'
7
- | 'EXPIRED';
8
-
9
- export interface PayPalSubscription {
10
- /**
11
- * The ID of the subscription.
12
- */
13
- id: string;
14
-
15
- /**
16
- * The ID of the plan.
17
- */
18
- plan_id: string;
19
-
20
- /**
21
- * The status of the subscription.
22
- */
23
- status: SubscriptionStatus;
24
-
25
- /**
26
- * The time the status was updated.
27
- */
28
- status_update_time?: string;
29
-
30
- /**
31
- * The time the subscription started.
32
- */
33
- start_time?: string;
34
-
35
- /**
36
- * The time the subscription was created.
37
- */
38
- create_time?: string;
39
-
40
- /**
41
- * The subscriber of the subscription.
42
- */
43
- subscriber?: {
44
- /**
45
- * The email of the subscriber.
46
- */
47
- email_address?: string;
48
-
49
- /**
50
- * The name of the subscriber.
51
- */
52
- name?: {
53
- /**
54
- * The given name of the subscriber.
55
- */
56
- given_name?: string;
57
- /**
58
- * The surname of the subscriber.
59
- */
60
- surname?: string;
61
- };
62
- };
63
-
64
- /**
65
- * The billing info of the subscription.
66
- */
67
- billing_info?: {
68
- /**
69
- * The outstanding balance of the subscription.
70
- */
71
- outstanding_balance?: {
72
- /**
73
- * The currency code of the outstanding balance.
74
- */
75
- currency_code?: string;
76
- /**
77
- * The value of the outstanding balance.
78
- */
79
- value?: string;
80
- };
81
- cycle_executions?: Array<{
82
- /**
83
- * The tenure type of the cycle execution.
84
- */
85
- tenure_type?: string;
86
-
87
- /**
88
- * The sequence of the cycle execution.
89
- */
90
- sequence?: number;
91
-
92
- /**
93
- * The number of cycles completed.
94
- */
95
- cycles_completed?: number;
96
-
97
- /**
98
- * The total number of cycles.
99
- */
100
- total_cycles?: number;
101
- }>;
102
- };
103
-
104
- /**
105
- * The links of the subscription.
106
- */
107
- links?: Array<{
108
- /**
109
- * The href of the link.
110
- */
111
- href?: string;
112
- /**
113
- * The rel of the link.
114
- */
115
- rel?: string;
116
- /**
117
- * The method of the link.
118
- */
119
- method?: string;
120
- }>;
121
-
122
- /**
123
- * The custom ID of the subscription, i.e metadata
124
- */
125
- customId?: string;
126
- }
@@ -1,111 +0,0 @@
1
- import {
2
- Checkout,
3
- omitInternalMetadata,
4
- Payment,
5
- Refund,
6
- Subscription,
7
- } from '@paykit-sdk/core';
8
- import { Order, Refund as PayPalRefund } from '@paypal/paypal-server-sdk';
9
- import { PayPalSubscription } from '../types';
10
-
11
- /**
12
- * Refund
13
- */
14
- export const paykitRefund$InboundSchema = (refund: PayPalRefund): Refund => {
15
- return {
16
- id: refund.id!,
17
- amount: refund.amount?.value ? parseFloat(refund.amount.value) : 0,
18
- currency: refund.amount?.currencyCode || 'USD',
19
- metadata: refund.customId ? omitInternalMetadata(JSON.parse(refund.customId)) : {},
20
- reason: refund.noteToPayer ?? null,
21
- };
22
- };
23
-
24
- /**
25
- * Checkout
26
- */
27
- export const paykitCheckout$InboundSchema = (order: Order): Checkout => {
28
- return {
29
- id: order.id!,
30
- payment_url: order.links?.find(l => l.rel === 'approve')?.href || '',
31
- amount: parseFloat(order.purchaseUnits?.[0]?.amount?.value || '0'),
32
- currency: order.purchaseUnits?.[0]?.amount?.currencyCode || 'USD',
33
- customer: order.payer?.payerId
34
- ? order.payer?.payerId
35
- : { email: order.payer?.emailAddress ?? '' },
36
- session_type: 'one_time',
37
- products: [{ id: order.purchaseUnits?.[0]?.items?.[0]?.sku || '', quantity: 1 }],
38
- metadata: order.purchaseUnits?.[0]?.customId
39
- ? omitInternalMetadata(JSON.parse(order.purchaseUnits?.[0]?.customId))
40
- : {},
41
- subscription: null,
42
- };
43
- };
44
-
45
- /**
46
- * Payment
47
- */
48
- export const paykitPayment$InboundSchema = (order: Order): Payment => {
49
- const statusMap: Record<string, Payment['status']> = {
50
- CREATED: 'pending',
51
- SAVED: 'pending',
52
- APPROVED: 'requires_capture',
53
- VOIDED: 'canceled',
54
- COMPLETED: 'succeeded',
55
- PAYER_ACTION_REQUIRED: 'requires_action',
56
- };
57
-
58
- const status = statusMap[order.status ?? statusMap.CREATED];
59
-
60
- return {
61
- id: order.id!,
62
- status,
63
- amount: parseFloat(order.purchaseUnits?.[0]?.amount?.value || '0'),
64
- currency: order.purchaseUnits?.[0]?.amount?.currencyCode || 'USD',
65
- metadata: order.purchaseUnits?.[0]?.customId
66
- ? omitInternalMetadata(JSON.parse(order.purchaseUnits?.[0]?.customId))
67
- : {},
68
- customer: order.payer?.payerId
69
- ? order.payer?.payerId
70
- : { email: order.payer?.emailAddress ?? '' },
71
- item_id: order.purchaseUnits?.[0]?.items?.[0]?.sku || '',
72
- };
73
- };
74
-
75
- /**
76
- * Subscription
77
- */
78
- export const paykitSubscription$InboundSchema = (
79
- subscription: PayPalSubscription,
80
- ): Subscription => {
81
- const statusMap: Record<string, Subscription['status']> = {
82
- APPROVAL_PENDING: 'pending',
83
- APPROVED: 'active',
84
- ACTIVE: 'active',
85
- SUSPENDED: 'past_due',
86
- CANCELLED: 'canceled',
87
- EXPIRED: 'canceled',
88
- };
89
-
90
- const status = statusMap[subscription.status ?? statusMap.APPROVAL_PENDING];
91
-
92
- return {
93
- id: subscription.id,
94
- customer: { email: subscription.subscriber?.email_address ?? '' },
95
- status,
96
- item_id: subscription.plan_id,
97
- current_period_start: subscription.start_time
98
- ? new Date(subscription.start_time)
99
- : new Date(),
100
- current_period_end: subscription.status_update_time
101
- ? new Date(subscription.status_update_time)
102
- : new Date(), // todo: Would need to calculate based on billing cycle
103
- metadata: subscription.customId
104
- ? omitInternalMetadata(JSON.parse(subscription.customId))
105
- : {},
106
- billing_interval: 'month',
107
- amount: 0,
108
- currency: 'USD',
109
- custom_fields: null,
110
- };
111
- };
package/tsconfig.json DELETED
@@ -1,17 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2022",
4
- "module": "ESNext",
5
- "moduleResolution": "bundler",
6
- "declaration": true,
7
- "outDir": "./dist",
8
- "rootDir": "./src",
9
- "strict": true,
10
- "esModuleInterop": true,
11
- "skipLibCheck": true,
12
- "forceConsistentCasingInFileNames": true,
13
- "resolveJsonModule": true
14
- },
15
- "include": ["src/**/*"],
16
- "exclude": ["node_modules", "dist"]
17
- }