Skip to content
This repository was archived by the owner on Apr 6, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
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
78 changes: 76 additions & 2 deletions servers/republik/modules/crowdfundings/express/paymentWebhooks.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const bodyParser = require('body-parser')
const logger = console
const payPledgePF = require('../lib/payments/postfinance/payPledge')
const payPledgePaypal = require('../lib/payments/paypal/payPledge')
const generateMemberships = require('../lib/generateMemberships')
const sendPendingPledgeConfirmations = require('../lib/sendPendingPledgeConfirmations')
Expand Down Expand Up @@ -59,11 +60,84 @@ module.exports = async (server, pgdb, t) => {

// https://e-payment-postfinance.v-psp.com/de/guides/integration%20guides/e-commerce/transaction-feedback#servertoserver-feedback
server.get('/payments/pf', async (req, res) => {
const { query: body } = req
debug('pf %O', body)

await pgdb.public.paymentsLog.insert({
method: 'POSTFINANCECARD',
pspPayload: req.query
pspPayload: body
})
return res.sendStatus(200)

// end connection
res.sendStatus(200)

// payPledge in case that didn't happen already:
// in case users close the paypal tab before being redirected back to us,
// we only get notified about the payment via this webhook.
// accepted status see: servers/republik/modules/crowdfundings/lib/payments/postfinance/payPledge.js
const status = parseInt(body.STATUS)
if (status !== 9 && status !== 91) {
return
}

const pledgeId = body.orderID

const transaction = await pgdb.transactionBegin()
let userId
try {
// load pledge
// FOR UPDATE to wait on other transactions
const pledge = (await transaction.query(`
SELECT *
FROM pledges
WHERE id = :pledgeId
FOR UPDATE
`, {
pledgeId
}))[0]

if (pledge && pledge.status !== 'SUCCESSFUL') {
debug('pf run payPledgePF via webhook')
userId = pledge.userId

const pspPayload = {
...body
}
const pledgeStatus = await payPledgePF({
pledgeId: pledge.id,
total: pledge.total,
pspPayload,
userId,
transaction,
t,
logger
})
if (pledge.status !== pledgeStatus) {
// generate Memberships
if (pledgeStatus === 'SUCCESSFUL') {
await generateMemberships(pledge.id, transaction, t, logger)
}

// update pledge status
await transaction.public.pledges.updateOne({
id: pledge.id
}, {
status: pledgeStatus,
sendConfirmMail: true
})
}
}
await transaction.transactionCommit()
} catch (e) {
await transaction.transactionRollback()
logger.info('transaction rollback', { req: req._log(), error: e })
throw e
}

if (userId) {
// send mail immediately
await sendPendingPledgeConfirmations(userId, pgdb, t)
}
})

// https://developer.paypal.com/docs/integration/direct/webhooks/rest-webhooks/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module.exports = async ({
}

// https://e-payment-postfinance.v-psp.com/de/guides/user%20guides/statuses-and-errors
// see: servers/republik/modules/crowdfundings/express/paymentWebhooks.js
const status = parseInt(STATUS)
if (status !== 9 && status !== 91) {
logger.error('STATUS not successfull', { pledgeId, shasum, SHASIGN, status, pspPayload })
Expand Down