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

Development #9

Merged
merged 7 commits into from
Jun 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add payment
  • Loading branch information
mwitaryo committed Jun 26, 2021
commit ab78cf402a5216f40979e77c224ad5b834a13622
39 changes: 39 additions & 0 deletions controllers/payment/stripe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const stripe = require("../../helpers/stripe");

module.exports = class Controller {
static async stripeCheckout(req, res, next) {
const { package: selectedPackage, quantity, category } = req.body

try {
const products = await stripe.products.list()
const prices = await stripe.prices.list()
const selectedProduct = products.data.find(product => product.name.includes(`${selectedPackage} - ${category}`))
const selectedPrice = prices.data.find(price => price.product === selectedProduct.id)

const checkoutInput = {
success_url: 'http://localhost:3000/checkout/success/{CHECKOUT_SESSION_ID}',
cancel_url: 'http://localhost:3000/checkout/',
payment_method_types: ['card'],
mode: 'payment',
line_items: [{
price: selectedPrice.id,
quantity
}]
}
const session = await stripe.checkout.session.create(checkoutInput)
res.status(200).json(session)
} catch (error) {
next(error)
}
}

static async successCheckout(req, res, next) {
const sessionId = req.params.id
try {
const LineItemList = await stripe.checkout.sessions.listLineItems(sessionId)
res.status(200).json(LineItemList.data[0])
} catch (error) {
next(error)
}
}
}
37 changes: 37 additions & 0 deletions controllers/payment/xendit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const xenditInstance = require("../../helpers/xendit")
const axios = require('axios')
const { VirtualAcc } = xenditInstance
const vaSpecificOptions = {}
const va = new VirtualAcc(vaSpecificOptions)

module.exports = class Controller {

static async createVirtualAccount(req, res, next) {
const uuid = await axios.get('https://www.uuidgenerator.net/api/version1')
const { bankCode, expectedAmount: expectedAmt, name } = req.body
const input = { bankCode, name, externalID: uuid.data, isClosed: true, expectedAmt, isSingleUse: true }
try {
const virtualAccount = await va.createFixedVA(input)
res.status(200).json(virtualAccount)
} catch (error) {
next(error)
}
}

static async payVirtualAccount(req, res, next) {
const { externalID, amount } = req.body
const apiKey = Buffer.from('xnd_development_8dXbmGB2nVRvfG5KNoAH79ns9VhT7DkVotShlIoFsegpW4gM3KL01BH52xBHDJ:').toString('base64')
const Authorization = 'Basic ' + apiKey
try {
const response = await axios({
url: `https://api.xendit.co/callback_virtual_accounts/external_id=${externalID}/simulate_payment`,
method: 'POST',
data: { amount },
headers: { Authorization }
})
res.status(200).json(response.data)
} catch (error) {
next(error)
}
}
}
3 changes: 3 additions & 0 deletions helpers/stripe/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const stripe = require('stripe')('sk_test_51J5y94FGoyuOvgcvjlILEhdea3ODA5XppPu9mILtpziNNhzI2ICBKhQF7Bcs8hqWEtk7yVNiTGEsYdfwALWoP4Yy00wlmdlTPV')

module.exports = stripe
6 changes: 6 additions & 0 deletions helpers/xendit/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const Xendit = require('xendit-node')
const xenditInstance = new Xendit({
secretKey: 'xnd_development_8dXbmGB2nVRvfG5KNoAH79ns9VhT7DkVotShlIoFsegpW4gM3KL01BH52xBHDJ'
})

module.exports = xenditInstance
Loading