Skip to content

Errors ​

Every error extends PaymentError from @laranex/myanmar-payments, so one catch (or one exception filter) covers the package.

ErrorThrown when
InvalidPaymentDataErrorA payment is started with values the gateway would reject
ApiErrorA gateway rejects a request, answers with an error (including errors sent with HTTP 200), or cannot be reached
SignatureVerificationErrorA callback, return redirect or gateway response fails signature verification
ConfigurationErrorA gateway is used without a setting it needs, or a time setting is not a whole number greater than 0

InvalidPaymentDataError ​

ts
import { InvalidPaymentDataError } from '@laranex/myanmar-payments';

try {
  await this.payments.kbzPay().pwa({
    orderId: 'ORDER-1',
    amount: 0,
    callbackUrl: 'https://shop.test/cb',
  });
} catch (error) {
  if (error instanceof InvalidPaymentDataError) {
    error.errors;
    // { orderId: 'The orderId field may only contain ...',
    //   amount: 'The amount field must be greater than 0.' }
  }
}

ApiError ​

PropertyTypeDescription
gatewayCodestring | undefinedThe gateway's error code, e.g. ORDER_ID_USED, 09, PAYMENT ALREADY EXISTS
gatewayMessagestring | undefinedThe gateway's error message
httpStatusnumberHTTP status of the response, 0 when no response was received
rawReadonly<Record<string, unknown>>The decoded response body

SignatureVerificationError ​

raw holds the unverified payload. Log it, never act on it. @VerifiedCallback() turns it into 400 Bad Request.

ConfigurationError ​

The message names the gateway and the key, e.g. The wave_money configuration is missing [merchant_id]., or The kbz_pay configuration [timeout_in_seconds] must be a whole number greater than 0. for a time setting. gateway and key hold both. autoSubmitUrl() throws it with form_route and ttl_minutes when the form link lifetime is missing or invalid, and with myanmar_payments and formLink.secret when no form link secret is set.

Exception Filter ​

All of them extend PaymentError, so one exception filter can answer them:

ts
import {
  ApiError,
  InvalidPaymentDataError,
  PaymentError,
} from '@laranex/myanmar-payments';
import { ArgumentsHost, Catch, ExceptionFilter } from '@nestjs/common';
import { HttpAdapterHost } from '@nestjs/core';

@Catch(PaymentError)
export class PaymentErrorsFilter implements ExceptionFilter {
  constructor(private readonly adapterHost: HttpAdapterHost) {}

  catch(error: PaymentError, host: ArgumentsHost): void {
    const status =
      error instanceof InvalidPaymentDataError
        ? 422
        : error instanceof ApiError
          ? 502
          : 500;
    const response = host.switchToHttp().getResponse();
    this.adapterHost.httpAdapter.reply(
      response,
      { error: error.message },
      status,
    );
  }
}

httpAdapter.reply() works on Express and Fastify alike.

Released under the MIT License, except where a package says otherwise.