2022-01-07 22:56:14 +00:00
|
|
|
import * as functions from 'firebase-functions'
|
|
|
|
import * as admin from 'firebase-admin'
|
|
|
|
import Stripe from 'stripe'
|
|
|
|
|
2022-05-11 15:51:58 +00:00
|
|
|
import { getPrivateUser, getUser, isProd, payUser } from './utils'
|
|
|
|
import { sendThankYouEmail } from './emails'
|
2022-01-07 22:56:14 +00:00
|
|
|
|
2022-05-26 22:22:44 +00:00
|
|
|
export type StripeSession = Stripe.Event.Data.Object & { id: any, metadata: any}
|
|
|
|
|
2022-01-17 21:39:26 +00:00
|
|
|
export type StripeTransaction = {
|
|
|
|
userId: string
|
|
|
|
manticDollarQuantity: number
|
|
|
|
sessionId: string
|
2022-05-26 22:22:44 +00:00
|
|
|
session: StripeSession
|
2022-01-17 21:39:26 +00:00
|
|
|
timestamp: number
|
|
|
|
}
|
|
|
|
|
2022-01-07 22:56:14 +00:00
|
|
|
const stripe = new Stripe(functions.config().stripe.apikey, {
|
|
|
|
apiVersion: '2020-08-27',
|
|
|
|
typescript: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
// manage at https://dashboard.stripe.com/test/products?active=true
|
2022-02-23 01:35:25 +00:00
|
|
|
const manticDollarStripePrice = isProd
|
|
|
|
? {
|
|
|
|
500: 'price_1KFQXcGdoFKoCJW770gTNBrm',
|
|
|
|
1000: 'price_1KFQp1GdoFKoCJW7Iu0dsF65',
|
|
|
|
2500: 'price_1KFQqNGdoFKoCJW7SDvrSaEB',
|
|
|
|
10000: 'price_1KFQraGdoFKoCJW77I4XCwM3',
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
500: 'price_1K8W10GdoFKoCJW7KWORLec1',
|
|
|
|
1000: 'price_1K8bC1GdoFKoCJW76k3g5MJk',
|
|
|
|
2500: 'price_1K8bDSGdoFKoCJW7avAwpV0e',
|
|
|
|
10000: 'price_1K8bEiGdoFKoCJW7Us4UkRHE',
|
|
|
|
}
|
2022-01-07 22:56:14 +00:00
|
|
|
|
|
|
|
export const createCheckoutSession = functions
|
|
|
|
.runWith({ minInstances: 1 })
|
|
|
|
.https.onRequest(async (req, res) => {
|
|
|
|
const userId = req.query.userId?.toString()
|
|
|
|
|
|
|
|
const manticDollarQuantity = req.query.manticDollarQuantity?.toString()
|
|
|
|
|
|
|
|
if (!userId) {
|
|
|
|
res.status(400).send('Invalid user ID')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
!manticDollarQuantity ||
|
|
|
|
!Object.keys(manticDollarStripePrice).includes(manticDollarQuantity)
|
|
|
|
) {
|
|
|
|
res.status(400).send('Invalid Mantic Dollar quantity')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const referrer =
|
2022-05-13 11:07:51 +00:00
|
|
|
req.query.referer || req.headers.referer || 'https://manifold.markets'
|
2022-01-07 22:56:14 +00:00
|
|
|
|
|
|
|
const session = await stripe.checkout.sessions.create({
|
|
|
|
metadata: {
|
|
|
|
userId,
|
|
|
|
manticDollarQuantity,
|
|
|
|
},
|
|
|
|
line_items: [
|
|
|
|
{
|
|
|
|
price:
|
|
|
|
manticDollarStripePrice[
|
|
|
|
manticDollarQuantity as unknown as keyof typeof manticDollarStripePrice
|
|
|
|
],
|
|
|
|
quantity: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
mode: 'payment',
|
|
|
|
success_url: `${referrer}?funding-success`,
|
|
|
|
cancel_url: `${referrer}?funding-failiure`,
|
|
|
|
})
|
|
|
|
|
|
|
|
res.redirect(303, session.url || '')
|
|
|
|
})
|
|
|
|
|
|
|
|
export const stripeWebhook = functions
|
|
|
|
.runWith({ minInstances: 1 })
|
|
|
|
.https.onRequest(async (req, res) => {
|
|
|
|
let event
|
|
|
|
|
|
|
|
try {
|
|
|
|
event = stripe.webhooks.constructEvent(
|
|
|
|
req.rawBody,
|
|
|
|
req.headers['stripe-signature'] as string,
|
|
|
|
functions.config().stripe.webhooksecret
|
|
|
|
)
|
|
|
|
} catch (e: any) {
|
|
|
|
console.log(`Webhook Error: ${e.message}`)
|
|
|
|
res.status(400).send(`Webhook Error: ${e.message}`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.type === 'checkout.session.completed') {
|
2022-05-26 22:22:44 +00:00
|
|
|
const session = event.data.object as StripeSession
|
2022-01-07 22:56:14 +00:00
|
|
|
await issueMoneys(session)
|
|
|
|
}
|
|
|
|
|
|
|
|
res.status(200).send('success')
|
|
|
|
})
|
|
|
|
|
2022-05-26 22:22:44 +00:00
|
|
|
const issueMoneys = async (session: StripeSession) => {
|
2022-01-07 22:56:14 +00:00
|
|
|
const { id: sessionId } = session
|
|
|
|
|
|
|
|
const query = await firestore
|
|
|
|
.collection('stripe-transactions')
|
|
|
|
.where('sessionId', '==', sessionId)
|
|
|
|
.get()
|
|
|
|
|
|
|
|
if (!query.empty) {
|
|
|
|
console.log('session', sessionId, 'already processed')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const { userId, manticDollarQuantity } = session.metadata
|
|
|
|
const payout = Number.parseInt(manticDollarQuantity)
|
|
|
|
|
2022-01-17 21:39:26 +00:00
|
|
|
const transaction: StripeTransaction = {
|
2022-01-07 22:56:14 +00:00
|
|
|
userId,
|
|
|
|
manticDollarQuantity: payout, // save as number
|
|
|
|
sessionId,
|
|
|
|
session,
|
2022-01-17 21:39:26 +00:00
|
|
|
timestamp: Date.now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
await firestore.collection('stripe-transactions').add(transaction)
|
2022-01-07 22:56:14 +00:00
|
|
|
|
2022-01-19 03:36:46 +00:00
|
|
|
await payUser(userId, payout, true)
|
2022-01-07 22:56:14 +00:00
|
|
|
console.log('user', userId, 'paid M$', payout)
|
2022-05-11 15:51:58 +00:00
|
|
|
|
|
|
|
const user = await getUser(userId)
|
|
|
|
if (!user) return
|
|
|
|
|
|
|
|
const privateUser = await getPrivateUser(userId)
|
|
|
|
if (!privateUser) return
|
|
|
|
|
|
|
|
await sendThankYouEmail(user, privateUser)
|
2022-01-07 22:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const firestore = admin.firestore()
|