94 lines
2.5 KiB
JavaScript
94 lines
2.5 KiB
JavaScript
|
|
import axios from 'axios';
|
||
|
|
|
||
|
|
const WREN_API_BASE_URL = 'https://api.wren.co/v1';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Create a carbon offset order via Wren Climate API
|
||
|
|
* @param {Object} orderData - Order data
|
||
|
|
* @param {number} orderData.tons - Number of tons to offset
|
||
|
|
* @param {number} orderData.portfolioId - Portfolio ID
|
||
|
|
* @param {string} orderData.customerEmail - Customer email
|
||
|
|
* @param {string} orderData.currency - Currency code
|
||
|
|
* @param {number} orderData.amountCents - Amount in cents
|
||
|
|
* @param {boolean} orderData.dryRun - Dry run mode (default: false)
|
||
|
|
* @returns {Promise<Object>} Wren order response
|
||
|
|
*/
|
||
|
|
export async function createWrenOffsetOrder({
|
||
|
|
tons,
|
||
|
|
portfolioId,
|
||
|
|
customerEmail,
|
||
|
|
currency,
|
||
|
|
amountCents,
|
||
|
|
dryRun = false
|
||
|
|
}) {
|
||
|
|
const apiToken = process.env.WREN_API_TOKEN;
|
||
|
|
|
||
|
|
if (!apiToken) {
|
||
|
|
throw new Error('WREN_API_TOKEN environment variable is required');
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
const response = await axios.post(
|
||
|
|
`${WREN_API_BASE_URL}/offset_orders`,
|
||
|
|
{
|
||
|
|
tons: parseFloat(tons),
|
||
|
|
portfolio: portfolioId,
|
||
|
|
currency: currency.toUpperCase(),
|
||
|
|
amount_charged: amountCents,
|
||
|
|
dry_run: dryRun,
|
||
|
|
source: {
|
||
|
|
name: 'Puffin App',
|
||
|
|
email: customerEmail
|
||
|
|
}
|
||
|
|
},
|
||
|
|
{
|
||
|
|
headers: {
|
||
|
|
'Authorization': `Bearer ${apiToken}`,
|
||
|
|
'Content-Type': 'application/json'
|
||
|
|
}
|
||
|
|
}
|
||
|
|
);
|
||
|
|
|
||
|
|
console.log('✅ Wren offset order created:', response.data.id);
|
||
|
|
return response.data;
|
||
|
|
} catch (error) {
|
||
|
|
console.error('❌ Wren API error:', error.response?.data || error.message);
|
||
|
|
throw new Error(`Wren API failed: ${error.response?.data?.message || error.message}`);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get Wren offset order details
|
||
|
|
* @param {string} orderId - Wren order ID
|
||
|
|
* @returns {Promise<Object>} Wren order details
|
||
|
|
*/
|
||
|
|
export async function getWrenOffsetOrder(orderId) {
|
||
|
|
const apiToken = process.env.WREN_API_TOKEN;
|
||
|
|
|
||
|
|
if (!apiToken) {
|
||
|
|
throw new Error('WREN_API_TOKEN environment variable is required');
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
const response = await axios.get(
|
||
|
|
`${WREN_API_BASE_URL}/offset_orders/${orderId}`,
|
||
|
|
{
|
||
|
|
headers: {
|
||
|
|
'Authorization': `Bearer ${apiToken}`,
|
||
|
|
'Content-Type': 'application/json'
|
||
|
|
}
|
||
|
|
}
|
||
|
|
);
|
||
|
|
|
||
|
|
return response.data;
|
||
|
|
} catch (error) {
|
||
|
|
console.error('❌ Wren API error:', error.response?.data || error.message);
|
||
|
|
throw new Error(`Wren API failed: ${error.response?.data?.message || error.message}`);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export default {
|
||
|
|
createWrenOffsetOrder,
|
||
|
|
getWrenOffsetOrder
|
||
|
|
};
|