Преглед изворни кода

Setup payment as default after confirming creation

Mauricio Araujo пре 2 година
родитељ
комит
f8967f6867

+ 7 - 1
dashboard/src/main/home/modals/BillingModal.tsx

@@ -13,7 +13,13 @@ import {
 
 import PaymentSetupForm from "./PaymentSetupForm";
 
-const BillingModal = ({ back, onCreate }) => {
+const BillingModal = ({
+  back,
+  onCreate,
+}: {
+  back: (value: React.SetStateAction<boolean>) => void;
+  onCreate: () => Promise<void>;
+}) => {
   const { publishableKey } = usePublishableKey();
   const stripePromise = loadStripe(publishableKey);
   checkBillingCustomerExists();

+ 12 - 3
dashboard/src/main/home/modals/PaymentSetupForm.tsx

@@ -10,15 +10,19 @@ import Button from "components/porter/Button";
 import Error from "components/porter/Error";
 import Spacer from "components/porter/Spacer";
 import SaveButton from "components/SaveButton";
-import { useCreatePaymentMethod } from "lib/hooks/useStripe";
+import {
+  useCreatePaymentMethod,
+  useSetDefaultPaymentMethod,
+} from "lib/hooks/useStripe";
 
-const PaymentSetupForm = ({ onCreate }: { onCreate: () => void }) => {
+const PaymentSetupForm = ({ onCreate }: { onCreate: () => Promise<void> }) => {
   const stripe = useStripe();
   const elements = useElements();
 
   const [errorMessage, setErrorMessage] = useState(null);
   const [loading, setLoading] = useState(false);
   const { createPaymentMethod } = useCreatePaymentMethod();
+  const { setDefaultPaymentMethod } = useSetDefaultPaymentMethod();
 
   const handleSubmit = async () => {
     if (!stripe || !elements) {
@@ -38,7 +42,7 @@ const PaymentSetupForm = ({ onCreate }: { onCreate: () => void }) => {
     const clientSecret = await createPaymentMethod();
 
     // Finally, confirm with Stripe so the payment method is saved
-    const { error } = await stripe.confirmSetup({
+    const { error, setupIntent } = await stripe.confirmSetup({
       elements,
       clientSecret,
       redirect: "if_required",
@@ -48,6 +52,11 @@ const PaymentSetupForm = ({ onCreate }: { onCreate: () => void }) => {
       setErrorMessage(error.message);
     }
 
+    // Confirm the setup and set as default
+    if (setupIntent?.payment_method !== null) {
+      await setDefaultPaymentMethod(setupIntent?.payment_method as string);
+    }
+
     onCreate();
   };
 

+ 17 - 12
internal/billing/stripe.go

@@ -81,21 +81,11 @@ func (s *StripeBillingManager) ListPaymentMethod(proj *models.Project) (paymentM
 	}
 	result := paymentmethod.List(params)
 
-	// Get customer to check default payment method
-	customer, err := customer.Get(proj.BillingID, nil)
+	defaultPaymentExists, defaultPaymentID, err := s.checkDefaultPaymentMethod(proj.BillingID)
 	if err != nil {
 		return paymentMethods, err
 	}
 
-	var (
-		defaultPaymentExists bool
-		defaultPaymentID     string
-	)
-	if customer.InvoiceSettings != nil && customer.InvoiceSettings.DefaultPaymentMethod != nil {
-		defaultPaymentExists = true
-		defaultPaymentID = customer.InvoiceSettings.DefaultPaymentMethod.ID
-	}
-
 	for result.Next() {
 		stripePaymentMethod := result.PaymentMethod()
 
@@ -146,7 +136,7 @@ func (s *StripeBillingManager) CreatePaymentMethod(proj *models.Project) (client
 	return intent.ClientSecret, nil
 }
 
-// CreatePaymentMethod will add a new payment method to the project in Stripe
+// SetDefaultPaymentMethod will add a new payment method to the project in Stripe
 func (s *StripeBillingManager) SetDefaultPaymentMethod(paymentMethodID string, proj *models.Project) (err error) {
 	stripe.Key = s.StripeSecretKey
 
@@ -180,3 +170,18 @@ func (s *StripeBillingManager) DeletePaymentMethod(paymentMethodID string) (err
 func (s *StripeBillingManager) GetPublishableKey() (key string) {
 	return s.StripePublishableKey
 }
+
+func (s *StripeBillingManager) checkDefaultPaymentMethod(customerID string) (defaultPaymentExists bool, defaultPaymentID string, err error) {
+	// Get customer to check default payment method
+	customer, err := customer.Get(customerID, nil)
+	if err != nil {
+		return defaultPaymentExists, defaultPaymentID, err
+	}
+
+	if customer.InvoiceSettings != nil && customer.InvoiceSettings.DefaultPaymentMethod != nil {
+		defaultPaymentExists = true
+		defaultPaymentID = customer.InvoiceSettings.DefaultPaymentMethod.ID
+	}
+
+	return defaultPaymentExists, defaultPaymentID, err
+}