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, or its signature matches but its signed values are malformed (e.g. Wave Pay Mini App callback is malformed.)
ConfigurationErrorA gateway is used without a setting it needs (URLs, time to live and Yoma's webhook secret included), a time setting is out of range, or the form link secret is shorter than 32 bytes

Outside this hierarchy: a callback body over 64 KiB throws PayloadTooLargeException (413), a JSON callback without rawBody: true throws BadRequestException (400), and an unknown name passed to gateway() or handleCallback(), autoSubmitUrl() with the form route disabled, handleCallback('wave-pay-mini-app', ...) given a CallbackRequest instead of the Nest request, or a Wave Pay Mini App callback request without a host, throws a plain Error.

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_pay 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. A secret shorter than 32 bytes throws it with myanmar_payments and the setting it came from (formLink.secret, MYANMAR_PAYMENTS_FORM_KEY or APP_KEY).

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.