magpie-mcp-server
Version:
Model Context Protocol server for Magpie Payment Platform APIs. Enables AI agents to process payments, create checkout sessions, manage payment requests, and handle payment links.
191 lines • 8.19 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MagpieClient = void 0;
const axios_1 = __importDefault(require("axios"));
class MagpieClient {
constructor(config) {
this.config = config;
// Create separate axios instances for different APIs and authentication
// Using exact URLs from OpenAPI specifications
const paymentsBaseUrl = config.paymentsBaseUrl || 'https://api.magpie.im';
const checkoutBaseUrl = config.checkoutBaseUrl || 'https://api.pay.magpie.im';
const requestsBaseUrl = config.requestsBaseUrl || 'https://request.magpie.im/api';
const linksBaseUrl = config.linksBaseUrl || 'https://buy.magpie.im/api';
this.paymentsApiPublic = this.createApiInstance(paymentsBaseUrl, true); // Uses public key
this.paymentsApiSecret = this.createApiInstance(paymentsBaseUrl, false); // Uses secret key
this.checkoutApi = this.createApiInstance(checkoutBaseUrl, false); // Uses secret key
this.requestsApi = this.createApiInstance(requestsBaseUrl, false); // Uses secret key
this.linksApi = this.createApiInstance(linksBaseUrl, false); // Uses secret key
}
createApiInstance(baseURL, usePublicKey = false) {
const instance = axios_1.default.create({
baseURL,
headers: {
'Content-Type': 'application/json',
},
timeout: 30000,
});
// Add request interceptor for authentication
instance.interceptors.request.use((config) => {
// Use public key for source creation, secret key for everything else
const authKey = usePublicKey ? this.config.publicKey : this.config.secretKey;
const credentials = Buffer.from(`${authKey}:`).toString('base64');
config.headers.Authorization = `Basic ${credentials}`;
return config;
});
// Add response interceptor for error handling
instance.interceptors.response.use((response) => response, (error) => {
const apiError = this.handleApiError(error);
return Promise.reject(apiError);
});
return instance;
}
handleApiError(error) {
const requestUrl = error.config?.baseURL ? `${error.config.baseURL}${error.config.url}` : error.config?.url || 'Unknown URL';
const method = error.config?.method?.toUpperCase() || 'Unknown Method';
if (error.response) {
// Server responded with error status
const errorData = error.response.data;
let errorMessage = `${method} ${requestUrl} - HTTP ${error.response.status}: ${error.response.statusText}`;
if (errorData) {
if (typeof errorData === 'string') {
errorMessage += `\nResponse: ${errorData}`;
}
else {
errorMessage += `\nResponse: ${JSON.stringify(errorData, null, 2)}`;
}
}
return {
success: false,
error: {
type: 'api_error',
message: errorMessage,
code: error.response.status.toString(),
}
};
}
else if (error.request) {
// Network error
return {
success: false,
error: {
type: 'network_error',
message: `Network error attempting ${method} ${requestUrl} - No response received from server`,
}
};
}
else {
// Other error
return {
success: false,
error: {
type: 'unknown_error',
message: `Error with ${method} ${requestUrl} - ${error.message || 'Unknown error occurred'}`,
}
};
}
}
async makeRequest(api, method, path, data) {
try {
const response = await api.request({
method,
url: path,
data,
});
return {
success: true,
data: response.data,
};
}
catch (error) {
return error;
}
}
// Payments API methods
async createSource(data) {
// Source creation uses public key
return this.makeRequest(this.paymentsApiPublic, 'POST', '/v2/sources/', data);
}
async getSource(sourceId) {
// Source retrieval uses secret key
return this.makeRequest(this.paymentsApiSecret, 'GET', `/v2/sources/${sourceId}`);
}
async createCharge(data) {
// All charge operations use secret key
return this.makeRequest(this.paymentsApiSecret, 'POST', '/v2/charges/', data);
}
async getCharge(chargeId) {
return this.makeRequest(this.paymentsApiSecret, 'GET', `/v2/charges/${chargeId}`);
}
async listCharges(startAfter) {
const params = startAfter ? `?start_after=${startAfter}` : '';
return this.makeRequest(this.paymentsApiSecret, 'GET', `/v2/charges/${params}`);
}
async captureCharge(chargeId, data) {
return this.makeRequest(this.paymentsApiSecret, 'POST', `/v2/charges/${chargeId}/capture`, data);
}
async voidCharge(chargeId) {
return this.makeRequest(this.paymentsApiSecret, 'POST', `/v2/charges/${chargeId}/void`);
}
async refundCharge(chargeId, data) {
return this.makeRequest(this.paymentsApiSecret, 'POST', `/v2/charges/${chargeId}/refund`, data);
}
// Checkout API methods
async createCheckoutSession(data) {
return this.makeRequest(this.checkoutApi, 'POST', '/', data);
}
async getCheckoutSession(sessionId) {
return this.makeRequest(this.checkoutApi, 'GET', `/${sessionId}`);
}
async listCheckoutSessions() {
return this.makeRequest(this.checkoutApi, 'GET', '/');
}
async expireCheckoutSession(sessionId) {
return this.makeRequest(this.checkoutApi, 'POST', `/${sessionId}/expire`);
}
async captureCheckoutSession(sessionId) {
return this.makeRequest(this.checkoutApi, 'POST', `/${sessionId}/capture`);
}
// Payment Requests API methods
async createPaymentRequest(data) {
return this.makeRequest(this.requestsApi, 'POST', '/v1/requests', data);
}
async getPaymentRequest(requestId) {
return this.makeRequest(this.requestsApi, 'GET', `/v1/requests/${requestId}`);
}
async listPaymentRequests(params) {
const query = params ? new URLSearchParams(params).toString() : '';
return this.makeRequest(this.requestsApi, 'GET', `/v1/requests${query ? '?' + query : ''}`);
}
async voidPaymentRequest(requestId, data) {
return this.makeRequest(this.requestsApi, 'POST', `/v1/requests/${requestId}/void`, data);
}
async resendPaymentRequest(requestId) {
return this.makeRequest(this.requestsApi, 'POST', `/v1/requests/${requestId}/resend`);
}
// Payment Links API methods
async createPaymentLink(data) {
return this.makeRequest(this.linksApi, 'POST', '/v1/links', data);
}
async getPaymentLink(linkId) {
return this.makeRequest(this.linksApi, 'GET', `/v1/links/${linkId}`);
}
async listPaymentLinks(params) {
const query = params ? new URLSearchParams(params).toString() : '';
return this.makeRequest(this.linksApi, 'GET', `/v1/links${query ? '?' + query : ''}`);
}
async updatePaymentLink(linkId, data) {
return this.makeRequest(this.linksApi, 'PUT', `/v1/links/${linkId}`, data);
}
async activatePaymentLink(linkId) {
return this.makeRequest(this.linksApi, 'POST', `/v1/links/${linkId}/activate`);
}
async deactivatePaymentLink(linkId) {
return this.makeRequest(this.linksApi, 'POST', `/v1/links/${linkId}/deactivate`);
}
}
exports.MagpieClient = MagpieClient;
//# sourceMappingURL=magpie-client.js.map