PayPal Rest API
PayPal Rest API Package Built In TypeScript Using Axios.
Installation
Using NPM
npm i @orjdev/paypal
Creating a client
import PayPalAPI from "@orjdev/paypal";
const client = new PayPalAPI.Client({
mode: "sandbox",
clientID: "",
clientSecret: "",
});
Basics
export type IResponse<T> = {
error: INullable<IError>;
data?: T;
};
let newFunction = client.successOrThrow(client.getOrder);
Orders
Create
async function myFunction() {
let newFunction = client.successOrThrow(client.createOrder);
try {
let response = await newFunction({
intent: "CAPTURE",
purchase_units: [
{
amount: {
currency_code: "USD",
value: "1.00",
},
},
],
});
console.log(response);
} catch (e) {
console.log(e);
}
let response = await client.createOrder({
intent: "CAPTURE",
purchase_units: [
{
amount: {
currency_code: "USD",
value: "1.00",
},
},
],
});
if (response.error) {
console.log(response.error);
} else {
console.log(response.data);
}
}
Show Order Details
async function myFunction() {
let newFunction = client.successOrThrow(client.getOrder);
try {
let response = await newFunction("F2FSDFSF9DF");
console.log(response);
} catch (e) {
console.log(e);
}
let response = await client.getOrder("F2FSDFSF9DF");
if (response.error) {
console.log(response.error);
} else {
console.log(response.data);
}
}
Authorize Order
async function myFunction() {
let newFunction = client.successOrThrow(client.authorizeOrder);
try {
let response = await newFunction("F2FSDFSF9DF", {
payment_source: {
id: "F5Z1LXZ1Z1-X1",
type: "BILLING_AGREEMENT",
},
});
console.log(response);
} catch (e) {
console.log;
}
let response = await client.authorizeOrder("F2FSDFSF9DF", {
payment_source: {
id: "F5Z1LXZ1Z1-X1",
type: "BILLING_AGREEMENT",
},
});
if (response.error) {
console.log(response.error);
} else {
console.log(response.data);
}
}
Capture Order
async function myFunction() {
let newFunction = client.successOrThrow(client.captureOrder);
try {
let response = await newFunction("F2FSDFSF9DF", {
payment_source: {
id: "F5Z1LXZ1Z1-X1",
type: "BILLING_AGREEMENT",
},
});
console.log(response);
} catch (e) {
console.log;
}
let response = await client.captureOrder("F2FSDFSF9DF", {
payment_source: {
id: "F5Z1LXZ1Z1-X1",
type: "BILLING_AGREEMENT",
},
});
if (response.error) {
console.log(response.error);
} else {
console.log(response.data);
}
}
Confirm Order
async function myFunction() {
let newFunction = client.successOrThrow(client.confirmOrder);
try {
let response = await newFunction("F2FSDFSF9DF", {
token: {
id: "F5Z1LXZ1Z1-X1",
type: "BILLING_AGREEMENT",
},
});
console.log(response);
} catch (e) {
console.log;
}
let response = await client.confirmOrder("F2FSDFSF9DF", {
token: {
id: "F5Z1LXZ1Z1-X1",
type: "BILLING_AGREEMENT",
},
});
if (response.error) {
console.log(response.error);
} else {
console.log(response.data);
}
}