Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/chapa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
InitializeResponse,
RefundOptions,
RefundResponse,
VerifyRefundOptions,
VerifyRefundResponse,
TransferOptions,
TransferResponse,
VerifyOptions,
Expand All @@ -40,6 +42,7 @@ import {
validateGetTransactionLogsOptions,
validateInitializeOptions,
validateRefundOptions,
validateVerifyRefundOptions,
validateTransferOptions,
validateVerifyOptions,
validateVerifyTransferOptions,
Expand Down Expand Up @@ -88,6 +91,10 @@ interface IChapa {
signal?: AbortSignal
): Promise<AuthorizeDirectChargeResponse>;
refund(options: RefundOptions, signal?: AbortSignal): Promise<RefundResponse>;
verifyRefund(
options: VerifyRefundOptions,
signal?: AbortSignal
): Promise<VerifyRefundResponse>;
verifyWebhook(payload: WebhookPayload | string, signature: string): boolean;
}

Expand Down Expand Up @@ -341,6 +348,20 @@ export class Chapa implements IChapa {
});
}

async verifyRefund(
options: VerifyRefundOptions,
signal?: AbortSignal
): Promise<VerifyRefundResponse> {
return withErrorHandling(async () => {
validateVerifyRefundOptions(options);
const response = await this.axiosInstance.get<VerifyRefundResponse>(
`${ChapaUrls.REFUND}/${options.ref_id}/verify`,
{ signal }
);
return response.data;
});
}

verifyWebhook(payload: WebhookPayload | string, signature: string): boolean {
if (!this.webhookSecret) {
throw new Error('Webhook secret not configured');
Expand Down
10 changes: 10 additions & 0 deletions src/interfaces/refund.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ export interface RefundResponse {
status: string;
data: Record<string, unknown>;
}

export interface VerifyRefundOptions {
ref_id: string;
}

export interface VerifyRefundResponse {
message: string;
status: string;
data: Record<string, unknown>;
}
10 changes: 9 additions & 1 deletion src/validations/refund.validation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from 'zod';
import { RefundOptions } from '../interfaces';
import { RefundOptions, VerifyRefundOptions } from '../interfaces';

const refundSchema = z.object({
tx_ref: z.string(),
Expand All @@ -16,3 +16,11 @@ const refundSchema = z.object({
export const validateRefundOptions = (options: RefundOptions) => {
return refundSchema.parse(options);
};

const verifyRefundSchema = z.object({
ref_id: z.string(),
});

export const validateVerifyRefundOptions = (options: VerifyRefundOptions) => {
return verifyRefundSchema.parse(options);
};
38 changes: 38 additions & 0 deletions test/refund.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,41 @@ describe('Refund', () => {
expect(body.toString()).toBe('');
});
});

describe('Verify Refund', () => {
const mockGet = jest.fn();
let chapa: Chapa;

beforeEach(() => {
mockGet.mockReset();
(axios.create as jest.Mock).mockReturnValue({ get: mockGet });
chapa = new Chapa({ secretKey: 'test-secret-key' });
});

it('should validate required ref_id', async () => {
await expect(
chapa.verifyRefund({} as any)
).rejects.toThrow();
expect(mockGet).not.toHaveBeenCalled();
});

it('should call get method with correct url', async () => {
const options = {
ref_id: 'REF-1234',
};
const responseData = {
message: 'ok',
status: 'success',
data: { amount: 100 },
};
mockGet.mockResolvedValue({ data: responseData });

const response = await chapa.verifyRefund(options);

expect(response).toEqual(responseData);
expect(mockGet).toHaveBeenCalledTimes(1);
const [url, config] = mockGet.mock.calls[0];
expect(url).toBe('/refund/REF-1234/verify');
expect(config.signal).toBeUndefined();
});
});
Loading