UNPKG

@unchainedshop/plugins

Version:

Official plugin collection for the Unchained Engine with payment, delivery, and pricing adapters

41 lines (40 loc) 1.52 kB
import { createLogger } from '@unchainedshop/logger'; const { MOCK_APIS } = process.env; const logger = createLogger('unchained:datatrans'); export default (endpoint, merchantId, secret) => { if (MOCK_APIS) { return async (path) => { try { const { default: json } = await import(`${import.meta.dirname}/../../../../tests/mock/datatrans/${path}.json`, { with: { type: 'json' }, }); return { json: async () => json, status: json?.error ? 500 : 204, }; } catch (error) { logger.error('Mock: Error while trying reading and parsing file', { ...error, }); return { json: async () => ({ error: { code: 'MOCK', message: 'MOCK' } }), status: 500, }; } }; } const token = `${merchantId}:${secret}`; return async (path, body) => { logger.debug(`Fetch ${endpoint}${path}: ${JSON.stringify(body)}`); return fetch(`${endpoint}${path}`, { method: body ? 'POST' : 'GET', body: body ? JSON.stringify(body) : undefined, duplex: body ? 'half' : undefined, headers: { 'Content-Type': 'application/json', Authorization: `Basic ${Buffer.from(token, 'utf-8').toString('base64')}`, }, }); }; };