92 lines
2.8 KiB
JavaScript
92 lines
2.8 KiB
JavaScript
// =============================================================================
|
|
// Payment API adapter
|
|
// -----------------------------------------------------------------------------
|
|
// This is the ONLY file you need to edit when integrating a real payment-link API.
|
|
// Just make sure createPaymentLink(payload) returns:
|
|
// { link: '<payment url>', raw: <the full original response for reference> }
|
|
//
|
|
// The payload it receives has the shape:
|
|
// {
|
|
// customerName, email, phone, address, currency,
|
|
// items: [ { sku, name, price, quantity, condition } ],
|
|
// total
|
|
// }
|
|
// =============================================================================
|
|
|
|
const PAYMENT_API_URL = process.env.PAYMENT_API_URL;
|
|
const PAYMENT_API_KEY = process.env.PAYMENT_API_KEY;
|
|
|
|
async function createPaymentLink(payload) {
|
|
// --- Map the form data to the body the API expects ------------------------
|
|
// TODO: adjust the body to match your real API.
|
|
const body = {
|
|
fullname: payload.customerName,
|
|
customer_email: payload.email,
|
|
phone: payload.phone,
|
|
address: payload.address,
|
|
items: payload.items.map((it) => ({
|
|
sku: it.sku,
|
|
name: it.name ?? it.sku,
|
|
price: it.price,
|
|
quantity: it.quantity,
|
|
condition: it.condition,
|
|
})),
|
|
total_price: payload.total,
|
|
currency: payload.currency,
|
|
quote_url: 'https://your-website.com/quote',
|
|
// API expects PHP format Y-m-d\TH:i:s.u\Z (microseconds = 6 digits).
|
|
// toISOString() only gives 3 (ms), so pad the fraction to 6 digits.
|
|
expired_at: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000)
|
|
.toISOString()
|
|
.replace('Z', '000Z'), // 7 days from now, e.g. 2026-07-09T12:34:56.789000Z
|
|
quoted_by: {
|
|
id: "1",
|
|
fullname: "Kay",
|
|
email: "kay@prology.net",
|
|
phone: "AU: +61 2 8061 6886 ext 8007, US: +1 714 689 3332 ext 8007",
|
|
},
|
|
quote_number: payload.quoteNumber,
|
|
};
|
|
|
|
const res = await fetch(PAYMENT_API_URL, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json',
|
|
'Authorization': `${PAYMENT_API_KEY}`,
|
|
},
|
|
body: JSON.stringify(body),
|
|
});
|
|
|
|
const text = await res.text();
|
|
let data;
|
|
try {
|
|
data = JSON.parse(text);
|
|
} catch {
|
|
data = { raw: text };
|
|
}
|
|
|
|
if (!res.ok) {
|
|
throw new Error(
|
|
`Payment API returned ${res.status}: ${typeof data === 'string' ? data : JSON.stringify(data)}`
|
|
);
|
|
}
|
|
|
|
// --- Extract the link from the response ----------------------------------
|
|
// TODO: adjust the field path to match your real API.
|
|
const link =
|
|
data.url ||
|
|
data.link ||
|
|
data.payment_link ||
|
|
data.paymentUrl ||
|
|
(data.data && (data.data.url || data.data.link));
|
|
|
|
if (!link) {
|
|
throw new Error('No payment link found in the response: ' + JSON.stringify(data));
|
|
}
|
|
|
|
return { link, raw: data };
|
|
}
|
|
|
|
module.exports = { createPaymentLink };
|