From 2a59a3336bd5b2342c38f173a9592256ee18bb3d Mon Sep 17 00:00:00 2001 From: PCoder Date: Sat, 28 Oct 2017 20:25:18 +0200 Subject: [PATCH] Add metadata (vm_id) to Stripe subscription --- datacenterlight/tasks.py | 5 +++++ utils/stripe_utils.py | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/datacenterlight/tasks.py b/datacenterlight/tasks.py index 3db6eb54..149781ec 100644 --- a/datacenterlight/tasks.py +++ b/datacenterlight/tasks.py @@ -18,6 +18,7 @@ from utils.hosting_utils import get_all_public_keys, get_or_create_vm_detail from utils.forms import UserBillingAddressForm from utils.mailer import BaseEmail from utils.models import BillingAddress +from utils.stripe_utils import StripeUtils logger = get_task_logger(__name__) @@ -118,6 +119,10 @@ def create_vm_task(self, vm_template_id, user, specs, template, # Associate an order with a stripe subscription order.set_subscription_id(stripe_subscription_id, cc_details) + stripe_utils = StripeUtils() + stripe_utils.set_subscription_meta_data( + stripe_subscription_id, {'ID': vm_id} + ) # If the Stripe payment succeeds, set order status approved order.set_approved() diff --git a/utils/stripe_utils.py b/utils/stripe_utils.py index e43304ca..74b6fa4a 100644 --- a/utils/stripe_utils.py +++ b/utils/stripe_utils.py @@ -304,3 +304,15 @@ class StripeUtils(object): cpu=cpu, memory=memory, disk_size=disk_size) + + @handleStripeError + def set_subscription_meta_data(self, subscription_id, meta_data): + """ + Adds VM metadata to a subscription + :param subscription_id: Stripe identifier for the subscription + :param meta_data: A dict of meta data to be added + :return: + """ + subscription = stripe.Subscription.retrieve(subscription_id) + subscription.metadata = meta_data + subscription.save() \ No newline at end of file