@donotdev/billing 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- "use client";import{useState as S,useCallback as c}from"react";import{DEGRADED_BILLING_API as N,FEATURE_STATUS as l,isClient as F,handleError as u,getPlatformEnvVar as d,FRAMEWORK_FEATURES as x,redirectToExternalUrlWithErrorHandling as M,useFeatureConsent as L}from"@donotdev/core";import{getFirebaseFunctions as w,httpsCallable as I}from"@donotdev/firebase";let h=!1;function K(i,E){const t=E?.user??null,p=(E?.status??l.DEGRADED)===l.READY,m=L(x.BILLING),[Y,a]=S(!1),[R,o]=S(null),A=c(async r=>{if(!F()||!m)throw new Error("Billing not available in SSR or consent required");if(!t){const e=u(new Error("User must be authenticated"),{userMessage:"Please sign in to continue with checkout.",severity:"warning"});throw o(e.message),e}a(!0),o(null);try{const e=d("FIREBASE_FUNCTIONS_REGION")||"europe-west1",s=await w(e),n=I(s,"createCheckoutSession"),f=t.providerData?.find(k=>k.providerId==="github.com"),y=f?.displayName,U=f?.uid,O={priceId:r.priceId,userId:t.id,userEmail:t.email||"",customerEmail:t.email||"",metadata:{firebaseUid:t.id,...y&&{githubUsername:y},...U&&{githubUid:U},...r.metadata},successUrl:r.successUrl||`${typeof window<"u"?window.location.origin:""}/billing/success`,cancelUrl:r.cancelUrl||(typeof window<"u"?window.location.href:""),allowPromotionCodes:r.allowPromotionCodes??!0,mode:r.mode},D=await n(O),{sessionId:B,sessionUrl:b}=D.data;if(!b)throw new Error("No checkout URL received from server");return h=!1,{sessionId:B,sessionUrl:b,success:!0,error:void 0}}catch(e){if(h){const s=e instanceof Error?e:new Error("Checkout failed");throw o(s.message),s}else{const s=u(e,{userMessage:"Unable to start checkout. Please try again or contact support if the problem persists.",context:{priceId:r.priceId,mode:r.mode},severity:"error"});throw o(s.message),h=!0,s}}finally{a(!1)}},[t,m]),C=c(async()=>{if(!t)throw new Error("User must be authenticated");a(!0),o(null);try{const r=d("FIREBASE_FUNCTIONS_REGION")||"europe-west1",e=await w(r);return(await I(e,"cancelSubscription")({userId:t.id})).data}catch(r){const e=u(r,{userMessage:"Failed to cancel subscription. Please try again or contact support.",context:{operation:"cancelSubscription",userId:t.id},severity:"error"});throw o(e.message),e}finally{a(!1)}},[t]),P=c(async(r,e)=>{if(!t)throw new Error("User must be authenticated");a(!0),o(null);try{const s=d("FIREBASE_FUNCTIONS_REGION")||"europe-west1",n=await w(s);return(await I(n,"changePlan")({userId:t.id,newPriceId:r,billingConfigKey:e})).data}catch(s){const n=u(s,{userMessage:"Failed to change plan. Please try again or contact support.",context:{operation:"changePlan",userId:t.id,newPriceId:r,billingConfigKey:e},severity:"error"});throw o(n.message),n}finally{a(!1)}},[t]),v=c(async()=>{if(!t)throw new Error("User must be authenticated");a(!0),o(null);try{const r=d("FIREBASE_FUNCTIONS_REGION")||"europe-west1",e=await w(r);await I(e,"refreshSubscriptionStatus")({userId:t.id})}catch(r){const e=u(r,{userMessage:"Failed to refresh subscription status. Please try again.",context:{operation:"refreshStatus",userId:t.id},severity:"error"});throw o(e.message),e}finally{a(!1)}},[t]),_=c(async r=>{if(!t)throw new Error("User must be authenticated");a(!0),o(null);try{const e=d("FIREBASE_FUNCTIONS_REGION")||"europe-west1",s=await w(e),n=await I(s,"createCustomerPortal")({userId:t.id,returnUrl:r||(typeof window<"u"?window.location.href:"")});await M(n.data.url,{},"Failed to redirect to customer portal")}catch(e){const s=u(e,{userMessage:"Failed to open customer portal. Please try again or contact support.",context:{operation:"openCustomerPortal",userId:t.id},severity:"error"});throw o(s.message),s}finally{a(!1)}},[t]),T=c(()=>{o(null),h=!1},[]),g={checkout:A,cancelSubscription:C,changePlan:P,refreshStatus:v,openCustomerPortal:_,clearError:T};if(!F()||!m)return N[i];const G=p?l.READY:l.INITIALIZING;return i==="status"?G:i==="isAvailable"?!!p:i==="status"?p?l.READY:l.INITIALIZING:i==="error"?R:i in g?g[i]:N[i]}export{K as useStripeBilling};
1
+ "use client";import{useState as U,useCallback as c}from"react";import{DEGRADED_BILLING_API as y,FEATURE_STATUS as u,isClient as C,handleError as l,getPlatformEnvVar as p,FRAMEWORK_FEATURES as k,redirectToExternalUrlWithErrorHandling as x,useFeatureConsent as M}from"@donotdev/core";import{getFirebaseFunctions as f,httpsCallable as h}from"@donotdev/firebase";let g=!1;function Z(a,E){const t=E?.user??null,w=(E?.status??u.DEGRADED)===u.READY,m=M(k.BILLING),[q,n]=U(!1),[F,s]=U(null),R=c(async r=>{if(!C()||!m)throw new Error("Billing not available in SSR or consent required");if(!t){const e=l(new Error("User must be authenticated"),{userMessage:"Please sign in to continue with checkout.",severity:"warning"});throw s(e.message),e}n(!0),s(null);try{const e=p("FIREBASE_FUNCTIONS_REGION")||"europe-west1",o=await f(e),i=h(o,"createCheckoutSession"),d=t.providerData?.find(L=>L.providerId==="github.com"),I=d?.displayName,N=d?.uid,T={priceId:r.priceId,userId:t.id,userEmail:t.email||"",customerEmail:t.email||"",metadata:{firebaseUid:t.id,...I&&{githubUsername:I},...N&&{githubUid:N},...r.metadata},successUrl:r.successUrl||`${typeof window<"u"?window.location.origin:""}/billing/success`,cancelUrl:r.cancelUrl||(typeof window<"u"?window.location.href:""),allowPromotionCodes:r.allowPromotionCodes??!0,mode:r.mode},G=await i(T),{sessionId:D,sessionUrl:b}=G.data;if(!b)throw new Error("No checkout URL received from server");return g=!1,{sessionId:D,sessionUrl:b,success:!0,error:void 0}}catch(e){if(g){const o=e instanceof Error?e:new Error("Checkout failed");throw s(o.message),o}else{const o=l(e,{userMessage:"Unable to start checkout. Please try again or contact support if the problem persists.",context:{priceId:r.priceId,mode:r.mode},severity:"error"});throw s(o.message),g=!0,o}}finally{n(!1)}},[t,m]),A=c(async()=>{if(!t)throw new Error("User must be authenticated");n(!0),s(null);try{const r=p("FIREBASE_FUNCTIONS_REGION")||"europe-west1",e=await f(r);return(await h(e,"cancelSubscription")({userId:t.id})).data}catch(r){const e=l(r,{userMessage:"Failed to cancel subscription. Please try again or contact support.",context:{operation:"cancelSubscription",userId:t.id},severity:"error"});throw s(e.message),e}finally{n(!1)}},[t]),P=c(async(r,e)=>{if(!t)throw new Error("User must be authenticated");n(!0),s(null);try{const o=p("FIREBASE_FUNCTIONS_REGION")||"europe-west1",i=await f(o);return(await h(i,"changePlan")({userId:t.id,newPriceId:r,billingConfigKey:e})).data}catch(o){const i=l(o,{userMessage:"Failed to change plan. Please try again or contact support.",context:{operation:"changePlan",userId:t.id,newPriceId:r,billingConfigKey:e},severity:"error"});throw s(i.message),i}finally{n(!1)}},[t]),v=c(async()=>{if(!t)throw new Error("User must be authenticated");n(!0),s(null);try{const r=p("FIREBASE_FUNCTIONS_REGION")||"europe-west1",e=await f(r);await h(e,"refreshSubscriptionStatus")({userId:t.id})}catch(r){const e=l(r,{userMessage:"Failed to refresh subscription status. Please try again.",context:{operation:"refreshStatus",userId:t.id},severity:"error"});throw s(e.message),e}finally{n(!1)}},[t]),_=c(async r=>{if(!t)throw new Error("User must be authenticated");n(!0),s(null);try{const e=p("FIREBASE_FUNCTIONS_REGION")||"europe-west1",o=await f(e),d=await h(o,"createCustomerPortal")({userId:t.id,returnUrl:r||(typeof window<"u"?window.location.href:"")});await x(d.data.url,{},"Failed to redirect to customer portal")}catch(e){const o=l(e,{userMessage:"Failed to open customer portal. Please try again or contact support.",context:{operation:"openCustomerPortal",userId:t.id},severity:"error"});throw s(o.message),o}finally{n(!1)}},[t]),B=c(()=>{s(null),g=!1},[]),S={checkout:R,cancelSubscription:A,changePlan:P,refreshStatus:v,openCustomerPortal:_,clearError:B};if(!C()||!m)return y[a];const O=w?u.READY:u.INITIALIZING;return a==="status"?O:a==="isAvailable"?!!w:a==="status"?w?u.READY:u.INITIALIZING:a==="error"?F:a in S?S[a]:y[a]}export{Z as useStripeBilling};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@donotdev/billing",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "license": "SEE LICENSE IN LICENSE.md",
@@ -33,10 +33,10 @@
33
33
  },
34
34
  "dependencies": {},
35
35
  "peerDependencies": {
36
- "@donotdev/auth": "0.0.3",
37
- "@donotdev/components": "0.0.3",
38
- "@donotdev/core": "0.0.3",
39
- "@donotdev/firebase": "0.0.3",
36
+ "@donotdev/auth": "0.0.4",
37
+ "@donotdev/components": "0.0.4",
38
+ "@donotdev/core": "0.0.4",
39
+ "@donotdev/firebase": "0.0.4",
40
40
  "lucide-react": "^0.562.0",
41
41
  "react": "^19.2.3",
42
42
  "react-dom": "^19.2.3",