auto recurring testing phase 2

This commit is contained in:
rizwanisready
2024-07-31 15:50:35 +05:30
parent e3189344ea
commit e3b86d64fd
5 changed files with 43 additions and 2 deletions

View File

@@ -26,6 +26,7 @@ class SubscriptionForm(forms.ModelForm):
model = Subscription
fields = [
"title",
"stripe_product",
"plan",
"high_amount",
"amount",

View File

@@ -22,6 +22,20 @@ urlpatterns = [
views.SubscriptionDeleteView.as_view(),
name="subscription_delete",
),
# Stripe Products
path(
"product/list/", views.StripeProductView.as_view(), name="stripe_product_list"
),
path(
"product/add/",
views.StripeProductCreateOrUpdateView.as_view(),
name="stripe_product_add",
),
path(
"product/delete/<int:pk>",
views.StripeProductDeleteView.as_view(),
name="stripe_product_delete",
),
# PLANS
path("plan/list/", views.PlanView.as_view(), name="plan_list"),
# path(

View File

@@ -226,7 +226,7 @@ class StripeProductCreateOrUpdateView(LoginRequiredMixin, generic.View):
template_name = "manage_subscriptions/product_add.html"
model = StripeProduct
form_class = StripeProductForm
success_url = reverse_lazy("manage_subscriptions:product_list")
success_url = reverse_lazy("manage_subscriptions:stripe_product_list")
error_message = "An error occurred while saving the data."
# Determine the success message dynamically based on whether it's an update or create
@@ -273,10 +273,35 @@ class StripeProductCreateOrUpdateView(LoginRequiredMixin, generic.View):
print(form.errors)
context = self.get_context_data(form=form)
return render(request, self.template_name, context=context)
success, message = self.handle_stripe_product(form)
if not success:
messages.error(self.request, message)
context = self.get_context_data(form=form)
return render(request, self.template_name, context=context)
form.save()
messages.success(self.request, self.get_success_message())
return redirect(self.success_url)
def handle_stripe_product(self, form):
try:
stripe.api_key = settings.STRIPE_SECRET_KEY
stripe_product = stripe.Product.create(
name=form.cleaned_data.get("title"),
description=form.cleaned_data.get("description"),
)
# Save Stripe Product ID to the form instance
form.instance.product_id = stripe_product.id
return True, "" # Success
except stripe.error.StripeError as e:
return False, f"Stripe error: {str(e)}"
except Exception as e:
return False, f"An error occurred: {str(e)}"
class StripeProductView(LoginRequiredMixin, generic.ListView):
page_name = resource_action.RESOURCE_MANAGE_SUBSCRIPTIONS

View File

@@ -16,7 +16,7 @@
<div class="col-lg-12">
<div class="row mb-2">
<div class="col">
<h3>{{operation}} {{page_name}}</h3>
<h3>Add Subscription</h3>
</div>
<div class="col text-end">
<button class="btn btn-dark mb-2 me-4" onclick="history.back()">

View File

@@ -22,6 +22,7 @@
</button>
-->
<a class="btn btn-primary mb-2" href="{% url 'manage_subscriptions:subscription_add' %}">Add Subscriptions</a>
<a class="btn btn-primary mb-2" href="{% url 'manage_subscriptions:stripe_product_add' %}">Add Stripe Product</a>
<!-- <a class="btn btn-primary mb-2" href="{% url 'manage_subscriptions:plan_list' %}">Plans</a>
<a class="btn btn-primary mb-2" href="{% url 'manage_subscriptions:principal_subscriptions_list' %}">Principal Subscription</a> -->
</div>