//native
type Paypal = {
clientId: string;
clientSecret: string;
};
type Base64 = 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;
}
/**
* Appeal dispute
* Appeals a dispute, by ID. To appeal a dispute, use the appeal link in the HATEOAS links from the show dispute details response. If this link does not appear, you cannot appeal the dispute. Submit new evidence as a document or notes in the JSON request body. For constraints and rules regarding documents, see documents.To make this request, specify the dispute ID in the URI and specify the evidence in the JSON request body. For information about dispute reasons, see dispute reasons.
*/
export async function main(
auth: Paypal,
id: string,
body: {
"evidence-file"?: {
base64: Base64;
type:
| "image/png"
| "image/jpeg"
| "image/gif"
| "application/pdf"
| "appication/json"
| "text/csv"
| "text/plain"
| "audio/mpeg"
| "audio/wav"
| "video/mp4";
name: string;
};
}
) {
const token = await getToken(auth);
const url = new URL(
`https://api-m.paypal.com/v1/customer/disputes/${id}/appeal`
);
const formData = new FormData();
for (const [k, v] of Object.entries(body)) {
if (v !== undefined) {
if (["evidence-file"].includes(k)) {
const { base64, type, name } = v as {
base64: Base64;
type: string;
name: string;
};
formData.append(
k,
new Blob([Uint8Array.from(atob(base64), (m) => m.codePointAt(0)!)], {
type,
}),
name
);
} else {
formData.append(k, String(v));
}
}
}
const response = await fetch(url, {
method: "POST",
headers: {
Authorization: "Bearer " + token,
},
body: formData,
});
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;
};
type Base64 = string;
/**
* Appeal dispute
* Appeals a dispute, by ID. To appeal a dispute, use the appeal link in the HATEOAS links from the show dispute details response. If this link does not appear, you cannot appeal the dispute. Submit new evidence as a document or notes in the JSON request body. For constraints and rules regarding documents, see documents.To make this request, specify the dispute ID in the URI and specify the evidence in the JSON request body. For information about dispute reasons, see dispute reasons.
*/
export async function main(
auth: Paypal,
id: string,
body: {
"evidence-file"?: {
base64: Base64;
type:
| "image/png"
| "image/jpeg"
| "image/gif"
| "application/pdf"
| "appication/json"
| "text/csv"
| "text/plain"
| "audio/mpeg"
| "audio/wav"
| "video/mp4";
name: string;
};
},
) {
const url = new URL(
`https://api-m.paypal.com/v1/customer/disputes/${id}/appeal`,
);
const formData = new FormData();
for (const [k, v] of Object.entries(body)) {
if (v !== undefined) {
if (["evidence-file"].includes(k)) {
const { base64, type, name } = v as {
base64: Base64;
type: string;
name: string;
};
formData.append(
k,
new Blob([Uint8Array.from(atob(base64), (m) => m.codePointAt(0)!)], {
type,
}),
name,
);
} else {
formData.append(k, String(v));
}
}
}
const response = await fetch(url, {
method: "POST",
headers: {
Authorization: "Bearer " + auth.token,
},
body: formData,
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 428 days ago