Using Payments#

The following guide explains how to incorporate the payments demo that ships with Pegasus into your own application.

This is a tedious process at the moment, and making it more seamless is a priority.

In the meantime, hopefully it helps show all the steps for doing this and how it could be applied to other examples.

Create the payments app#

First create the app:

mkdir apps/payments
./ startapp payments apps/payments

Then, in the created apps/payments/ file, change the name = 'apps.payments' to fix the path.

Finally, in add 'apps.payments.apps.PaymentsConfig', to PROJECT_APPS.

Setup Database Models#

First copy the models file from the example into your new app:

cp pegasus/apps/examples/models/ apps/payments/

Then change the related_name attribute on the user field to 'payments' (this is to prevent a conflict with the Pegasus example payment model).

Create migrations and migrate:

./ makemigrations
./ migrate

Setup UI#

Create Views#

Start with the relevant example views

cp pegasus/apps/examples/views/ apps/payments/

You’ll need to fix the models import (remove one of the periods from the import so it reads like this:

from .models import Payment

Then change the template references by replacing pegasus/examples/payments/ with payments/ throughout the file. (We’ll create these templates next.)

And fix the URL references by replacing pegasus_examples: with payments: throughout. (We’ll also create these momentarily.)

Create Templates#

First copy the example templates into the new app:

cp -r templates/pegasus/examples/payments/ templates/payments/

Then edit the first line of templates/payments/payments.html to extend your app template instead of the example template:

{% extends "web/app/app_base.html" %}

And find the line containing the <form> definition and change it as follows (changing the url from pegasus_examples):

<form action="{% url 'payments:accept_payment' %}" method="post" id="payment-form">

Create URLs#

Create a file in apps/payments and make sure its contents look like this:

from django.urls import path

from . import views

app_name = 'payments'

urlpatterns = [
    path(r'', views.PaymentView.as_view(), name='payments'),
    path(r'create/', views.accept_payment, name='accept_payment'),
    path(r'confirm/<slug:payment_id>/', views.payment_confirm, name='payment_confirm'),

Then in your main project file add the following line to urlpatterns:

    path('payments/', include('apps.payments.urls')),

Update Navigation#

To add a navigation element to your new page, just paste the following snippet in one of the menus:

  <a href="{% url 'payments:payments' %}">
    <span class="icon"><i class="fa fa-dollar"></i></span><span class="is-hidden-tablet-only">Payments</span>


If you’ve successfully completed these steps you should be able to go to click on the navigation element (or visit http://localhost:8000/payments/) to see your new payment module working.

Note: if payments are giving a Stripe error you likely need to fix your Stripe configuration.