38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
import os
|
|
|
|
import stripe
|
|
from django.conf import settings
|
|
from django.http import HttpResponse, request
|
|
from django.shortcuts import redirect
|
|
from django.urls import reverse
|
|
|
|
|
|
class StripeClient:
|
|
def __init__(self, api_key: str):
|
|
self.api_key = api_key
|
|
|
|
self.stripe = stripe.StripeClient(api_key=self.api_key)
|
|
|
|
def create_checkout_session(self, price_id: str):
|
|
try:
|
|
checkout_session = self.stripe.v1.checkout.sessions.create(
|
|
params={
|
|
"line_items": [
|
|
{
|
|
"price": price_id,
|
|
"quantity": 1,
|
|
},
|
|
],
|
|
"mode": "payment",
|
|
# "success_url": f"http://{Site.objects.get_current().domain}{reverse('purchase_success')}",
|
|
"success_url": f"http://localhost:8000{reverse('purchase_success')}",
|
|
}
|
|
)
|
|
except Exception as e:
|
|
return HttpResponse(str(e))
|
|
|
|
return redirect(checkout_session.url, code=303)
|
|
|
|
|
|
stripe_client = StripeClient(api_key=os.getenv("STRIPE_SECRET_KEY"))
|