//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;
}
/**
* List transactions
* Lists transactions. Specify one or more query parameters to filter the transaction that appear in the response.Notes: If you specify one or more optional query parameters, the ending_balance response field is empty.It takes a maximum of three hours for executed transactions to appear in the list transactions call.This call lists transaction for the previous three years.
*/
export async function main(
auth: Paypal,
transaction_id: string | undefined,
transaction_type: string | undefined,
transaction_status: string | undefined,
transaction_amount: string | undefined,
transaction_currency: string | undefined,
start_date: string | undefined,
end_date: string | undefined,
payment_instrument_type: string | undefined,
store_id: string | undefined,
terminal_id: string | undefined,
fields: string | undefined,
balance_affecting_records_only: string | undefined,
page_size: string | undefined,
page: string | undefined
) {
const token = await getToken(auth);
const url = new URL(
`https://api-m.paypal.com/v1/reporting/v1/reporting/transactions`
);
for (const [k, v] of [
["transaction_id", transaction_id],
["transaction_type", transaction_type],
["transaction_status", transaction_status],
["transaction_amount", transaction_amount],
["transaction_currency", transaction_currency],
["start_date", start_date],
["end_date", end_date],
["payment_instrument_type", payment_instrument_type],
["store_id", store_id],
["terminal_id", terminal_id],
["fields", fields],
["balance_affecting_records_only", balance_affecting_records_only],
["page_size", page_size],
["page", page],
]) {
if (v !== undefined && v !== "" && k !== undefined) {
url.searchParams.append(k, v);
}
}
const response = await fetch(url, {
method: "GET",
headers: {
Authorization: "Bearer " + token,
},
body: undefined,
});
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;
};
/**
* List transactions
* Lists transactions. Specify one or more query parameters to filter the transaction that appear in the response.Notes: If you specify one or more optional query parameters, the ending_balance response field is empty.It takes a maximum of three hours for executed transactions to appear in the list transactions call.This call lists transaction for the previous three years.
*/
export async function main(
auth: Paypal,
transaction_id: string | undefined,
transaction_type: string | undefined,
transaction_status: string | undefined,
transaction_amount: string | undefined,
transaction_currency: string | undefined,
start_date: string | undefined,
end_date: string | undefined,
payment_instrument_type: string | undefined,
store_id: string | undefined,
terminal_id: string | undefined,
fields: string | undefined,
balance_affecting_records_only: string | undefined,
page_size: string | undefined,
page: string | undefined,
) {
const url = new URL(
`https://api-m.paypal.com/v1/reporting/v1/reporting/transactions`,
);
for (const [k, v] of [
["transaction_id", transaction_id],
["transaction_type", transaction_type],
["transaction_status", transaction_status],
["transaction_amount", transaction_amount],
["transaction_currency", transaction_currency],
["start_date", start_date],
["end_date", end_date],
["payment_instrument_type", payment_instrument_type],
["store_id", store_id],
["terminal_id", terminal_id],
["fields", fields],
["balance_affecting_records_only", balance_affecting_records_only],
["page_size", page_size],
["page", page],
]) {
if (v !== undefined && v !== "" && k !== undefined) {
url.searchParams.append(k, v);
}
}
const response = await fetch(url, {
method: "GET",
headers: {
Authorization: "Bearer " + auth.token,
},
body: undefined,
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 428 days ago