Stripe NG SDE VO
1245

You are given:

A payment string in the format: "payment_name, amount, memo"
An array of invoice strings:
["invoice_id_1, due_date_1, amount_1", "invoice_id_2, due_date_2, amount_2", ...]

There are three parts:

Part 1 — Match by invoice_id (from memo)

The memo may contain an invoice reference in the format:
"XXXX:invoice_id"

Task:

Extract invoice_id from memo
Match it with the invoice_id in the invoices array
If matched, output: payment_name, amount, due_date, invoice_id

Approach:

Split the payment string to get payment_name and payment_amount
Split memo again to extract invoice_id
Iterate through invoices, split each one, and compare invoice_id

Part 2 — Fallback to amount matching

The memo may not contain an invoice_id. If matching by invoice_id fails, match using amount.

Rules:

Match invoices whose amount == payment_amount
If multiple matches exist, choose the one with the earliest due_date

Approach:

After splitting memo, check whether invoice_id exists (can use split length instead of string search)
If no valid invoice_id, treat it as None
Iterate through invoices:
First try matching invoice_id
If matched, return immediately
Otherwise, if amount matches, keep track of the invoice with the earliest due_date
After iteration, if an amount match exists, return that result

Part 3 — Amount matching with tolerance (forgiveness)

Now introduce an integer forgiveness.

Rules:

A payment matches an invoice if:
invoice_amount - forgiveness <= payment_amount <= invoice_amount + forgiveness
If there is a difference between amounts, include the difference in the output

Approach:

Modify the amount matching condition from Part 2
Track the difference:
diff = payment_amount - invoice_amount
If a match is found:
If diff == 0, output same as Part 2
Otherwise, include the difference in the output

Comments (2)