Django application to handle payment processing
Project description
mitol-django-payment_gateway
This is the Open Learning Django Payment Gateway app. Its purpose is to wrap up payments and refunds processing in a reasonably generic and pluggable way, so that payment and refund processing is relatively straight-forward to implement in a given app.
The payment gateway supports the following payment processors:
Additional payment processors can be added in when necessary.
Setup
Add the payment_gateway app:
INSTALLED_APPS = [
...
"mitol.payment_gateway.apps.PaymentGatewayApp",
]
Add/Append this to your settings file:
# Imports the Payment Gateway settings
from mitol.common.envs import import_settings_modules
import_settings_modules(globals(), "mitol.payment_gateway.settings")
The Payment Gateway has one setting that needs to be configured:
ECOMMERCE_DEFAULT_PAYMENT_GATEWAY- This is set to the gateway name as set in src/constants.py. This defaults to"None". This is a convenience setting but leaving it unset may break apps that use it.
You will additionally want to read through the documentation for the gateway you wish to use as it will have its own settings to configure.
Usage
For specifics, see the PaymentGateway class in the api and the linked documentation above.
For processing a payment
- Import the PaymentGateway class and the constants.
- Assemble the information about the transaction you're about to process.
a. There should be a set of line items representing the items to be purchased and some order metadata. You can optionally pass in custom data if your app requires that.
b. You should also have two URLs set up. These are used as the redirect target for the user - once they have completed payment (successfully or not), the system will send them to one of these URLs. One is a receipt/success URL and one is a cancel URL. These can be passed in
start_paymentor they can usually be configured in the payment processor settings (the CyberSource EBC or the Stripe Dashboard, etc.). c. For CyberSource SA specifically, you can also specify a custom "backoffice" URL. This is a webhook location that instructs CyberSource where to post events about the order. This can also be configured in the EBC; you don't explicitly have to supply this per transaction. - Call
start_paymentwith the payment processor to use (there's a constant defined for each supported one) and the data assembled in step 2. You should receive a dictionary back with the data you'll need to redirect the customer to so they can complete the purchasing process. a. If the returned data setsmethodtoPOST, then you're expected to use the payload to create an HTML form that the user then has to submit to the supplied URL. (Generally, you'd build this out and submit it automatically using JavaScript.) This does need to be a regular form within the page - the user needs to end up at the action URL set. b. If the returned data setsmethodtoGET, you're expected to redirect the user to the action URL. No other processing is needed. - Elsewhere in your app, provide handlers for the receipt display and cancel endpoints. How these will work specifically will depend on the processor(s) being used.
The custom data (merchant_fields) should be a list of items to pass along with the transaction data. Each processor will handle this data differently (if the processor supports it at all) and there are usually limits what can be passed. For CyberSource, things passed in merchant_fields get enumerated out into the "merchant_defined_dataX" fields that are available in its API. Stripe allows for any arbitrary key/value pairs but each pair has limits on data size.
For processing a refund (CyberSource REST API)
- Import the PaymentGateway, Refund classes and the constants.
- Assemble the information(transaction_id, refund_amount, refund_currency) about the refund you're about to process. You would probably get this data from your existing completed payments.
- Call
start_refundwith the payment processor(e.g. CyberSource) to use (there's a constant defined for each supported one) and the data assembled in step 2.
Refund Code Example:
from mitol.payment_gateway.api import PaymentGateway
# Create a Refund request object to perform operations on
refund_gateway_request = PaymentGateway.create_refund_request(
ECOMMERCE_DEFAULT_PAYMENT_GATEWAY,
transaction_dict # Ideally this dict should have transaction_id, req_amount, req_currency
)
# Call start_refund from PaymentGateway with the Refund object you just created above
response = PaymentGateway.start_refund(
ECOMMERCE_DEFAULT_PAYMENT_GATEWAY, # Default Gateway to be used for processing e.g. CyberSource
refund_gateway_request,
)
Adding Gateways
Adding a new gateway consists of adding in the gateway class itself, adding necessary configuration settings, and adding in a new constant to name the gateway.
The class itself needs to implement methods:
prepare_checkout, which should accept all the pertinent order information, perform any processing needed to make it suitable for the payment processor, and return back the data to send to the customer so they can start the actual purchasing process.perform_refund, which should accept an object ofmitol.payment_gateway.api.Refundwith the required data set in the object.
The data returned from a successful prepare_checkout call should be a dictionary containing:
method- the HTTP method that the customer's browser should use (CyberSource expects aPOSTed form, Stripe expects you to redirect the customer so it sets this toGET.)url- the URL to send the customer topayload- the data to send to the aforementioned URL
Generally, you'll need to construct a form in the front end to POST the payload data to the URL specified in the customer's browser, and the customer then completes the process on the payment processor's site.
The data returned from a perform_refund call could be:
- For a successful refund you should receive a
ProcessorResponseobject back with refund details(i.e. state, message, response_date etc.) - In case of failure:
- If the request fails because of duplication, you would receive
RefundDuplicateExceptionwith details so that you can decide how you want to treat it (e.g. Duplicate request is considered as success for MITxOnline, but it might be different for other applications). - For all other failures you would receive a general exception(i.e.
CyberSource.rest.ApiException) with details so that you can handle your this failure appropriately in your application.
- If the request fails because of duplication, you would receive
The CyberSource gateway can be used as a template for future gateways. Most of the internal methods in it contain CyberSource-specific implementation details, but it should illustrate how to add in a new processor.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mitol_django_payment_gateway-2026.7.15.tar.gz.
File metadata
- Download URL: mitol_django_payment_gateway-2026.7.15.tar.gz
- Upload date:
- Size: 19.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e60f35d750aa3f545e67af877a47eda408c3dc12084c92d85536fd6b34153687
|
|
| MD5 |
a3147e142c1253b790607174f9e2e506
|
|
| BLAKE2b-256 |
ad463466166a6e2c669b8b68a21f47f3cf52124fae7497ce24de916489541458
|
File details
Details for the file mitol_django_payment_gateway-2026.7.15-py3-none-any.whl.
File metadata
- Download URL: mitol_django_payment_gateway-2026.7.15-py3-none-any.whl
- Upload date:
- Size: 27.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
816461f4a375ec2d74e2ce3c38dec3d5c541d7a5cbe84c6cae1a334ee685874e
|
|
| MD5 |
b9973ff5e7756ac7422bcaa0966da89d
|
|
| BLAKE2b-256 |
03e6eb31c907bd6a0d71b0b4ab82f33e7d9000deff4850e1d921dc3b78b1fe30
|