Migrate stripeWebhook and createCheckoutSession to v2 (#636)

This commit is contained in:
Marshall Polaris 2022-07-09 13:43:18 -07:00 committed by GitHub
parent e7e686d579
commit 581a42f288
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 13 deletions

View File

@ -1,4 +1,4 @@
import * as functions from 'firebase-functions' import { onRequest } from 'firebase-functions/v2/https'
import * as admin from 'firebase-admin' import * as admin from 'firebase-admin'
import Stripe from 'stripe' import Stripe from 'stripe'
@ -42,9 +42,9 @@ const manticDollarStripePrice = isProd()
10000: 'price_1K8bEiGdoFKoCJW7Us4UkRHE', 10000: 'price_1K8bEiGdoFKoCJW7Us4UkRHE',
} }
export const createCheckoutSession = functions export const createcheckoutsession = onRequest(
.runWith({ minInstances: 1, secrets: ['STRIPE_APIKEY'] }) { minInstances: 1, secrets: ['STRIPE_APIKEY'] },
.https.onRequest(async (req, res) => { async (req, res) => {
const userId = req.query.userId?.toString() const userId = req.query.userId?.toString()
const manticDollarQuantity = req.query.manticDollarQuantity?.toString() const manticDollarQuantity = req.query.manticDollarQuantity?.toString()
@ -86,14 +86,15 @@ export const createCheckoutSession = functions
}) })
res.redirect(303, session.url || '') res.redirect(303, session.url || '')
}) }
)
export const stripeWebhook = functions export const stripewebhook = onRequest(
.runWith({ {
minInstances: 1, minInstances: 1,
secrets: ['MAILGUN_KEY', 'STRIPE_APIKEY', 'STRIPE_WEBHOOKSECRET'], secrets: ['MAILGUN_KEY', 'STRIPE_APIKEY', 'STRIPE_WEBHOOKSECRET'],
}) },
.https.onRequest(async (req, res) => { async (req, res) => {
const stripe = initStripe() const stripe = initStripe()
let event let event
@ -115,7 +116,8 @@ export const stripeWebhook = functions
} }
res.status(200).send('success') res.status(200).send('success')
}) }
)
const issueMoneys = async (session: StripeSession) => { const issueMoneys = async (session: StripeSession) => {
const { id: sessionId } = session const { id: sessionId } = session

View File

@ -53,6 +53,7 @@ export function getFunctionUrl(name: string) {
export function createAnswer(params: any) { export function createAnswer(params: any) {
return call(getFunctionUrl('createanswer'), 'POST', params) return call(getFunctionUrl('createanswer'), 'POST', params)
} }
export function changeUserInfo(params: any) { export function changeUserInfo(params: any) {
return call(getFunctionUrl('changeuserinfo'), 'POST', params) return call(getFunctionUrl('changeuserinfo'), 'POST', params)
} }

View File

@ -1,12 +1,11 @@
import { PROJECT_ID } from 'common/envs/constants' import { getFunctionUrl } from 'web/lib/firebase/api-call'
export const checkoutURL = ( export const checkoutURL = (
userId: string, userId: string,
manticDollarQuantity: number, manticDollarQuantity: number,
referer = '' referer = ''
) => { ) => {
const endpoint = `https://us-central1-${PROJECT_ID}.cloudfunctions.net/createCheckoutSession` const endpoint = getFunctionUrl('createcheckoutsession')
return `${endpoint}?userId=${userId}&manticDollarQuantity=${manticDollarQuantity}&referer=${encodeURIComponent( return `${endpoint}?userId=${userId}&manticDollarQuantity=${manticDollarQuantity}&referer=${encodeURIComponent(
referer referer
)}` )}`