ValulogDashboard

Stripe PaymentIntent and partial refund recipe

Charge a discounted basket with Stripe, then refund one line of it for exactly what the customer paid for that line. Valulog works out the amounts and keeps the record; Stripe moves the money.

charge() and refundLine() in Node.js
// Node 18+, npm i stripe. Keys come from environment variables.
import Stripe from 'stripe';
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
const vl = (path, key, body) => fetch(`https://api.valulog.com${path}`, { method: 'POST', body: JSON.stringify(body),
  headers: { Authorization: `Bearer ${process.env.VALULOG_KEY}`, 'Idempotency-Key': key, 'Content-Type': 'application/json' },
}).then(async (r) => { if (!r.ok) throw new Error(JSON.stringify(await r.json())); return r.json(); });

// 1. Checkout: freeze the basket, then charge exactly the frozen total.
export async function charge(orderId, basket) {
  const calc = await vl('/v1/calculate', `order-${orderId}`, basket);
  return stripe.paymentIntents.create({
    amount: calc.totals.final_total_cents, currency: basket.currency.toLowerCase(),
    automatic_payment_methods: { enabled: true },
    metadata: { order_id: orderId, valulog_transaction_id: calc.transaction_id },
  }, { idempotencyKey: `pi-${orderId}` });
}

// 2. Return: ask Valulog what that line was worth, then refund that amount in Stripe.
export async function refundLine(paymentIntentId, transactionId, lineId, quantity, returnId) {
  const r = await vl('/v1/refunds', `return-${returnId}`, { transaction_id: transactionId, line_item_id: lineId, quantity });
  if (r.refund_amount_cents === 0) return null; // ALREADY_REFUNDED: nothing left on this line
  return stripe.refunds.create({ payment_intent: paymentIntentId, amount: r.refund_amount_cents,
    metadata: { valulog_refund_id: r.refund_id } }, { idempotencyKey: `refund-${returnId}` });
}

Why it is built this way

  • The PaymentIntent amount is the frozen total. Store valulog_transaction_id in its metadata so every later refund can find the snapshot.
  • Same idempotency key, same answer. Use one key per order on both calls, and one key per return on both refund calls. A retry after a timeout returns the original result and never refunds twice, in Valulog or in Stripe.
  • The refund amount comes from the snapshot. Valulog divides what was paid for the line, not today’s shelf price, and refuses to go past it (INSUFFICIENT_REFUNDABLE).
  • If Valulog is unavailable at checkout. There is no contractual SLA, so decide your fallback. Calls fail with SERVICE_UNAVAILABLE rather than return a wrong result. Because the basket and prices travel in the request, you can retry the same call later with the same Idempotency-Key and get one transaction.

Try the amounts first, without a key, in the simulator, then run it with a free sandbox key. Use Stripe test mode keys while you build.

Valulog is not a payment processor and does not give tax advice. Payments stay with your payment provider; Valulog records how prices, discounts and refunds were calculated.