1.0.14 • Published 9 months ago

@falaleev/payment-sdk v1.0.14

Weekly downloads
-
License
MIT
Repository
-
Last release
9 months ago
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: 'vanya@mail.ru', 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);
1.0.14

9 months ago

1.0.13

9 months ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.7

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago