type Stripe = {
token: string;
};
/**
* Post invoices invoice lines line item id
* Updates an invoice’s line item. Some fields, such as tax_amounts, only live on the invoice line item,
so they can only be updated through this endpoint. Other fields, such as amount, live on both the invoice
item and the invoice line item, so updates on this endpoint will propagate to the invoice item as well.
Updating an invoice’s line item is only possible before the invoice is finalized.
*/
export async function main(
auth: Stripe,
invoice: string,
line_item_id: string,
body: {
amount?: number;
description?: string;
discountable?: boolean;
discounts?:
| { coupon?: string; discount?: string; [k: string]: unknown }[]
| "";
expand?: string[];
metadata?: { [k: string]: string } | "";
period?: { end: number; start: number; [k: string]: unknown };
price?: string;
price_data?: {
currency: string;
product?: string;
product_data?: {
description?: string;
images?: string[];
metadata?: { [k: string]: string };
name: string;
tax_code?: string;
[k: string]: unknown;
};
tax_behavior?: "exclusive" | "inclusive" | "unspecified";
unit_amount?: number;
unit_amount_decimal?: string;
[k: string]: unknown;
};
quantity?: number;
tax_amounts?:
| {
amount: number;
tax_rate_data: {
country?: string;
description?: string;
display_name: string;
inclusive: boolean;
jurisdiction?: string;
percentage: number;
state?: string;
tax_type?:
| "amusement_tax"
| "communications_tax"
| "gst"
| "hst"
| "igst"
| "jct"
| "lease_tax"
| "pst"
| "qst"
| "rst"
| "sales_tax"
| "vat";
[k: string]: unknown;
};
taxable_amount: number;
[k: string]: unknown;
}[]
| "";
tax_rates?: string[] | "";
}
) {
const url = new URL(
`https://api.stripe.com/v1/invoices/${invoice}/lines/${line_item_id}`
);
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Authorization: "Bearer " + auth.token,
},
body: encodeParams(body),
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
function encodeParams(o: any) {
function iter(o: any, path: string) {
if (Array.isArray(o)) {
o.forEach(function (a) {
iter(a, path + "[]");
});
return;
}
if (o !== null && typeof o === "object") {
Object.keys(o).forEach(function (k) {
iter(o[k], path + "[" + k + "]");
});
return;
}
data.push(path + "=" + o);
}
const data: string[] = [];
Object.keys(o).forEach(function (k) {
if (o[k] !== undefined) {
iter(o[k], k);
}
});
return new URLSearchParams(data.join("&"));
}
Submitted by hugo697 368 days ago
type Stripe = {
token: string;
};
/**
* Post invoices invoice lines line item id
* Updates an invoice’s line item. Some fields, such as tax_amounts, only live on the invoice line item,
so they can only be updated through this endpoint. Other fields, such as amount, live on both the invoice
item and the invoice line item, so updates on this endpoint will propagate to the invoice item as well.
Updating an invoice’s line item is only possible before the invoice is finalized.
*/
export async function main(
auth: Stripe,
invoice: string,
line_item_id: string,
body: {
amount?: number;
description?: string;
discountable?: boolean;
discounts?:
| { coupon?: string; discount?: string; [k: string]: unknown }[]
| "";
expand?: string[];
metadata?: { [k: string]: string } | "";
period?: { end: number; start: number; [k: string]: unknown };
price?: string;
price_data?: {
currency: string;
product?: string;
product_data?: {
description?: string;
images?: string[];
metadata?: { [k: string]: string };
name: string;
tax_code?: string;
[k: string]: unknown;
};
tax_behavior?: "exclusive" | "inclusive" | "unspecified";
unit_amount?: number;
unit_amount_decimal?: string;
[k: string]: unknown;
};
quantity?: number;
tax_amounts?:
| {
amount: number;
tax_rate_data: {
country?: string;
description?: string;
display_name: string;
inclusive: boolean;
jurisdiction?: string;
percentage: number;
state?: string;
tax_type?:
| "amusement_tax"
| "communications_tax"
| "gst"
| "hst"
| "igst"
| "jct"
| "lease_tax"
| "pst"
| "qst"
| "rst"
| "sales_tax"
| "vat";
[k: string]: unknown;
};
taxable_amount: number;
[k: string]: unknown;
}[]
| "";
tax_rates?: string[] | "";
}
) {
const url = new URL(
`https://api.stripe.com/v1/invoices/${invoice}/lines/${line_item_id}`
);
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Authorization: "Bearer " + auth.token,
},
body: encodeParams(body),
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
function encodeParams(o: any) {
function iter(o: any, path: string) {
if (Array.isArray(o)) {
o.forEach(function (a) {
iter(a, path + "[]");
});
return;
}
if (o !== null && typeof o === "object") {
Object.keys(o).forEach(function (k) {
iter(o[k], path + "[" + k + "]");
});
return;
}
data.push(path + "=" + o);
}
const data: string[] = [];
Object.keys(o).forEach(function (k) {
if (o[k] !== undefined) {
iter(o[k], k);
}
});
return new URLSearchParams(data.join("&"));
}
Submitted by hugo697 795 days ago