//native
type Paypal = {
clientId: string;
clientSecret: string;
};
async function getToken(auth: Paypal): Promise<string> {
const url = new URL(`https://api-m.paypal.com/v1/oauth2/token`);
const response = await fetch(url, {
method: "POST",
headers: {
Authorization: `Basic ${btoa(`${auth.clientId}:${auth.clientSecret}`)}`,
},
body: new URLSearchParams({
grant_type: "client_credentials",
}),
});
if (!response.ok) {
const text = await response.text();
throw new Error(`Could not get token: ${response.status} ${text}`);
}
const json = await response.json();
return json.access_token;
}
/**
* Record payment for invoice
* Records a payment for the invoice. If no payment is due, the invoice is marked as `PAID`. Otherwise, the invoice is marked as `PARTIALLY PAID`.
*/
export async function main(
auth: Paypal,
invoice_id: string,
body: {
type?: "PAYPAL" | "EXTERNAL";
payment_id?: string;
payment_date?: string;
method:
| "PAYPAL"
| "BANK_TRANSFER"
| "CASH"
| "CHECK"
| "CREDIT_CARD"
| "DEBIT_CARD"
| "WIRE_TRANSFER"
| "OTHER";
note?: string;
amount?: { currency_code: string; value: string };
shipping_info?: { business_name?: string } & {
name?: {
prefix?: string;
given_name?: string;
surname?: string;
middle_name?: string;
suffix?: string;
alternate_full_name?: string;
full_name?: string;
};
address?: {
address_line_1?: string;
address_line_2?: string;
address_line_3?: string;
admin_area_4?: string;
admin_area_3?: string;
admin_area_2?: string;
admin_area_1?: string;
postal_code?: string;
country_code: string;
address_details?: {
street_number?: string;
street_name?: string;
street_type?: string;
delivery_service?: string;
building_name?: string;
sub_building?: string;
};
};
};
},
) {
const token = await getToken(auth);
const url = new URL(
`https://api-m.paypal.com/v2/invoicing/invoices/${invoice_id}/payments`,
);
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
body: JSON.stringify(body),
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 428 days ago
//native
type Paypal = {
token: string;
};
/**
* Record payment for invoice
* Records a payment for the invoice. If no payment is due, the invoice is marked as `PAID`. Otherwise, the invoice is marked as `PARTIALLY PAID`.
*/
export async function main(
auth: Paypal,
invoice_id: string,
body: {
type?: "PAYPAL" | "EXTERNAL";
payment_id?: string;
payment_date?: string;
method:
| "PAYPAL"
| "BANK_TRANSFER"
| "CASH"
| "CHECK"
| "CREDIT_CARD"
| "DEBIT_CARD"
| "WIRE_TRANSFER"
| "OTHER";
note?: string;
amount?: { currency_code: string; value: string };
shipping_info?: { business_name?: string } & {
name?: {
prefix?: string;
given_name?: string;
surname?: string;
middle_name?: string;
suffix?: string;
alternate_full_name?: string;
full_name?: string;
};
address?: {
address_line_1?: string;
address_line_2?: string;
address_line_3?: string;
admin_area_4?: string;
admin_area_3?: string;
admin_area_2?: string;
admin_area_1?: string;
postal_code?: string;
country_code: string;
address_details?: {
street_number?: string;
street_name?: string;
street_type?: string;
delivery_service?: string;
building_name?: string;
sub_building?: string;
};
};
};
},
) {
const url = new URL(
`https://api-m.paypal.com/v2/invoicing/invoices/${invoice_id}/payments`,
);
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + auth.token,
},
body: JSON.stringify(body),
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 428 days ago