@falaleev/payment-sdk
v1.0.19
Published
```javascript const sdk = require('@falaleev/payment-sdk'); const { ClientError, Status } = require('nice-grpc'); const { RichClientError, BadRequest } = require('nice-grpc-error-details');
Downloads
232
Readme
const sdk = require('@falaleev/payment-sdk');
const { ClientError, Status } = require('nice-grpc');
const { RichClientError, BadRequest } = require('nice-grpc-error-details');
const client = sdk.createClient(process.env.PAYMENT_TOKEN || '');
function handleErr(error) {
const validationErrors = {};
if (
error instanceof ClientError &&
error.code === Status.INVALID_ARGUMENT
) {
if (false === error.hasOwnProperty('extra')) {
return;
}
for (const detail of error.extra) {
if (detail.$type !== BadRequest.$type) {
continue
}
for (const violation of detail.fieldViolations) {
if (validationErrors.hasOwnProperty(violation.field)) {
validationErrors[violation.field].push(violation.description);
} else {
validationErrors[violation.field] = [violation.description];
}
}
}
}
if (Object.keys(validationErrors).length > 0) {
console.group('validation errors');
console.log(validationErrors);
console.groupEnd();
} else {
console.error(error);
}
}
async function main() {
try {
const result = await client.create({
method: 0,
duration: 60,
customer: { name: 'ivan bobrov', email: '[email protected]', phone: '+79123492503' },
items: [
{
qty: 1,
price: 19098,
name: 'demoror adulatio **test** (abutor assentator **test**)'
}
],
order_uuid: '722cf002-270d-449b-b3a7-3d87f981abc2'
});
console.log(result);
} catch (error) {
handleErr(error);
}
}
function shutdown() {
process.exit(0);
}
main().then(shutdown).catch(shutdown);