Meteor package for PayPal REST API.
- Basic payments
- Credit card payments
- Adapted forms & templates
meteor add smaltcreation:paypal-rest-api
If you haven't already, sign up for a developer account at: https://developer.paypal.com/ and create a sandbox application.
In your project, create a file server/lib/paypal.js
with your application configuration including:
Smalt.PayPal.configure({
host: 'api.sandbox.paypal.com',
client_id: 'YOUR_CLIENT_ID',
client_secret: 'YOUR_SECRET_KEY'
});
Proceed a payment with a credit card is a two steps work:
First, you should display the form template like this:
You can provide several parameters for the template. Those three are required.
totalAmount
show the total price of your transactioncurrency
€, $, £, wathever you want to displaytax
indicate if it's tax free or notid
the #id of your form. Usefull if you have several form in the same pagenoButton
true if you don't want to display the submit button
Then, you need to look for the event which validate the form like a click on a button or when the form is submited:
Template.smaltPayPalCreditCardForm.events({
'submit form': function (event) {
Meteor.call('youServerSideMethod', PaymentForm.getCreditCard(), function (error, result) {
// Do wathever you want to do client side
// like open a sweet alert with the result or the error
});
}
});
All transactions must be validated server side with your methods !
That's why: Meteor.call('youServerSideMethod'...
Note that PaymentForm.getCreditCard()
returns an object with card's informations exported from our form.
Create a server/methods/paypal.js
file when you are going to place all you logic.
It's a good practice to follow those steps:
- Validations: check if all your informations other than the credit card are good, like if the user is log in
- Set up the amount: if you need to do operation like apply taxes...
- Call the package and create the payment with the card and amount objects
- Transmit errors or the result client side if you need to
youServerSideMethod: function (card) {
/**
* Before calling the package, you need to make sure that your inputs are well submitted.
* Remember : never trust user input.
* That's why validations.
*/
if (!Meteor.userId()) {
throw new Meteor.Error('not-authorized');
}
// Then, set up the amount with your values.
var transactions = [];
transactions.push({
amount: {
total: 42,
currency: EUR
}
});
// Call the package to create the Payment
try {
var payment = Smalt.PayPal.createCardPayment(card, transactions);
// Error are catched, so
// YOUR PAYMENT SUCCEED
// Do whatever you want like loging the payment
// You can also return it to the client
return payment;
} catch (e) {
console.log(e);
throw new Meteor.Error('paypal');
}
}
var transactions = []
is an array of amount
objects who require a total
and a currency
The package is gonna implement a function who automaticly treats those objects with user data.*
And that's all you need to do for a complete credit card payment.