Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing for the use of multiple payment methods #169

Merged
merged 13 commits into from
Sep 21, 2020
Prev Previous commit
Next Next commit
Payment fixes
  • Loading branch information
mrvautin committed Sep 15, 2020
commit c2a16132b98e8816a0d3574d337a7a9d29fb5d4c
6 changes: 3 additions & 3 deletions lib/payments/adyen.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { emptyCart } = require('../cart');
const router = express.Router();

router.post('/setup', async (req, res, next) => {
const adyenConfig = getPaymentConfig();
const adyenConfig = getPaymentConfig('adyen');

const client = new Client({
apiKey: adyenConfig.apiKey,
Expand All @@ -33,14 +33,14 @@ router.post('/setup', async (req, res, next) => {
res.status(200).json({
paymentsResponse,
environment: adyenConfig.environment,
publicKey: adyenConfig.publicKey
originKey: adyenConfig.originKey
});
});

router.post('/checkout_action', async (req, res, next) => {
const db = req.app.db;
const config = req.app.config;
const adyenConfig = getPaymentConfig();
const adyenConfig = getPaymentConfig('adyen');

const client = new Client({
apiKey: adyenConfig.apiKey,
Expand Down
2 changes: 1 addition & 1 deletion lib/payments/authorizenet.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const router = express.Router();
router.post('/checkout_action', (req, res, next) => {
const db = req.app.db;
const config = req.app.config;
const authorizenetConfig = getPaymentConfig();
const authorizenetConfig = getPaymentConfig('authorizenet');

let authorizeUrl = 'https://api.authorize.net/xml/v1/request.api';
if(authorizenetConfig.mode === 'test'){
Expand Down
2 changes: 1 addition & 1 deletion lib/payments/blockonomics.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ router.get('/checkout_return', async (req, res, next) => {
});

router.post('/checkout_action', (req, res, next) => {
const blockonomicsConfig = getPaymentConfig();
const blockonomicsConfig = getPaymentConfig('blockonomics');
const config = req.app.config;
const db = req.app.db;
const blockonomicsParams = {};
Expand Down
11 changes: 5 additions & 6 deletions lib/payments/instore.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ const router = express.Router();
router.post('/checkout_action', async (req, res, next) => {
const db = req.app.db;
const config = req.app.config;
const instoreConfig = getPaymentConfig();
const paymentConfig = getPaymentConfig('instore');

const orderDoc = {
orderPaymentId: getId(),
orderPaymentGateway: 'Instore',
orderPaymentMessage: 'Your payment was successfully completed',
orderTotal: req.session.totalCartAmount,
orderTotal: req.session.totalCartNetAmount,
orderShipping: 0,
orderItemCount: req.session.totalCartItems,
orderProductCount: req.session.totalCartProducts,
Expand All @@ -31,7 +31,7 @@ router.post('/checkout_action', async (req, res, next) => {
orderPostcode: req.session.customerPostcode,
orderPhoneNumber: req.session.customerPhone,
orderComment: req.session.orderComment,
orderStatus: instoreConfig.orderStatus,
orderStatus: paymentConfig.orderStatus,
orderDate: new Date(),
orderProducts: req.session.cart,
orderType: 'Single'
Expand Down Expand Up @@ -73,11 +73,10 @@ router.post('/checkout_action', async (req, res, next) => {
// TODO: Should fix this to properly handle result
sendEmail(req.session.paymentEmailAddr, `Your order with ${config.cartTitle}`, getEmailTemplate(paymentResults));

// redirect to outcome
res.redirect(`/payment/${newId}`);
// Return outcome
res.json({ paymentId: newId });
});
}catch(ex){
console.log('Error sending payment to API', ex);
res.status(400).json({ err: 'Your order declined. Please try again' });
}
});
Expand Down
4 changes: 2 additions & 2 deletions lib/payments/paypal.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ router.get('/checkout_return', (req, res, next) => {
router.post('/checkout_action', (req, res, next) => {
const db = req.app.db;
const config = req.app.config;
const paypalConfig = getPaymentConfig();
const paypalConfig = getPaymentConfig('paypal');

// setup the payment object
const payment = {
Expand Down Expand Up @@ -138,8 +138,8 @@ router.post('/checkout_action', (req, res, next) => {
if(error){
req.session.message = 'There was an error processing your payment. You have not been charged and can try again.';
req.session.messageType = 'danger';
res.redirect('/checkout/payment');
console.log(error);
res.redirect('/checkout/payment');
return;
}
if(payment.payer.payment_method === 'paypal'){
Expand Down
2 changes: 1 addition & 1 deletion lib/payments/payway.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const router = express.Router();
router.post('/checkout_action', async (req, res, next) => {
const db = req.app.db;
const config = req.app.config;
const paymentConfig = getPaymentConfig();
const paymentConfig = getPaymentConfig('payway');

// Create the payload
const payload = {
Expand Down
31 changes: 16 additions & 15 deletions lib/payments/stripe.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ const stripe = require('stripe')(getPaymentConfig().secretKey);
const router = express.Router();

// The homepage of the site
router.post('/checkout_action', (req, res, next) => {
router.post('/checkout_action', async (req, res, next) => {
const db = req.app.db;
const config = req.app.config;
const stripeConfig = getPaymentConfig();
const stripeConfig = getPaymentConfig('stripe');
const stripe = require('stripe')(stripeConfig.secretKey);

// Create the Stripe payload
const chargePayload = {
amount: numeral(req.session.totalCartAmount).format('0.00').replace('.', ''),
currency: stripeConfig.stripeCurrency.toLowerCase(),
source: req.body.stripeToken,
source: req.body.token,
description: stripeConfig.stripeDescription,
shipping: {
name: `${req.session.customerFirstname} ${req.session.customerFirstname}`,
Expand All @@ -34,7 +34,6 @@ router.post('/checkout_action', (req, res, next) => {
// charge via stripe
stripe.charges.create(chargePayload, (err, charge) => {
if(err){
console.info(err.stack);
req.session.messageType = 'danger';
req.session.message = 'Your payment has declined. Please try again';
req.session.paymentApproved = false;
Expand Down Expand Up @@ -99,6 +98,7 @@ router.post('/checkout_action', (req, res, next) => {

// set payment results for email
const paymentResults = {
paymentId: newId,
message: req.session.message,
messageType: req.session.messageType,
paymentEmailAddr: req.session.paymentEmailAddr,
Expand All @@ -115,16 +115,17 @@ router.post('/checkout_action', (req, res, next) => {
// TODO: Should fix this to properly handle result
sendEmail(req.session.paymentEmailAddr, `Your payment with ${config.cartTitle}`, getEmailTemplate(paymentResults));

// redirect to outcome
res.redirect(`/payment/${newId}`);
}else{
// redirect to failure
req.session.messageType = 'danger';
req.session.message = 'Your payment has declined. Please try again';
req.session.paymentApproved = false;
req.session.paymentDetails = `<p><strong>Order ID: </strong>${newId}</p><p><strong>Transaction ID: </strong>${charge.id}</p>`;
res.redirect(`/payment/${newId}`);
// Return the outcome
return res.send(paymentResults);
}
// Return failure
req.session.messageType = 'danger';
req.session.message = 'Your payment has declined. Please try again';
req.session.paymentApproved = false;
req.session.paymentDetails = `<p><strong>Order ID: </strong>${newId}</p><p><strong>Transaction ID: </strong>${charge.id}</p>`;
return res.status(400).json({
paymentId: newId
});
});
});
});
Expand All @@ -133,7 +134,7 @@ router.post('/checkout_action', (req, res, next) => {
// Subscription hook from Stripe
router.all('/subscription_update', async (req, res, next) => {
const db = req.app.db;
const stripeSigSecret = getPaymentConfig().stripeWebhookSecret;
const stripeSigSecret = getPaymentConfig('stripe').stripeWebhookSecret;
const stripeSig = req.headers['stripe-signature'];

let hook;
Expand Down
2 changes: 1 addition & 1 deletion routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ router.get('/blockonomics_payment', (req, res, next) => {
if(req.session.cartSubscription){
paymentType = '_subscription';
}
// show bitcoin address and wait for payment, subscribing to wss

// show bitcoin address and wait for payment, subscribing to wss
res.render(`${config.themeViews}checkout-blockonomics`, {
title: 'Checkout - Payment',
config: req.app.config,
Expand Down
1 change: 1 addition & 0 deletions views/layouts/layout.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
{{#if admin}}
<link rel="stylesheet" href="/stylesheets/admin{{config.env}}.css">
{{/if}}
<script src="https://js.stripe.com/v3/"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js" integrity="sha256-x3YZWtRjM8bJqf48dFAv/qmgL68SI4jqNWeSLMZaMGA=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha256-WqU1JavFxSAMcLP2WIOI+GB2zWmShMI82mTpLDcqFUg=" crossorigin="anonymous"></script>
Expand Down