2021-08-29 15:33:29 +02:00
|
|
|
import logging
|
2021-12-03 00:23:56 +01:00
|
|
|
from typing import Any, Dict
|
2021-08-29 15:33:29 +02:00
|
|
|
|
|
|
|
import stripe
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from django.utils.translation import gettext as _
|
|
|
|
|
2023-11-06 13:52:12 +01:00
|
|
|
from corporate.lib.stripe import RealmBillingSession
|
|
|
|
from corporate.models import PaymentIntent, Session
|
2021-08-29 15:33:29 +02:00
|
|
|
from zerver.decorator import require_billing_access, require_organization_member
|
|
|
|
from zerver.lib.exceptions import JsonableError
|
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
|
|
|
from zerver.lib.response import json_success
|
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
|
|
|
billing_logger = logging.getLogger("corporate.stripe")
|
|
|
|
|
|
|
|
|
|
|
|
@require_billing_access
|
|
|
|
@has_request_variables
|
|
|
|
def start_card_update_stripe_session(request: HttpRequest, user: UserProfile) -> HttpResponse:
|
2023-11-06 13:52:12 +01:00
|
|
|
billing_session = RealmBillingSession(user)
|
|
|
|
assert billing_session.get_customer() is not None
|
|
|
|
metadata = {
|
|
|
|
"type": "card_update",
|
|
|
|
"user_id": user.id,
|
|
|
|
}
|
|
|
|
stripe_session = billing_session.create_stripe_checkout_session(
|
|
|
|
metadata, Session.CARD_UPDATE_FROM_BILLING_PAGE
|
2021-08-29 15:33:29 +02:00
|
|
|
)
|
|
|
|
return json_success(
|
2022-01-31 13:44:02 +01:00
|
|
|
request,
|
2021-08-29 15:33:29 +02:00
|
|
|
data={
|
|
|
|
"stripe_session_url": stripe_session.url,
|
|
|
|
"stripe_session_id": stripe_session.id,
|
2022-01-31 13:44:02 +01:00
|
|
|
},
|
2021-08-29 15:33:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@require_organization_member
|
|
|
|
@has_request_variables
|
|
|
|
def start_retry_payment_intent_session(
|
|
|
|
request: HttpRequest, user: UserProfile, stripe_payment_intent_id: str = REQ()
|
|
|
|
) -> HttpResponse:
|
2023-11-06 13:52:12 +01:00
|
|
|
billing_session = RealmBillingSession(user)
|
|
|
|
customer = billing_session.get_customer()
|
2021-08-29 15:33:29 +02:00
|
|
|
if customer is None:
|
|
|
|
raise JsonableError(_("Please create a customer first."))
|
|
|
|
|
|
|
|
try:
|
|
|
|
payment_intent = PaymentIntent.objects.get(
|
|
|
|
stripe_payment_intent_id=stripe_payment_intent_id, customer=customer
|
|
|
|
)
|
|
|
|
except PaymentIntent.DoesNotExist:
|
|
|
|
raise JsonableError(_("Invalid payment intent id."))
|
|
|
|
|
|
|
|
stripe_payment_intent = stripe.PaymentIntent.retrieve(stripe_payment_intent_id)
|
|
|
|
|
|
|
|
if stripe_payment_intent.status == "succeeded":
|
|
|
|
raise JsonableError(_("Payment already succeeded."))
|
|
|
|
|
|
|
|
if (
|
|
|
|
stripe_payment_intent.status == "processing"
|
|
|
|
): # nocoverage: Hard to arrive at this state using card
|
|
|
|
raise JsonableError(_("Payment processing."))
|
|
|
|
|
2021-12-03 00:23:56 +01:00
|
|
|
metadata: Dict[str, Any] = {
|
|
|
|
"user_id": user.id,
|
|
|
|
}
|
|
|
|
metadata.update(stripe_payment_intent.metadata)
|
2023-11-06 13:52:12 +01:00
|
|
|
stripe_session = billing_session.create_stripe_checkout_session(
|
|
|
|
metadata, Session.RETRY_UPGRADE_WITH_ANOTHER_PAYMENT_METHOD, payment_intent
|
2021-08-29 15:33:29 +02:00
|
|
|
)
|
|
|
|
return json_success(
|
2022-01-31 13:44:02 +01:00
|
|
|
request,
|
2021-08-29 15:33:29 +02:00
|
|
|
data={
|
|
|
|
"stripe_session_id": stripe_session.id,
|
|
|
|
"stripe_session_url": stripe_session.url,
|
2022-01-31 13:44:02 +01:00
|
|
|
},
|
2021-08-29 15:33:29 +02:00
|
|
|
)
|