type Stripe = {
token: string;
};
/**
* Post customers customer sources id
* Update a specified source for a given customer.
*/
export async function main(
auth: Stripe,
customer: string,
id: string,
body: {
account_holder_name?: string;
account_holder_type?: "company" | "individual";
address_city?: string;
address_country?: string;
address_line1?: string;
address_line2?: string;
address_state?: string;
address_zip?: string;
exp_month?: string;
exp_year?: string;
expand?: string[];
metadata?: { [k: string]: string } | "";
name?: string;
owner?: {
address?: {
city?: string;
country?: string;
line1?: string;
line2?: string;
postal_code?: string;
state?: string;
[k: string]: unknown;
};
email?: string;
name?: string;
phone?: string;
[k: string]: unknown;
};
}
) {
const url = new URL(
`https://api.stripe.com/v1/customers/${customer}/sources/${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 291 days ago
type Stripe = {
token: string;
};
/**
* Post customers customer sources id
* <p>Update a specified source for a given customer.</p>
*/
export async function main(
auth: Stripe,
customer: string,
id: string,
body: {
account_holder_name?: string;
account_holder_type?: "company" | "individual";
address_city?: string;
address_country?: string;
address_line1?: string;
address_line2?: string;
address_state?: string;
address_zip?: string;
exp_month?: string;
exp_year?: string;
expand?: string[];
metadata?: { [k: string]: string } | "";
name?: string;
owner?: {
address?: {
city?: string;
country?: string;
line1?: string;
line2?: string;
postal_code?: string;
state?: string;
[k: string]: unknown;
};
email?: string;
name?: string;
phone?: string;
[k: string]: unknown;
};
}
) {
const url = new URL(
`https://api.stripe.com/v1/customers/${customer}/sources/${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 419 days ago