functions.ts•4.07 kB
import axios from 'axios';
import { z } from 'zod';
import {
createPaymentLinkParameters,
createRefundParameters,
retrieveOrderParameters,
} from './parameters.js';
interface AuthInfo {
apiKey: string;
}
export const createPaymentLink = async (
auth: AuthInfo,
baseUrl: string,
params: z.infer<typeof createPaymentLinkParameters>
) => {
try {
const headers: Record<string, string> = {
'Content-Type': 'application/json',
'x-api-key': auth.apiKey
};
const response = await axios.post(
`${baseUrl}/orders`,
params,
{ headers }
);
if (response.data.checkoutUrl) {
return {
checkoutUrl: response.data.checkoutUrl,
orderId: response.data.orderId
};
} else {
return 'Failed to create payment link: No checkout URL in response';
}
} catch (error: unknown) {
if (axios.isAxiosError(error)) {
return `Failed to create payment link: ${error.response?.data?.message || error.message}`;
}
return 'Failed to create payment link';
}
};
export const createRefund = async (
auth: AuthInfo,
baseUrl: string,
params: z.infer<typeof createRefundParameters>
) => {
try {
const { transactionId, ...requestBody } = params;
const headers: Record<string, string> = {
'Content-Type': 'application/json',
'accept': 'application/json',
'x-api-key': auth.apiKey
};
const response = await axios.post(
`${baseUrl}/refund/${transactionId}`,
requestBody,
{ headers }
);
if (response.data.code === 'BYRD200') {
return {
refundTransactionId: response.data.transactionId,
success: true,
code: response.data.code,
description: response.data.description
};
} else {
return {
success: false,
code: response.data.code,
description: response.data.description || 'Refund operation failed'
};
}
} catch (error: unknown) {
if (axios.isAxiosError(error)) {
return `Failed to create refund: ${error.response?.data?.message || error.message}`;
}
return 'Failed to create refund';
}
};
export const retrieveOrder = async (
auth: AuthInfo,
baseUrl: string,
params: z.infer<typeof retrieveOrderParameters>
) => {
try {
const { orderId } = params;
const headers: Record<string, string> = {
'accept': 'application/json',
'x-api-key': auth.apiKey
};
const response = await axios.get(
`${baseUrl}/orders/${orderId}`,
{ headers }
);
if (response.status === 200) {
return response.data as {
orderDate: string;
expiresAt: string;
status: string;
checkoutUrl: string;
transactions: any[];
orderId: string;
amount: string;
currency: string;
orderRef: string;
shopper: {
firstName: string | null;
lastName: string | null;
email: string | null;
phoneCountryCode: number | null;
phoneNumber: string | null;
} | null;
orderOptions: {
redirectUrl: string | null;
culture: string;
expiresIn: string | null;
notifyBy: {
email: { address: string } | null;
whatsApp: {
firstName: string | null;
lastName: string | null;
phoneCountryCode: number;
phoneNumber: string;
} | null;
sms: {
firstName: string | null;
lastName: string | null;
phoneCountryCode: number;
phoneNumber: string;
} | null;
} | null;
};
code: string;
description: string;
};
} else {
return 'Failed to retrieve order details';
}
} catch (error: unknown) {
if (axios.isAxiosError(error)) {
return `Failed to retrieve order: ${error.response?.data?.message || error.message}`;
}
return 'Failed to retrieve order';
}
};