Skip to main content

Django models for Omise

Project description

django-omise Django + Omise

Test Coverage Status contributions welcome

Django models for Omise. Currently, we support the following features:

  • Creating a customer

  • Allowing customer to add/delete credit/debit cards

  • Collect payments with new card with the option to keep the card

  • Collect payments with saved cards

  • Collect payments with Internet Banking

  • Collect payments with TrueMoney Wallet

  • Collect payments with Promptpay

  • Collect payments with Rabbit LINE Pay

  • Webhook handler, saving raw data as an Event object and update related objects, currently supporting

    • Customer
    • Card
    • Charge
    • Source
    • Refund
    • Schedule
    • Scheduled Charge
    • Schedule Occurrence
  • 3DS pending charges handling

See the roadmap for the plan of this project. Contributions are welcome!

Upgrading


0.3.0 (breaking change)

Charge.charge() (and Customer.charge_with_card()) no longer silently build a broken return_uri when OMISE_CHARGE_RETURN_HOST is unset. Behavior now:

  • If you use CheckoutMixin / CheckoutWithCardsMixin, no change needed — the request is forwarded automatically.
  • If you call Charge.charge() or customer.charge_with_card() manually outside a request (e.g. management command, background job) and don't pass return_uri or request, and OMISE_CHARGE_RETURN_HOST isn't set, this now raises ValueError instead of producing an invalid return_uri. Fix by passing request= (if available), return_uri= directly, or setting OMISE_CHARGE_RETURN_HOST.

Quick start


  1. Add "django_omise" to your INSTALLED_APPS setting like this:
    INSTALLED_APPS = [
        ...
        "django_omise",
    ]
  1. Include the django_omise URLconf in your project urls.py like this:
    path("payments/", include("django_omise.urls")),
  1. Add Omise keys and operating mode in settings.py:
OMISE_PUBLIC_KEY = xxxx
OMISE_SECRET_KEY = xxxx
OMISE_LIVE_MODE = True | False

# Optional. The default payment method is credit/debit card only.
# You must specify additional payment methods.
OMISE_PAYMENT_METHODS = [
    "card",
    "internet_banking", # Internet Banking
    "truemoney_wallet", # TrueMoney Wallet
    "promptpay", # Promptpay
    "rabbit_linepay", # Rabbit LINE Pay
]

By default, Charge.charge() builds the Omise return_uri from the host of the current request (via CheckoutMixin/CheckoutWithCardsMixin, or by passing request= yourself), so each site in a multi-site deployment returns to its own host automatically.

# Optional. Overrides the request-derived host, e.g. when running behind a
# proxy that reports the wrong host, or when charging outside of a request
# (management commands, background jobs) without passing return_uri directly.
OMISE_CHARGE_RETURN_HOST = localhost:8000
  1. Run python manage.py migrate to create the Omise models.

  2. Add Omise endpoint webhook url https://www.your-own-domain.com/payments/webhook/

Basic usage


  1. Create an Omise customer from User:

    from django.contrib.auth import get_user_model
    from django_omise.models.core import Customer
    
    User = get_user_model()
    user = User.objects.first()
    customer = Customer.get_or_create(user=user)
    
  2. Add card to Customer

    2.1 With the built-in view (Recommended)

    We have built a basic card collecting view where logged in users can add and remove their cards. Run Django server and visit /payments/payment_methods/ to see it in action. You could override the template used in the view by creating a new template in your project's directory /templates/django_omise/manage_payment_methods.html.

    2.2 Manually

    from django_omise.models.core import Customer
    from django_omise.omise import omise
    
    omise_token = omise.Token.retrieve(token_id)
    Customer.objects.live().first().add_card(token=omise_token)
    
  3. Charge a customer (Currently supporting new/saved cards, Internet Banking, TrueMoney Wallet, Promptpay)

    3.1 With the build-in mixin

    This package comes with a built-in mixin, with which you can create a class-based-view and write a few methods to charge a customer. See below for an example or see Example 1:

    from django.contrib.auth.mixins import LoginRequiredMixin
    from django_omise.mixins import CheckoutMixin
    from django_omise.models.choices import Currency
    
    # Your own class-based-view
    class CheckoutView(LoginRequiredMixin, CheckoutMixin):
    
        template_name = "yourapp/template.html"
        success_url = ...
    
        def get_charge_details(self):
            return {
                "amount": 100000,
                "currency": Currency.THB,
            }
    
        def process_charge_and_form(self, charge, form):
            if charge.status in [ChargeStatus.SUCCESSFUL, ChargeStatus.PENDING]:
                # Create new order and attach a charge object
                # And handle form data
                handle_form_data(form.cleaned_data)
    

    3.2 Manually

    from django_omise.models.choices import Currency, ChargeStatus
    from django_omise.models.core import Customer
    
    customer = Customer.objects.first()
    card = customer.cards.live().first()
    
    # `request` is required unless OMISE_CHARGE_RETURN_HOST is set or you pass
    # return_uri= directly. See "Upgrading" above.
    charge = customer.charge_with_card(
        amount=100000,
        currency=Currency.THB,
        card=card,
        request=request,
    )
    
    if charge.status == ChargeStatus.SUCCESSFUL:
        # Do something
    elif charge.status == ChargeStatus.FAILED:
        # Do something else
    
  4. Create a charge schedule for a customer:

At the moment, you can create a new schedule for a customer manually by calling the method create_schedule from a Custoemr object. See below for an example:

import datetime
from django_omise.models.choices import Currency
from django_omise.models.core import Customer

customer = Customer.objects.first()
card = customer.default_card

customer.create_schedule(
    amount=100000,
    currency=Currency.THB,
    card=card,
    every=1,
    period="month",
    start_date=datetime.date(year=2022, month=5, day=22),
    end_date=datetime.date(year=2032, month=5, day=22),
    on={
        'days_of_month': [22],
    },
    description="Monthly subscription",
)

Roadmap and contributions


Here are our immediate plans for this package, and more will be added! All contributions are welcome. I am new to publishing a public package, so if you have any recommendations, please feel free to create an issue on this repository or feel free to send me an email at siwatjames@gmail.com.

Omise Features

  • Handle refunds API
  • Handle webhook events and update related objects
  • Create charge with Sources
    • Internet banking
    • TrueMoney Wallet
    • Promptpay
    • Rabbit LINE Pay
    • Installment
  • Schedule
    • Scheduled Charges
    • Scheduled Transfer

Others

  • Implement tests
  • Add documentations

Development


You can run tests with either coverage or pytest.

To run with pytest

pip install pytest-django
python -m pytest [path_to_file] [--verbose -s --cov-report=html --cov=.]

To run with coverage

pip install coverage
coverage run run_tests.py
coverage report
coverage html

Project details


Download files

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

Source Distribution

django_omise-0.3.0.tar.gz (165.4 kB view details)

Uploaded Source

Built Distribution

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

django_omise-0.3.0-py3-none-any.whl (206.9 kB view details)

Uploaded Python 3

File details

Details for the file django_omise-0.3.0.tar.gz.

File metadata

  • Download URL: django_omise-0.3.0.tar.gz
  • Upload date:
  • Size: 165.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.12.2 Linux/6.17.0-1018-azure

File hashes

Hashes for django_omise-0.3.0.tar.gz
Algorithm Hash digest
SHA256 b7b2115fc56fcfae7bfec4544c75dd12704c0a2b045f117df55b835630daa393
MD5 2f94306f04b7cb5fee9baba731b66398
BLAKE2b-256 4b965aef5dccc7396e823c81f7ebae5fc538a98db4e6404480aed2960b5805c8

See more details on using hashes here.

File details

Details for the file django_omise-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: django_omise-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 206.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.12.2 Linux/6.17.0-1018-azure

File hashes

Hashes for django_omise-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 28768152b3844b03faeb4bfce45964c61feec0933d0a53f7023310b378fae450
MD5 0e7614fdaf2dff1264210a77c45af760
BLAKE2b-256 5a809b38829b3856055b18c33289a07640d904e61e80055104283229f67fba15

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page