← All projects

node

  • TypeScript 96.64%
  • JavaScript 3.36%
git@gitlab.com:aice-lab/payment/node.git

Latest commit

706b8432 ·

README

@aice-lab/payment

Node.js / TypeScript client SDK for the AiCE-Lab payment service.

Installation

npm install @aice-lab/payment

Usage

import { PaymentClient } from '@aice-lab/payment';

const client = new PaymentClient({
  baseUrl: 'https://payment.aice-lab.org',
  apiKey: process.env.PAYMENT_API_KEY!,
});

// Create a checkout
const checkout = await client.createCheckout({
  orderRef: 'ORD-001',
  gateway: 'bkash',
  amountMinor: 50000,   // 500.00 BDT in paisa
  currency: 'BDT',
  returnUrls: {
    success: 'https://myapp.com/payment/success',
    cancel: 'https://myapp.com/payment/cancel',
  },
});

// Redirect the user to checkout.redirectUrl

// Later: fetch the transaction
const tx = await client.getTransaction(checkout.transactionId);
if (tx.isSucceeded()) {
  // fulfil the order
}

// Refund
const refunded = await client.refund(checkout.transactionId);

Testing

Use MockPaymentClient (exported from @aice-lab/payment/mock) in your own tests:

import { MockPaymentClient } from '@aice-lab/payment/mock';

const mock = new MockPaymentClient();
// Override behavior:
mock.onGetTransaction = async (id) => ({ ... });
// Assert recorded calls:
expect(mock.lastCheckout?.orderRef).toBe('ORD-001');

Error handling

All errors extend PaymentError (with code and message). Specific subclasses:

ClassStatus
UnauthorizedError401
NotFoundError404
ConflictError409
AmountMismatchError400, code=amount_mismatch
BadRequestError400
GatewayUnavailableError503 (has retryAfterSeconds)
TransportErrornetwork failure

License

FSL-1.1-Apache-2.0. See LICENSE and LICENSE.FAQ.md.

This is a snapshot generated from GitLab. For the live README, see the project page.