-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
398 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
create table if not exists | ||
"stripe"."credit_notes" ( | ||
"id" text primary key, | ||
object text, | ||
amount integer, | ||
amount_shipping integer, | ||
created integer, | ||
currency text, | ||
customer text, | ||
customer_balance_transaction text, | ||
discount_amount integer, | ||
discount_amounts jsonb, | ||
invoice text, | ||
lines jsonb, | ||
livemode boolean, | ||
memo text, | ||
metadata jsonb, | ||
number text, | ||
out_of_band_amount integer, | ||
pdf text, | ||
reason text, | ||
refund text, | ||
shipping_cost jsonb, | ||
status text, | ||
subtotal integer, | ||
subtotal_excluding_tax integer, | ||
tax_amounts jsonb, | ||
total integer, | ||
total_excluding_tax integer, | ||
type text, | ||
voided_at text | ||
); | ||
|
||
create index stripe_credit_notes_customer_idx on "stripe"."credit_notes" using btree (customer); | ||
|
||
create index stripe_credit_notes_invoice_idx on "stripe"."credit_notes" using btree (invoice); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import Stripe from 'stripe' | ||
import { getConfig } from '../utils/config' | ||
import { constructUpsertSql } from '../utils/helpers' | ||
import { backfillInvoices } from './invoices' | ||
import { backfillCustomers } from './customers' | ||
import { findMissingEntries, getUniqueIds, upsertMany } from './database_utils' | ||
import { stripe } from '../utils/StripeClientManager' | ||
import { creditNoteSchema } from '../schemas/credit_note' | ||
|
||
const config = getConfig() | ||
|
||
export const upsertCreditNotes = async ( | ||
creditNotes: Stripe.CreditNote[], | ||
backfillRelatedEntities: boolean = true | ||
): Promise<Stripe.CreditNote[]> => { | ||
if (backfillRelatedEntities) { | ||
await Promise.all([ | ||
backfillCustomers(getUniqueIds(creditNotes, 'customer')), | ||
backfillInvoices(getUniqueIds(creditNotes, 'invoice')), | ||
]) | ||
} | ||
|
||
// Stripe only sends the first 10 refunds by default, the option will actively fetch all refunds | ||
if (getConfig().AUTO_EXPAND_LISTS) { | ||
for (const creditNote of creditNotes) { | ||
if (creditNote.lines?.has_more) { | ||
const allLines: Stripe.CreditNoteLineItem[] = [] | ||
for await (const lineItem of stripe.creditNotes.listLineItems(creditNote.id, { | ||
limit: 100, | ||
})) { | ||
allLines.push(lineItem) | ||
} | ||
|
||
creditNote.lines = { | ||
...creditNote.lines, | ||
data: allLines, | ||
has_more: false, | ||
} | ||
} | ||
} | ||
} | ||
|
||
return upsertMany(creditNotes, () => | ||
constructUpsertSql(config.SCHEMA, 'credit_notes', creditNoteSchema) | ||
) | ||
} | ||
|
||
export const backfillCreditNotes = async (creditNoteIds: string[]) => { | ||
const missingCreditNoteIds = await findMissingEntries('credit_notes', creditNoteIds) | ||
await fetchAndInsertCreditNotes(missingCreditNoteIds) | ||
} | ||
|
||
const fetchAndInsertCreditNotes = async (creditNoteIds: string[]) => { | ||
if (!creditNoteIds.length) return | ||
|
||
const creditNotes: Stripe.CreditNote[] = [] | ||
|
||
for (const creditNoteId of creditNoteIds) { | ||
const creditNote = await stripe.creditNotes.retrieve(creditNoteId) | ||
creditNotes.push(creditNote) | ||
} | ||
|
||
await upsertCreditNotes(creditNotes, true) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { JsonSchema } from '../types/types' | ||
|
||
export const creditNoteSchema: JsonSchema = { | ||
$id: 'creditNoteSchema', | ||
type: 'object', | ||
properties: { | ||
id: { type: 'string' }, | ||
object: { type: 'string' }, | ||
amount: { type: 'number' }, | ||
amount_shipping: { type: 'number' }, | ||
created: { type: 'number' }, | ||
currency: { type: 'string' }, | ||
customer: { type: 'string' }, | ||
customer_balance_transaction: { type: 'string' }, | ||
discount_amount: { type: 'number' }, | ||
discount_amounts: { type: 'object' }, | ||
invoice: { type: 'string' }, | ||
lines: { type: 'object' }, | ||
livemode: { type: 'boolean' }, | ||
memo: { type: 'string' }, | ||
metadata: { type: 'object' }, | ||
number: { type: 'string' }, | ||
out_of_band_amount: { type: 'number' }, | ||
pdf: { type: 'string' }, | ||
reason: { type: 'string' }, | ||
refund: { type: 'string' }, | ||
shipping_cost: { type: 'object' }, | ||
status: { type: 'string' }, | ||
subtotal: { type: 'number' }, | ||
subtotal_excluding_tax: { type: 'number' }, | ||
tax_amounts: { type: 'object' }, | ||
total: { type: 'number' }, | ||
total_excluding_tax: { type: 'number' }, | ||
type: { type: 'string' }, | ||
voided_at: { type: 'string' }, | ||
}, | ||
required: ['id'], | ||
} as const |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
{ | ||
"id": "evt_1KJrLuJDPojXS6LNKLCh0CEr", | ||
"object": "event", | ||
"api_version": "2020-03-02", | ||
"created": 1642649422, | ||
"data": { | ||
"object": { | ||
"id": "cn_1MxvRqLkdIwHu7ixY0xbUcxk", | ||
"object": "credit_note", | ||
"amount": 1099, | ||
"amount_shipping": 0, | ||
"created": 1681750958, | ||
"currency": "usd", | ||
"customer": "cus_NjLgPhUokHubJC", | ||
"customer_balance_transaction": null, | ||
"discount_amount": 0, | ||
"discount_amounts": [], | ||
"invoice": "in_1MxvRkLkdIwHu7ixABNtI99m", | ||
"lines": { | ||
"object": "list", | ||
"data": [ | ||
{ | ||
"id": "cnli_1MxvRqLkdIwHu7ixFpdhBFQf", | ||
"object": "credit_note_line_item", | ||
"amount": 1099, | ||
"amount_excluding_tax": 1099, | ||
"description": "T-shirt", | ||
"discount_amount": 0, | ||
"discount_amounts": [], | ||
"invoice_line_item": "il_1MxvRlLkdIwHu7ixnkbntxUV", | ||
"livemode": false, | ||
"quantity": 1, | ||
"tax_amounts": [], | ||
"tax_rates": [], | ||
"type": "invoice_line_item", | ||
"unit_amount": 1099, | ||
"unit_amount_decimal": "1099", | ||
"unit_amount_excluding_tax": "1099" | ||
} | ||
], | ||
"has_more": false, | ||
"url": "/v1/credit_notes/cn_1MxvRqLkdIwHu7ixY0xbUcxk/lines" | ||
}, | ||
"livemode": false, | ||
"memo": null, | ||
"metadata": {}, | ||
"number": "C9E0C52C-0036-CN-01", | ||
"out_of_band_amount": null, | ||
"pdf": "https://pay.stripe.com/credit_notes/acct_1M2JTkLkdIwHu7ix/test_YWNjdF8xTTJKVGtMa2RJd0h1N2l4LF9Oak9FOUtQNFlPdk52UXhFd2Z4SU45alpEd21kd0Y4LDcyMjkxNzU50200cROQsSK2/pdf?s=ap", | ||
"reason": null, | ||
"refund": null, | ||
"shipping_cost": null, | ||
"status": "issued", | ||
"subtotal": 1099, | ||
"subtotal_excluding_tax": 1099, | ||
"tax_amounts": [], | ||
"total": 1099, | ||
"total_excluding_tax": 1099, | ||
"type": "pre_payment", | ||
"voided_at": null | ||
}, | ||
"previous_attributes": { | ||
"custom_fields": null | ||
} | ||
}, | ||
"livemode": false, | ||
"pending_webhooks": 3, | ||
"request": { | ||
"id": "req_m87bnWeVxyQPx0", | ||
"idempotency_key": "010d8300-b837-46e0-a795-6247dd0e05e1" | ||
}, | ||
"type": "credit_note.created" | ||
} |
Oops, something went wrong.