-
-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathwebhook-platform.ts
More file actions
97 lines (90 loc) · 4.18 KB
/
Copy pathwebhook-platform.ts
File metadata and controls
97 lines (90 loc) · 4.18 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* eslint-disable no-console */
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
const stripe = require('../../../client/payment/stripe').default()
const chargeSucceeded = require('../../../modules/webhooks/chargeSucceeded')
const checkoutSessionCompleted = require('../../../modules/webhooks/checkoutSessionCompleted')
const customerSourceCreated = require('../../../modules/webhooks/customerSourceCreated')
const chargeUpdated = require('../../../modules/webhooks/chargeUpdated')
const chargeFailed = require('../../../modules/webhooks/chargeFailed')
import { handleChargeRefunded } from '../../../modules/webhooks/charges/chargeRefunded/chargeRefunded'
import {
chargeDisputeCreatedWebhookHandler,
chargeDisputeClosedWebhookHandler
} from '../../../modules/webhooks/charges'
const invoiceCreated = require('../../../modules/webhooks/invoiceCreated')
const invoiceUpdated = require('../../../modules/webhooks/invoiceUpdated')
const invoicePaid = require('../../../modules/webhooks/invoicePaid')
const invoiceFinalized = require('../../../modules/webhooks/invoiceFinalized')
import { transferCreated } from '../../../modules/webhooks/transfers'
import { transferReversed } from '../../../modules/webhooks/transfers'
import { chargeDisputeFundsWithdrawnWebhookHandler } from '../../../modules/webhooks/charges/chargeDisputeFundsWithdrawn/chargeDisputeFundsWithdrawn'
const balanceAvailable = require('../../../modules/webhooks/balanceAvailable')
const invoicePaymentSucceeded = require('../../../modules/webhooks/invoicePaymentSucceeded')
const invoicePaymentFailed = require('../../../modules/webhooks/invoicePaymentFailed')
exports.webhookPlatform = async (req: any, res: any) => {
const sig = req.headers['stripe-signature']
const secret = process.env.STRIPE_WEBHOOK_SECRET_PLATFORM
let event
try {
if (process.env.NODE_ENV === 'test') {
event =
typeof req.body === 'string' || Buffer.isBuffer(req.body)
? JSON.parse(req.body.toString())
: req.body
} else {
event = stripe.webhooks.constructEvent(req.body, sig, secret)
}
} catch (err: any) {
console.error('❌ Webhook signature verification failed:', err.message)
return res.status(400).send(`Webhook Error: ${err.message}`)
}
console.log('✅ Received event:', event.type)
if (event) {
const paid = event.data.object.paid || false
const status = event.data.object.status
switch (event.type) {
case 'customer.source.created':
return customerSourceCreated(event, req, res)
case 'charge.updated':
return chargeUpdated(event, paid, status, req, res)
case 'charge.refunded':
return handleChargeRefunded(event, req, res)
case 'charge.succeeded':
return chargeSucceeded(event, paid, status, req, res)
case 'charge.failed':
return chargeFailed(event, paid, status, req, res)
case 'charge.dispute.created':
return chargeDisputeCreatedWebhookHandler(event, req, res)
case 'charge.dispute.funds_withdrawn':
return chargeDisputeFundsWithdrawnWebhookHandler(event, req, res)
case 'charge.dispute.closed':
return chargeDisputeClosedWebhookHandler(event, req, res)
case 'invoice.created':
return await invoiceCreated(event, req, res)
case 'invoice.updated':
return await invoiceUpdated(event, req, res)
case 'invoice.paid':
return await invoicePaid(event, req, res)
case 'invoice.finalized':
return await invoiceFinalized(event, req, res)
case 'transfer.created':
return await transferCreated(event, req, res)
case 'transfer.reversed':
return await transferReversed(event, req, res)
case 'balance.available':
return balanceAvailable(event, req, res)
case 'invoice.payment_succeeded':
return await invoicePaymentSucceeded(event, req, res)
case 'invoice.payment_failed':
return await invoicePaymentFailed(event, req, res)
case 'checkout.session.completed':
return await checkoutSessionCompleted(event, req, res)
default:
return res.status(200).json(event) // Respond with 200 OK for unhandled events
}
} else {
return res.send(false)
}
}