<reponame>yarons/readthedocs.org
# -*- coding: utf-8 -*-

"""
Payment utility functions.

These are mostly one-off functions. Define the bulk of Stripe operations on
:py:class:`readthedocs.payments.forms.StripeResourceMixin`.
"""

import structlog

import stripe

from django.conf import settings


stripe.api_key = settings.STRIPE_SECRET
log = structlog.get_logger(__name__)


def delete_customer(customer_id):
    """Delete customer from Stripe, cancelling subscriptions."""
    try:
        customer = stripe.Customer.retrieve(customer_id)
        return customer.delete()
    except stripe.error.InvalidRequestError:
        log.exception(
            'Customer not deleted. Customer not found on Stripe.',
            stripe_customer=customer_id,
        )


def cancel_subscription(customer_id, subscription_id):
    """Cancel Stripe subscription, if it exists."""
    try:
        customer = stripe.Customer.retrieve(customer_id)
        if hasattr(customer, 'subscriptions'):
            subscription = customer.subscriptions.retrieve(subscription_id)
            return subscription.delete()
    except stripe.error.StripeError:
        log.exception(
            'Subscription not cancelled. Customer/Subscription not found on Stripe. ',
            stripe_customer=customer_id,
            stripe_subscription=subscription_id,
        )

# -*- coding: utf-8 -*-

"""
Payment utility functions.

These are mostly one-off functions. Define the bulk of Stripe operations on
:py:class:`readthedocs.payments.forms.StripeResourceMixin`.
"""

import structlog

import stripe

from django.conf import settings


stripe.api_key = settings.STRIPE_SECRET
log = structlog.get_logger(__name__)


def delete_customer(customer_id):
    """Delete customer from Stripe, cancelling subscriptions."""
    try:
        customer = stripe.Customer.retrieve(customer_id)
        return customer.delete()
    except stripe.error.InvalidRequestError:
        log.exception(
            'Customer not deleted. Customer not found on Stripe.',
            stripe_customer=customer_id,
        )


def cancel_subscription(customer_id, subscription_id):
    """Cancel Stripe subscription, if it exists."""
    try:
        customer = stripe.Customer.retrieve(customer_id)
        if hasattr(customer, 'subscriptions'):
            subscription = customer.subscriptions.retrieve(subscription_id)
            return subscription.delete()
    except stripe.error.StripeError:
        log.exception(
            'Subscription not cancelled. Customer/Subscription not found on Stripe. ',
            stripe_customer=customer_id,
            stripe_subscription=subscription_id,
        )
# -*- coding: utf-8 -*- Payment utility functions.  These are mostly one-off functions. Define the bulk of Stripe operations on :py:class:`readthedocs.payments.forms.StripeResourceMixin`. Delete customer from Stripe, cancelling subscriptions. Cancel Stripe subscription, if it exists.