[guadec-web-regcfp] Add paypal payment



commit 85abe402b6b3883f28855e126104b1dd80b31943
Author: Patrick Uiterwijk <puiterwijk redhat com>
Date:   Fri Jun 19 14:03:46 2015 +0200

    Add paypal payment

 package.json                                     |    3 +-
 routes/registration.js                           |  117 +++++++++++++++++++++-
 views/index/index.hbs                            |    2 +-
 views/partials/registration/payment.hbs          |    1 +
 views/registration/payment_paypal_registered.hbs |    5 +
 5 files changed, 124 insertions(+), 4 deletions(-)
---
diff --git a/package.json b/package.json
index ffdc6e4..7bbbeb9 100644
--- a/package.json
+++ b/package.json
@@ -20,6 +20,7 @@
     "pg": "*",
     "pg-hstore": "*",
     "browserid-local-verify": "*",
-    "browserid-verify": "*"
+    "browserid-verify": "*",
+    "paypal-rest-sdk": "*"
   }
 }
diff --git a/routes/registration.js b/routes/registration.js
index e7bee0e..1ee6a43 100644
--- a/routes/registration.js
+++ b/routes/registration.js
@@ -8,6 +8,12 @@ var User = models.User;
 var Registration = models.Registration;
 var RegistrationPayment = models.RegistrationPayment;
 
+var env       = process.env.NODE_ENV || "development";
+var config = require('../config/config.json')[env];
+
+var paypal = require('paypal-rest-sdk');
+paypal.configure(config['paypal']);
+
 router.all('/', utils.require_feature("registration"));
 
 router.all('/list', utils.require_permission('registration/view_public'));
@@ -39,6 +45,110 @@ router.post('/pay', function(req, res, next) {
   }
 });
 
+router.all('/pay/paypal/return', utils.require_user);
+router.all('/pay/paypal/return', utils.require_permission('registration/pay_extra'));
+router.get('/pay/paypal/return', function(req, res, next) {
+  console.log("VERIFYING PAYMENT");
+  var execute_payment = {
+    'payer_id': req.query.PayerID,
+    'transactions': [{
+      'amount': req.session.payment['request']['transactions'][0]['amount']
+    }]
+  };
+  console.log('Request');
+  console.log(JSON.stringify(execute_payment));
+  var paymentID = req.session.payment['response']['id'];
+  paypal.payment.execute(paymentID, execute_payment, function(error, payment) {
+    if(!!error) {
+      console.log('ERROR');
+      console.log(JSON.stringify(error));
+      res.status(500).send('Error authorizing payment');
+    } else {
+      console.log('Response: ');
+      console.log(JSON.stringify(payment));
+      var info = {
+        amount: req.session.regfee,
+        paid: payment.state == 'approved',
+        type: 'paypal',
+        details: payment.id
+      };
+      console.log('Storing');
+      console.log(info);
+      RegistrationPayment
+        .create(info)
+        .complete(function(err, payment) {
+          if(!!err) {
+            console.log('Error saving payment: ' + err);
+            res.status(500).send('ERROR saving payment');
+          } else {
+            req.user.getRegistration()
+              .complete(function(err, reg) {
+                reg.addRegistrationPayment(payment)
+                  .complete(function(err) {
+                    if(!!err) {
+                      console.log('Error attaching payment to reg: ' + err);
+                      res.status(500).send('Error attaching payment');
+                    } else {
+                      res.render('registration/payment_paypal_registered', {amount: info.amount});
+                    }
+                  });
+              });
+          }
+        });
+    }
+  });
+});
+
+function execute_paypal(req, res, next, amount) {
+  var create_payment = {
+    'intent': 'sale',
+    'payer': {
+      'payment_method': 'paypal'
+    },
+    'redirect_urls': {
+      'return_url': config['persona_audience'] + '/registration/pay/paypal/return',
+      'cancel_url': config['persona_audience'] + '/registration/pay'
+    },
+    'transactions': [{
+      'item_list': {
+        'items': [{
+          'name': 'GUADEC Registration',
+          'sku': 'regfee',
+          'price': amount.toString(),
+          'currency': config['registration']['currency_value'],
+          'quantity': 1
+        }]
+      },
+      'amount': {
+        'currency': config['registration']['currency_value'],
+        'total': amount.toString()
+      },
+      'description': 'GUADEC Registration fee'
+    }]
+  };
+
+  console.log('*************STARTING PAYMENT***********');
+  console.log('************REQUEST***********');
+  console.log(create_payment);
+  paypal.payment.create(create_payment, function(error, payment) {
+    console.log('***********RESPONSE********');
+    if(!!error) {
+      console.log('ERROR: ');
+      console.log(error);
+      console.log(error['response']['details']);
+      res.error(500).send('Error requesting payment authorization');
+    } else {
+      console.log(payment);
+      req.session.payment = {'request': create_payment, 'response': payment};
+      for(var index = 0; index < payment.links.length; index++) {
+        if(payment.links[index].rel == 'approval_url') {
+          res.redirect(payment.links[index].href);
+        }
+      }
+    }
+  });
+};
+
 router.all('/pay/do', utils.require_user);
 router.all('/pay/do', utils.require_permission('registration/pay_extra'));
 router.post('/pay/do', function(req, res, next) {
@@ -70,6 +180,9 @@ router.post('/pay/do', function(req, res, next) {
             });
         }
       });
+  } else if(method == 'paypal') {
+    req.session.regfee = req.body.regfee;
+    execute_paypal(req, res, next, req.body.regfee);
   } else {
     res.status(402).send('Invalid payment method selected');
   }
@@ -112,7 +225,7 @@ router.post('/register', function(req, res, next) {
   } else {
     return handle_registration();
   }
-}
+});
 
 function handle_registration() {
   req.user.getRegistration({include: [RegistrationPayment]})
@@ -167,7 +280,7 @@ function handle_registration() {
       }
     }
   });
-});
+};
 
 
 router.all('/admin/list', utils.require_user);
diff --git a/views/index/index.hbs b/views/index/index.hbs
index c00ebe9..f2389e5 100644
--- a/views/index/index.hbs
+++ b/views/index/index.hbs
@@ -39,7 +39,7 @@ Hello, please login, or choose an option below.
 {{#ifGTE registration.paid config.registration.min_amount_for_receipt}}
 <div class="button"><a href="/registration/receipt">Get receipt</a></div>
 {{else}}
-Not enough paid for receipt (Paid {{config.registration.currency_symbol}}{{registration.paid}}, outstanding 
for onsite payment: {{config.registration.currency_symbol}}{{registration.outstanding}})<br/>
+Not enough paid for receipt (Paid {{config.registration.currency_symbol}}{{registration.paid}}, outstanding 
for onsite payment: {{config.registration.currency_symbol}}{{registration.outstanding}}, PayPal outstanding: 
{{config.registration.currency_symbol}}{{registration.paypal_outstanding}})<br/>
 {{/ifGTE}}
 {{#has_permission "registration/pay_extra"}}
 <div class="button"><a href="/registration/pay">Pay more</a></div>
diff --git a/views/partials/registration/payment.hbs b/views/partials/registration/payment.hbs
index a2888d4..9475a4f 100644
--- a/views/partials/registration/payment.hbs
+++ b/views/partials/registration/payment.hbs
@@ -1,5 +1,6 @@
 To complete your registration, choose a payment for {{config.registration.currency_symbol}}{{regfee}} 
below:<br/><br/>
 <form class="submission-form" action="/registration/pay/do" method="POST">
+<input type="hidden" name="regfee" value="{{regfee}}">
 <input type="radio" name="method" value="paypal" checked="checked"> Pay using PayPal<br/>
 <input type="radio" name="method" value="on-site" style="vertical-align: top;"> <div style="display: 
inline-block;">Pay at the conference<p class="note">If you select this option, you can pay at the 
registration desk at GUADEC</p></div><br/><br/>
 <input class="submit-talk" type="submit" value="Complete registration" style="float: right;">
diff --git a/views/registration/payment_paypal_registered.hbs 
b/views/registration/payment_paypal_registered.hbs
new file mode 100644
index 0000000..0840ba7
--- /dev/null
+++ b/views/registration/payment_paypal_registered.hbs
@@ -0,0 +1,5 @@
+Thank you!<br/>
+Your PayPal payment has come in, and will be processed.
+It can take up to 5 minutes for your payment to be verified.<br/>
+See you in Sweden!<br />
+<a href="/">Return</a>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]