Skip to content

Commit 497eca1

Browse files
committed
refactor(api): create base urls for vendor and checkout api
1 parent cfee2b2 commit 497eca1

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

lib/paddle/api.js

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
'use strict'
22

3-
const SANDBOX_BASE_URL = 'https://sandbox-vendors.paddle.com/api/'
4-
const BASE_URL = 'https://vendors.paddle.com/api/'
3+
const VENDORS_SANDBOX_BASE_URL = 'https://sandbox-vendors.paddle.com/api/'
4+
const VENDORS_BASE_URL = 'https://vendors.paddle.com/api/'
55

6-
const VERSION_1 = 1.0
7-
const VERSION_2 = 2.0
6+
const CHECKOUT_SANDBOX_BASE_URL = 'https://sandbox-checkout.paddle.com/api/'
7+
const CHECKOUT_BASE_URL = 'https://checkout.paddle.com/api/'
8+
9+
const VERSION_1 = '1.0'
10+
const VERSION_2 = '2.0'
811

912
const PATH_CANCEL_SUBSCRIPTION = `${VERSION_2}/subscription/users_cancel`
1013
const PATH_LIST_USERS = `${VERSION_2}/subscription/users`
@@ -21,14 +24,13 @@ module.exports = class {
2124
} = {}) {
2225
this._vendorId = vendorId
2326
this._authCode = authCode
27+
this._vendorsBaseUrl = useSandbox ? VENDORS_SANDBOX_BASE_URL : VENDORS_BASE_URL
2428
this._baseUrl = useSandbox ? SANDBOX_BASE_URL : BASE_URL
2529
}
2630

2731
async init({ connectTimeout = 5_000, readTimeout = 20_000, retries = 0 } = {}) {
2832
if (!this._got) {
2933
this._got = (await import('got')).default.extend({
30-
prefixUrl: this._baseUrl,
31-
method: 'POST',
3234
retry: {
3335
limit: retries
3436
},
@@ -55,7 +57,7 @@ module.exports = class {
5557
* @returns
5658
*/
5759
async refundFullPayment({ order_id }) {
58-
return this._returnResponseIf200(this._request(PATH_LIST_USERS, {
60+
return this._returnResponseIf200(this._request(this._vendorsBaseUrl + PATH_LIST_USERS, {
5961
form: {
6062
vendor_id: this._vendorId,
6163
vendor_auth_code: this._authCode,
@@ -83,7 +85,7 @@ module.exports = class {
8385
if (perPage) {
8486
form.results_per_page = perPage
8587
}
86-
return this._returnResponseIf200(this._request(PATH_LIST_USERS, {
88+
return this._returnResponseIf200(this._request(this._vendorsBaseUrl + PATH_LIST_USERS, {
8789
form
8890
}))
8991
}
@@ -100,13 +102,13 @@ module.exports = class {
100102
subscription_id
101103
}
102104

103-
return this._returnResponseIf200(this._request(PATH_LIST_USERS, {
105+
return this._returnResponseIf200(this._request(this._vendorsBaseUrl + PATH_LIST_USERS, {
104106
form
105107
}))
106108
.catch((e) => {
107109
if (e.message.includes('119')) {
108110
form.state = 'deleted'
109-
return this._returnResponseIf200(this._request(PATH_LIST_USERS, {
111+
return this._returnResponseIf200(this._request(this._vendorsBaseUrl + PATH_LIST_USERS, {
110112
form
111113
}))
112114
} else {
@@ -155,7 +157,7 @@ module.exports = class {
155157
if (plan) {
156158
form.plan = plan
157159
}
158-
return this._returnResponseIf200(this._request(PATH_LIST_PLANS, {
160+
return this._returnResponseIf200(this._request(this._vendorsBaseUrl + PATH_LIST_PLANS, {
159161
form
160162
}))
161163
}
@@ -165,7 +167,7 @@ module.exports = class {
165167
* @returns
166168
*/
167169
async listProducts() {
168-
return this._returnResponseIf200(this._request(PATH_LIST_PRODUCTS, {
170+
return this._returnResponseIf200(this._request(this._vendorsBaseUrl + PATH_LIST_PRODUCTS, {
169171
form: {
170172
vendor_id: this._vendorId,
171173
vendor_auth_code: this._authCode
@@ -180,7 +182,7 @@ module.exports = class {
180182
* @returns
181183
*/
182184
async updateSubscriptionPlan({ subscription_id }, planId) {
183-
return this._returnResponseIf200(this._request(PATH_UPDATE_USERS, {
185+
return this._returnResponseIf200(this._request(this._vendorsBaseUrl + PATH_UPDATE_USERS, {
184186
form: {
185187
vendor_id: this._vendorId,
186188
vendor_auth_code: this._authCode,
@@ -199,7 +201,7 @@ module.exports = class {
199201
* @returns
200202
*/
201203
async updatePostcode({ subscription_id }, postcode) {
202-
return this._returnResponseIf200(this._request(this._baseUrl, {
204+
return this._returnResponseIf200(this._request(this._vendorsBaseUrl + this._vendorsBaseUrl, {
203205
form: {
204206
vendor_id: this._vendorId,
205207
vendor_auth_code: this._authCode,
@@ -215,7 +217,7 @@ module.exports = class {
215217
* @returns
216218
*/
217219
async cancelSubscription({ subscription_id }) {
218-
return this._returnResponseIf200(this._request(PATH_CANCEL_SUBSCRIPTION, {
220+
return this._returnResponseIf200(this._request(this._vendorsBaseUrl + PATH_CANCEL_SUBSCRIPTION, {
219221
form: {
220222
vendor_id: this._vendorId,
221223
vendor_auth_code: this._authCode,

0 commit comments

Comments
 (0)