
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
paypal-handler
Advanced tools
PayPal Handler is a utility file designed to handle and streamline PayPal API interactions. It provides methods for configuring PayPal, creating payment plans, executing payments, and managing recurring payment cycles.
npm i paypal-handler
Before using the PayPal Handler
, ensure you have:
client_id
and client_secret
).configurePaypal(config)
Configures PayPal SDK with client_id
, client_secret
, and mode
(sandbox/live).
config
(object): Configuration object containing:
mode
(string): PayPal environment (sandbox
or live
).client_id
(string): PayPal API Client ID.client_secret
(string): PayPal API Client Secret.const initializePaypal = async (mode, client_id, client_secret) => {
try {
let configuration = {
mode: mode,
client_id: client_id,
client_secret: client_secret,
};
const { error, message, response } = await configurePaypal(configuration);
if (error) {
console.log(error);
throw new Error(error);
}
return response;
} catch (error) {
return error;
}
};
createOneTimePayment(payload)
Creates a one-time payment transaction.
payload
(object): Validated payload for one-time payment. Must include:
amount
(number): Payment amount.currency
(string): ISO currency code (e.g., USD
).return_url
(string): Redirect URL after payment approval.discount
, discount_type
, and tax
(optional): Payment adjustments.const createOneTimePayment = async (
paypal,
amount,
currency,
discount_type,
discount,
tax,
return_url
) => {
try {
let onetime_payment_obj = {
amount: amount,
currency: currency,
discount_type: discount_type,
discount: discount,
tax: tax,
return_url: return_url,
};
const { error, message, response } = await createPaymentPlanOneTime(
onetime_payment_obj,
paypal
);
if (error) {
console.log(error);
throw new Error(error);
}
return response;
} catch (error) {
return error;
}
};
executePayment(paymentId, payerId)
Executes a PayPal payment after it has been approved by the user.
paymentId
(string): The ID of the payment to execute.payerId
(string): The PayPal payer ID.const paymentId = "PAY-12345";
const payerId = "PAYER-67890";
const executePaymentFunction = async (paypal, paymentId, payerId) => {
try {
let execute_payment_obj = {
payment_id: paymentId,
payer_id:payerId ,
};
const { error, message, response } = await executePayment(
execute_payment_obj,
paypal
);
if (error) {
console.log(error);
throw new Error(error);
}
return response;
} catch (error) {
return error;
}
};
createRecurringPaymentPlan(payload)
Creates a recurring payment plan (e.g., subscriptions).
payload
(object): Validated payload for a recurring payment plan. Must include:
amount
, currency
, frequency
, plan_name
, and trial_period_days
.discount
, discount_type
, tax
, or custom_days
.const createRecurringPayment = async (
paypal,
amount,
currency,
discount_type,
discount,
tax,
return_url,
frequency,
interval_count
) => {
try {
let recurring_payment_obj = {
amount: amount,
currency: currency,
frequency: frequency,
plan_name: "Recurring Payment Plan",
trial_period_days: 0,
return_url: return_url,
discount_type: discount_type,
discount: discount,
tax: tax,
custom_days: interval_count,
};
const { error, message, response } = await createPaymentPlanRecurring(
recurring_payment_obj,
paypal
);
if (error) {
console.log(error);
throw new Error(error);
}
return response;
} catch (error) {
return error;
}
};
createFixedRecurringPayment(payload)
Creates a recurring payment plan with a fixed number of cycles.
payload
(object): Validated payload for fixed-cycle recurring payments.const createFixedRecurringPayment = async (
paypal,
amount,
currency,
discount_type,
discount,
tax,
return_url,
frequency,
interval_count
) => {
try {
let recurring_payment_obj = {
amount: amount,
currency: currency,
frequency: frequency,
plan_name: "Fixed Recurring Payment Plan",
trial_period_days: 0,
cycles: 1,
return_url: return_url,
discount_type: discount_type,
discount: discount,
tax: tax,
custom_days: interval_count,
};
const { error, message, response } = await createPaymentFixedRecurring(
recurring_payment_obj,
paypal
);
if (error) {
console.log(error);
throw new Error(error);
}
return response;
} catch (error) {
return error;
}
};
createInstallmentPayment(payload)
Creates a payment plan based on installments.
payload
(object): Validated payload for installment payments.const createInstallmentsPayment = async (
paypal,
amount,
initial_amount,
currency,
discount_type,
discount,
tax,
return_url,
frequency,
interval_count
) => {
try {
let installments_payment_obj = {
amount: amount,
initial_amount: initial_amount,
currency: currency,
frequency: frequency,
plan_name: "Installments Payment Plan",
trial_period_days: 0,
cycles: 1,
return_url: return_url,
discount_type: discount_type,
discount: discount,
tax: tax,
custom_days: interval_count,
};
const { error, message, response } = await createPaymentInstallments(
installments_payment_obj,
paypal
);
if (error) {
console.log(error);
throw new Error(error);
}
return response;
} catch (error) {
return error;
}
};
executeBillingAgreement(token)
Executes a billing agreement for recurring payments.
token
(string): The token returned from PayPal after user approval.const token = "EC-12345";
const billingAgreementExecuteFunction = async (paypal, token) => {
try {
let billing_agreement_execute_obj = {
token: token,
};
const { error, message, response } = await billingAgreementExecute(
billing_agreement_execute_obj,
paypal
);
if (error) {
console.log(error);
throw new Error(error);
}
return response;
} catch (error) {
return error;
}
};
All functions return a consistent response format to simplify integration:
On Success:
{
"success": true,
"data": "<PayPal API response>"
}
On Failure:
{
"success": false,
"error": "<error details>"
}
Please Refer to Examples Folder for better understanding
This utility is open-source and licensed under the MIT License.
Contributions are welcome! Please feel free to open issues, submit PRs, or suggest improvements.
FAQs
Paypal handler
The npm package paypal-handler receives a total of 3 weekly downloads. As such, paypal-handler popularity was classified as not popular.
We found that paypal-handler demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.