Tiny PayPal
The missing PayPal tool for Node
PayPal has literally A TON of documentation, but little to none is targeted for NodeJS users.
This is a simple tool to create payments using the modern and elegant version of PayPal, solving hours of research, trial and error.
Install the module
yarn add tiny-paypal# npm install tiny-paypal
Initialize it
const PayPal = ; const CLIENT_ID = "your-client-id-here";const CLIENT_SECRET = "your-client-secret-here";const SUCCESS_CALLBACK_URL = "https://www.your-store.com/success";const CANCEL_CALLBACK_URL = "https://www.your-store.com/checkout"; const defaults = sandbox: true // leave blank for production (default) currency: "EUR" // by default "USD" successCallbackUrl: SUCCESS_CALLBACK_URL cancelCallbackUrl: CANCEL_CALLBACK_URL; const paypal = CLIENT_ID CLIENT_SECRET defaults;
Create a simple payment
If you just want to charge an amount of money, you can invoke this method:
try const payment = await paypal; // Handle the result here console; console; // Redirect your client's browser to this URL console; // Get the payment info from this URL console; // Execute the payment through this URL (payment approval is needed)catch error // Handle the error console;
Create a payment based on your cart
To display a list of the items on the PayPal screen, you can use TinyPaypal like this:
try const payment = await paypal; // Handle the result here console; console; // Redirect your client's browser to this URL console; // Get the payment info from this URL console; // Execute the payment through this URL (payment approval is needed)catch error // Handle the error console;
Redirect
Next, you need to redirect your client to result.redirect
. When the client confirms the payment, PayPal will redirect the browser to the SUCCESS_CALLBACK_URL
with three query string parameters: paymentId
, token
and PayerID
.
Get these parameters and execute the payment
try const result = await paypal; console;catch error // Handle the error console;
This will log to the console something like:
Response: { id: 'PAY-1NA38651KW861730HK6HVNHQ',
intent: 'sale',
state: 'approved',
cart: '9H784395YC168205K',
payer:
{ payment_method: 'paypal',
status: 'VERIFIED',
payer_info:
{
email: 'buyer@company.pro',
first_name: 'Test',
last_name: 'Buyer',
payer_id: 'MEFWRPEQDM(2J',
shipping_address: [Object],
country_code: 'ES',
billing_address: [Object] } },
transactions:
[ { amount: [Object],
payee: [Object],
description: 'ACME Corporation Store',
item_list: [Object],
related_resources: [Object] } ],
create_time: '2016-07-20T11:45:47Z',
links:
[ { href: 'https://api.sandbox.paypal.com/v1/payments/payment/PAY-1NA38651KW861730HK6HVNHQ',
rel: 'self',
method: 'GET'
}
]
}