Allow creating yearly/monthly Stripe plans
This commit is contained in:
		
					parent
					
						
							
								3bf2654b50
							
						
					
				
			
			
				commit
				
					
						e493a9f3d1
					
				
			
		
					 2 changed files with 19 additions and 4 deletions
				
			
		|  | @ -437,7 +437,9 @@ class PaymentOrderView(FormView): | ||||||
|                             'description' |                             'description' | ||||||
|                         ), |                         ), | ||||||
|                         "product_id": product.id, |                         "product_id": product.id, | ||||||
|                         "product_slug": product.product_slug |                         "product_slug": product.product_slug, | ||||||
|  |                         "recurring_interval": | ||||||
|  |                             product.product_subscription_interval | ||||||
|                     } |                     } | ||||||
|                     request.session["generic_payment_details"] = ( |                     request.session["generic_payment_details"] = ( | ||||||
|                         gp_details |                         gp_details | ||||||
|  | @ -756,6 +758,7 @@ class OrderConfirmationView(DetailView, FormView): | ||||||
| 
 | 
 | ||||||
|         if ('generic_payment_type' not in request.session or |         if ('generic_payment_type' not in request.session or | ||||||
|                 (request.session['generic_payment_details']['recurring'])): |                 (request.session['generic_payment_details']['recurring'])): | ||||||
|  |             recurring_interval = 'month' | ||||||
|             if 'generic_payment_details' in request.session: |             if 'generic_payment_details' in request.session: | ||||||
|                 amount_to_be_charged = ( |                 amount_to_be_charged = ( | ||||||
|                     round( |                     round( | ||||||
|  | @ -768,6 +771,10 @@ class OrderConfirmationView(DetailView, FormView): | ||||||
|                     amount_to_be_charged |                     amount_to_be_charged | ||||||
|                 ) |                 ) | ||||||
|                 stripe_plan_id = plan_name |                 stripe_plan_id = plan_name | ||||||
|  |                 recurring_interval = request.session['generic_payment_details']['recurring_interval'] | ||||||
|  |                 if recurring_interval == "year": | ||||||
|  |                     plan_name = "{}-yearly".format(plan_name) | ||||||
|  |                     stripe_plan_id = plan_name | ||||||
|             else: |             else: | ||||||
|                 template = request.session.get('template') |                 template = request.session.get('template') | ||||||
|                 specs = request.session.get('specs') |                 specs = request.session.get('specs') | ||||||
|  | @ -794,7 +801,9 @@ class OrderConfirmationView(DetailView, FormView): | ||||||
|             stripe_plan = stripe_utils.get_or_create_stripe_plan( |             stripe_plan = stripe_utils.get_or_create_stripe_plan( | ||||||
|                 amount=amount_to_be_charged, |                 amount=amount_to_be_charged, | ||||||
|                 name=plan_name, |                 name=plan_name, | ||||||
|                 stripe_plan_id=stripe_plan_id) |                 stripe_plan_id=stripe_plan_id, | ||||||
|  |                 interval=recurring_interval | ||||||
|  |             ) | ||||||
|             subscription_result = stripe_utils.subscribe_customer_to_plan( |             subscription_result = stripe_utils.subscribe_customer_to_plan( | ||||||
|                 stripe_api_cus_id, |                 stripe_api_cus_id, | ||||||
|                 [{"plan": stripe_plan.get( |                 [{"plan": stripe_plan.get( | ||||||
|  |  | ||||||
|  | @ -226,7 +226,8 @@ class StripeUtils(object): | ||||||
|         return charge |         return charge | ||||||
| 
 | 
 | ||||||
|     @handleStripeError |     @handleStripeError | ||||||
|     def get_or_create_stripe_plan(self, amount, name, stripe_plan_id): |     def get_or_create_stripe_plan(self, amount, name, stripe_plan_id, | ||||||
|  |                                   interval=""): | ||||||
|         """ |         """ | ||||||
|         This function checks if a StripePlan with the given |         This function checks if a StripePlan with the given | ||||||
|         stripe_plan_id already exists. If it exists then the function |         stripe_plan_id already exists. If it exists then the function | ||||||
|  | @ -238,6 +239,10 @@ class StripeUtils(object): | ||||||
|         :param stripe_plan_id: The id of the Stripe plan to be |         :param stripe_plan_id: The id of the Stripe plan to be | ||||||
|                created. Use get_stripe_plan_id_string function to |                created. Use get_stripe_plan_id_string function to | ||||||
|                obtain the name of the plan to be created |                obtain the name of the plan to be created | ||||||
|  |         :param interval: str representing the interval of the Plan | ||||||
|  |                Specifies billing frequency. Either day, week, month or year. | ||||||
|  |                Ref: https://stripe.com/docs/api/plans/create#create_plan-interval | ||||||
|  |                The default is month | ||||||
|         :return: The StripePlan object if it exists else creates a |         :return: The StripePlan object if it exists else creates a | ||||||
|                Plan object in Stripe and a local StripePlan and |                Plan object in Stripe and a local StripePlan and | ||||||
|                returns it. Returns None in case of Stripe error |                returns it. Returns None in case of Stripe error | ||||||
|  | @ -245,6 +250,7 @@ class StripeUtils(object): | ||||||
|         _amount = float(amount) |         _amount = float(amount) | ||||||
|         amount = int(_amount * 100)  # stripe amount unit, in cents |         amount = int(_amount * 100)  # stripe amount unit, in cents | ||||||
|         stripe_plan_db_obj = None |         stripe_plan_db_obj = None | ||||||
|  |         plan_interval = interval if interval is not "" else self.INTERVAL | ||||||
|         try: |         try: | ||||||
|             stripe_plan_db_obj = StripePlan.objects.get( |             stripe_plan_db_obj = StripePlan.objects.get( | ||||||
|                 stripe_plan_id=stripe_plan_id) |                 stripe_plan_id=stripe_plan_id) | ||||||
|  | @ -252,7 +258,7 @@ class StripeUtils(object): | ||||||
|             try: |             try: | ||||||
|                 self.stripe.Plan.create( |                 self.stripe.Plan.create( | ||||||
|                     amount=amount, |                     amount=amount, | ||||||
|                     interval=self.INTERVAL, |                     interval=plan_interval, | ||||||
|                     name=name, |                     name=name, | ||||||
|                     currency=self.CURRENCY, |                     currency=self.CURRENCY, | ||||||
|                     id=stripe_plan_id) |                     id=stripe_plan_id) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue