72 lines
2.5 KiB
JavaScript
72 lines
2.5 KiB
JavaScript
// BCI api-business-notifications client
|
|
// Sandbox: https://apipartner.bci.cl/sandbox/v2/api-business-notifications
|
|
// Prod: https://apipartner.bci.cl/v2/api-business-notifications
|
|
|
|
function baseUrl() {
|
|
return process.env.BCI_SANDBOX === '1'
|
|
? 'https://apipartner.bci.cl/sandbox/v2/api-business-notifications'
|
|
: 'https://apipartner.bci.cl/v2/api-business-notifications';
|
|
}
|
|
|
|
function bciHeaders(subscriptionKey, extra = {}) {
|
|
return {
|
|
'Content-Type': 'application/json',
|
|
'x-apikey': subscriptionKey,
|
|
'Referer': 'https://apipartner.bci.cl/',
|
|
...extra,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Register our callback URL with BCI.
|
|
* webhookSecret is a key WE choose — BCI echoes it back in every notification
|
|
* payload as APIKey so we can verify the webhook came from BCI.
|
|
*/
|
|
export async function createSubscription({ organizationName, account, rut, checkDigit, callbackUrl, subscriptionKey, webhookSecret }) {
|
|
// Embed secret as ?token=... so BCI calls us with it; BCI does not echo APIKey in webhook bodies
|
|
const secureCallbackUrl = webhookSecret
|
|
? `${callbackUrl}?token=${encodeURIComponent(webhookSecret)}`
|
|
: callbackUrl;
|
|
|
|
const res = await fetch(`${baseUrl()}/subscription`, {
|
|
method: 'POST',
|
|
headers: bciHeaders(subscriptionKey),
|
|
body: JSON.stringify({
|
|
OrganizationName: organizationName,
|
|
Account: account,
|
|
RUT: rut,
|
|
CheckDigit: checkDigit,
|
|
URLCallback: secureCallbackUrl,
|
|
APIKey: webhookSecret,
|
|
}),
|
|
});
|
|
const text = await res.text();
|
|
if (!res.ok) throw new Error(`BCI createSubscription ${res.status}: ${text}`);
|
|
try { return JSON.parse(text); } catch { return { raw: text }; }
|
|
}
|
|
|
|
/**
|
|
* Trigger a test notification to our callback URL.
|
|
* Useful after subscription setup to confirm the full flow works and
|
|
* to discover the exact notification payload shape BCI sends.
|
|
*/
|
|
export async function simulateNotification({ callbackUrl, amount, movementType = '1', subscriptionKey, webhookSecret }) {
|
|
const secureCallbackUrl = webhookSecret
|
|
? `${callbackUrl}?token=${encodeURIComponent(webhookSecret)}`
|
|
: callbackUrl;
|
|
|
|
const res = await fetch(`${baseUrl()}/simulations`, {
|
|
method: 'POST',
|
|
headers: bciHeaders(subscriptionKey),
|
|
body: JSON.stringify({
|
|
URLCallback: secureCallbackUrl,
|
|
Amount: String(amount),
|
|
MovementType: movementType,
|
|
APIKey: webhookSecret,
|
|
}),
|
|
});
|
|
const text = await res.text();
|
|
if (!res.ok) throw new Error(`BCI simulateNotification ${res.status}: ${text}`);
|
|
try { return JSON.parse(text); } catch { return { raw: text }; }
|
|
}
|