//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;
}
/**
* Settle dispute
* Important: This method is for sandbox use only. Settles a dispute in either the customer's or merchant's favor. Merchants can make this call in the sandbox to complete end-to-end dispute resolution testing, which mimics the dispute resolution that PayPal agents normally complete. To make this call, the dispute status must be UNDER_REVIEW and adjudicate link should be available in the HATEOAS links of the show dispute details response.
*/
export async function main(
auth: Paypal,
id: string,
body: { adjudication_outcome: "BUYER_FAVOR" | "SELLER_FAVOR" },
) {
const token = await getToken(auth);
const url = new URL(
`https://api-m.paypal.com/v1/customer/disputes/${id}/adjudicate`,
);
const formData = new FormData();
for (const [k, v] of Object.entries(body)) {
if (v !== undefined) {
formData.append(k, String(v));
}
}
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;
};
/**
* Settle dispute
* Important: This method is for sandbox use only. Settles a dispute in either the customer's or merchant's favor. Merchants can make this call in the sandbox to complete end-to-end dispute resolution testing, which mimics the dispute resolution that PayPal agents normally complete. To make this call, the dispute status must be UNDER_REVIEW and adjudicate link should be available in the HATEOAS links of the show dispute details response.
*/
export async function main(
auth: Paypal,
id: string,
body: { adjudication_outcome: "BUYER_FAVOR" | "SELLER_FAVOR" },
) {
const url = new URL(
`https://api-m.paypal.com/v1/customer/disputes/${id}/adjudicate`,
);
const formData = new FormData();
for (const [k, v] of Object.entries(body)) {
if (v !== undefined) {
formData.append(k, String(v));
}
}
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