-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
creating stripe checkout session endpoint #42
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5bd9e03
creating checkout session endpoint
610b009
updated schema design
61e26af
created and applied migrations for payments table
roskzhu 99619bc
reversed payment and donation relation
roskzhu a4f56a2
update checkout endpoint
andy-liuu e1a673d
remove subscription stuff from single payment enpoint
andy-liuu 2e42041
made payment and donations link optional
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
backend/typescript/prisma/migrations/20240526175138_create_payments_table/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
-- CreateEnum | ||
CREATE TYPE "PaymentStatus" AS ENUM ('PAID', 'PROCESSING', 'UNPAID', 'DENIED'); | ||
|
||
-- CreateTable | ||
CREATE TABLE "Payment" ( | ||
"id" SERIAL NOT NULL, | ||
"stripePaymentId" TEXT NOT NULL, | ||
"creationDate" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updateDate" TIMESTAMP(3) NOT NULL, | ||
"donation_id" INTEGER NOT NULL, | ||
"amount" INTEGER NOT NULL, | ||
"currency" TEXT NOT NULL, | ||
"status" "PaymentStatus" NOT NULL, | ||
|
||
CONSTRAINT "Payment_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "Payment_stripePaymentId_key" ON "Payment"("stripePaymentId"); |
1 change: 1 addition & 0 deletions
1
backend/typescript/prisma/migrations/20240526175607_create_payments_table/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-- This is an empty migration. |
16 changes: 16 additions & 0 deletions
16
backend/typescript/prisma/migrations/20240611002441_/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
Warnings: | ||
|
||
- You are about to drop the column `donation_id` on the `Payment` table. All the data in the column will be lost. | ||
- A unique constraint covering the columns `[payment_id]` on the table `Donation` will be added. If there are existing duplicate values, this will fail. | ||
|
||
*/ | ||
-- AlterTable | ||
ALTER TABLE "Donation" ADD COLUMN "payment_id" INTEGER; | ||
|
||
-- AlterTable | ||
ALTER TABLE "Payment" DROP COLUMN "donation_id", | ||
ALTER COLUMN "amount" SET DATA TYPE DOUBLE PRECISION; | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "Donation_payment_id_key" ON "Donation"("payment_id"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import express, { Request, Response, Router } from "express"; | ||
|
||
import StripeService from "../services/implementations/stripeService"; | ||
import IStripeService from "../services/interfaces/stripeService"; | ||
|
||
const stripeService: IStripeService = new StripeService(); | ||
|
||
const stripeRouter = Router(); | ||
|
||
// const app = express(); | ||
// app.use(express.json()); | ||
|
||
// Endpoint to create a Stripe Checkout session | ||
stripeRouter.post( | ||
"/create-checkout-session", | ||
async (req: Request, res: Response) => { | ||
try { | ||
const { user_id, amount, cause_id } = req.body; | ||
const sessionUrl = await stripeService.createCheckoutSession( | ||
user_id, | ||
amount, | ||
cause_id, | ||
); | ||
console.log(`Created checkout session`); | ||
res.json({ url: sessionUrl }); | ||
} catch (error) { | ||
res | ||
.status(500) | ||
.json({ error: "Error creating payment checkout session." }); | ||
} | ||
}, | ||
); | ||
|
||
/* | ||
stripeRouter.post("/create-checkout-session-subscription", async (req, res) => { | ||
try { | ||
const prices = await stripe.prices.list({ | ||
lookup_keys: [req.body.lookup_key], | ||
expand: ["data.product"], | ||
}); | ||
const session = await stripe.checkout.sessions.create({ | ||
billing_address_collection: "auto", | ||
line_items: [ | ||
{ | ||
price: prices.data[0].id, | ||
// For metered billing, do not pass quantity | ||
quantity: 1, | ||
}, | ||
], | ||
mode: "subscription", | ||
success_url: `http://localhost:5173/checkout-success`, | ||
cancel_url: `http://localhost:5173/checkout-cancel`, | ||
}); | ||
res.send({url: session.url}); | ||
//res.redirect(303, session.url); | ||
} catch (error) { | ||
res | ||
.status(500) | ||
.json({ error: "Error creating subscription checkout session." }); | ||
} | ||
}); | ||
|
||
stripeRouter.post("/create-portal-session", async (req, res) => { | ||
// For demonstration purposes, we're using the Checkout session to retrieve the customer ID. | ||
// Typically this is stored alongside the authenticated user in your database. | ||
const { session_id } = req.body; | ||
const checkoutSession = await stripe.checkout.sessions.retrieve(session_id); | ||
|
||
// This is the url to which the customer will be redirected when they are done | ||
// managing their billing with the portal. | ||
const returnUrl = YOUR_DOMAIN; | ||
|
||
const portalSession = await stripe.billingPortal.sessions.create({ | ||
customer: checkoutSession.customer, | ||
return_url: returnUrl, | ||
}); | ||
|
||
res.redirect(303, portalSession.url); | ||
}); | ||
*/ | ||
export default stripeRouter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
backend/typescript/services/implementations/stripeService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import dotenv from "dotenv"; | ||
|
||
import Stripe from "stripe"; | ||
import IStripeService from "../interfaces/stripeService"; | ||
import logger from "../../utilities/logger"; | ||
|
||
dotenv.config(); | ||
|
||
const stripe = new Stripe(process.env.STRIPE_PRIVATE_KEY as string); | ||
const Logger = logger(__filename); | ||
const SUCCESS_URL = `http://localhost:5000/checkout-success`; | ||
const CANCEL_URL = `http://localhost:5000/checkout-cancel`; | ||
|
||
const checkoutSessionDefaultOptions: Stripe.Checkout.SessionCreateParams = { | ||
// ui_mode: "embedded", | ||
payment_method_types: ["card"], | ||
mode: "payment", | ||
success_url: SUCCESS_URL, | ||
cancel_url: CANCEL_URL, | ||
}; | ||
|
||
console.log(process.env.STRIPE_PRIVATE_KEY); // Log the value of STRIPE_PRIVATE_KEY | ||
|
||
class StripeService implements IStripeService { | ||
createCheckoutSession = async ( | ||
user_id: string, | ||
amount: number, // in cents (euro) | ||
cause_id: number, | ||
): Promise<string> => { | ||
try { | ||
const session = await stripe.checkout.sessions.create({ | ||
...checkoutSessionDefaultOptions, | ||
line_items: [ | ||
{ | ||
price_data: { | ||
currency: "EUR", | ||
unit_amount: amount, | ||
}, | ||
quantity: 1, | ||
}, | ||
], | ||
}); | ||
|
||
if (!session.url) { | ||
throw new Error("Session URL is null"); | ||
} | ||
// store session details in db | ||
|
||
return session.url; | ||
} catch (error) { | ||
Logger.error( | ||
`Error creating a checkout session for a payment for user ${user_id} = ${error}`, | ||
); | ||
throw error; | ||
} | ||
}; | ||
} | ||
export default StripeService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { DonationDTO, Recurrence } from "../../types"; | ||
|
||
interface IStripeService { | ||
/** | ||
* Create a checkout session for a one time payemnt | ||
* @param user_id user's id | ||
* @param amount the amount the user is donating toward this cause in the donation | ||
* @param cause_id the id of the cause the donation is associated with | ||
* @returns the newly created session url | ||
* @throws Error if | ||
*/ | ||
createCheckoutSession( | ||
user_id: string, | ||
amount: number, | ||
cause_id: number, | ||
): Promise<string>; | ||
} | ||
|
||
export default IStripeService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1375,10 +1375,10 @@ | |
resolved "https://registry.yarnpkg.com/@panva/asn1.js/-/asn1.js-1.0.0.tgz#dd55ae7b8129e02049f009408b97c61ccf9032f6" | ||
integrity sha512-UdkG3mLEqXgnlKsWanWcgb6dOjUzJ+XC5f+aWw30qrtjxeNUSfKX1cd5FBzOaXQumoe9nIqeZUvrRJS03HCCtw== | ||
|
||
"@prisma/client@^5.10.2": | ||
version "5.11.0" | ||
resolved "https://registry.yarnpkg.com/@prisma/client/-/client-5.11.0.tgz#d8e55fab85163415b2245fb408b9106f83c8106d" | ||
integrity sha512-SWshvS5FDXvgJKM/a0y9nDC1rqd7KG0Q6ZVzd+U7ZXK5soe73DJxJJgbNBt2GNXOa+ysWB4suTpdK5zfFPhwiw== | ||
"@prisma/client@^5.11.0": | ||
version "5.14.0" | ||
resolved "https://registry.yarnpkg.com/@prisma/client/-/client-5.14.0.tgz#dadca5bb1137ddcebb454bbdaf89423823d3363f" | ||
integrity sha512-akMSuyvLKeoU4LeyBAUdThP/uhVP3GuLygFE3MlYzaCb3/J8SfsYBE5PkaFuLuVpLyA6sFoW+16z/aPhNAESqg== | ||
|
||
"@prisma/[email protected]": | ||
version "5.11.0" | ||
|
@@ -2256,6 +2256,13 @@ | |
dependencies: | ||
undici-types "~5.26.4" | ||
|
||
"@types/node@>=8.1.0": | ||
version "20.14.0" | ||
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.14.0.tgz#49ceec7b34f8621470cff44677fa9d461a477f17" | ||
integrity sha512-5cHBxFGJx6L4s56Bubp4fglrEpmyJypsqI6RgzMfBHWUJQGWAAi8cWcgetEbZXHYXo9C2Fa4EEds/uSyS4cxmA== | ||
dependencies: | ||
undici-types "~5.26.4" | ||
|
||
"@types/node@^10.1.0": | ||
version "10.17.60" | ||
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.17.60.tgz#35f3d6213daed95da7f0f73e75bcc6980e90597b" | ||
|
@@ -7600,6 +7607,13 @@ [email protected]: | |
dependencies: | ||
side-channel "^1.0.4" | ||
|
||
qs@^6.11.0: | ||
version "6.12.1" | ||
resolved "https://registry.yarnpkg.com/qs/-/qs-6.12.1.tgz#39422111ca7cbdb70425541cba20c7d7b216599a" | ||
integrity sha512-zWmv4RSuB9r2mYQw3zxQuHWeU+42aKi1wWig/j4ele4ygELZ7PEO6MM7rim9oAQH2A5MWfsAVf/jPvTPgCbvUQ== | ||
dependencies: | ||
side-channel "^1.0.6" | ||
|
||
qs@~6.5.2: | ||
version "6.5.3" | ||
resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.3.tgz#3aeeffc91967ef6e35c0e488ef46fb296ab76aad" | ||
|
@@ -8020,7 +8034,7 @@ shebang-regex@^3.0.0: | |
resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-3.0.0.tgz#ae16f1644d873ecad843b0307b143362d4c42172" | ||
integrity sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A== | ||
|
||
side-channel@^1.0.4: | ||
side-channel@^1.0.4, side-channel@^1.0.6: | ||
version "1.0.6" | ||
resolved "https://registry.yarnpkg.com/side-channel/-/side-channel-1.0.6.tgz#abd25fb7cd24baf45466406b1096b7831c9215f2" | ||
integrity sha512-fDW/EZ6Q9RiO8eFG8Hj+7u/oW+XrPTIChwCOM2+th2A6OblDtYYIpve9m+KvI9Z4C9qSEXlaGR6bTEYHReuglA== | ||
|
@@ -8306,6 +8320,14 @@ strip-json-comments@^3.1.0, strip-json-comments@^3.1.1: | |
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-3.1.1.tgz#31f1281b3832630434831c310c01cccda8cbe006" | ||
integrity sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig== | ||
|
||
stripe@^15.9.0: | ||
version "15.9.0" | ||
resolved "https://registry.yarnpkg.com/stripe/-/stripe-15.9.0.tgz#fbe6434496afa27cb08be7dc730064a719e7c925" | ||
integrity sha512-C7NAK17wGr6DOybxThO0n1zVcqSShWno7kx/UcJorQ/nWZ5KnfHQ38DUTb9NgizC8TCII3BPIhqq6Zc/1aUkrg== | ||
dependencies: | ||
"@types/node" ">=8.1.0" | ||
qs "^6.11.0" | ||
|
||
strnum@^1.0.5: | ||
version "1.0.5" | ||
resolved "https://registry.yarnpkg.com/strnum/-/strnum-1.0.5.tgz#5c4e829fe15ad4ff0d20c3db5ac97b73c9b072db" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This endpoint still a WIP?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, will be filled out in https://www.notion.so/uwblueprintexecs/create-Stripe-endpoint-to-create-a-custom-subscription-8ee7f9c3c4b04090bc5d7904406dfe63?pvs=4