forked from PayRam/merchant-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
59 lines (51 loc) · 1.82 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const express = require('express')
const path = require('path')
const axios = require('axios')
const app = express()
const PORT = 8081 // Port for the merchant mock server
app.use(express.urlencoded({ extended: true }))
app.use(express.static(path.join(__dirname))) // Serve static files from the merchant_mock directory
app.use(express.json())
// Serve the mock merchant page
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'))
})
// Handle the form submission by sending a POST request to the Payram server
app.post('/pay-with-payram', async (req, res) => {
try {
const { amount, currency, userId } = req.body
const payramResponse = await axios.post(
'http://localhost:2357/api/v1/payram-payment-session',
{
amount,
currency,
userId,
}
)
console.log('Payram response:', payramResponse.data)
// Redirect the user to the Payram payment page
res.redirect(303, payramResponse.data.url)
} catch (error) {
console.error('Error submitting invoice to Payram:', error)
res.status(500).send('Error processing payment.')
}
})
// Webhook endpoint to receive notifications from Payram
app.post('/payram-webhook', async (req, res) => {
const { invoice_id, reference_id, status, amount, currency, filled_amount, payment_info } = req.body
console.log(
'Received webhook for transaction:',
invoice_id,
'with reference:',
reference_id,
'and status:',
status
)
// Here you would update your merchant's database and trigger any business logic
// For example, confirming an order or updating an account balance
// Respond to the Payram server to acknowledge receipt of the webhook
res.status(200).send({ received: true })
})
app.listen(PORT, () => {
console.log(`Merchant mock server running at http://localhost:${PORT}`)
})