اذهب إلى المحتوى
  • 0

كيف ادمج بوابة الدفع stripe مع django

Ecommerce Vente

السؤال

Recommended Posts

  • 0

بوابة stripe من أفضل بوابات الدفع التي تعاملت معها وذلك لسهولة التعامل معها . لتقوم بدمج هذه الأخيرة مع مشروع جانغو عليك باتباع بعض الخطوات الأساسية .

  1. ضبط إعدادات stripe 
    pip install stripe
    pip freeze > requirements.txt

    توجه إلى الرابط الموضح أدناه واحصل على مفتاح Stripe العام ومفتاح Stripe السري.
    https://dashboard.stripe.com/test/apikeys

    قم بإنشاء متغيرين في file settings.py وأضفهما هناك. 

    STRIPE_PUBLIC_KEY = "pk_test_sdasda7dyasdsa7dysadsahdas7dysa"
    STRIPE_SECRET_KEY = "sk_test_sd87da87d78sagsadas7dasv2e378e7" 

     

  2. انتقل إلى https://dashboard.stripe.com/test/products/create وأنشئ منتجًا جديدًا بدفع دفعة واحدة كما هو موضح أدناه. 

  3. الآن سنحتاج إلى إضافة طرق عرض لاستدعاء StripeAPI لإنشاء جلسة تسجيل الخروج.

    سنحتاج إلى إضافة متغير آخر في.setting.py بالاسم BASE_URL وتعيين قيمته على http://127.0.0.1:8000. بالإضافة إلى ذلك ، أضف الكود التالي إلى views.py من تطبيق المدفوعات.

    import stripe
    from django.conf import settings
    from django.shortcuts import redirect
    from django.views import View
    
    from payments.models import Price
    
    stripe.api_key = settings.STRIPE_SECRET_KEY
    
    
    class CreateCheckoutSessionView(View):
    
        def post(self, request, *args, **kwargs):
            price = Price.objects.get(id=self.kwargs["pk"])
            checkout_session = stripe.checkout.Session.create(
                payment_method_types=['card'],
                line_items=[
                    {
                        'price': price.stripe_price_id,
                        'quantity': 1,
                    },
                ],
                mode='payment',
                success_url=settings.BASE_URL + '/payments/success/',
                cancel_url=settings.BASE_URL + '/payments/cancel/',
            )
            return redirect(checkout_session.url)
          
    class SuccessView(TemplateView):
        template_name = "success.html"
    
    
    class CancelView(TemplateView):
        template_name = "cancel.html"

    لا تنس إضافة النماذج الخاصة بهم أيضًا. 

    <html>
    
    <head>
        <title>شكرا لإتمام طلبك!</title>
    </head>
    
    <body>
        <section>
            <p>
                سنتصل بك عندما نتحقق من الطلب
            </p>
            <a href="{% url 'home' %}">الرجوع</a>
        </section>
    </body>
    
    </html>
    <html>
    
    <head>
        <title>إلغاء</title>
    </head>
    
    <body>
        <section>
            <p>هل نسيت معلومات بطاقتك <a href="{% url 'home' %}">أعد المحاولة</a></p>
        </section>
    </body>
    
    </html>

     

  4. ملف urls.py

    from django.urls import path
    
    from payments.views import CancelView, SuccessView, CreateCheckoutSessionView
    
    urlpatterns = [
        path('cancel/', CancelView.as_view(), name='cancel'),
        path('success/', SuccessView.as_view(), name='success'),
        path('create-checkout-session/<pk>/', CreateCheckoutSessionView.as_view(), name='create-checkout-session')
    ]

    إضافة دالة عرض جديدة في views.py من تطبيق المدفوعات.

    class HomePageView(TemplateView):
        template_name = "home.html"
    
        def get_context_data(self, **kwargs):
            product = Product.objects.get(name="Basic Plan")
            prices = Price.objects.filter(product=product)
            context = super(HomePageView, self).get_context_data(**kwargs)
            context.update({
                "product": product,
                "prices": prices
            })
            return context
    from django.contrib import admin
    from django.urls import path, include
    
    from payments.views import HomePageView
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('payments/', include('payments.urls')),
        path('', HomePageView.as_view(), name='home')
    ]

     

  5. وفي الأخير نعرضها في صفحة html 

    <!DOCTYPE html>
    <html>
    
    <head>
        <title>Buy now</title>
        <link rel="stylesheet" href="style.css">
        <script src="https://polyfill.io/v3/polyfill.min.js?version=3.52.1&features=fetch"></script>
        <script src="https://js.stripe.com/v3/"></script>
    </head>
    
    <body>
        <section>
            <div class="product">
                <div class="description">
                    <h3>{{ product.name }}</h3>
                    <hr />
                    {% for price in prices %}
    
                    <div>
                        <h5>${{ price.price }}</h5>
                        <form action="{% url 'create-checkout-session' price.id %}" method="POST">
                            {% csrf_token %}
                            <button type="submit">دفع</button>
                        </form>
                    </div>
    
                    {% endfor %}
                </div>
            </div>
        </section>
    </body>
    
    </html>

     

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...