Skip to main content

Write Django and your store as usual, and let django-spgateway handle your transactions with Spgateway

Requirements

  • Python 3.7

  • Django 2.1

  • pycrypto 2.6.1

  • We only tested on environment as below

Installation

  1. Install using pip or pipenv:

    pip install django-spgageway

    Alternatively, you can install download or clone this repo and call pip install -e ..

  2. Add to INSTALLED_APPS in your settings.py:

    'spgateway',

  3. Add settings in your settings.py:

    SPGATEWAY_PROFILE = {
        'YOUR_MerchantID': {
            'MerchantID': 'YOUR_MerchantID',
            'HashKey': 'YOUR_HashKey',
            'HashIV': 'YOUR_HashIV',
        },
    }
    SPGATEWAY_MERCHANTID = 'YOUR_MerchantID'
    SPGATEWAY_ORDERMODEL = 'yourapp.Order'
  4. Add urlpattern into your urls.py:

    path('spgateway/', include('spgateway.urls')), for Django 2

    url(r'^spgateway/', include('spgateway.urls')), for Django 1

  5. Import from spgateway.models import SpgatewayOrderMixin and inherit from it with your order model.

  6. Run python manage.py makemigrations and python manage.py migrate as usual.

  7. Get form from your order by calling generate_credit_form in view:

    credit_form = order_object.generate_credit_form(request)

    And use it in your template:

    <form action="{{ credit_form.action }}" method="POST">
        {{ credit_form.as_p }}
        <input type="submit">
    </form>
  8. Add SpgatewaySameSiteCookieMiddleware before SessionMiddleware to avoid SameSite cookie while return from payment gateway:

    MIDDLEWARE = [
        'django.middleware.security.SecurityMiddleware',
        'spgateway.middleware.SpgatewaySameSiteCookieMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.common.CommonMiddleware',
        ...
    ]

Example model

from spgateway.models import SpgatewayOrderMixin

class Order(SpgatewayOrderMixin, models.Model):
    total = models.IntegerField(default=0, verbose_name='Total Price')
    user = models.ForeignKey(User, verbose_name='Order by user')
    is_paid = models.BooleanField(default=False)

    # Add this parameter to let django-spgateway know which field is amount of price
    SpgatewayAmtFieldName = 'total'

    # Add this parameter to let django-spgateway set item description
    SpgatewayItemDesc = 'Items descriptions'

    # Add this method to let django-spgateway retrieve email
    def get_SpgatewayEmail(self, **kwargs):
        return self.user.email

    # Add this method let django-spgateway call when notify from Spgateway server
    # Change your order with this method
    def spgateway_notify(self, request, trade_info):
        status = trade_info['Status']
        status_msg = trade_info['Message']

        if status == 'SUCCESS':
            self.is_paid = True
            self.save()
        else:
            # TODO: Handle error

    # Add this method let django-spgateway call when client return from Spgateway server
    # Never trust data from client. Display messages only
    def spgateway_return(self, request, trade_info):
        status = trade_info['Status']
        status_msg = trade_info['Message']

        if status == 'SUCCESS':
            # TODO: Display success message to user
        else:
            # TODO: Display error message to user

    # django-spgateway will call this method to redirect user after user return from Spgateway server
    def get_absolute_url(self):
        return reverse('ORDER_DETAIL_VIEW_NAME_HERE')

Bugs and suggestions

If you have found a bug or if you have a request for additional functionality, please use the issue tracker on GitHub.

https://github.com/cjltsod/django-spgateway/issues

License

You can use this under MIT. See LICENSE file for details.

Author

Developed and maintained by CJLTSOD.

Thanks to everybody that has contributed pull requests, ideas, issues, comments and kind words.

Please see AUTHORS.rst for a list of contributors.

Download files

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

Source Distribution

django-spgateway-0.7.5.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

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

django_spgateway-0.7.5-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file django-spgateway-0.7.5.tar.gz.

File metadata

  • Download URL: django-spgateway-0.7.5.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.7

File hashes

Hashes for django-spgateway-0.7.5.tar.gz
Algorithm Hash digest
SHA256 66ea7edfaf717bb9a5c523ededf8410ee42a02d8d65c5f025c0b8700ea3bb863
MD5 bdd462aff91ff6a94d9d186c2279efc2
BLAKE2b-256 be356ac9df4af99ddeb676071926c8eb03ea92332b2af8a2acb52ff868ca45b7

See more details on using hashes here.

File details

Details for the file django_spgateway-0.7.5-py3-none-any.whl.

File metadata

File hashes

Hashes for django_spgateway-0.7.5-py3-none-any.whl
Algorithm Hash digest
SHA256 0ab1c004fd84cb142b851a19533d8a836195a435b7ec4730ffc43065bf2fd647
MD5 66e385cce2f23fa62ee37bd15f1b8fd0
BLAKE2b-256 d548d625bc48c52b77929f89f521372d66fae57f255cb9f791a69a0fda70e633

See more details on using hashes here.

Release history Release notifications | RSS feed

0.7.6

2 files

This release

0.7.5 This release

2 files

0.7.4

2 files

0.7.3

2 files

0.7.2

2 files

0.7.1

2 files

0.7.0

2 files

0.6.0

2 files

0.5.0

2 files

0.4.0

2 files

0.3.3

2 files

0.3.2

2 files

0.3.1

2 files

0.2.1

2 files

0.2.0

2 files

0.1.9

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