@coursebuilder/core
Version:
Core package for Course Builder
261 lines (259 loc) • 8.06 kB
JavaScript
import {
parseSubscriptionInfoFromCheckoutSession
} from "./chunk-GGYEUHN2.js";
import {
parsePurchaseInfoFromCheckoutSession
} from "./chunk-MUVDMT7G.js";
import {
stripeCheckout
} from "./chunk-RLQCDCZC.js";
import {
logger
} from "./chunk-B73XG2UO.js";
import {
__name,
__publicField
} from "./chunk-VLQXSCFN.js";
// src/providers/stripe.ts
import Stripe from "stripe";
function StripeProvider(options) {
return {
id: "stripe",
name: "Stripe",
type: "payment",
...options,
options,
getSubscription: async (subscriptionId) => {
return options.paymentsAdapter.getSubscription(subscriptionId);
},
getBillingPortalUrl: async (customerId, returnUrl) => {
return options.paymentsAdapter.getBillingPortalUrl(customerId, returnUrl);
},
getSubscriptionInfo: async (checkoutSessionId, _) => {
const checkoutSession = await options.paymentsAdapter.getCheckoutSession(checkoutSessionId);
console.log("checkoutSession", checkoutSession);
return parseSubscriptionInfoFromCheckoutSession(checkoutSession);
},
getPurchaseInfo: async (checkoutSessionId, adapter) => {
const checkoutSession = await options.paymentsAdapter.getCheckoutSession(checkoutSessionId);
return parsePurchaseInfoFromCheckoutSession(checkoutSession, adapter);
},
createCheckoutSession: async (checkoutParams, adapter) => {
return stripeCheckout({
params: checkoutParams,
config: options,
adapter
});
},
getCustomer: async (customerId) => {
return options.paymentsAdapter.getCustomer(customerId);
},
updateCustomer: async (customerId, customer) => {
return options.paymentsAdapter.updateCustomer(customerId, customer);
},
refundCharge: async (chargeId) => {
return options.paymentsAdapter.refundCharge(chargeId);
},
getProduct: async (productId) => {
return options.paymentsAdapter.getProduct(productId);
},
getPrice: async (priceId) => {
return options.paymentsAdapter.getPrice(priceId);
},
updateProduct: async (productId, product) => {
return options.paymentsAdapter.updateProduct(productId, product);
},
updatePrice: async (priceId, price) => {
return options.paymentsAdapter.updatePrice(priceId, price);
},
createPrice: async (price) => {
return options.paymentsAdapter.createPrice(price);
},
createProduct: async (product) => {
return options.paymentsAdapter.createProduct(product);
}
};
}
__name(StripeProvider, "StripeProvider");
var STRIPE_VERSION = "2024-06-20";
var _StripePaymentAdapter = class _StripePaymentAdapter {
constructor({ stripeToken, stripeWebhookSecret }) {
__publicField(this, "webhookSecret");
__publicField(this, "stripe");
const stripe = this.createStripeClient(stripeToken);
if (!stripeWebhookSecret) {
throw new Error("Stripe webhook secret not found");
}
this.webhookSecret = stripeWebhookSecret;
this.stripe = stripe;
}
createStripeClient(token) {
return new Stripe(token, {
apiVersion: STRIPE_VERSION
});
}
async verifyWebhookSignature(rawBody, sig) {
const event = this.stripe.webhooks.constructEvent(rawBody, sig, this.webhookSecret);
return Boolean(event);
}
async getCouponPercentOff(identifier) {
const coupon = await this.stripe.coupons.retrieve(identifier);
return coupon && coupon.percent_off ? coupon.percent_off / 100 : 0;
}
async createCoupon(params) {
const coupon = await this.stripe.coupons.create(params);
return coupon.id;
}
async createPromotionCode(params) {
const { id } = await this.stripe.promotionCodes.create(params);
return id;
}
async createCheckoutSession(params) {
const session = await this.stripe.checkout.sessions.create(params);
return session.url;
}
async getCheckoutSession(checkoutSessionId) {
logger.debug("getCheckoutSession", {
checkoutSessionId
});
return await this.stripe.checkout.sessions.retrieve(checkoutSessionId, {
expand: [
"customer",
"line_items.data.price.product",
"line_items.data.discounts",
"payment_intent.latest_charge",
"subscription",
"subscription.plan.product"
]
});
}
async createCustomer(params) {
const stripeCustomer = await this.stripe.customers.create({
email: params.email,
metadata: {
userId: params.userId
}
});
return stripeCustomer.id;
}
async getCustomer(customerId) {
return await this.stripe.customers.retrieve(customerId);
}
async updateCustomer(customerId, customer) {
const stripeCustomer = await this.stripe.customers.retrieve(customerId);
await this.stripe.customers.update(customerId, {
name: customer.name || stripeCustomer.name || void 0,
email: customer.email || stripeCustomer.email || void 0,
metadata: {
...stripeCustomer.metadata,
...customer.metadata
}
});
}
async refundCharge(chargeId) {
return await this.stripe.refunds.create({
charge: chargeId
});
}
async getProduct(productId) {
return this.stripe.products.retrieve(productId);
}
async getPrice(priceId) {
return this.stripe.prices.retrieve(priceId);
}
async updateProduct(productId, product) {
await this.stripe.products.update(productId, product);
}
async updatePrice(priceId, price) {
await this.stripe.prices.update(priceId, price);
}
async createPrice(price) {
return this.stripe.prices.create(price);
}
async createProduct(product) {
return this.stripe.products.create(product);
}
async getSubscription(subscriptionId) {
return this.stripe.subscriptions.retrieve(subscriptionId);
}
async getBillingPortalUrl(customerId, returnUrl) {
return this.stripe.billingPortal.sessions.create({
customer: customerId,
return_url: returnUrl
}).then((session) => session.url);
}
};
__name(_StripePaymentAdapter, "StripePaymentAdapter");
var StripePaymentAdapter = _StripePaymentAdapter;
var mockStripeAdapter = {
getCouponPercentOff: async () => 0,
createCoupon: async () => "mock-coupon-id",
createPromotionCode: async () => "mock-promotion-code-id",
createCheckoutSession: async () => "mock-checkout-session-id",
createCustomer: async () => "mock-customer-id",
verifyWebhookSignature: async () => true,
getCheckoutSession: async () => ({
id: "mock-checkout-session-id"
}),
getCustomer: async () => ({
id: "mock-customer-id"
}),
updateCustomer: async () => {
},
refundCharge: async () => ({}),
updateProduct: async () => {
},
updatePrice: async () => {
},
getProduct: async () => ({}),
getPrice: async () => ({}),
createPrice: async () => ({}),
createProduct: async () => ({}),
getSubscription: async () => ({}),
getBillingPortalUrl: async () => "mock-billing-portal-url"
};
var MockStripeProvider = {
id: "mock-stripe",
name: "Mock Stripe",
type: "payment",
options: {
errorRedirectUrl: "mock-error-redirect-url",
cancelUrl: "mock-cancel-url",
baseSuccessUrl: "mock-base-success-url",
paymentsAdapter: mockStripeAdapter
},
getSubscriptionInfo: async () => ({}),
getPurchaseInfo: async (checkoutSessionId, adapter) => {
return {};
},
createCheckoutSession: async () => {
return {
redirect: "mock-checkout-session-id",
status: 303
};
},
getCustomer: async () => ({
id: "mock-customer-id"
}),
updateCustomer: async () => {
},
refundCharge: async () => ({}),
updateProduct: async () => {
},
updatePrice: async () => {
},
getProduct: async () => ({}),
getPrice: async () => ({}),
createPrice: async () => ({}),
createProduct: async () => ({}),
getSubscription: async () => ({}),
getBillingPortalUrl: async () => "mock-billing-portal-url"
};
export {
StripeProvider,
STRIPE_VERSION,
StripePaymentAdapter,
mockStripeAdapter,
MockStripeProvider
};
//# sourceMappingURL=chunk-CMJIT4RT.js.map