← back to Costa Rica

lib/payments/index.js

31 lines

'use strict';
// Payment provider abstraction. CR-native processors only (no Stripe): they
// accept international USD cards AND local CRC + SINPE Movil in one integration.
//
// A provider implements this interface:
//   name
//   liveMode                       -> bool (false = sandbox/test)
//   createCharge({ amount, currency, method, booking, customer, returnUrl })
//        -> { providerRef, status, clientAction, raw }
//        status: 'requires_action' | 'succeeded' | 'processing' | 'failed'
//        clientAction: { type:'redirect'|'sinpe_instructions'|'sdk', url?, ... }
//   getCharge(providerRef)         -> { status, raw }
//   refund(providerRef, amount?)   -> { status, raw }
//   payout({ method, amount, currency, reference }) -> { providerRef, status, raw }  (SINPE)
//   verifyWebhook(headers, rawBody)-> { ok, event }   // HMAC / signature check
//
// Swap Tilopay<->ONVO by PAYMENT_PROVIDER env with zero call-site changes.

const tilopay = require('./tilopay');
const onvo = require('./onvo');

const REGISTRY = { tilopay, onvo };

function getProvider(name = process.env.PAYMENT_PROVIDER || 'tilopay') {
  const p = REGISTRY[name];
  if (!p) throw new Error(`unknown payment provider ${name}`);
  return p;
}

module.exports = { getProvider, REGISTRY };