feat: first stripe imp
This commit is contained in:
37
purchase/stripe_client.py
Normal file
37
purchase/stripe_client.py
Normal file
@@ -0,0 +1,37 @@
|
||||
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"))
|
||||
12
purchase/templates/success.html
Normal file
12
purchase/templates/success.html
Normal file
@@ -0,0 +1,12 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
{% block content %}
|
||||
<div class="container mt-5">
|
||||
<div class="card">
|
||||
<div class="text-center">
|
||||
<h1 class="text-9xl">BRAWO KURWA!!!!</h1>
|
||||
<p>WYDAŁEŚ PIENIĄDZE NA JAKIEŚ TOTALNE GÓWNO!!!!</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
@@ -13,4 +13,6 @@ urlpatterns = [
|
||||
views.mock_refund_purchase,
|
||||
name="mock_refund_purchase",
|
||||
),
|
||||
path("test-purchase/", views.test_purchase, name="test_purchase"),
|
||||
path("success/", views.purchase_success, name="purchase_success"),
|
||||
]
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
from django.shortcuts import redirect, render
|
||||
from django.urls import reverse
|
||||
from django.views.decorators.http import require_POST
|
||||
|
||||
from home.models import CoursePage
|
||||
from purchase.stripe_client import stripe_client
|
||||
from purchase.models import CoursePurchase
|
||||
|
||||
|
||||
@@ -19,3 +21,12 @@ def mock_refund_purchase(request, purchase_id):
|
||||
purchase.mock_refund()
|
||||
|
||||
return redirect(purchase.course.url)
|
||||
|
||||
|
||||
@require_POST
|
||||
def test_purchase(request):
|
||||
return stripe_client.create_checkout_session("price_1TU8ZqK3lJAo3tbKX3T1jsBg")
|
||||
|
||||
|
||||
def purchase_success(request):
|
||||
return render(request, "success.html")
|
||||
|
||||
Reference in New Issue
Block a user