Better Payment 0.11.1 API
    Preparing search index...

    Function chaos

    • Injects failures and delays into operations, so that you can test how your application handles declines, lost responses (NETWORK_ERROR) and slow providers. Failed calls never reach the provider. Throws in production mode unless allowProduction is set.

      Parameters

      Returns {
          hooks: {
              before: [
                  {
                      handler: (
                          ctx: OperationContext,
                      ) => Promise<
                          | {
                              result: {
                                  code: PaymentErrorCode;
                                  errorCode: PaymentErrorCode;
                                  errorMessage: string;
                                  status: PaymentStatus;
                              };
                          }
                          | undefined,
                      >;
                      matcher: () => boolean;
                  },
              ];
          };
          id: "chaos";
          init: (ctx: PluginContext) => void;
          methods: (
              ctx: PluginContext,
          ) => {
              chaos: {
                  disable: () => void;
                  enable: () => void;
                  get enabled(): boolean;
              };
          };
          options: ChaosOptions;
      }

      import { chaos } from 'better-payment/plugins';

      const payment = betterPayment({
      providers: { ... },
      plugins: [
      chaos({
      enabled: process.env.CHAOS === '1',
      seed: 42,
      rules: [
      { operation: 'createPayment', probability: 0.2, fail: 'NETWORK_ERROR' },
      { operation: 'refund', delay: '3s' },
      ],
      }),
      ],
      });