UNPKG

mpesa-mz-sdk

Version:

SDK para integração com a API M-Pesa de Moçambique

65 lines (64 loc) 3.08 kB
import { MpesaAPIConfig, MpesaErrorDetail, C2BArgs, B2CArgs, B2BArgs, QueryArgs, ReversalArgs, MpesaResponse, C2BResponseData, B2CResponseData, B2BResponseData, QueryResponseData, ReversalResponseData } from './types'; /** * Custom Error class for M-Pesa API errors. */ export declare class MpesaError extends Error { details: MpesaErrorDetail; constructor(message: string, details: MpesaErrorDetail); } export declare class MpesaService { private config; private httpClient; private encodedPublicKey; constructor(config: MpesaAPIConfig); /** * Helper method to handle M-Pesa specific API errors. * This method ensures consistent error reporting across all API calls. * @param error The AxiosError object or a generic error. * @param context A string describing the context of the error (e.g., "Token", "C2B"). * @throws MpesaError */ private handleMpesaError; /** * Helper method to transform M-Pesa API responses into a clean, readable format. * @param response The raw M-Pesa API response * @param context The operation context (C2B, B2C, B2B, Query, Reversal) * @returns A clean, readable response object */ private transformResponse; /** * Initiates a C2B (Customer to Business) transaction. * @param args Object containing amount, number, transactionReference, thirdPartyReference * @returns Clean, readable M-Pesa response for the C2B transaction. * @throws MpesaError if the C2B transaction fails. */ c2b(args: C2BArgs): Promise<MpesaResponse<C2BResponseData>>; /** * Initiates a B2C (Business to Customer) transaction. * @param args Object containing amount, number, transactionReference, thirdPartyReference, paymentServices * @returns Clean, readable M-Pesa response for the B2C transaction. * @throws MpesaError if the B2C transaction fails. */ b2c(args: B2CArgs): Promise<MpesaResponse<B2CResponseData>>; /** * Initiates a B2B (Business to Business) transaction. * @param args Object containing amount, primaryPartyCode, recipientPartyCode, transactionReference, thirdPartyReference, paymentServices * @returns Clean, readable M-Pesa response for the B2B transaction. * @throws MpesaError if the B2B transaction fails. */ b2b(args: B2BArgs): Promise<MpesaResponse<B2BResponseData>>; /** * Queries the status of an M-Pesa transaction. * @param args Object containing queryReference, thirdPartyReference * @returns Clean, readable M-Pesa response with the transaction status. * @throws MpesaError if the query fails. */ query(args: QueryArgs): Promise<MpesaResponse<QueryResponseData>>; /** * Reverses an M-Pesa transaction. * @param args Object containing originalTransactionId, reversalAmount, thirdPartyReference * @returns Clean, readable M-Pesa response for the reversal. * @throws MpesaError if the reversal fails. */ reversal(args: ReversalArgs): Promise<MpesaResponse<ReversalResponseData>>; }