-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(payment): user pay for premium article
- setup payment processor - setup db for payment - grant user article access on payment success [Delivers #159593957]
- Loading branch information
Showing
20 changed files
with
2,372 additions
and
2,201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import {} from 'dotenv/config'; | ||
|
||
module.exports = { | ||
development: { | ||
username: process.env.DB_USER, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import stripePackage from 'stripe'; | ||
import db from '../models'; | ||
import utils from '../helpers/utilities'; | ||
|
||
const { Payment } = db; | ||
/** * Class representing payments */ | ||
export default class PaymentController { | ||
/** | ||
* Opens up article for user if payment is successful | ||
* @param {*} req - Request object | ||
* @param {*} res - Response object | ||
* @param {function} next - for errors | ||
* @returns {link} redirects to create payment | ||
*/ | ||
static makePayment(req, res, next) { | ||
const stripe = stripePackage(process.env.STRIPE_SECRET_KEY); | ||
stripe.customers.create({ | ||
email: req.body.email, | ||
source: req.body.stripeToken | ||
}) | ||
.then(customer => stripe.charges.create({ | ||
amount: req.body.amount * 100, | ||
description: `Payment for ${req.params.slug}`, | ||
currency: 'usd', | ||
customer: customer.id | ||
})) | ||
.then(res.redirect(307, `/api/pay/${req.params.slug}/success`)) | ||
.catch(next); | ||
} | ||
|
||
/** | ||
* Opens up article for user if payment is successful | ||
* @param {*} req - Request object | ||
* @param {*} res - Response object | ||
* @param {function} next - for errors | ||
* @returns {boolean} representing if the payment was successful or not | ||
*/ | ||
static afterPayment(req, res, next) { | ||
const { | ||
amount, | ||
stripeToken, | ||
stripeTokenType, | ||
stripeEmail | ||
} = req.body; | ||
Payment.create({ | ||
userId: req.userId, | ||
articleId: req.articleObject.id, | ||
amount, | ||
stripeToken, | ||
stripeTokenType, | ||
stripeEmail, | ||
}) | ||
.then(() => res.status(200).json({ | ||
payment: { | ||
message: 'Payment Successful', | ||
amount, | ||
article: req.articleObject, | ||
user: utils.userToJson(req.userObject), | ||
} | ||
})) | ||
.catch(next); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Op } from 'sequelize'; | ||
import db from '../models'; | ||
|
||
const { Payment } = db; | ||
|
||
/** | ||
* Processes payment for a user | ||
* @param {*} req - Request object | ||
* @param {*} res - Response object | ||
* @param {function} next - for errors | ||
* @returns {boolean} representing if the payment was successful or not | ||
*/ | ||
const beforePayment = (req, res, next) => { | ||
Payment.find({ | ||
where: { | ||
[Op.and]: [ | ||
{ userId: req.userId }, | ||
{ articleId: req.articleObject.id } | ||
], | ||
}, | ||
}) | ||
.then((payment) => { | ||
if (!payment) { | ||
next(); | ||
} | ||
return res.status(400).json({ | ||
success: false, | ||
errors: { | ||
body: ['You already purchased this article'] | ||
} | ||
}); | ||
}) | ||
.catch(next); | ||
}; | ||
|
||
export default beforePayment; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import verifyToken from './verifyToken'; | ||
|
||
const toggleFree = (req, res, next) => { | ||
if (req.articleObject.isPaidFor === true) { | ||
return verifyToken(req, res, next); | ||
} | ||
next(); | ||
}; | ||
|
||
export default toggleFree; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('Payments', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
userId: { | ||
type: Sequelize.INTEGER, | ||
onDelete: 'CASCADE', | ||
references: { | ||
model: 'Users', | ||
key: 'id', | ||
as: 'userId', | ||
}, | ||
}, | ||
articleId: { | ||
type: Sequelize.INTEGER, | ||
onDelete: 'CASCADE', | ||
references: { | ||
model: 'Articles', | ||
key: 'id', | ||
as: 'articleId', | ||
}, | ||
}, | ||
amount: { | ||
type: Sequelize.INTEGER | ||
}, | ||
stripeToken: { | ||
type: Sequelize.STRING | ||
}, | ||
stripeTokenType: { | ||
type: Sequelize.STRING | ||
}, | ||
stripeEmail: { | ||
type: Sequelize.STRING | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
down: queryInterface/* , Sequelize */ => queryInterface.dropTable('Payments') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const Payment = sequelize.define('Payment', { | ||
amount: { | ||
type: DataTypes.INTEGER, | ||
allowNull: false, | ||
}, | ||
stripeToken: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
stripeTokenType: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
stripeEmail: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
}, {}); | ||
|
||
Payment.associate = (models) => { | ||
Payment.belongsTo( | ||
models.Article, | ||
{ | ||
foreignKey: 'articleId', | ||
onDelete: 'CASCADE', | ||
} | ||
); | ||
Payment.belongsTo( | ||
models.User, | ||
{ | ||
foreignKey: 'userId', | ||
onDelete: 'CASCADE', | ||
} | ||
); | ||
}; | ||
return Payment; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.