Skip to content

Commit

Permalink
feat: replace stripeData call with Provider call
Browse files Browse the repository at this point in the history
just in TJS for now, the only place we are using the Stripe Provider at
the moment
  • Loading branch information
jbranchaud authored and kodiakhq[bot] committed Mar 22, 2024
1 parent 96b2b51 commit 04c1621
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 23 deletions.
Expand Up @@ -7,7 +7,7 @@ import {
} from '@skillrecordings/commerce-server'
import {nextAuthOptions} from '../auth/[...nextauth]'

const paymentOptions = defaultPaymentOptions({
export const paymentOptions = defaultPaymentOptions({
stripeProvider: StripeProvider({
stripeSecretKey: process.env.STRIPE_SECRET_TOKEN,
apiVersion: '2020-08-27',
Expand Down
23 changes: 12 additions & 11 deletions apps/testing-javascript/src/pages/thanks/purchase.tsx
Expand Up @@ -5,7 +5,6 @@ import {
convertToSerializeForNextResponse,
determinePurchaseType,
type PurchaseType,
stripeData,
} from '@skillrecordings/commerce-server'
import type {SanityProduct} from '@skillrecordings/commerce-server/dist/@types'
import {
Expand All @@ -23,32 +22,34 @@ import {getAllProducts} from '@/server/products.server'
import {type SanityDocument} from '@sanity/client'
import {InvoiceCard} from '@/pages/invoices'
import {MailIcon} from '@heroicons/react/solid'
import {paymentOptions} from '../api/skill/[...skillRecordings]'

export const getServerSideProps: GetServerSideProps = async (context) => {
const {query} = context

const {session_id} = query
const session_id =
query.session_id instanceof Array ? query.session_id[0] : query.session_id

if (!session_id) {
const paymentProvider = paymentOptions.providers.stripe

if (!session_id || !paymentProvider) {
return {
notFound: true,
}
}

const purchaseInfo = await stripeData({
checkoutSessionId: session_id as string,
})
const purchaseInfo = await paymentProvider.getPurchaseInfo(session_id)

const {
email,
stripeChargeId,
chargeIdentifier,
quantity: seatsPurchased,
stripeProduct,
product,
} = purchaseInfo

const stripeProductName = stripeProduct.name
const stripeProductName = product.name

const purchase = await getSdk().getPurchaseForStripeCharge(stripeChargeId)
const purchase = await getSdk().getPurchaseForStripeCharge(chargeIdentifier)

if (!purchase || !email) {
return {
Expand All @@ -57,7 +58,7 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
}

const purchaseType = await determinePurchaseType({
checkoutSessionId: session_id as string,
checkoutSessionId: session_id,
})

const products = await getAllProducts()
Expand Down
21 changes: 11 additions & 10 deletions apps/testing-javascript/src/pages/welcome/index.tsx
@@ -1,8 +1,5 @@
import * as React from 'react'
import {
convertToSerializeForNextResponse,
stripeData,
} from '@skillrecordings/commerce-server'
import {convertToSerializeForNextResponse} from '@skillrecordings/commerce-server'
import type {SanityProduct} from '@skillrecordings/commerce-server/dist/@types'
import {useSession} from 'next-auth/react'
import {type GetServerSideProps} from 'next'
Expand All @@ -17,22 +14,26 @@ import {getAllProducts} from '@/server/products.server'
import Image from 'next/legacy/image'
import {trpc} from '../../trpc/trpc.client'
import {Transfer} from '@/purchase-transfer/purchase-transfer'
import {paymentOptions} from '../api/skill/[...skillRecordings]'

export const getServerSideProps: GetServerSideProps = async ({req, query}) => {
const {purchaseId: purchaseQueryParam, session_id, upgrade} = query
const {purchaseId: purchaseQueryParam, upgrade} = query
const session_id =
query.session_id instanceof Array ? query.session_id[0] : query.session_id
const token = await getToken({req})
const {getPurchaseDetails} = getSdk()

const paymentProvider = paymentOptions.providers.stripe

let purchaseId = purchaseQueryParam

if (session_id) {
const {stripeChargeId} = await stripeData({
checkoutSessionId: session_id as string,
})
if (session_id && paymentProvider) {
const {chargeIdentifier} = await paymentProvider.getPurchaseInfo(session_id)

const purchase = await prisma.purchase.findFirst({
where: {
merchantCharge: {
identifier: stripeChargeId,
identifier: chargeIdentifier,
},
},
})
Expand Down
2 changes: 1 addition & 1 deletion packages/commerce-server/src/index.ts
Expand Up @@ -9,4 +9,4 @@ export * from './props-for-commerce'
export * from './record-new-purchase'
export * from './determine-purchase-type'
export * from './get-valid-purchases'
export * from './providers/default-payment-options'
export * from './providers'
2 changes: 2 additions & 0 deletions packages/commerce-server/src/providers/index.ts
@@ -0,0 +1,2 @@
export * from './default-payment-options'
export * from './stripe-provider'

0 comments on commit 04c1621

Please sign in to comment.