@@ -182,10 +182,57 @@ export async function createPrice(
182182 unit_amount : amountCents ,
183183 currency : "cad" ,
184184 } ) ;
185- await stripe . products . update ( productId , { default_price : price . id } ) ;
185+
186186 return { priceId : price . id } ;
187187}
188188
189+ export async function replaceProductPrice (
190+ productId : string ,
191+ amountCents : number ,
192+ ) : Promise < { priceId : string } > {
193+ // get the current default price for the product
194+ const product = await stripe . products . retrieve ( productId , {
195+ expand : [ "default_price" ] ,
196+ } ) ;
197+
198+ const currentDefaultPrice =
199+ typeof product . default_price === "string"
200+ ? product . default_price
201+ : product . default_price ?. id ;
202+
203+ // create the new price FIRST (rlly important)
204+ const newPrice = await stripe . prices . create ( {
205+ product : productId ,
206+ unit_amount : amountCents ,
207+ currency : "cad" ,
208+ } ) ;
209+
210+ // set the new price as the product default
211+ await stripe . products . update ( productId , {
212+ default_price : newPrice . id ,
213+ } ) ;
214+
215+ // archive old active prices but NEVER archive the new default price
216+ const prices = await stripe . prices . list ( {
217+ product : productId ,
218+ active : true ,
219+ limit : 100 ,
220+ } ) ;
221+
222+ for ( const p of prices . data ) {
223+ if ( p . id !== newPrice . id && p . id !== currentDefaultPrice ) {
224+ await stripe . prices . update ( p . id , { active : false } ) ;
225+ }
226+ }
227+
228+ // archive the old default price after default_price has been swapped
229+ if ( currentDefaultPrice && currentDefaultPrice !== newPrice . id ) {
230+ await stripe . prices . update ( currentDefaultPrice , { active : false } ) ;
231+ }
232+
233+ return { priceId : newPrice . id } ;
234+ }
235+
189236export async function deactivateActivePricesForProduct (
190237 productId : string ,
191238) : Promise < void > {
0 commit comments