Skip to main content

Django models for Omise

Project description

django-omise Django + Omise

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

  • Creating a customer
  • Allowing customer to add/delete credit/debit cards
  • Charge customer with a card
  • Basic event webhook handler, saving raw data as an Event object
  • 3DS pending charges handling

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

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
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.first().add_card(token=omise_token)
    
  3. Charge a customer (Currently supporting only credit/debit card payment)

    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:

    from django.contrib.auth.mixins import LoginRequiredMixin
    from django_omise.mixins import CheckoutWithCardsMixin
    from django_omise.models.choices import Currency
    
    # Your own class-based-view
    class CheckoutView(LoginRequiredMixin, CheckoutWithCardsMixin):
    
        template_name = "yourapp/template.html"
    
        def get_form_kwargs(self, *args, **kwargs):
            kwargs = super().get_form_kwargs(*args, **kwargs)
            kwargs["user"] = self.request.user
            return kwargs
    
        def get_charge_details(self):
            return {
                "amount": 100000,
                "currency": Currency.THB,
            }
    
        def process_new_charge(self, charge):
            if charge.status in [ChargeStatus.SUCCESSFUL, ChargeStatus.PENDING]:
                # Create new order and attach a charge object
    

    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()
    
    charge = customer.charge_with_card(
        amount=100000,
        currency=Currency.THB,
        card=card,
    )
    
    if charge.status == ChargeStatus.SUCCESSFUL:
        # Do something
    elif charge.status == ChargeStatus.FAILED:
        # Do something else
    

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

Others

  • Implement tests
  • Add documentations

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.0.2.tar.gz (67.9 kB view hashes)

Uploaded Source

Built Distribution

django_omise-0.0.2-py3-none-any.whl (72.3 kB view hashes)

Uploaded Python 3

Supported by

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