@johntad/m-pesa
Version:
A TypeScript SDK for integrating M-Pesa mobile payment services into applications, enabling seamless money transfers and transactions.
40 lines (39 loc) • 1.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.StkPushResponse = void 0;
/**
* Represents a response object from the STK Push API.
* Provides methods for checking transaction success and generating a formatted string representation.
*/
class StkPushResponse {
constructor(MerchantRequestID, CheckoutRequestID, ResponseCode, ResponseDescription, CustomerMessage) {
this.MerchantRequestID = MerchantRequestID;
this.CheckoutRequestID = CheckoutRequestID;
this.ResponseCode = ResponseCode;
this.ResponseDescription = ResponseDescription;
this.CustomerMessage = CustomerMessage;
}
/**
* Factory method to create an instance from API response data.
* @param data - The raw response data from the API.
* @returns An instance of StkPushResponse.
*/
static fromApiResponse(data) {
return new StkPushResponse(data.MerchantRequestID, data.CheckoutRequestID, data.ResponseCode, data.ResponseDescription, data.CustomerMessage);
}
/**
* Determines if the response indicates success.
* @returns True if the response was successful, false otherwise.
*/
isSuccess() {
return this.ResponseCode === '0';
}
/**
* Returns a formatted string for logging or displaying the response.
* @returns A formatted string summarizing the response.
*/
toString() {
return `STK Push Response: ${this.ResponseDescription} (Code: ${this.ResponseCode})`;
}
}
exports.StkPushResponse = StkPushResponse;