Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
35af7be
commit 96b2b51
Showing
2 changed files
with
67 additions
and
62 deletions.
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
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,64 @@ | ||
import {getStripeSdk} from '@skillrecordings/stripe-sdk' | ||
import {first} from 'lodash' | ||
import Stripe from 'stripe' | ||
import { | ||
StripeProviderFunction, | ||
PurchaseMetadata, | ||
PurchaseInfo, | ||
PurchaseInfoSchema, | ||
} from './default-payment-options' | ||
|
||
export const StripeProvider: StripeProviderFunction = (config) => { | ||
const stripeClient = | ||
'defaultStripeClient' in config | ||
? config.defaultStripeClient | ||
: new Stripe(config.stripeSecretKey, { | ||
apiVersion: config.apiVersion, | ||
}) | ||
|
||
const getStripePurchaseInfo = async (checkoutSessionId: string) => { | ||
const {getCheckoutSession} = getStripeSdk({ctx: {stripe: stripeClient}}) | ||
|
||
const checkoutSession = await getCheckoutSession(checkoutSessionId) | ||
|
||
const {customer, line_items, payment_intent, metadata} = checkoutSession | ||
const {email, name, id: stripeCustomerId} = customer as Stripe.Customer | ||
const lineItem = first(line_items?.data) as Stripe.LineItem | ||
const stripePrice = lineItem.price | ||
const quantity = lineItem.quantity || 1 | ||
const stripeProduct = stripePrice?.product as Stripe.Product | ||
const {charges} = payment_intent as Stripe.PaymentIntent | ||
const stripeCharge = first<Stripe.Charge>(charges.data) | ||
const stripeChargeId = stripeCharge?.id as string | ||
const stripeChargeAmount = stripeCharge?.amount || 0 | ||
|
||
// extract MerchantCoupon identifier if used for purchase | ||
const discount = first(lineItem.discounts) | ||
const stripeCouponId = discount?.discount.coupon.id | ||
|
||
const parsedMetadata = metadata | ||
? PurchaseMetadata.parse(metadata) | ||
: undefined | ||
|
||
const info: PurchaseInfo = { | ||
customerIdentifier: stripeCustomerId, | ||
email, | ||
name, | ||
productIdentifier: stripeProduct.id, | ||
product: stripeProduct, | ||
chargeIdentifier: stripeChargeId, | ||
couponIdentifier: stripeCouponId, | ||
quantity, | ||
chargeAmount: stripeChargeAmount, | ||
metadata: parsedMetadata, | ||
} | ||
|
||
return PurchaseInfoSchema.parse(info) | ||
} | ||
|
||
return { | ||
name: 'stripe', | ||
paymentClient: stripeClient, | ||
getPurchaseInfo: getStripePurchaseInfo, | ||
} | ||
} |