type Stripe = {
token: string;
};
/**
* Post subscriptions
* Creates a new subscription on an existing customer.
*/
export async function main(
auth: Stripe,
body: {
add_invoice_items?: {
price?: string;
price_data?: {
currency: string;
product: string;
tax_behavior?: "exclusive" | "inclusive" | "unspecified";
unit_amount?: number;
unit_amount_decimal?: string;
[k: string]: unknown;
};
quantity?: number;
tax_rates?: string[] | "";
[k: string]: unknown;
}[];
application_fee_percent?: number;
automatic_tax?: {
enabled: boolean;
liability?: {
account?: string;
type: "account" | "self";
[k: string]: unknown;
};
[k: string]: unknown;
};
backdate_start_date?: number;
billing_cycle_anchor?: number;
billing_cycle_anchor_config?: {
day_of_month: number;
hour?: number;
minute?: number;
month?: number;
second?: number;
[k: string]: unknown;
};
billing_thresholds?:
| {
amount_gte?: number;
reset_billing_cycle_anchor?: boolean;
[k: string]: unknown;
}
| "";
cancel_at?: number;
cancel_at_period_end?: boolean;
collection_method?: "charge_automatically" | "send_invoice";
coupon?: string;
currency?: string;
customer: string;
days_until_due?: number;
default_payment_method?: string;
default_source?: string;
default_tax_rates?: string[] | "";
description?: string;
expand?: string[];
invoice_settings?: {
account_tax_ids?: string[] | "";
issuer?: {
account?: string;
type: "account" | "self";
[k: string]: unknown;
};
[k: string]: unknown;
};
items?: {
billing_thresholds?: { usage_gte: number; [k: string]: unknown } | "";
metadata?: { [k: string]: string };
price?: string;
price_data?: {
currency: string;
product: string;
recurring: {
interval: "day" | "month" | "week" | "year";
interval_count?: number;
[k: string]: unknown;
};
tax_behavior?: "exclusive" | "inclusive" | "unspecified";
unit_amount?: number;
unit_amount_decimal?: string;
[k: string]: unknown;
};
quantity?: number;
tax_rates?: string[] | "";
[k: string]: unknown;
}[];
metadata?: { [k: string]: string } | "";
off_session?: boolean;
on_behalf_of?: string | "";
payment_behavior?:
| "allow_incomplete"
| "default_incomplete"
| "error_if_incomplete"
| "pending_if_incomplete";
payment_settings?: {
payment_method_options?: {
acss_debit?:
| {
mandate_options?: {
transaction_type?: "business" | "personal";
[k: string]: unknown;
};
verification_method?: "automatic" | "instant" | "microdeposits";
[k: string]: unknown;
}
| "";
bancontact?:
| {
preferred_language?: "de" | "en" | "fr" | "nl";
[k: string]: unknown;
}
| "";
card?:
| {
mandate_options?: {
amount?: number;
amount_type?: "fixed" | "maximum";
description?: string;
[k: string]: unknown;
};
network?:
| "amex"
| "cartes_bancaires"
| "diners"
| "discover"
| "eftpos_au"
| "interac"
| "jcb"
| "mastercard"
| "unionpay"
| "unknown"
| "visa";
request_three_d_secure?: "any" | "automatic" | "challenge";
[k: string]: unknown;
}
| "";
customer_balance?:
| {
bank_transfer?: {
eu_bank_transfer?: { country: string; [k: string]: unknown };
type?: string;
[k: string]: unknown;
};
funding_type?: string;
[k: string]: unknown;
}
| "";
konbini?: { [k: string]: unknown } | "";
us_bank_account?:
| {
financial_connections?: {
permissions?: (
| "balances"
| "ownership"
| "payment_method"
| "transactions"
)[];
prefetch?: ("balances" | "transactions")[];
[k: string]: unknown;
};
verification_method?: "automatic" | "instant" | "microdeposits";
[k: string]: unknown;
}
| "";
[k: string]: unknown;
};
payment_method_types?:
| (
| "ach_credit_transfer"
| "ach_debit"
| "acss_debit"
| "au_becs_debit"
| "bacs_debit"
| "bancontact"
| "boleto"
| "card"
| "cashapp"
| "customer_balance"
| "eps"
| "fpx"
| "giropay"
| "grabpay"
| "ideal"
| "konbini"
| "link"
| "p24"
| "paynow"
| "paypal"
| "promptpay"
| "sepa_debit"
| "sofort"
| "us_bank_account"
| "wechat_pay"
)[]
| "";
save_default_payment_method?: "off" | "on_subscription";
[k: string]: unknown;
};
pending_invoice_item_interval?:
| {
interval: "day" | "month" | "week" | "year";
interval_count?: number;
[k: string]: unknown;
}
| "";
promotion_code?: string;
proration_behavior?: "always_invoice" | "create_prorations" | "none";
transfer_data?: {
amount_percent?: number;
destination: string;
[k: string]: unknown;
};
trial_end?: "now" | number;
trial_from_plan?: boolean;
trial_period_days?: number;
trial_settings?: {
end_behavior: {
missing_payment_method: "cancel" | "create_invoice" | "pause";
[k: string]: unknown;
};
[k: string]: unknown;
};
}
) {
const url = new URL(`https://api.stripe.com/v1/subscriptions`);
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 291 days ago
type Stripe = {
token: string;
};
/**
* Post subscriptions
* <p>Creates a new subscription on an existing customer.
*/
export async function main(
auth: Stripe,
body: {
add_invoice_items?: {
price?: string;
price_data?: {
currency: string;
product: string;
tax_behavior?: "exclusive" | "inclusive" | "unspecified";
unit_amount?: number;
unit_amount_decimal?: string;
[k: string]: unknown;
};
quantity?: number;
tax_rates?: string[] | "";
[k: string]: unknown;
}[];
application_fee_percent?: number;
automatic_tax?: { enabled: boolean; [k: string]: unknown };
backdate_start_date?: number;
billing_cycle_anchor?: number;
billing_thresholds?:
| {
amount_gte?: number;
reset_billing_cycle_anchor?: boolean;
[k: string]: unknown;
}
| "";
cancel_at?: number;
cancel_at_period_end?: boolean;
collection_method?: "charge_automatically" | "send_invoice";
coupon?: string;
currency?: string;
customer: string;
days_until_due?: number;
default_payment_method?: string;
default_source?: string;
default_tax_rates?: string[] | "";
description?: string;
expand?: string[];
items?: {
billing_thresholds?: { usage_gte: number; [k: string]: unknown } | "";
metadata?: { [k: string]: string };
price?: string;
price_data?: {
currency: string;
product: string;
recurring: {
interval: "day" | "month" | "week" | "year";
interval_count?: number;
[k: string]: unknown;
};
tax_behavior?: "exclusive" | "inclusive" | "unspecified";
unit_amount?: number;
unit_amount_decimal?: string;
[k: string]: unknown;
};
quantity?: number;
tax_rates?: string[] | "";
[k: string]: unknown;
}[];
metadata?: { [k: string]: string } | "";
off_session?: boolean;
on_behalf_of?: string | "";
payment_behavior?:
| "allow_incomplete"
| "default_incomplete"
| "error_if_incomplete"
| "pending_if_incomplete";
payment_settings?: {
payment_method_options?: {
acss_debit?:
| {
mandate_options?: {
transaction_type?: "business" | "personal";
[k: string]: unknown;
};
verification_method?: "automatic" | "instant" | "microdeposits";
[k: string]: unknown;
}
| "";
bancontact?:
| {
preferred_language?: "de" | "en" | "fr" | "nl";
[k: string]: unknown;
}
| "";
card?:
| {
mandate_options?: {
amount?: number;
amount_type?: "fixed" | "maximum";
description?: string;
[k: string]: unknown;
};
network?:
| "amex"
| "cartes_bancaires"
| "diners"
| "discover"
| "eftpos_au"
| "interac"
| "jcb"
| "mastercard"
| "unionpay"
| "unknown"
| "visa";
request_three_d_secure?: "any" | "automatic";
[k: string]: unknown;
}
| "";
customer_balance?:
| {
bank_transfer?: {
eu_bank_transfer?: { country: string; [k: string]: unknown };
type?: string;
[k: string]: unknown;
};
funding_type?: string;
[k: string]: unknown;
}
| "";
konbini?: { [k: string]: unknown } | "";
us_bank_account?:
| {
financial_connections?: {
permissions?: (
| "balances"
| "ownership"
| "payment_method"
| "transactions"
)[];
prefetch?: "balances"[];
[k: string]: unknown;
};
verification_method?: "automatic" | "instant" | "microdeposits";
[k: string]: unknown;
}
| "";
[k: string]: unknown;
};
payment_method_types?:
| (
| "ach_credit_transfer"
| "ach_debit"
| "acss_debit"
| "au_becs_debit"
| "bacs_debit"
| "bancontact"
| "boleto"
| "card"
| "cashapp"
| "customer_balance"
| "fpx"
| "giropay"
| "grabpay"
| "ideal"
| "konbini"
| "link"
| "paynow"
| "paypal"
| "promptpay"
| "sepa_debit"
| "sofort"
| "us_bank_account"
| "wechat_pay"
)[]
| "";
save_default_payment_method?: "off" | "on_subscription";
[k: string]: unknown;
};
pending_invoice_item_interval?:
| {
interval: "day" | "month" | "week" | "year";
interval_count?: number;
[k: string]: unknown;
}
| "";
promotion_code?: string;
proration_behavior?: "always_invoice" | "create_prorations" | "none";
transfer_data?: {
amount_percent?: number;
destination: string;
[k: string]: unknown;
};
trial_end?: "now" | number;
trial_from_plan?: boolean;
trial_period_days?: number;
trial_settings?: {
end_behavior: {
missing_payment_method: "cancel" | "create_invoice" | "pause";
[k: string]: unknown;
};
[k: string]: unknown;
};
}
) {
const url = new URL(`https://api.stripe.com/v1/subscriptions`);
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 419 days ago