Skip to content

Commit

Permalink
Merge branch 'master' into 124-stripe-fetch-products
Browse files Browse the repository at this point in the history
  • Loading branch information
asun555 committed Mar 29, 2024
2 parents 9360a12 + 19da4a0 commit 88b7c48
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy-server.production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: superfly/flyctl-actions/setup-flyctl@master
- run: flyctl deploy --remote-only --config fly.production.toml --env "GOOGLE_SERVICE_ACCOUNT_JSON=$(echo ${{ secrets.PROD_SERVER_SERVICE_ACCOUNT }} | base64 --decode)"
- run: flyctl deploy --remote-only --config fly.production.toml --env "GOOGLE_SERVICE_ACCOUNT_JSON=$(echo ${{ secrets.PROD_SERVER_SERVICE_ACCOUNT }} | base64 --decode)" --env "STRIPE_API_SECRET=${{ secrets.PROD_SERVER_STRIPE_API_SECRET }}" --env "STRIPE_API_KEY=${{ secrets.PROD_SERVER_STRIPE_API_KEY }}"
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_PRODUCTION_API_TOKEN }}
2 changes: 1 addition & 1 deletion .github/workflows/deploy-server.staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: superfly/flyctl-actions/setup-flyctl@master
- run: flyctl deploy --remote-only --config fly.staging.toml --env "GOOGLE_SERVICE_ACCOUNT_JSON=$(echo ${{ secrets.STAGING_SERVER_SERVICE_ACCOUNT }} | base64 --decode)"
- run: flyctl deploy --remote-only --config fly.staging.toml --env "GOOGLE_SERVICE_ACCOUNT_JSON=$(echo ${{ secrets.STAGING_SERVER_SERVICE_ACCOUNT }} | base64 --decode)" --env "STRIPE_API_SECRET=${{ secrets.STAGING_SERVER_STRIPE_API_SECRET }}" --env "STRIPE_API_KEY=${{ secrets.STAGING_SERVER_STRIPE_API_KEY }}"
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_STAGING_API_TOKEN }}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import type { Meta, StoryObj } from "@storybook/react"

import SignUpForm from "./SignUpForm"
import { BrowserRouter } from "react-router-dom"

// 👇 This default export determines where your story goes in the story list
const meta: Meta<typeof SignUpForm> = {
Expand All @@ -12,6 +13,13 @@ export default meta
type Story = StoryObj<typeof SignUpForm>

export const FirstStory: Story = {
decorators: [
(Story) => (
<BrowserRouter>
<Story />
</BrowserRouter>
)
],
args: {
// 👇 The args you need here will depend on your component
}
Expand Down
29 changes: 0 additions & 29 deletions client/src/components/composite/SignUpForm/SignUpForm.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import React, { useState } from "react"
import {
getAuth,
createUserWithEmailAndPassword,
updateProfile
} from "firebase/auth"
import { useNavigate } from "react-router-dom"
import { db } from "../../../firebase"
import { setDoc, doc } from "firebase/firestore"
import Button from "@mui/material/Button"
import TextField from "@mui/material/TextField"
import Snackbar from "@mui/material/Snackbar"
Expand Down Expand Up @@ -62,7 +55,6 @@ const SignUpForm = () => {
setOpen(false)
}

const auth = getAuth()
const navigate = useNavigate()

const handleSubmit = async (event: any) => {
Expand Down Expand Up @@ -106,29 +98,8 @@ const SignUpForm = () => {
}

try {
const { user } = await createUserWithEmailAndPassword(
auth,
email,
password
)
console.log("User created")

await updateProfile(user, { displayName: `${firstName} ${lastName}` })
console.log("Profile updated")

await setDoc(doc(db, "users", user.uid), {
uid: user.uid,
firstName,
lastName,
email,
phoneNumber,
dob,
studentId,
yearLevel,
faculty,
sportType,
interestedInRacing
})
console.log("Document set in Firestore")

setOpen(true)
Expand Down
11 changes: 11 additions & 0 deletions client/src/models/__generated__/schema.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,19 @@ const importSwaggerJson = async () => {
}

const app: Express = express()
function keepRawBody(
req: any,
res: any,
buf: Buffer,
encoding: BufferEncoding
) {
if (buf && buf.length) {
req.rawBody = buf
}
}

app.use(helmet())
app.use(express.json())
app.use(express.json({ verify: keepRawBody }))
app.use(cors())

app.use("/api-docs", swaggerUi.serve, async (_req: Request, res: Response) => {
Expand Down
27 changes: 27 additions & 0 deletions server/src/middleware/__generated__/routes.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions server/src/middleware/__generated__/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions server/src/service-layer/controllers/StripeWebhook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Controller, SuccessResponse, Route, Post, Request } from "tsoa"
import Stripe from "stripe"

@Route("webhook")
export class StripeWebhook extends Controller {
@Post()
@SuccessResponse(200, "Webhook post received")
public async receiveWebhook(@Request() request: any): Promise<void> {
console.log("webhook received")

const stripe = new Stripe(process.env.STRIPE_API_KEY)

try {
const event: Stripe.Event = stripe.webhooks.constructEvent(
request.rawBody,
request.headers["stripe-signature"],
// process.env.STRIPE_LOCAL // test local api secret
process.env.STRIPE_API_SECRET
)
switch (event.type) {
case "payment_intent.succeeded":
console.log("payment_intent.succeeded")
break
case "payment_method.attached":
console.log("payment_method.attached")
break
default:
console.log(`Unhandled event type ${event.type}.`)
}

return this.setStatus(200) // set status to 200 as success
} catch (err) {
console.error(err)
// set status to 400 due to bad request
return this.setStatus(400)
}
}
}
89 changes: 89 additions & 0 deletions server/src/test-config/mocks/Stripe.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import * as Stripe from "stripe"

export const customerMock: Stripe.Stripe.Customer = {
id: "cus_123456789",
object: "customer",
address: {
city: "city",
country: "NZ",
line1: "line 1",
line2: "line 2",
postal_code: "90210",
state: "Auckland"
},
balance: 0,
created: 1483565364,
currency: null,
default_source: null,
delinquent: false,
description: null,
discount: null,
email: null,
invoice_prefix: "C11F7E1",
invoice_settings: {
custom_fields: null,
default_payment_method: null,
footer: null,
rendering_options: null
},
livemode: false,
metadata: {
order_id: "6735"
},
name: null,
next_invoice_sequence: 1,
phone: null,
preferred_locales: [],
shipping: null,
tax_exempt: "none"
}

export const productMock: Stripe.Stripe.Product = {
id: "prod_NWjs8kKbJWmuuc",
object: "product",
active: true,
created: 1678833149,
default_price: null,
description: null,
images: [],
features: [],
livemode: false,
metadata: {},
name: "Gold Plan",
package_dimensions: null,
shippable: null,
statement_descriptor: null,
tax_code: null,
unit_label: null,
updated: 1678833149,
url: null,
type: "good"
}

export const priceMock: Stripe.Stripe.Price = {
id: "price_1MoBy5LkdIwHu7ixZhnattbh",
object: "price",
active: true,
billing_scheme: "per_unit",
created: 1679431181,
currency: "usd",
custom_unit_amount: null,
livemode: false,
lookup_key: null,
metadata: {},
nickname: null,
product: "prod_NZKdYqrwEYx6iK",
recurring: {
aggregate_usage: null,
interval: "month",
interval_count: 1,
trial_period_days: null,
usage_type: "licensed"
},
tax_behavior: "unspecified",
tiers_mode: null,
transform_quantity: null,
type: "recurring",
unit_amount: 1000,
unit_amount_decimal: "1000"
}

0 comments on commit 88b7c48

Please sign in to comment.