import config from 'config'; import { getConfig } from '../../lib/util/getConfig.js'; import { hookAfter } from '../../lib/util/hookable.js'; import { registerPaymentMethod } from '../checkout/services/getAvailablePaymentMethods.js'; import { getSetting } from '../setting/services/setting.js'; import { voidPaymentTransaction } from './services/voidPaymentTransaction.js'; export default async () => { const paypalPaymentStatus = { order: { paymentStatus: { paypal_authorized: { name: 'Authorized', badge: 'warning' }, paypal_captured: { name: 'Captured', badge: 'success' } }, psoMapping: { 'paypal_authorized:*': 'processing', 'paypal_captured:*': 'processing', 'paypal_captured:delivered': 'completed' } } }; config.util.setModuleDefaults('oms', paypalPaymentStatus); hookAfter('changePaymentStatus', async (order, orderID, status) => { if (status !== 'canceled') { return; } if (order.payment_method !== 'paypal') { return; } await voidPaymentTransaction(orderID); }); registerPaymentMethod({ init: async () => ({ code: 'paypal', name: await getSetting('paypalDisplayName', 'PayPal') }), validator: async () => { const paypalConfig = getConfig('system.paypal', {}); let paypalStatus; if (paypalConfig?.status) { paypalStatus = paypalConfig.status; } else { paypalStatus = await getSetting('paypalPaymentStatus', 0); } if (parseInt(paypalStatus, 10) === 1) { return true; } else { return false; } } }); };