UNPKG

@paybyrd/ai-agent-toolkit

Version:

Toolkit for building AI agents with various models

97 lines (96 loc) 3.38 kB
import axios from 'axios'; export const createPaymentLink = async (auth, baseUrl, params) => { var _a, _b; try { const headers = { 'Content-Type': 'application/json' }; if (auth.isApiKey) { headers['x-api-key'] = auth.authToken; } else { headers['Authorization'] = `Bearer ${auth.authToken}`; } 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', }; if (auth.isApiKey) { headers['x-api-key'] = auth.authToken; } else { headers['Authorization'] = `Bearer ${auth.authToken}`; } const response = await axios.post(`${baseUrl}/refund/${transactionId}`, requestBody, { headers }); if (response.data.code === 'BYRD200') { return { 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', }; if (auth.isApiKey) { headers['x-api-key'] = auth.authToken; } else { headers['Authorization'] = `Bearer ${auth.authToken}`; } 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'; } };