Fix Wren API request body to match official API specification
All checks were successful
Build and Push Docker Images / docker (push) Successful in 45s

Changed request parameters to match Wren API docs:
- 'portfolio' → 'portfolioId' (correct parameter name)
- 'dry_run' → 'dryRun' (camelCase as per API)
- Removed 'currency' and 'amount_charged' (API calculates amount)
- Removed 'source' object (not in API spec)
- Added 'note' with customer email for record keeping

API will now calculate and return the amount charged based on tons and portfolio.
Reference: https://www.wren.co/api/offset-orders documentation
This commit is contained in:
Matt 2025-10-31 12:58:48 +01:00
parent a6e655c4a9
commit 837411699f
2 changed files with 7 additions and 20 deletions

View File

@ -231,8 +231,6 @@ async function fulfillOrder(order, session) {
tons: order.tons, tons: order.tons,
portfolioId: order.portfolio_id, portfolioId: order.portfolio_id,
customerEmail: session.customer_details?.email || order.customer_email, customerEmail: session.customer_details?.email || order.customer_email,
currency: order.currency,
amountCents: order.total_amount,
dryRun: isDryRun, dryRun: isDryRun,
}); });

View File

@ -7,18 +7,14 @@ const WREN_API_BASE_URL = 'https://www.wren.co/api';
* @param {Object} orderData - Order data * @param {Object} orderData - Order data
* @param {number} orderData.tons - Number of tons to offset * @param {number} orderData.tons - Number of tons to offset
* @param {number} orderData.portfolioId - Portfolio ID * @param {number} orderData.portfolioId - Portfolio ID
* @param {string} orderData.customerEmail - Customer email * @param {string} orderData.customerEmail - Customer email (included in note)
* @param {string} orderData.currency - Currency code
* @param {number} orderData.amountCents - Amount in cents
* @param {boolean} orderData.dryRun - Dry run mode (default: false) * @param {boolean} orderData.dryRun - Dry run mode (default: false)
* @returns {Promise<Object>} Wren order response * @returns {Promise<Object>} Wren order response with amountCharged, currency, portfolio, tons
*/ */
export async function createWrenOffsetOrder({ export async function createWrenOffsetOrder({
tons, tons,
portfolioId, portfolioId,
customerEmail, customerEmail,
currency,
amountCents,
dryRun = false dryRun = false
}) { }) {
const startTime = Date.now(); const startTime = Date.now();
@ -32,10 +28,8 @@ export async function createWrenOffsetOrder({
console.log('🔵 [WREN API SERVER] Parameters:', JSON.stringify({ console.log('🔵 [WREN API SERVER] Parameters:', JSON.stringify({
tons: parseFloat(tons), tons: parseFloat(tons),
portfolioId, portfolioId,
customerEmail, dryRun,
currency: currency.toUpperCase(), note: `Puffin App order - Customer: ${customerEmail}`
amountCents,
dryRun
}, null, 2)); }, null, 2));
if (!apiToken) { if (!apiToken) {
@ -48,14 +42,9 @@ export async function createWrenOffsetOrder({
`${WREN_API_BASE_URL}/offset-orders`, `${WREN_API_BASE_URL}/offset-orders`,
{ {
tons: parseFloat(tons), tons: parseFloat(tons),
portfolio: portfolioId, portfolioId: portfolioId,
currency: currency.toUpperCase(), dryRun: dryRun,
amount_charged: amountCents, note: `Puffin App order - Customer: ${customerEmail}`
dry_run: dryRun,
source: {
name: 'Puffin App',
email: customerEmail
}
}, },
{ {
headers: { headers: {