Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 38 ยท Projects and Practice

Payments Practice Exercises

Practice webhook verification, idempotency, Connect fee splits, refunds, tax and fraud rules with hands-on exercises, each with a runnable starting point and a solution to compare.

Reading about payments is not the same as writing the code. These exercises revisit the Webhooks, Connect, Testing and Compliance chapters. Each one has a runnable starter with failing checks. Change the code until every line prints PASS, then compare with the solution that follows. Try first; the learning is in the attempt.

Exercise 1: reject stale webhooks

Write isFresh(timestamp, now, toleranceSeconds) so a request is accepted only when its timestamp is within the tolerance in either direction.

Node.js Exercise 1: starter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Solution
JavaScript
function isFresh(timestamp, now, toleranceSeconds) {
  return Math.abs(now - timestamp) <= toleranceSeconds;
}

Exercise 2: fee splitting without losing a cent

Implement split(amount, percent, fixed) returning { fee, seller } where fee = round(amount * percent / 100) + fixed, never more than amount, and fee + seller === amount.

Node.js Exercise 2: starter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Solution
JavaScript
function split(amount, percent, fixed) {
  const fee = Math.min(amount, Math.round((amount * percent) / 100) + fixed);
  return { fee, seller: amount - fee };
}

Exercise 3: an idempotent credit handler

handle(event) must add event.amount to the balance only the first time an event id is seen, and return 'applied' or 'duplicate'.

Node.js Exercise 3: starter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Solution
JavaScript
function handle(event) {
  if (processed.has(event.id)) return 'duplicate';
  processed.add(event.id);
  balance += event.amount;
  return 'applied';
}

Exercise 4: choose the PaymentIntent next step

Return the right action string for each status, including requires_action.

Node.js Exercise 4: starter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Solution
JavaScript
function action(status) {
  switch (status) {
    case 'requires_payment_method': return 'collect card';
    case 'requires_action': return 'authenticate';
    case 'processing': return 'wait';
    case 'succeeded': return 'fulfil';
    case 'canceled': return 'restart';
    default: return 'collect card';
  }
}

Exercise 5: tax-inclusive prices

Given a gross price and a rate, return { net, tax } so that net + tax === gross. Compute net by rounding, tax by subtraction.

Node.js Exercise 5: starter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Solution
JavaScript
function extractTax(gross, ratePercent) {
  const net = Math.round((gross * 100) / (100 + ratePercent));
  return { net, tax: gross - net };
}

Exercise 6: a first fraud rule set

Return 'block', 'review' or 'allow': block when score >= 80; review when the amount is at least 20000 and the card country differs from the IP country; otherwise allow.

Node.js Exercise 6: starter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Solution
JavaScript
function decide({ score, amount, cardCountry, ipCountry }) {
  if (score >= 80) return 'block';
  if (amount >= 20000 && cardCountry !== ipCountry) return 'review';
  return 'allow';
}

Design questions

Answer these in your own words, then test yourself against the lessons:

  1. Your endpoint returns 200 but the order is never marked paid. List three places to look.
  2. A seller's balance goes negative after a dispute. Who is financially responsible in your platform, and how can you limit the risk?
  3. Why is "I verified the card in the browser" not enough to ship goods?
  4. Which data may a payments table store, and which may it never store?
  5. How would you prove to an auditor that your payout totals match Stripe's records?

Good answers mention webhooks as the source of truth, balance transactions for reconciliation, holds and reserves for risk, and keeping only Stripe ids, brand and last four digits.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. Which approach is correct for splitting a sale into fee and seller share?

    Subtracting keeps the parts exactly equal to the total.
  2. 2. What should a handler do for an event id it has seen before?

    Duplicates are normal. Acknowledge them and skip the work.
  3. 3. Which status requires the customer to complete a bank challenge?

    requires_action indicates authentication such as 3D Secure is needed.

Summary

  • You practised freshness checks, fee splits, idempotency, status handling, tax extraction and fraud rules.
  • The same habits repeat: integer cents, subtraction for the remainder, webhooks as truth, ids for deduplication.
  • Revisit any chapter where an exercise felt hard, then rebuild the project lessons from memory.

Next: Back to the Stripe and Online Payments course