UNPKG

4.06 kBJavaScriptView Raw
1var _ = require('lodash');
2var async = require('async');
3
4module.exports = function(self, options, paypal_sdk){
5 self.donate = function(req, data, callback){
6 var errors = {};
7 var values = {};
8
9 return self._schemas.convertFields(req, self.schema, 'form', data, values, function(err) {
10
11 if (values.expire_year < 2000) {
12 values.expire_year = values.expire_year + 2000;
13 }
14
15 values.expire_month = parseInt(values.expire_month);
16
17 var payment_details = {
18 "intent": "sale",
19 "payer": {
20 "payment_method": "credit_card",
21 "funding_instruments": [{
22 "credit_card": {
23 "type": values.type,
24 "number": values.number,
25 "expire_month": values.expire_month,
26 "expire_year": values.expire_year,
27 "cvv2": values.cvv2,
28 "first_name": values.first_name,
29 "last_name": values.last_name,
30 "billing_address": {
31 "line1": values.line1,
32 "city": values.city,
33 "state": values.state,
34 "postal_code": values.postal_code,
35 "country_code": values.country_code,
36 "phone": values.phone }}}]},
37 "transactions": [{
38 "amount": {
39 "total": values.total,
40 "currency": "USD",
41 "details": {}
42 },
43 "description": options.description || "Donation"
44 }]
45 };
46
47 return paypal_sdk.payment.create(payment_details, function(error, payment){
48 if(error){
49 console.error(error);
50 if (error.response.details) {
51 console.error(error.response.details);
52 }
53 errors.number = "Your credit card was not accepted. Please double check your information.";
54 return send();
55 } else {
56 var donation = _.cloneDeep(values);
57 donation.when = new Date();
58 delete donation.cvv2;
59 delete donation.expire_month;
60 delete donation.expire_year;
61 donation._id = 'd' + self._apos.generateId();
62 donation.number = donation.number.substr(donation.length - 4, 4);
63
64 return async.series({
65 afterDonate: function(callback){
66 self.afterDonate(req, donation, callback);
67 },
68 storeInDatabase: function(callback) {
69 self._donations.insert(donation, callback);
70 },
71 //expose data for external api
72 //exposeData: self.exposeData,
73 emailDonor: function(callback) {
74 return self.email(
75 req,
76 {
77 email: options.from.email,
78 fullName: options.from.name
79 },
80 {
81 email: values.email,
82 fullName: values.first_name + ' ' + values.last_name
83 },
84 options.thankYouSubject || 'Thank you for your donation',
85 'thankYouEmail',
86 values,
87 callback
88 );
89 },
90 emailRecipient: function(callback){
91 return self.email(
92 req,
93 {
94 email: options.from.email,
95 fullName: options.from.name
96 },
97 {
98 email: options.recipient.email,
99 fullName: options.recipient.name
100 },
101 options.confirmationSubject || 'Donation Received',
102 'recipientEmail',
103 values,
104 callback
105 );
106 }
107 },
108 function(err) {
109 if (err) {
110 console.log(err);
111 }
112 return send();
113 });
114 }
115 });
116 });
117 return send();
118
119 function send() {
120 if(_.isEmpty(errors)) {
121 return callback(null);
122 }
123
124 return callback(errors);
125 }
126 }
127
128 self.afterDonate = function(req, donation, callback){
129 return callback(null);
130 }
131}
\No newline at end of file