Skip to main content

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

  1. Import the PaymentGateway class and the constants.
  2. 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_payment or 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.
  3. Call start_payment with 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 sets method to POST, 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 sets method to GET, you're expected to redirect the user to the action URL. No other processing is needed.
  4. 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)

  1. Import the PaymentGateway, Refund classes and the constants.
  2. 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.
  3. Call start_refund with 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 of mitol.payment_gateway.api.Refund with 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 a POSTed form, Stripe expects you to redirect the customer so it sets this to GET.)
  • url - the URL to send the customer to
  • payload - 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 ProcessorResponse object 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 RefundDuplicateException with 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.

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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mitol_django_payment_gateway-2026.8.26.tar.gz (27.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file mitol_django_payment_gateway-2026.8.26.tar.gz.

File metadata

File hashes

Hashes for mitol_django_payment_gateway-2026.8.26.tar.gz
Algorithm Hash digest
SHA256 316b55e33c228d5cca3e0ce7d6d7c16fe3dd419c900b07d5b8aa64c8904001ce
MD5 8af2062d305fed8d895f43aaf3df150c
BLAKE2b-256 2f8a10dd7fee05d3af39475586740109fc00f6cad66831f4fa9e560a25cbb39c

See more details on using hashes here.

File details

Details for the file mitol_django_payment_gateway-2026.8.26-py3-none-any.whl.

File metadata

File hashes

Hashes for mitol_django_payment_gateway-2026.8.26-py3-none-any.whl
Algorithm Hash digest
SHA256 99ba9ffcbec28ff21cc92181a470c83860b37aad81cca5070e230f1c1997d5c6
MD5 ee193e84edf7506326f722a8d6c4a321
BLAKE2b-256 eccdffbc20473b14695d6de644b8e4d0f26ab9eed2e24e1325ac283b35eea58b

See more details on using hashes here.

Release history Release notifications | RSS feed

2026.8.28

2 files

This release

2026.8.26 This release

2 files

2026.8.10

2 files

2026.8.5

2 files

2026.7.15

2 files

2026.4.29

2 files

2026.4.2

2 files

2025.3.17

2 files

2025.3.6

2 files

2024.11.12

2 files

2023.12.19

2 files

2023.1.17

2 files

1.9.1

2 files

1.8.0

2 files

1.7.2

2 files

1.7.1

2 files

1.7.0

2 files

1.6.0

2 files

1.5.0

2 files

1.4.0

2 files

1.3.0

2 files

1.2.2

2 files

1.2.1

2 files

1.2.0

2 files

1.1.0

2 files

1.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page