@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/dist/controllers/subscription.d.mts +37 -0
- package/dist/controllers/subscription.d.ts +37 -0
- package/dist/controllers/subscription.js +179 -0
- package/dist/controllers/subscription.mjs +177 -0
- package/dist/controllers/webhook.d.mts +18 -0
- package/dist/controllers/webhook.d.ts +18 -0
- package/dist/controllers/webhook.js +122 -0
- package/dist/controllers/webhook.mjs +120 -0
- package/dist/index.d.mts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +4669 -0
- package/dist/index.mjs +4666 -0
- package/dist/paypal-provider.d.mts +66 -0
- package/dist/paypal-provider.d.ts +66 -0
- package/dist/paypal-provider.js +4651 -0
- package/dist/paypal-provider.mjs +4649 -0
- package/dist/schema.d.mts +138 -0
- package/dist/schema.d.ts +138 -0
- package/dist/schema.js +92 -0
- package/dist/schema.mjs +85 -0
- package/dist/types.d.mts +108 -0
- package/dist/types.d.ts +108 -0
- package/dist/types.js +2 -0
- package/dist/types.mjs +1 -0
- package/dist/utils/mapper.d.mts +22 -0
- package/dist/utils/mapper.d.ts +22 -0
- package/dist/utils/mapper.js +77 -0
- package/dist/utils/mapper.mjs +72 -0
- package/package.json +35 -13
- package/CHANGELOG.md +0 -9
- package/src/controllers/subscription.ts +0 -118
- package/src/controllers/webhook.ts +0 -45
- package/src/index.ts +0 -20
- package/src/paypal-provider.ts +0 -504
- package/src/schema.ts +0 -230
- package/src/types.ts +0 -126
- package/src/utils/mapper.ts +0 -111
- package/tsconfig.json +0 -17
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Subscription } from '../schema.mjs';
|
|
2
|
+
import { CreateSubscriptionSchema } from '@paykit-sdk/core';
|
|
3
|
+
import { ApiResponse } from '@paypal/paypal-server-sdk';
|
|
4
|
+
import { BaseController } from '@paypal/paypal-server-sdk/dist/types/controllers/baseController';
|
|
5
|
+
import '@paypal/paypal-server-sdk/dist/types/schema';
|
|
6
|
+
|
|
7
|
+
declare class SubscriptionsController extends BaseController {
|
|
8
|
+
private catchAllErrors;
|
|
9
|
+
/**
|
|
10
|
+
* @return Response from the API call
|
|
11
|
+
*/
|
|
12
|
+
createSubscription({ body, }: {
|
|
13
|
+
body: CreateSubscriptionSchema;
|
|
14
|
+
}): Promise<ApiResponse<any>>;
|
|
15
|
+
/**
|
|
16
|
+
* @returns Response from the API call
|
|
17
|
+
*/
|
|
18
|
+
resumeSubscription({ body, subscriptionId, }: {
|
|
19
|
+
body: {
|
|
20
|
+
reason: string;
|
|
21
|
+
};
|
|
22
|
+
subscriptionId: string;
|
|
23
|
+
}): Promise<void>;
|
|
24
|
+
retrieveSubscription({ subscriptionId }: {
|
|
25
|
+
subscriptionId: string;
|
|
26
|
+
}): Promise<ApiResponse<Subscription>>;
|
|
27
|
+
cancelSubscription({ subscriptionId, reason, }: {
|
|
28
|
+
subscriptionId: string;
|
|
29
|
+
reason: string;
|
|
30
|
+
}): Promise<ApiResponse<Subscription>>;
|
|
31
|
+
updateSubscription({ subscriptionId, metadata, }: {
|
|
32
|
+
subscriptionId: string;
|
|
33
|
+
metadata: Record<string, unknown>;
|
|
34
|
+
}): Promise<void>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export { SubscriptionsController };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Subscription } from '../schema.js';
|
|
2
|
+
import { CreateSubscriptionSchema } from '@paykit-sdk/core';
|
|
3
|
+
import { ApiResponse } from '@paypal/paypal-server-sdk';
|
|
4
|
+
import { BaseController } from '@paypal/paypal-server-sdk/dist/types/controllers/baseController';
|
|
5
|
+
import '@paypal/paypal-server-sdk/dist/types/schema';
|
|
6
|
+
|
|
7
|
+
declare class SubscriptionsController extends BaseController {
|
|
8
|
+
private catchAllErrors;
|
|
9
|
+
/**
|
|
10
|
+
* @return Response from the API call
|
|
11
|
+
*/
|
|
12
|
+
createSubscription({ body, }: {
|
|
13
|
+
body: CreateSubscriptionSchema;
|
|
14
|
+
}): Promise<ApiResponse<any>>;
|
|
15
|
+
/**
|
|
16
|
+
* @returns Response from the API call
|
|
17
|
+
*/
|
|
18
|
+
resumeSubscription({ body, subscriptionId, }: {
|
|
19
|
+
body: {
|
|
20
|
+
reason: string;
|
|
21
|
+
};
|
|
22
|
+
subscriptionId: string;
|
|
23
|
+
}): Promise<void>;
|
|
24
|
+
retrieveSubscription({ subscriptionId }: {
|
|
25
|
+
subscriptionId: string;
|
|
26
|
+
}): Promise<ApiResponse<Subscription>>;
|
|
27
|
+
cancelSubscription({ subscriptionId, reason, }: {
|
|
28
|
+
subscriptionId: string;
|
|
29
|
+
reason: string;
|
|
30
|
+
}): Promise<ApiResponse<Subscription>>;
|
|
31
|
+
updateSubscription({ subscriptionId, metadata, }: {
|
|
32
|
+
subscriptionId: string;
|
|
33
|
+
metadata: Record<string, unknown>;
|
|
34
|
+
}): Promise<void>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export { SubscriptionsController };
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var paypalServerSdk = require('@paypal/paypal-server-sdk');
|
|
4
|
+
var baseController = require('@paypal/paypal-server-sdk/dist/types/controllers/baseController');
|
|
5
|
+
var schema = require('@paypal/paypal-server-sdk/dist/types/schema');
|
|
6
|
+
|
|
7
|
+
// src/controllers/subscription.ts
|
|
8
|
+
schema.object({
|
|
9
|
+
currency_code: ["currency_code", schema.string()],
|
|
10
|
+
value: ["value", schema.string()]
|
|
11
|
+
});
|
|
12
|
+
var createSubscriptionApticSchema = schema.object({
|
|
13
|
+
plan_id: ["plan_id", schema.string()],
|
|
14
|
+
quantity: ["quantity", schema.number()],
|
|
15
|
+
custom_id: ["custom_id", schema.string()],
|
|
16
|
+
start_time: ["start_time", schema.string()],
|
|
17
|
+
subscriber: [
|
|
18
|
+
"subscriber",
|
|
19
|
+
schema.lazy(
|
|
20
|
+
() => schema.object({
|
|
21
|
+
email_address: ["email_address", schema.string()],
|
|
22
|
+
name: [
|
|
23
|
+
"name",
|
|
24
|
+
schema.lazy(
|
|
25
|
+
() => schema.object({
|
|
26
|
+
given_name: ["given_name", schema.string()],
|
|
27
|
+
surname: ["surname", schema.string()]
|
|
28
|
+
})
|
|
29
|
+
)
|
|
30
|
+
],
|
|
31
|
+
phone: [
|
|
32
|
+
"phone",
|
|
33
|
+
schema.lazy(() => schema.object({ phone_number: ["phone_number", schema.string()] }))
|
|
34
|
+
]
|
|
35
|
+
})
|
|
36
|
+
)
|
|
37
|
+
]
|
|
38
|
+
});
|
|
39
|
+
var SubscriptionStatus = /* @__PURE__ */ ((SubscriptionStatus2) => {
|
|
40
|
+
SubscriptionStatus2["APPROVAL_PENDING"] = "APPROVAL_PENDING";
|
|
41
|
+
SubscriptionStatus2["APPROVED"] = "APPROVED";
|
|
42
|
+
SubscriptionStatus2["ACTIVE"] = "ACTIVE";
|
|
43
|
+
SubscriptionStatus2["SUSPENDED"] = "SUSPENDED";
|
|
44
|
+
SubscriptionStatus2["CANCELLED"] = "CANCELLED";
|
|
45
|
+
SubscriptionStatus2["EXPIRED"] = "EXPIRED";
|
|
46
|
+
return SubscriptionStatus2;
|
|
47
|
+
})(SubscriptionStatus || {});
|
|
48
|
+
var subscriptionApticSchema = schema.object({
|
|
49
|
+
id: ["id", schema.string()],
|
|
50
|
+
plan_id: ["plan_id", schema.string()],
|
|
51
|
+
quantity: ["quantity", schema.number()],
|
|
52
|
+
custom_id: ["custom_id", schema.string()],
|
|
53
|
+
plan_overridden: ["plan_overridden", schema.boolean()],
|
|
54
|
+
start_time: ["start_time", schema.string()],
|
|
55
|
+
subscriber: [
|
|
56
|
+
"subscriber",
|
|
57
|
+
schema.lazy(
|
|
58
|
+
() => schema.object({
|
|
59
|
+
email_address: ["email_address", schema.string()],
|
|
60
|
+
name: [
|
|
61
|
+
"name",
|
|
62
|
+
schema.lazy(
|
|
63
|
+
() => schema.object({
|
|
64
|
+
given_name: ["given_name", schema.string()],
|
|
65
|
+
surname: ["surname", schema.string()]
|
|
66
|
+
})
|
|
67
|
+
)
|
|
68
|
+
],
|
|
69
|
+
payer_id: ["payer_id", schema.string()]
|
|
70
|
+
})
|
|
71
|
+
)
|
|
72
|
+
],
|
|
73
|
+
status: ["status", schema.stringEnum(SubscriptionStatus)],
|
|
74
|
+
status_change_note: ["status_change_note", schema.string()],
|
|
75
|
+
status_update_time: ["status_update_time", schema.string()]
|
|
76
|
+
});
|
|
77
|
+
var resumeSubscriptionApticSchemaRequest = schema.object({
|
|
78
|
+
reason: ["reason", schema.string()]
|
|
79
|
+
});
|
|
80
|
+
var VerifyWebhookStatus = /* @__PURE__ */ ((VerifyWebhookStatus2) => {
|
|
81
|
+
VerifyWebhookStatus2["SUCCESS"] = "SUCCESS";
|
|
82
|
+
VerifyWebhookStatus2["FAILURE"] = "FAILURE";
|
|
83
|
+
return VerifyWebhookStatus2;
|
|
84
|
+
})(VerifyWebhookStatus || {});
|
|
85
|
+
schema.object({
|
|
86
|
+
verification_status: ["verification_status", schema.stringEnum(VerifyWebhookStatus)]
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// src/controllers/subscription.ts
|
|
90
|
+
var SubscriptionsController = class extends baseController.BaseController {
|
|
91
|
+
catchAllErrors(req) {
|
|
92
|
+
req.throwOn(
|
|
93
|
+
400,
|
|
94
|
+
paypalServerSdk.CustomError,
|
|
95
|
+
"Request is not well-formed, syntactically incorrect, or violates schema."
|
|
96
|
+
);
|
|
97
|
+
req.throwOn(
|
|
98
|
+
401,
|
|
99
|
+
paypalServerSdk.CustomError,
|
|
100
|
+
"Authentication failed due to missing authorization header, or invalid authentication credentials."
|
|
101
|
+
);
|
|
102
|
+
req.throwOn(
|
|
103
|
+
422,
|
|
104
|
+
paypalServerSdk.CustomError,
|
|
105
|
+
"The requested action could not be performed, semantically incorrect, or failed business validation."
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* @return Response from the API call
|
|
110
|
+
*/
|
|
111
|
+
async createSubscription({
|
|
112
|
+
body
|
|
113
|
+
}) {
|
|
114
|
+
const req = this.createRequest("POST", "/v1/billing/subscriptions");
|
|
115
|
+
const mapped = req.prepareArgs({ body: [body, createSubscriptionApticSchema] });
|
|
116
|
+
req.header("Content-Type", "application/json");
|
|
117
|
+
req.header("PayPal-Request-Id", Math.random().toString(36).substring(2, 15));
|
|
118
|
+
req.json(mapped.body);
|
|
119
|
+
this.catchAllErrors(req);
|
|
120
|
+
req.authenticate([{ oauth2: true }]);
|
|
121
|
+
return req.callAsJson(subscriptionApticSchema);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* @returns Response from the API call
|
|
125
|
+
*/
|
|
126
|
+
async resumeSubscription({
|
|
127
|
+
body,
|
|
128
|
+
subscriptionId
|
|
129
|
+
}) {
|
|
130
|
+
const req = this.createRequest(
|
|
131
|
+
"POST",
|
|
132
|
+
`v1/billing/subscriptions/${subscriptionId}/activate `
|
|
133
|
+
);
|
|
134
|
+
const mapped = req.prepareArgs({
|
|
135
|
+
body: [body, resumeSubscriptionApticSchemaRequest]
|
|
136
|
+
});
|
|
137
|
+
req.header("Content-Type", "application/json");
|
|
138
|
+
req.header("PayPal-Request-Id", Math.random().toString(36).substring(2, 15));
|
|
139
|
+
req.json(mapped.body);
|
|
140
|
+
this.catchAllErrors(req);
|
|
141
|
+
req.authenticate([{ oauth2: true }]);
|
|
142
|
+
}
|
|
143
|
+
async retrieveSubscription({ subscriptionId }) {
|
|
144
|
+
const req = this.createRequest("GET", `v1/billing/subscriptions/${subscriptionId}`);
|
|
145
|
+
req.header("PayPal-Request-Id", Math.random().toString(36).substring(2, 15));
|
|
146
|
+
this.catchAllErrors(req);
|
|
147
|
+
req.authenticate([{ oauth2: true }]);
|
|
148
|
+
return req.callAsJson(subscriptionApticSchema);
|
|
149
|
+
}
|
|
150
|
+
async cancelSubscription({
|
|
151
|
+
subscriptionId,
|
|
152
|
+
reason
|
|
153
|
+
}) {
|
|
154
|
+
const req = this.createRequest(
|
|
155
|
+
"POST",
|
|
156
|
+
`v1/billing/subscriptions/${subscriptionId}/cancel`
|
|
157
|
+
);
|
|
158
|
+
const mapped = req.prepareArgs({
|
|
159
|
+
body: [reason, resumeSubscriptionApticSchemaRequest]
|
|
160
|
+
});
|
|
161
|
+
req.header("PayPal-Request-Id", Math.random().toString(36).substring(2, 15));
|
|
162
|
+
req.json(mapped.body);
|
|
163
|
+
this.catchAllErrors(req);
|
|
164
|
+
req.authenticate([{ oauth2: true }]);
|
|
165
|
+
return req.callAsJson(subscriptionApticSchema);
|
|
166
|
+
}
|
|
167
|
+
async updateSubscription({
|
|
168
|
+
subscriptionId,
|
|
169
|
+
metadata
|
|
170
|
+
}) {
|
|
171
|
+
const req = this.createRequest("PATCH", `v1/billing/subscriptions/${subscriptionId}`);
|
|
172
|
+
req.header("PayPal-Request-Id", Math.random().toString(36).substring(2, 15));
|
|
173
|
+
req.json({ op: "replace", path: "/custom_id", value: JSON.stringify(metadata) });
|
|
174
|
+
this.catchAllErrors(req);
|
|
175
|
+
req.authenticate([{ oauth2: true }]);
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
exports.SubscriptionsController = SubscriptionsController;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { CustomError } from '@paypal/paypal-server-sdk';
|
|
2
|
+
import { BaseController } from '@paypal/paypal-server-sdk/dist/types/controllers/baseController';
|
|
3
|
+
import { object, string, lazy, number, stringEnum, boolean } from '@paypal/paypal-server-sdk/dist/types/schema';
|
|
4
|
+
|
|
5
|
+
// src/controllers/subscription.ts
|
|
6
|
+
object({
|
|
7
|
+
currency_code: ["currency_code", string()],
|
|
8
|
+
value: ["value", string()]
|
|
9
|
+
});
|
|
10
|
+
var createSubscriptionApticSchema = object({
|
|
11
|
+
plan_id: ["plan_id", string()],
|
|
12
|
+
quantity: ["quantity", number()],
|
|
13
|
+
custom_id: ["custom_id", string()],
|
|
14
|
+
start_time: ["start_time", string()],
|
|
15
|
+
subscriber: [
|
|
16
|
+
"subscriber",
|
|
17
|
+
lazy(
|
|
18
|
+
() => object({
|
|
19
|
+
email_address: ["email_address", string()],
|
|
20
|
+
name: [
|
|
21
|
+
"name",
|
|
22
|
+
lazy(
|
|
23
|
+
() => object({
|
|
24
|
+
given_name: ["given_name", string()],
|
|
25
|
+
surname: ["surname", string()]
|
|
26
|
+
})
|
|
27
|
+
)
|
|
28
|
+
],
|
|
29
|
+
phone: [
|
|
30
|
+
"phone",
|
|
31
|
+
lazy(() => object({ phone_number: ["phone_number", string()] }))
|
|
32
|
+
]
|
|
33
|
+
})
|
|
34
|
+
)
|
|
35
|
+
]
|
|
36
|
+
});
|
|
37
|
+
var SubscriptionStatus = /* @__PURE__ */ ((SubscriptionStatus2) => {
|
|
38
|
+
SubscriptionStatus2["APPROVAL_PENDING"] = "APPROVAL_PENDING";
|
|
39
|
+
SubscriptionStatus2["APPROVED"] = "APPROVED";
|
|
40
|
+
SubscriptionStatus2["ACTIVE"] = "ACTIVE";
|
|
41
|
+
SubscriptionStatus2["SUSPENDED"] = "SUSPENDED";
|
|
42
|
+
SubscriptionStatus2["CANCELLED"] = "CANCELLED";
|
|
43
|
+
SubscriptionStatus2["EXPIRED"] = "EXPIRED";
|
|
44
|
+
return SubscriptionStatus2;
|
|
45
|
+
})(SubscriptionStatus || {});
|
|
46
|
+
var subscriptionApticSchema = object({
|
|
47
|
+
id: ["id", string()],
|
|
48
|
+
plan_id: ["plan_id", string()],
|
|
49
|
+
quantity: ["quantity", number()],
|
|
50
|
+
custom_id: ["custom_id", string()],
|
|
51
|
+
plan_overridden: ["plan_overridden", boolean()],
|
|
52
|
+
start_time: ["start_time", string()],
|
|
53
|
+
subscriber: [
|
|
54
|
+
"subscriber",
|
|
55
|
+
lazy(
|
|
56
|
+
() => object({
|
|
57
|
+
email_address: ["email_address", string()],
|
|
58
|
+
name: [
|
|
59
|
+
"name",
|
|
60
|
+
lazy(
|
|
61
|
+
() => object({
|
|
62
|
+
given_name: ["given_name", string()],
|
|
63
|
+
surname: ["surname", string()]
|
|
64
|
+
})
|
|
65
|
+
)
|
|
66
|
+
],
|
|
67
|
+
payer_id: ["payer_id", string()]
|
|
68
|
+
})
|
|
69
|
+
)
|
|
70
|
+
],
|
|
71
|
+
status: ["status", stringEnum(SubscriptionStatus)],
|
|
72
|
+
status_change_note: ["status_change_note", string()],
|
|
73
|
+
status_update_time: ["status_update_time", string()]
|
|
74
|
+
});
|
|
75
|
+
var resumeSubscriptionApticSchemaRequest = object({
|
|
76
|
+
reason: ["reason", string()]
|
|
77
|
+
});
|
|
78
|
+
var VerifyWebhookStatus = /* @__PURE__ */ ((VerifyWebhookStatus2) => {
|
|
79
|
+
VerifyWebhookStatus2["SUCCESS"] = "SUCCESS";
|
|
80
|
+
VerifyWebhookStatus2["FAILURE"] = "FAILURE";
|
|
81
|
+
return VerifyWebhookStatus2;
|
|
82
|
+
})(VerifyWebhookStatus || {});
|
|
83
|
+
object({
|
|
84
|
+
verification_status: ["verification_status", stringEnum(VerifyWebhookStatus)]
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// src/controllers/subscription.ts
|
|
88
|
+
var SubscriptionsController = class extends BaseController {
|
|
89
|
+
catchAllErrors(req) {
|
|
90
|
+
req.throwOn(
|
|
91
|
+
400,
|
|
92
|
+
CustomError,
|
|
93
|
+
"Request is not well-formed, syntactically incorrect, or violates schema."
|
|
94
|
+
);
|
|
95
|
+
req.throwOn(
|
|
96
|
+
401,
|
|
97
|
+
CustomError,
|
|
98
|
+
"Authentication failed due to missing authorization header, or invalid authentication credentials."
|
|
99
|
+
);
|
|
100
|
+
req.throwOn(
|
|
101
|
+
422,
|
|
102
|
+
CustomError,
|
|
103
|
+
"The requested action could not be performed, semantically incorrect, or failed business validation."
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* @return Response from the API call
|
|
108
|
+
*/
|
|
109
|
+
async createSubscription({
|
|
110
|
+
body
|
|
111
|
+
}) {
|
|
112
|
+
const req = this.createRequest("POST", "/v1/billing/subscriptions");
|
|
113
|
+
const mapped = req.prepareArgs({ body: [body, createSubscriptionApticSchema] });
|
|
114
|
+
req.header("Content-Type", "application/json");
|
|
115
|
+
req.header("PayPal-Request-Id", Math.random().toString(36).substring(2, 15));
|
|
116
|
+
req.json(mapped.body);
|
|
117
|
+
this.catchAllErrors(req);
|
|
118
|
+
req.authenticate([{ oauth2: true }]);
|
|
119
|
+
return req.callAsJson(subscriptionApticSchema);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* @returns Response from the API call
|
|
123
|
+
*/
|
|
124
|
+
async resumeSubscription({
|
|
125
|
+
body,
|
|
126
|
+
subscriptionId
|
|
127
|
+
}) {
|
|
128
|
+
const req = this.createRequest(
|
|
129
|
+
"POST",
|
|
130
|
+
`v1/billing/subscriptions/${subscriptionId}/activate `
|
|
131
|
+
);
|
|
132
|
+
const mapped = req.prepareArgs({
|
|
133
|
+
body: [body, resumeSubscriptionApticSchemaRequest]
|
|
134
|
+
});
|
|
135
|
+
req.header("Content-Type", "application/json");
|
|
136
|
+
req.header("PayPal-Request-Id", Math.random().toString(36).substring(2, 15));
|
|
137
|
+
req.json(mapped.body);
|
|
138
|
+
this.catchAllErrors(req);
|
|
139
|
+
req.authenticate([{ oauth2: true }]);
|
|
140
|
+
}
|
|
141
|
+
async retrieveSubscription({ subscriptionId }) {
|
|
142
|
+
const req = this.createRequest("GET", `v1/billing/subscriptions/${subscriptionId}`);
|
|
143
|
+
req.header("PayPal-Request-Id", Math.random().toString(36).substring(2, 15));
|
|
144
|
+
this.catchAllErrors(req);
|
|
145
|
+
req.authenticate([{ oauth2: true }]);
|
|
146
|
+
return req.callAsJson(subscriptionApticSchema);
|
|
147
|
+
}
|
|
148
|
+
async cancelSubscription({
|
|
149
|
+
subscriptionId,
|
|
150
|
+
reason
|
|
151
|
+
}) {
|
|
152
|
+
const req = this.createRequest(
|
|
153
|
+
"POST",
|
|
154
|
+
`v1/billing/subscriptions/${subscriptionId}/cancel`
|
|
155
|
+
);
|
|
156
|
+
const mapped = req.prepareArgs({
|
|
157
|
+
body: [reason, resumeSubscriptionApticSchemaRequest]
|
|
158
|
+
});
|
|
159
|
+
req.header("PayPal-Request-Id", Math.random().toString(36).substring(2, 15));
|
|
160
|
+
req.json(mapped.body);
|
|
161
|
+
this.catchAllErrors(req);
|
|
162
|
+
req.authenticate([{ oauth2: true }]);
|
|
163
|
+
return req.callAsJson(subscriptionApticSchema);
|
|
164
|
+
}
|
|
165
|
+
async updateSubscription({
|
|
166
|
+
subscriptionId,
|
|
167
|
+
metadata
|
|
168
|
+
}) {
|
|
169
|
+
const req = this.createRequest("PATCH", `v1/billing/subscriptions/${subscriptionId}`);
|
|
170
|
+
req.header("PayPal-Request-Id", Math.random().toString(36).substring(2, 15));
|
|
171
|
+
req.json({ op: "replace", path: "/custom_id", value: JSON.stringify(metadata) });
|
|
172
|
+
this.catchAllErrors(req);
|
|
173
|
+
req.authenticate([{ oauth2: true }]);
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
export { SubscriptionsController };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ApiResponse } from '@paypal/paypal-server-sdk';
|
|
2
|
+
import { BaseController } from '@paypal/paypal-server-sdk/dist/types/controllers/baseController';
|
|
3
|
+
import { VerifyWebhookSchema } from '../schema.mjs';
|
|
4
|
+
import '@paypal/paypal-server-sdk/dist/types/schema';
|
|
5
|
+
|
|
6
|
+
declare class WebhookController extends BaseController {
|
|
7
|
+
verifyWebhook(body: {
|
|
8
|
+
authAlgo: string;
|
|
9
|
+
certUrl: string;
|
|
10
|
+
transmissionId: string;
|
|
11
|
+
transmissionSig: string;
|
|
12
|
+
transmissionTime: string;
|
|
13
|
+
webhookId: string;
|
|
14
|
+
webhookEvent: string;
|
|
15
|
+
}): Promise<ApiResponse<VerifyWebhookSchema>>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { WebhookController };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ApiResponse } from '@paypal/paypal-server-sdk';
|
|
2
|
+
import { BaseController } from '@paypal/paypal-server-sdk/dist/types/controllers/baseController';
|
|
3
|
+
import { VerifyWebhookSchema } from '../schema.js';
|
|
4
|
+
import '@paypal/paypal-server-sdk/dist/types/schema';
|
|
5
|
+
|
|
6
|
+
declare class WebhookController extends BaseController {
|
|
7
|
+
verifyWebhook(body: {
|
|
8
|
+
authAlgo: string;
|
|
9
|
+
certUrl: string;
|
|
10
|
+
transmissionId: string;
|
|
11
|
+
transmissionSig: string;
|
|
12
|
+
transmissionTime: string;
|
|
13
|
+
webhookId: string;
|
|
14
|
+
webhookEvent: string;
|
|
15
|
+
}): Promise<ApiResponse<VerifyWebhookSchema>>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { WebhookController };
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var paypalServerSdk = require('@paypal/paypal-server-sdk');
|
|
4
|
+
var baseController = require('@paypal/paypal-server-sdk/dist/types/controllers/baseController');
|
|
5
|
+
var schema = require('@paypal/paypal-server-sdk/dist/types/schema');
|
|
6
|
+
|
|
7
|
+
// src/controllers/webhook.ts
|
|
8
|
+
schema.object({
|
|
9
|
+
currency_code: ["currency_code", schema.string()],
|
|
10
|
+
value: ["value", schema.string()]
|
|
11
|
+
});
|
|
12
|
+
schema.object({
|
|
13
|
+
plan_id: ["plan_id", schema.string()],
|
|
14
|
+
quantity: ["quantity", schema.number()],
|
|
15
|
+
custom_id: ["custom_id", schema.string()],
|
|
16
|
+
start_time: ["start_time", schema.string()],
|
|
17
|
+
subscriber: [
|
|
18
|
+
"subscriber",
|
|
19
|
+
schema.lazy(
|
|
20
|
+
() => schema.object({
|
|
21
|
+
email_address: ["email_address", schema.string()],
|
|
22
|
+
name: [
|
|
23
|
+
"name",
|
|
24
|
+
schema.lazy(
|
|
25
|
+
() => schema.object({
|
|
26
|
+
given_name: ["given_name", schema.string()],
|
|
27
|
+
surname: ["surname", schema.string()]
|
|
28
|
+
})
|
|
29
|
+
)
|
|
30
|
+
],
|
|
31
|
+
phone: [
|
|
32
|
+
"phone",
|
|
33
|
+
schema.lazy(() => schema.object({ phone_number: ["phone_number", schema.string()] }))
|
|
34
|
+
]
|
|
35
|
+
})
|
|
36
|
+
)
|
|
37
|
+
]
|
|
38
|
+
});
|
|
39
|
+
var SubscriptionStatus = /* @__PURE__ */ ((SubscriptionStatus2) => {
|
|
40
|
+
SubscriptionStatus2["APPROVAL_PENDING"] = "APPROVAL_PENDING";
|
|
41
|
+
SubscriptionStatus2["APPROVED"] = "APPROVED";
|
|
42
|
+
SubscriptionStatus2["ACTIVE"] = "ACTIVE";
|
|
43
|
+
SubscriptionStatus2["SUSPENDED"] = "SUSPENDED";
|
|
44
|
+
SubscriptionStatus2["CANCELLED"] = "CANCELLED";
|
|
45
|
+
SubscriptionStatus2["EXPIRED"] = "EXPIRED";
|
|
46
|
+
return SubscriptionStatus2;
|
|
47
|
+
})(SubscriptionStatus || {});
|
|
48
|
+
schema.object({
|
|
49
|
+
id: ["id", schema.string()],
|
|
50
|
+
plan_id: ["plan_id", schema.string()],
|
|
51
|
+
quantity: ["quantity", schema.number()],
|
|
52
|
+
custom_id: ["custom_id", schema.string()],
|
|
53
|
+
plan_overridden: ["plan_overridden", schema.boolean()],
|
|
54
|
+
start_time: ["start_time", schema.string()],
|
|
55
|
+
subscriber: [
|
|
56
|
+
"subscriber",
|
|
57
|
+
schema.lazy(
|
|
58
|
+
() => schema.object({
|
|
59
|
+
email_address: ["email_address", schema.string()],
|
|
60
|
+
name: [
|
|
61
|
+
"name",
|
|
62
|
+
schema.lazy(
|
|
63
|
+
() => schema.object({
|
|
64
|
+
given_name: ["given_name", schema.string()],
|
|
65
|
+
surname: ["surname", schema.string()]
|
|
66
|
+
})
|
|
67
|
+
)
|
|
68
|
+
],
|
|
69
|
+
payer_id: ["payer_id", schema.string()]
|
|
70
|
+
})
|
|
71
|
+
)
|
|
72
|
+
],
|
|
73
|
+
status: ["status", schema.stringEnum(SubscriptionStatus)],
|
|
74
|
+
status_change_note: ["status_change_note", schema.string()],
|
|
75
|
+
status_update_time: ["status_update_time", schema.string()]
|
|
76
|
+
});
|
|
77
|
+
schema.object({
|
|
78
|
+
reason: ["reason", schema.string()]
|
|
79
|
+
});
|
|
80
|
+
var VerifyWebhookStatus = /* @__PURE__ */ ((VerifyWebhookStatus2) => {
|
|
81
|
+
VerifyWebhookStatus2["SUCCESS"] = "SUCCESS";
|
|
82
|
+
VerifyWebhookStatus2["FAILURE"] = "FAILURE";
|
|
83
|
+
return VerifyWebhookStatus2;
|
|
84
|
+
})(VerifyWebhookStatus || {});
|
|
85
|
+
var verifyWebhookSchema = schema.object({
|
|
86
|
+
verification_status: ["verification_status", schema.stringEnum(VerifyWebhookStatus)]
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// src/controllers/webhook.ts
|
|
90
|
+
var WebhookController = class extends baseController.BaseController {
|
|
91
|
+
async verifyWebhook(body) {
|
|
92
|
+
const req = this.createRequest("POST", "/v1/notifications/verify-webhook-signature");
|
|
93
|
+
req.header("Content-Type", "application/json");
|
|
94
|
+
req.throwOn(
|
|
95
|
+
400,
|
|
96
|
+
paypalServerSdk.CustomError,
|
|
97
|
+
"Request is not well-formed, syntactically incorrect, or violates schema."
|
|
98
|
+
);
|
|
99
|
+
req.throwOn(
|
|
100
|
+
401,
|
|
101
|
+
paypalServerSdk.CustomError,
|
|
102
|
+
"Authentication failed due to missing authorization header, or invalid authentication credentials."
|
|
103
|
+
);
|
|
104
|
+
req.throwOn(
|
|
105
|
+
422,
|
|
106
|
+
paypalServerSdk.CustomError,
|
|
107
|
+
"The requested action could not be performed, semantically incorrect, or failed business validation."
|
|
108
|
+
);
|
|
109
|
+
req.json(
|
|
110
|
+
Object.fromEntries(
|
|
111
|
+
Object.entries(body).map(([key, value]) => [
|
|
112
|
+
key.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`),
|
|
113
|
+
value
|
|
114
|
+
])
|
|
115
|
+
)
|
|
116
|
+
);
|
|
117
|
+
req.authenticate([{ oauth2: true }]);
|
|
118
|
+
return req.callAsJson(verifyWebhookSchema);
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
exports.WebhookController = WebhookController;
|