next-stripe-helper
Advanced tools
Comparing version 1.1.17 to 1.1.18
@@ -28,2 +28,10 @@ import Stripe from "stripe"; | ||
/** | ||
* Retrieves the product metadata associated with a given subscription ID. | ||
* | ||
* @param {string} subscriptionId - The ID of the subscription. | ||
* @returns {Promise<Stripe.Metadata | null>} - A promise that resolves to the product metadata or null if not found. | ||
* @throws {Error} - Throws an error if there's an issue retrieving the product metadata. | ||
*/ | ||
declare function getProductMetadataFromSubscription(subscriptionId: string): Promise<Stripe.Metadata | null>; | ||
/** | ||
* Updates a customer's subscription to a new plan. Deletes the old one plan and adds the new one to the subscription. | ||
@@ -67,2 +75,2 @@ * | ||
}>; | ||
export { createSubscription, getUserCurrentPlan, getUserSubscription, getUserSubscriptions, getUserSubscriptionDetails, updateUserSubscriptionMetadata, listUserSubscriptions, changeSubscriptionPlan, updateSubscriptionPlan, cancelUserSubscription, getSubscriptionPeriod }; | ||
export { createSubscription, getUserCurrentPlan, getUserSubscription, getUserSubscriptions, getUserSubscriptionDetails, updateUserSubscriptionMetadata, listUserSubscriptions, changeSubscriptionPlan, updateSubscriptionPlan, cancelUserSubscription, getSubscriptionPeriod, getProductMetadataFromSubscription }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getSubscriptionPeriod = exports.cancelUserSubscription = exports.updateSubscriptionPlan = exports.changeSubscriptionPlan = exports.listUserSubscriptions = exports.updateUserSubscriptionMetadata = exports.getUserSubscriptionDetails = exports.getUserSubscriptions = exports.getUserSubscription = exports.getUserCurrentPlan = exports.createSubscription = void 0; | ||
exports.getProductMetadataFromSubscription = exports.getSubscriptionPeriod = exports.cancelUserSubscription = exports.updateSubscriptionPlan = exports.changeSubscriptionPlan = exports.listUserSubscriptions = exports.updateUserSubscriptionMetadata = exports.getUserSubscriptionDetails = exports.getUserSubscriptions = exports.getUserSubscription = exports.getUserCurrentPlan = exports.createSubscription = void 0; | ||
const stripe_1 = require("../utils/stripe"); | ||
@@ -106,2 +106,34 @@ /** | ||
/** | ||
* Retrieves the product metadata associated with a given subscription ID. | ||
* | ||
* @param {string} subscriptionId - The ID of the subscription. | ||
* @returns {Promise<Stripe.Metadata | null>} - A promise that resolves to the product metadata or null if not found. | ||
* @throws {Error} - Throws an error if there's an issue retrieving the product metadata. | ||
*/ | ||
async function getProductMetadataFromSubscription(subscriptionId) { | ||
try { | ||
const subscription = await stripe_1.stripe.subscriptions.retrieve(subscriptionId, { | ||
expand: ['items.data.price.product'], | ||
}); | ||
const subscriptionItem = subscription.items.data[0]; | ||
if (!subscriptionItem || !subscriptionItem.price) { | ||
console.error('No associated product found for the subscription.'); | ||
return null; | ||
} | ||
const product = subscriptionItem.price.product; | ||
// Ensure product is neither a string nor a DeletedProduct | ||
if (typeof product === 'string' || 'deleted' in product) { | ||
console.error('The product is either deleted or not a valid object.'); | ||
return null; | ||
} | ||
// Access the metadata from the product directly | ||
return product.metadata; | ||
} | ||
catch (error) { | ||
console.error("Error fetching product metadata:", error); | ||
return null; | ||
} | ||
} | ||
exports.getProductMetadataFromSubscription = getProductMetadataFromSubscription; | ||
/** | ||
* Updates a customer's subscription to a new plan. Deletes the old one plan and adds the new one to the subscription. | ||
@@ -108,0 +140,0 @@ * |
{ | ||
"name": "next-stripe-helper", | ||
"version": "1.1.17", | ||
"version": "1.1.18", | ||
"description": "Easily add Stripe boilerplate code to Nextjs, like webhook handling, and subscription updates. This package provides a thin wrapper around the Stripe API, and makes integrating Stripe and NextJS a lot faster!", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -308,3 +308,3 @@ # next-stripe-helper | ||
```javascript | ||
const subscriptionDetails = await changeSubscriptionPlan('subscription_id', 'old_plan_id', 'new_plan_id') | ||
const subscriptionDetails = await changeSubscriptionPlan('subscription_id', 'item_id', 'new_price_id') | ||
``` | ||
@@ -314,5 +314,5 @@ | ||
- `subscriptionID` (required): The Stripe ID of the existing subscription. | ||
- `new_plan_id` (required): The Stripe plan ID of the old plan (price_id). | ||
- `new_plan_id` (required): The Stripe plan ID of the new plan (price_id). | ||
- `subscriptionID` (required): The Stripe subscription ID of the existing subscription. | ||
- `item_id` (required): The Stripe Item ID of the plan. | ||
- `new_price_id` (required): The Stripe Price ID of the new plan (price_id). | ||
@@ -319,0 +319,0 @@ |
import Stripe from "stripe"; | ||
import { handleStripeError, stripe } from "../utils/stripe"; | ||
/** | ||
@@ -114,2 +113,39 @@ * Create a new subscription for a customer. | ||
/** | ||
* Retrieves the product metadata associated with a given subscription ID. | ||
* | ||
* @param {string} subscriptionId - The ID of the subscription. | ||
* @returns {Promise<Stripe.Metadata | null>} - A promise that resolves to the product metadata or null if not found. | ||
* @throws {Error} - Throws an error if there's an issue retrieving the product metadata. | ||
*/ | ||
async function getProductMetadataFromSubscription(subscriptionId: string): Promise<Stripe.Metadata | null> { | ||
try { | ||
const subscription = await stripe.subscriptions.retrieve(subscriptionId, { | ||
expand: ['items.data.price.product'], | ||
}); | ||
const subscriptionItem = subscription.items.data[0]; | ||
if (!subscriptionItem || !subscriptionItem.price) { | ||
console.error('No associated product found for the subscription.'); | ||
return null; | ||
} | ||
const product = subscriptionItem.price.product; | ||
// Ensure product is neither a string nor a DeletedProduct | ||
if (typeof product === 'string' || 'deleted' in product) { | ||
console.error('The product is either deleted or not a valid object.'); | ||
return null; | ||
} | ||
// Access the metadata from the product directly | ||
return product.metadata; | ||
} catch (error) { | ||
console.error("Error fetching product metadata:", error); | ||
return null; | ||
} | ||
} | ||
/** | ||
* Updates a customer's subscription to a new plan. Deletes the old one plan and adds the new one to the subscription. | ||
@@ -233,3 +269,4 @@ * | ||
cancelUserSubscription, | ||
getSubscriptionPeriod | ||
getSubscriptionPeriod, | ||
getProductMetadataFromSubscription | ||
} |
Sorry, the diff of this file is not supported yet
94981
1405