payrex-node 0.1.0
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/.prettierrc +4 -0
- package/CHANGELOG.md +5 -0
- package/LICENSE +21 -0
- package/README.md +76 -0
- package/eslint.config.mjs +9 -0
- package/package.json +40 -0
- package/src/ApiResource.js +5 -0
- package/src/Client.js +19 -0
- package/src/Config.js +6 -0
- package/src/Error.js +7 -0
- package/src/HttpClient.js +55 -0
- package/src/entities/CheckoutSessionEntity.js +26 -0
- package/src/entities/EventEntity.js +13 -0
- package/src/entities/ListingEntity.js +6 -0
- package/src/entities/MerchantEntity.js +12 -0
- package/src/entities/PaymentIntentEntity.js +24 -0
- package/src/entities/PaymentMethodEntity.js +14 -0
- package/src/entities/RefundEntity.js +19 -0
- package/src/entities/WebhookEntity.js +16 -0
- package/src/errors/AuthenticationInvalidError.js +12 -0
- package/src/errors/BaseError.js +13 -0
- package/src/errors/RequestInvalidError.js +9 -0
- package/src/errors/ResourceNotFoundError.js +9 -0
- package/src/errors/SignatureInvalidError.js +8 -0
- package/src/errors/ValueUnexpectedError.js +8 -0
- package/src/services/BaseService.js +18 -0
- package/src/services/CheckoutSessionService.js +60 -0
- package/src/services/MerchantService.js +21 -0
- package/src/services/PaymentIntentService.js +40 -0
- package/src/services/PaymentMethodService.js +21 -0
- package/src/services/RefundService.js +21 -0
- package/src/services/WebhookService.js +128 -0
- package/src/services/index.js +15 -0
package/.prettierrc
ADDED
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Evolut10n Labs, Inc. (https://payrexhq.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# PayRex Node.js
|
|
2
|
+
|
|
3
|
+
PayRex Node library provides Node applications an easy access to the PayRex API. Explore various classes that represents PayRex API resources on object instantiation.
|
|
4
|
+
|
|
5
|
+
## Requirements
|
|
6
|
+
|
|
7
|
+
Node 12 or higher.
|
|
8
|
+
|
|
9
|
+
## Gems
|
|
10
|
+
|
|
11
|
+
If you want to use the package, run the following command:
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm install payrex-node
|
|
15
|
+
# or
|
|
16
|
+
yarn add payrex-node
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Getting Started
|
|
20
|
+
|
|
21
|
+
Simple usage looks like:
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
const payrex = require('payrex-node')('sk_test_...');
|
|
25
|
+
|
|
26
|
+
payrex.paymentIntents.retrieve('pi_...')
|
|
27
|
+
.then(paymentIntent => console.log(paymentIntent.id))
|
|
28
|
+
|
|
29
|
+
payrex.paymentIntents.create({
|
|
30
|
+
amount: 10000,
|
|
31
|
+
currency: 'PHP',
|
|
32
|
+
description: 'Dino Treat',
|
|
33
|
+
payment_methods: ['gcash', 'card']
|
|
34
|
+
})
|
|
35
|
+
.then(paymentIntent => console.log(paymentIntent.id))
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Or using async/await:
|
|
39
|
+
|
|
40
|
+
```js
|
|
41
|
+
const payrex = require('payrex-node')('sk_test_...');
|
|
42
|
+
|
|
43
|
+
const paymentIntent = await payrex.paymentIntents.retrieve('pi_...');
|
|
44
|
+
|
|
45
|
+
console.log(paymentIntent.id);
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Handle errors
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
const payrex = require('payrex-node')('sk_test_...');
|
|
52
|
+
|
|
53
|
+
payrex.paymentIntents.create({
|
|
54
|
+
amount: 10000
|
|
55
|
+
})
|
|
56
|
+
.then(paymentIntent => console.log(paymentIntent.id))
|
|
57
|
+
.catch(error => {
|
|
58
|
+
// Handle errors
|
|
59
|
+
console.error(error.name);
|
|
60
|
+
console.error(error.errors);
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Verify webhook signature
|
|
65
|
+
|
|
66
|
+
```js
|
|
67
|
+
try {
|
|
68
|
+
payload = '{"id":"evt_...","resource":"event","type":"payment_intent.succeeded","data":{...';
|
|
69
|
+
signatureHeader = 't=1715236958,te=,li=...';
|
|
70
|
+
webhookSecretKey = 'whsk_...';
|
|
71
|
+
|
|
72
|
+
payrex.webhooks.parseEvent(payload, signatureHeader, webhookSecretKey);
|
|
73
|
+
} catch (error) {
|
|
74
|
+
// Handle invalid signature
|
|
75
|
+
}
|
|
76
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "payrex-node",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "PayRex Node JS Library",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"payrex",
|
|
7
|
+
"payment processing",
|
|
8
|
+
"credit cards",
|
|
9
|
+
"api"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/payrexhq/payrex-node",
|
|
12
|
+
"author": "PayRex <support@payrexhq.com> (https://payrexhq.com/)",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/payrexhq/payrex-node.git"
|
|
16
|
+
},
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/payrexhq/payrex-node/issues"
|
|
19
|
+
},
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=12.*"
|
|
22
|
+
},
|
|
23
|
+
"main": "src/Client.js",
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@eslint/js": "^9.3.0",
|
|
26
|
+
"eslint": "^9.3.0",
|
|
27
|
+
"eslint-config-prettier": "^9.1.0",
|
|
28
|
+
"eslint-plugin-prettier": "^5.1.3",
|
|
29
|
+
"globals": "^15.3.0",
|
|
30
|
+
"prettier": "^3.2.5"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"axios": "^1.6.8",
|
|
34
|
+
"qs": "^6.12.1"
|
|
35
|
+
},
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"scripts": {
|
|
38
|
+
"lint": "eslint ."
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/Client.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const Config = require('./Config');
|
|
2
|
+
const services = require('./services');
|
|
3
|
+
|
|
4
|
+
function initializeServices(client) {
|
|
5
|
+
for (const service in services) {
|
|
6
|
+
if (Object.prototype.hasOwnProperty.call(services, service)) {
|
|
7
|
+
client[service] = new services[service](client);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function Client(apiKey) {
|
|
13
|
+
this.config = new Config(apiKey);
|
|
14
|
+
initializeServices(this);
|
|
15
|
+
|
|
16
|
+
return this;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
module.exports = Client;
|
package/src/Config.js
ADDED
package/src/Error.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
const axios = require('axios');
|
|
2
|
+
const qs = require('qs');
|
|
3
|
+
|
|
4
|
+
const ApiResource = require('./ApiResource');
|
|
5
|
+
const RequestInvalidError = require('./errors/RequestInvalidError');
|
|
6
|
+
const AuthenticationInvalidError = require('./errors/AuthenticationInvalidError');
|
|
7
|
+
const ResourceNotFoundError = require('./errors/ResourceNotFoundError');
|
|
8
|
+
const BaseError = require('./errors/BaseError');
|
|
9
|
+
|
|
10
|
+
function HttpClient(apiKey, baseUrl) {
|
|
11
|
+
this.apiKey = apiKey;
|
|
12
|
+
this.baseUrl = baseUrl;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
HttpClient.prototype.request = async function ({ path, method, payload }) {
|
|
16
|
+
const url = `${this.baseUrl}/${path}`;
|
|
17
|
+
|
|
18
|
+
const auth = {
|
|
19
|
+
username: this.apiKey,
|
|
20
|
+
password: '',
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const headers = {
|
|
24
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
let data = null;
|
|
28
|
+
if (method === 'post' || method === 'put') {
|
|
29
|
+
data = qs.stringify(payload, { arrayFormat: 'brackets' });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
try {
|
|
33
|
+
const response = await axios.request({
|
|
34
|
+
method: method,
|
|
35
|
+
url: url,
|
|
36
|
+
auth: auth,
|
|
37
|
+
headers: headers,
|
|
38
|
+
data: data,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
return new ApiResource(response.data);
|
|
42
|
+
} catch (error) {
|
|
43
|
+
if (error.response.status === 400) {
|
|
44
|
+
throw new RequestInvalidError(error.response.data);
|
|
45
|
+
} else if (error.status === 401) {
|
|
46
|
+
throw new AuthenticationInvalidError(error.response.data);
|
|
47
|
+
} else if (error.status === 404) {
|
|
48
|
+
throw new ResourceNotFoundError(error.response.data);
|
|
49
|
+
} else {
|
|
50
|
+
throw new BaseError(error.response.data);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
module.exports = HttpClient;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
function CheckoutSessionEntity(apiResource) {
|
|
2
|
+
const data = apiResource.data;
|
|
3
|
+
|
|
4
|
+
this.id = data.id;
|
|
5
|
+
this.resource = data.resource;
|
|
6
|
+
this.customerReferenceId = data.customer_reference_id;
|
|
7
|
+
this.clientSecret = data.client_secret;
|
|
8
|
+
this.status = data.status;
|
|
9
|
+
this.currency = data.currency;
|
|
10
|
+
this.lineItems = data.line_items;
|
|
11
|
+
this.livemode = data.livemode;
|
|
12
|
+
this.url = data.url;
|
|
13
|
+
this.paymentIntent = data.payment_intent;
|
|
14
|
+
this.metadata = data.metadata;
|
|
15
|
+
this.successUrl = data.success_url;
|
|
16
|
+
this.cancelUrl = data.cancel_url;
|
|
17
|
+
this.paymentMethods = data.payment_methods;
|
|
18
|
+
this.captureType = data.capture_type;
|
|
19
|
+
this.description = data.description;
|
|
20
|
+
this.submitType = data.submit_type;
|
|
21
|
+
this.expiresAt = data.expires_at;
|
|
22
|
+
this.createdAt = data.created_at;
|
|
23
|
+
this.updatedAt = data.updated_at;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
module.exports = CheckoutSessionEntity;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
function EventEntity(apiResource) {
|
|
2
|
+
const data = apiResource.data;
|
|
3
|
+
|
|
4
|
+
this.id = data.id;
|
|
5
|
+
this.data = data.data;
|
|
6
|
+
this.type = data.type;
|
|
7
|
+
this.pendingWebhooks = data.pending_webhooks;
|
|
8
|
+
this.previousAttributes = data.previous_attributes;
|
|
9
|
+
this.createdAt = data.created_at;
|
|
10
|
+
this.updatedAt = data.updated_at;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
module.exports = EventEntity;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
function MerchantEntity(apiResource) {
|
|
2
|
+
const data = apiResource.data;
|
|
3
|
+
|
|
4
|
+
this.id = data.id;
|
|
5
|
+
this.resource = data.resource;
|
|
6
|
+
this.connectionType = data.connection_type;
|
|
7
|
+
this.livemode = data.livemode;
|
|
8
|
+
this.createdAt = data.created_at;
|
|
9
|
+
this.updatedAt = data.updated_at;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
module.exports = MerchantEntity;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
function PaymentIntentEntity(apiResource) {
|
|
2
|
+
const data = apiResource.data;
|
|
3
|
+
|
|
4
|
+
this.id = data.id;
|
|
5
|
+
this.resource = data.resource;
|
|
6
|
+
this.amount = data.amount;
|
|
7
|
+
this.captureType = data.capture_type;
|
|
8
|
+
this.clientSecret = data.client_secret;
|
|
9
|
+
this.currency = data.currency;
|
|
10
|
+
this.description = data.description;
|
|
11
|
+
this.livemode = data.livemode;
|
|
12
|
+
this.metadata = data.metadata;
|
|
13
|
+
this.latestPayment = data.latest_payment;
|
|
14
|
+
this.paymentMethodId = data.payment_method_id;
|
|
15
|
+
this.paymentMethods = data.payment_methods;
|
|
16
|
+
this.status = data.status;
|
|
17
|
+
this.nextAction = data.next_action;
|
|
18
|
+
this.returnUrl = data.return_url;
|
|
19
|
+
this.captureBeforeAt = data.capture_before_at;
|
|
20
|
+
this.createdAt = data.created_at;
|
|
21
|
+
this.updatedAt = data.updated_at;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
module.exports = PaymentIntentEntity;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
function PaymentMethodEntity(apiResource) {
|
|
2
|
+
const data = apiResource.data;
|
|
3
|
+
|
|
4
|
+
this.id = data.id;
|
|
5
|
+
this.resource = data.resource;
|
|
6
|
+
this.type = data.type;
|
|
7
|
+
this.billingDetails = data.billing_details;
|
|
8
|
+
this.livemode = data.livemode;
|
|
9
|
+
this.metadata = data.metadata;
|
|
10
|
+
this.createdAt = data.created_at;
|
|
11
|
+
this.updatedAt = data.updated_at;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
module.exports = PaymentMethodEntity;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
function RefundEntity(apiResource) {
|
|
2
|
+
const data = apiResource.data;
|
|
3
|
+
|
|
4
|
+
this.id = data.id;
|
|
5
|
+
this.resource = data.resource;
|
|
6
|
+
this.amount = data.amount;
|
|
7
|
+
this.currency = data.currency;
|
|
8
|
+
this.livemode = data.livemode;
|
|
9
|
+
this.status = data.status;
|
|
10
|
+
this.description = data.description;
|
|
11
|
+
this.reason = data.reason;
|
|
12
|
+
this.remarks = data.remarks;
|
|
13
|
+
this.paymentId = data.payment_id;
|
|
14
|
+
this.metadata = data.metadata;
|
|
15
|
+
this.createdAt = data.created_at;
|
|
16
|
+
this.updatedAt = data.updated_at;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
module.exports = RefundEntity;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
function WebhookEntity(apiResource) {
|
|
2
|
+
const data = apiResource.data;
|
|
3
|
+
|
|
4
|
+
this.id = data.id;
|
|
5
|
+
this.resource = data.resource;
|
|
6
|
+
this.secretKey = data.secret_key;
|
|
7
|
+
this.status = data.status;
|
|
8
|
+
this.description = data.description;
|
|
9
|
+
this.livemode = data.livemode;
|
|
10
|
+
this.url = data.url;
|
|
11
|
+
this.events = data.events;
|
|
12
|
+
this.createdAt = data.created_at;
|
|
13
|
+
this.updatedAt = data.updated_at;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
module.exports = WebhookEntity;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const BaseError = require('./BaseError');
|
|
2
|
+
|
|
3
|
+
function AuthenticationInvalidError(response) {
|
|
4
|
+
return BaseError.call(this, response, 'AuthenticationInvalidError');
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
Object.setPrototypeOf(
|
|
8
|
+
AuthenticationInvalidError.prototype,
|
|
9
|
+
BaseError.prototype
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
module.exports = AuthenticationInvalidError;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const PayrexError = require('../Error');
|
|
2
|
+
|
|
3
|
+
function BaseError(response, className = 'BaseError') {
|
|
4
|
+
const baseError = new Error();
|
|
5
|
+
baseError.name = className;
|
|
6
|
+
baseError.errors = (response.errors || []).map(
|
|
7
|
+
(error) => new PayrexError(error)
|
|
8
|
+
);
|
|
9
|
+
|
|
10
|
+
return baseError;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
module.exports = BaseError;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
const BaseError = require('./BaseError');
|
|
2
|
+
|
|
3
|
+
function RequestInvalidError(response) {
|
|
4
|
+
return BaseError.call(this, response, 'RequestInvalidError');
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
Object.setPrototypeOf(RequestInvalidError.prototype, BaseError.prototype);
|
|
8
|
+
|
|
9
|
+
module.exports = RequestInvalidError;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
const BaseError = require('./BaseError');
|
|
2
|
+
|
|
3
|
+
function ResourceNotFoundError(response) {
|
|
4
|
+
return BaseError.call(this, response, 'ResourceNotFoundError');
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
Object.setPrototypeOf(ResourceNotFoundError.prototype, BaseError.prototype);
|
|
8
|
+
|
|
9
|
+
module.exports = ResourceNotFoundError;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const HttpClient = require('../HttpClient');
|
|
2
|
+
|
|
3
|
+
function BaseService(client) {
|
|
4
|
+
this.httpClient = new HttpClient(
|
|
5
|
+
client.config.apiKey,
|
|
6
|
+
client.config.apiBaseUrl
|
|
7
|
+
);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
BaseService.prototype.request = function ({ path, method, payload = null }) {
|
|
11
|
+
return this.httpClient.request({
|
|
12
|
+
method: method,
|
|
13
|
+
path: path,
|
|
14
|
+
payload: payload,
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
module.exports = BaseService;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
const BaseService = require('./BaseService');
|
|
2
|
+
const CheckoutSessionEntity = require('../entities/CheckoutSessionEntity');
|
|
3
|
+
const ListingEntity = require('../entities/ListingEntity');
|
|
4
|
+
const ApiResource = require('../ApiResource');
|
|
5
|
+
|
|
6
|
+
function CheckoutSessionService(client) {
|
|
7
|
+
BaseService.call(this, client);
|
|
8
|
+
this.path = 'checkout_sessions';
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
CheckoutSessionService.prototype.create = function (payload) {
|
|
12
|
+
return this.request({
|
|
13
|
+
path: this.path,
|
|
14
|
+
payload: payload,
|
|
15
|
+
method: 'post',
|
|
16
|
+
}).then(function (response) {
|
|
17
|
+
return new CheckoutSessionEntity(response);
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
CheckoutSessionService.prototype.expire = function (id, payload) {
|
|
22
|
+
return this.request({
|
|
23
|
+
path: `${this.path}/${id}/expire`,
|
|
24
|
+
payload: payload,
|
|
25
|
+
method: 'post',
|
|
26
|
+
}).then(function (response) {
|
|
27
|
+
return new CheckoutSessionEntity(response);
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
CheckoutSessionService.prototype.list = function (payload) {
|
|
32
|
+
return this.request({
|
|
33
|
+
path: this.path,
|
|
34
|
+
payload: payload,
|
|
35
|
+
method: 'get',
|
|
36
|
+
}).then(function (response) {
|
|
37
|
+
const sessionsData = response.data.data;
|
|
38
|
+
|
|
39
|
+
const data = sessionsData.map((session) => {
|
|
40
|
+
const apiResource = new ApiResource(session);
|
|
41
|
+
|
|
42
|
+
return new CheckoutSessionEntity(apiResource);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
return new ListingEntity(data, response.data.has_more);
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
CheckoutSessionService.prototype.retrieve = function (id) {
|
|
50
|
+
return this.request({
|
|
51
|
+
path: `${this.path}/${id}`,
|
|
52
|
+
method: 'get',
|
|
53
|
+
}).then(function (response) {
|
|
54
|
+
return new CheckoutSessionEntity(response);
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
Object.setPrototypeOf(CheckoutSessionService.prototype, BaseService.prototype);
|
|
59
|
+
|
|
60
|
+
module.exports = CheckoutSessionService;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const BaseService = require('./BaseService');
|
|
2
|
+
const MerchantEntity = require('../entities/MerchantEntity');
|
|
3
|
+
|
|
4
|
+
function MerchantService(client) {
|
|
5
|
+
BaseService.call(this, client);
|
|
6
|
+
this.path = 'merchants';
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
MerchantService.prototype.create = function (payload) {
|
|
10
|
+
return this.request({
|
|
11
|
+
path: this.path,
|
|
12
|
+
payload: payload,
|
|
13
|
+
method: 'post',
|
|
14
|
+
}).then(function (response) {
|
|
15
|
+
return new MerchantEntity(response);
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
Object.setPrototypeOf(MerchantService.prototype, BaseService.prototype);
|
|
20
|
+
|
|
21
|
+
module.exports = MerchantService;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const BaseService = require('./BaseService');
|
|
2
|
+
const PaymentIntentEntity = require('../entities/PaymentIntentEntity');
|
|
3
|
+
|
|
4
|
+
function PaymentIntentService(client) {
|
|
5
|
+
BaseService.call(this, client);
|
|
6
|
+
this.path = 'payment_intents';
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
PaymentIntentService.prototype.capture = function (id, payload) {
|
|
10
|
+
return this.request({
|
|
11
|
+
path: `${this.path}/${id}/capture`,
|
|
12
|
+
payload: payload,
|
|
13
|
+
method: 'post',
|
|
14
|
+
}).then(function (response) {
|
|
15
|
+
return new PaymentIntentEntity(response);
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
PaymentIntentService.prototype.create = function (payload) {
|
|
20
|
+
return this.request({
|
|
21
|
+
path: this.path,
|
|
22
|
+
payload: payload,
|
|
23
|
+
method: 'post',
|
|
24
|
+
}).then(function (response) {
|
|
25
|
+
return new PaymentIntentEntity(response);
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
PaymentIntentService.prototype.retrieve = function (id) {
|
|
30
|
+
return this.request({
|
|
31
|
+
path: `${this.path}/${id}`,
|
|
32
|
+
method: 'get',
|
|
33
|
+
}).then(function (response) {
|
|
34
|
+
return new PaymentIntentEntity(response);
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
Object.setPrototypeOf(PaymentIntentService.prototype, BaseService.prototype);
|
|
39
|
+
|
|
40
|
+
module.exports = PaymentIntentService;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const BaseService = require('./BaseService');
|
|
2
|
+
const PaymentMethodEntity = require('../entities/PaymentMethodEntity');
|
|
3
|
+
|
|
4
|
+
function PaymentMethodService(client) {
|
|
5
|
+
BaseService.call(this, client);
|
|
6
|
+
this.path = 'payment_methods';
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
PaymentMethodService.prototype.create = function (payload) {
|
|
10
|
+
return this.request({
|
|
11
|
+
path: this.path,
|
|
12
|
+
payload: payload,
|
|
13
|
+
method: 'post',
|
|
14
|
+
}).then(function (response) {
|
|
15
|
+
return new PaymentMethodEntity(response);
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
Object.setPrototypeOf(PaymentMethodService.prototype, BaseService.prototype);
|
|
20
|
+
|
|
21
|
+
module.exports = PaymentMethodService;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const BaseService = require('./BaseService');
|
|
2
|
+
const RefundEntity = require('../entities/RefundEntity');
|
|
3
|
+
|
|
4
|
+
function RefundService(client) {
|
|
5
|
+
BaseService.call(this, client);
|
|
6
|
+
this.path = 'refunds';
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
RefundService.prototype.create = function (payload) {
|
|
10
|
+
return this.request({
|
|
11
|
+
path: this.path,
|
|
12
|
+
payload: payload,
|
|
13
|
+
method: 'post',
|
|
14
|
+
}).then(function (response) {
|
|
15
|
+
return new RefundEntity(response);
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
Object.setPrototypeOf(RefundService.prototype, BaseService.prototype);
|
|
20
|
+
|
|
21
|
+
module.exports = RefundService;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
const crypto = require('crypto');
|
|
2
|
+
const BaseService = require('./BaseService');
|
|
3
|
+
const WebhookEntity = require('../entities/WebhookEntity');
|
|
4
|
+
const ListingEntity = require('../entities/ListingEntity');
|
|
5
|
+
const ApiResource = require('../ApiResource');
|
|
6
|
+
const EventEntity = require('../entities/EventEntity');
|
|
7
|
+
const ValueUnexpectedError = require('../errors/ValueUnexpectedError');
|
|
8
|
+
const SignatureInvalidError = require('../errors/SignatureInvalidError');
|
|
9
|
+
|
|
10
|
+
function WebhookService(client) {
|
|
11
|
+
BaseService.call(this, client);
|
|
12
|
+
this.path = 'webhooks';
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
WebhookService.prototype.create = function (payload) {
|
|
16
|
+
return this.request({
|
|
17
|
+
path: this.path,
|
|
18
|
+
payload: payload,
|
|
19
|
+
method: 'post',
|
|
20
|
+
}).then(function (response) {
|
|
21
|
+
return new WebhookEntity(response);
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
WebhookService.prototype.update = function (id, payload) {
|
|
26
|
+
return this.request({
|
|
27
|
+
path: `${this.path}/${id}`,
|
|
28
|
+
payload: payload,
|
|
29
|
+
method: 'put',
|
|
30
|
+
}).then(function (response) {
|
|
31
|
+
return new WebhookEntity(response);
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
WebhookService.prototype.list = function (payload) {
|
|
36
|
+
return this.request({
|
|
37
|
+
path: this.path,
|
|
38
|
+
payload: payload,
|
|
39
|
+
method: 'get',
|
|
40
|
+
}).then(function (response) {
|
|
41
|
+
const webhooksData = response.data.data;
|
|
42
|
+
|
|
43
|
+
const data = webhooksData.map((session) => {
|
|
44
|
+
const apiResource = new ApiResource(session);
|
|
45
|
+
|
|
46
|
+
return new WebhookEntity(apiResource);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
return new ListingEntity(data, response.data.has_more);
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
WebhookService.prototype.retrieve = function (id) {
|
|
54
|
+
return this.request({
|
|
55
|
+
path: `${this.path}/${id}`,
|
|
56
|
+
method: 'get',
|
|
57
|
+
}).then(function (response) {
|
|
58
|
+
return new WebhookEntity(response);
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
WebhookService.prototype.enable = function (id) {
|
|
63
|
+
return this.request({
|
|
64
|
+
path: `${this.path}/${id}/enable`,
|
|
65
|
+
method: 'post',
|
|
66
|
+
}).then(function (response) {
|
|
67
|
+
return new WebhookEntity(response);
|
|
68
|
+
});
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
WebhookService.prototype.disable = function (id) {
|
|
72
|
+
return this.request({
|
|
73
|
+
path: `${this.path}/${id}/disable`,
|
|
74
|
+
method: 'post',
|
|
75
|
+
}).then(function (response) {
|
|
76
|
+
return new WebhookEntity(response);
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
WebhookService.prototype.delete = function (id) {
|
|
81
|
+
return this.request({
|
|
82
|
+
path: `${this.path}/${id}`,
|
|
83
|
+
method: 'delete',
|
|
84
|
+
}).then(function (response) {
|
|
85
|
+
return new WebhookEntity(response);
|
|
86
|
+
});
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
WebhookService.prototype.parseEvent = function (
|
|
90
|
+
payload,
|
|
91
|
+
signatureHeader,
|
|
92
|
+
webhookSecretKey
|
|
93
|
+
) {
|
|
94
|
+
if (typeof signatureHeader !== 'string') {
|
|
95
|
+
throw new ValueUnexpectedError('The signature must be a string.');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const signatureArray = signatureHeader.split(',');
|
|
99
|
+
|
|
100
|
+
if (signatureArray.length < 3) {
|
|
101
|
+
throw new ValueUnexpectedError(
|
|
102
|
+
`The format of signature ${signatureHeader} is invalid.`
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const timestamp = signatureArray[0].split('=')[1];
|
|
107
|
+
const testModeSignature = signatureArray[1].split('=')[1];
|
|
108
|
+
const liveModeSignature = signatureArray[2].split('=')[1];
|
|
109
|
+
|
|
110
|
+
const comparisonSignature = liveModeSignature || testModeSignature;
|
|
111
|
+
|
|
112
|
+
const computedHash = crypto
|
|
113
|
+
.createHmac('sha256', webhookSecretKey)
|
|
114
|
+
.update(`${timestamp}.${payload}`)
|
|
115
|
+
.digest('hex');
|
|
116
|
+
|
|
117
|
+
if (computedHash !== comparisonSignature) {
|
|
118
|
+
throw new SignatureInvalidError('The signature is invalid.');
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const apiResource = new ApiResource(JSON.parse(payload));
|
|
122
|
+
|
|
123
|
+
return new EventEntity(apiResource);
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
Object.setPrototypeOf(WebhookService.prototype, BaseService.prototype);
|
|
127
|
+
|
|
128
|
+
module.exports = WebhookService;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const CheckoutSessionService = require('./CheckoutSessionService');
|
|
2
|
+
const MerchantService = require('./MerchantService');
|
|
3
|
+
const PaymentIntentService = require('./PaymentIntentService');
|
|
4
|
+
const PaymentMethodService = require('./PaymentMethodService');
|
|
5
|
+
const RefundService = require('./RefundService');
|
|
6
|
+
const WebhookService = require('./WebhookService');
|
|
7
|
+
|
|
8
|
+
module.exports = {
|
|
9
|
+
checkoutSessions: CheckoutSessionService,
|
|
10
|
+
merchants: MerchantService,
|
|
11
|
+
paymentIntents: PaymentIntentService,
|
|
12
|
+
paymentMethods: PaymentMethodService,
|
|
13
|
+
refunds: RefundService,
|
|
14
|
+
webhooks: WebhookService,
|
|
15
|
+
};
|