Skip to content

Commit 4585275

Browse files
authored
Merge pull request #9 from Zefanrakh/development
Development
2 parents 5f96374 + 1f515a2 commit 4585275

File tree

13 files changed

+826
-47
lines changed

13 files changed

+826
-47
lines changed

controllers/payment/stripe.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const stripe = require("../../helpers/stripe");
2+
3+
module.exports = class Controller {
4+
static async stripeCheckout(req, res, next) {
5+
const { package: selectedPackage, quantity, category } = req.body
6+
7+
try {
8+
const products = await stripe.products.list()
9+
const prices = await stripe.prices.list()
10+
const selectedProduct = products.data.find(product => product.name.includes(`${selectedPackage} - ${category}`))
11+
const selectedPrice = prices.data.find(price => price.product === selectedProduct.id)
12+
13+
const checkoutInput = {
14+
success_url: 'http://localhost:3000/checkout/success/{CHECKOUT_SESSION_ID}',
15+
cancel_url: 'http://localhost:3000/checkout/',
16+
payment_method_types: ['card'],
17+
mode: 'payment',
18+
line_items: [{
19+
price: selectedPrice.id,
20+
quantity
21+
}]
22+
}
23+
const session = await stripe.checkout.session.create(checkoutInput)
24+
res.status(200).json(session)
25+
} catch (error) {
26+
next(error)
27+
}
28+
}
29+
30+
static async successCheckout(req, res, next) {
31+
const sessionId = req.params.id
32+
try {
33+
const LineItemList = await stripe.checkout.sessions.listLineItems(sessionId)
34+
res.status(200).json(LineItemList.data[0])
35+
} catch (error) {
36+
next(error)
37+
}
38+
}
39+
}

controllers/payment/xendit.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const xenditInstance = require("../../helpers/xendit")
2+
const axios = require('axios')
3+
const { VirtualAcc } = xenditInstance
4+
const vaSpecificOptions = {}
5+
const va = new VirtualAcc(vaSpecificOptions)
6+
7+
module.exports = class Controller {
8+
9+
static async createVirtualAccount(req, res, next) {
10+
const uuid = await axios.get('https://www.uuidgenerator.net/api/version1')
11+
const { bankCode, expectedAmount: expectedAmt, name } = req.body
12+
const input = { bankCode, name, externalID: uuid.data, isClosed: true, expectedAmt, isSingleUse: true }
13+
try {
14+
const virtualAccount = await va.createFixedVA(input)
15+
res.status(200).json(virtualAccount)
16+
} catch (error) {
17+
next(error)
18+
}
19+
}
20+
21+
static async payVirtualAccount(req, res, next) {
22+
const { externalID, amount } = req.body
23+
const apiKey = Buffer.from('xnd_development_8dXbmGB2nVRvfG5KNoAH79ns9VhT7DkVotShlIoFsegpW4gM3KL01BH52xBHDJ:').toString('base64')
24+
const Authorization = 'Basic ' + apiKey
25+
try {
26+
const response = await axios({
27+
url: `https://api.xendit.co/callback_virtual_accounts/external_id=${externalID}/simulate_payment`,
28+
method: 'POST',
29+
data: { amount },
30+
headers: { Authorization }
31+
})
32+
res.status(200).json(response.data)
33+
} catch (error) {
34+
next(error)
35+
}
36+
}
37+
}

helpers/stripe/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const stripe = require('stripe')('sk_test_51J5y94FGoyuOvgcvjlILEhdea3ODA5XppPu9mILtpziNNhzI2ICBKhQF7Bcs8hqWEtk7yVNiTGEsYdfwALWoP4Yy00wlmdlTPV')
2+
3+
module.exports = stripe

helpers/xendit/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const Xendit = require('xendit-node')
2+
const xenditInstance = new Xendit({
3+
secretKey: 'xnd_development_8dXbmGB2nVRvfG5KNoAH79ns9VhT7DkVotShlIoFsegpW4gM3KL01BH52xBHDJ'
4+
})
5+
6+
module.exports = xenditInstance

middlewares/errorHandler.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const errorHandler = async (err, req, res, next) => {
1+
const errorHandler = (err, req, res, next) => {
22
if (err.status) {
33
res.status(err.status).json({ message: err.msg });
44
} else if (err.errors) {

models/customer.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ module.exports = (sequelize, DataTypes) => {
99
*/
1010
static associate(models) {
1111
// define association here
12-
Customer.belongsTo(models.User)
13-
Customer.hasMany(models.Appointment)
12+
Customer.belongsTo(models.User);
13+
Customer.hasMany(models.Appointment);
1414
}
15+
1516
};
1617
Customer.init({
1718
name: {type:DataTypes.STRING,
@@ -48,5 +49,6 @@ module.exports = (sequelize, DataTypes) => {
4849
sequelize,
4950
modelName: 'Customer',
5051
});
52+
5153
return Customer;
5254
};

models/presencelist.js

+27-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
'use strict';
2-
const {
3-
Model
4-
} = require('sequelize');
1+
"use strict";
2+
const { Model } = require("sequelize");
53
module.exports = (sequelize, DataTypes) => {
64
class PresenceList extends Model {
75
/**
@@ -11,17 +9,30 @@ module.exports = (sequelize, DataTypes) => {
119
*/
1210
static associate(models) {
1311
// define association here
14-
PresenceList.belongsTo(models.Appointment)
12+
PresenceList.belongsTo(models.Appointment);
1513
}
16-
};
17-
PresenceList.init({
18-
dropperName: {
19-
type: DataTypes.STRING,
20-
allowNull:false,
21-
validate: {
22-
notEmpty: {msg: 'Dropper name can not be empty'},
23-
notNull: {msg: 'Dropper name can not null'}
24-
}
14+
}
15+
PresenceList.init(
16+
{
17+
dropperName: {
18+
type: DataTypes.STRING,
19+
allowNull: false,
20+
validate: {
21+
notEmpty: { msg: "Dropper name can not be empty" },
22+
notNull: { msg: "Dropper name can not null" },
23+
},
24+
},
25+
pickuperName: {
26+
type: DataTypes.STRING,
27+
allowNull: false,
28+
validate: {
29+
notEmpty: { msg: "Pickuper name can not be empty" },
30+
notNull: { msg: "Pickuper name can not null" },
31+
},
32+
},
33+
pickupTime: DataTypes.TIME,
34+
presenceDate: DataTypes.DATEONLY,
35+
AppointmentId: DataTypes.INTEGER,
2536
},
2637
pickupperName: {
2738
type: DataTypes.STRING,
@@ -38,5 +49,6 @@ module.exports = (sequelize, DataTypes) => {
3849
sequelize,
3950
modelName: 'PresenceList',
4051
});
52+
4153
return PresenceList;
42-
};
54+
};

models/price.js

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
'use strict';
2-
const {
3-
Model
4-
} = require('sequelize');
1+
"use strict";
2+
const { Model } = require("sequelize");
53
module.exports = (sequelize, DataTypes) => {
64
class Price extends Model {
75
/**
@@ -11,16 +9,19 @@ module.exports = (sequelize, DataTypes) => {
119
*/
1210
static associate(models) {
1311
// define association here
14-
Price.hasMany(models.Appointment)
12+
Price.hasMany(models.Appointment);
1513
}
16-
};
17-
Price.init({
18-
category: DataTypes.STRING,
19-
package: DataTypes.STRING,
20-
price: DataTypes.INTEGER
21-
}, {
22-
sequelize,
23-
modelName: 'Price',
24-
});
14+
}
15+
Price.init(
16+
{
17+
category: DataTypes.STRING,
18+
package: DataTypes.STRING,
19+
price: DataTypes.INTEGER,
20+
},
21+
{
22+
sequelize,
23+
modelName: "Price",
24+
}
25+
);
2526
return Price;
26-
};
27+
};

models/user.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = (sequelize, DataTypes) => {
99
*/
1010
static associate(models) {
1111
// define association here
12-
User.hasOne(models.Customer)
12+
User.hasOne(models.Customer);
1313
}
1414
};
1515
User.init({
@@ -21,5 +21,6 @@ module.exports = (sequelize, DataTypes) => {
2121
sequelize,
2222
modelName: 'User',
2323
});
24+
2425
return User;
2526
};

0 commit comments

Comments
 (0)