@paybyrd/ai-agent-toolkit
Version:
Toolkit for building AI agents with various models
83 lines (82 loc) • 3.01 kB
JavaScript
import axios from 'axios';
export const createPaymentLink = async (auth, baseUrl, params) => {
var _a, _b;
try {
const headers = {
'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) {
if (axios.isAxiosError(error)) {
return `Failed to create payment link: ${((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) || error.message}`;
}
return 'Failed to create payment link';
}
};
export const createRefund = async (auth, baseUrl, params) => {
var _a, _b;
try {
const { transactionId, ...requestBody } = params;
const headers = {
'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) {
if (axios.isAxiosError(error)) {
return `Failed to create refund: ${((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) || error.message}`;
}
return 'Failed to create refund';
}
};
export const retrieveOrder = async (auth, baseUrl, params) => {
var _a, _b;
try {
const { orderId } = params;
const headers = {
'accept': 'application/json',
'x-api-key': auth.apiKey
};
const response = await axios.get(`${baseUrl}/orders/${orderId}`, { headers });
if (response.status === 200) {
return response.data;
}
else {
return 'Failed to retrieve order details';
}
}
catch (error) {
if (axios.isAxiosError(error)) {
return `Failed to retrieve order: ${((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) || error.message}`;
}
return 'Failed to retrieve order';
}
};