@coursebuilder/core
Version:
Core package for Course Builder
94 lines (92 loc) • 2.98 kB
JavaScript
import {
STRIPE_CHECKOUT_SESSION_COMPLETED_EVENT
} from "./chunk-JIYITLP7.js";
import {
PURCHASE_STATUS_UPDATED_EVENT
} from "./chunk-F3UZSGOU.js";
import {
checkoutSessionCompletedEvent
} from "./chunk-GTLQQGKR.js";
import {
__name
} from "./chunk-VLQXSCFN.js";
// src/lib/pricing/process-stripe-webhook.ts
async function updatePurchaseStatus({ status, stripeChargeId, options }) {
await options.inngest.send({
name: PURCHASE_STATUS_UPDATED_EVENT,
data: {
stripeChargeId,
status
}
});
const purchase = await options.adapter?.getPurchaseForStripeCharge(stripeChargeId);
if (!purchase)
throw new Error("No purchase");
if (!purchase.merchantChargeId)
throw new Error("No merchant charge");
const merchantCharge = await options.adapter?.getMerchantCharge(purchase.merchantChargeId);
if (!merchantCharge)
throw new Error("No merchant charge");
const merchantChargeId = merchantCharge.id;
return options.adapter?.updatePurchaseStatusForCharge(merchantChargeId, status);
}
__name(updatePurchaseStatus, "updatePurchaseStatus");
async function processStripeWebhook(event, options) {
const stripeProvider = options.provider;
const stripeAdapter = stripeProvider.options.paymentsAdapter;
const courseBuilderAdapter = options.adapter;
switch (event.type) {
case "charge.dispute.funds_withdrawn":
console.log("charge.dispute.funds_withdrawn", event);
break;
case "charge.succeeded":
console.log("charge.succeeded", event);
break;
case "customer.updated":
console.log("customer.updated", event);
break;
case "customer.subscription.created":
console.log("customer.subscription.created", event);
break;
case "customer.subscription.deleted":
console.log("customer.subscription.deleted", event);
break;
case "customer.subscription.updated":
console.log("customer.subscription.updated", event);
break;
case "checkout.session.completed":
await options.inngest.send({
name: STRIPE_CHECKOUT_SESSION_COMPLETED_EVENT,
data: {
stripeEvent: checkoutSessionCompletedEvent.parse(event)
}
});
break;
case "checkout.session.async_payment_failed":
console.log("checkout.session.async_payment_failed", event);
break;
case "checkout.session.async_payment_succeeded":
console.log("checkout.session.async_payment_succeeded", event);
break;
case "charge.refunded":
await updatePurchaseStatus({
stripeChargeId: event.data.object.id,
status: "Refunded",
options
});
break;
case "charge.dispute.created":
await updatePurchaseStatus({
stripeChargeId: event.data.object.id,
status: "Disputed",
options
});
break;
}
}
__name(processStripeWebhook, "processStripeWebhook");
export {
updatePurchaseStatus,
processStripeWebhook
};
//# sourceMappingURL=chunk-7JAQB4AI.js.map