knpay-plugin

connects knpay into your project


Keywords
knpay_plugin
License
BSD-3-Clause
Install
pip install knpay-plugin==0.2.8

Documentation

KNPay Plugin

Connects KNPay into your project

Quick start

Installation

  1. Using pip:
$ pip install knpay_plugin
  1. Add it to your INSTALLED_APPS:
INSTALLED_APPS = (
    ...
    'knpay_plugin.apps.KnpayPluginConfig',
    ...
)
  1. Add KNPay Plugin's URL patterns:
from knpay_plugin import urls as knpay_plugin_urls


urlpatterns = [
    ...
    url(r'^payment/', include(knpay_plugin_urls)),
    ...
]
  1. Sync database (./manage.py migrate).

Demo

Let's say we have a django app called checkout and the below files.

checkout/views.py

from django.views.generic import FormView
from knpay_plugin.views import CreatePaymentMixin, BaseCompletedView
from knpay_plugin.forms import PaymentForm


class CreatePaymentView(CreatePaymentMixin, FormView):
    template_name = 'knpay_plugin/payment_method.html'

    def get_initial(self):
        initial = super(CreatePaymentView, self).get_initial()
        initial.update({
            'amount': '123.000',
            # and rest of the model fields can be filled here
        })
        return initial


class CompletedView(BaseCompletedView):
    template_name = 'checkout/payment_details.html'

    def handle_successful_payment(self, request, *args, **kwargs):
        # this method will be called only once
        # place order
        # add success message
        # return to get method
        return self.get(request, *args, **kwargs)

    def handle_canceled_payment(self, request, *args, **kwargs):
        # this method will be called only once
        # payment failed to various reasons
        # add error message
        # return to get
        return self.get(request, *args, **kwargs)

checkout/urls.py

from knpay_plugin.conf import config

urlpatterns = [
    url(r'^choose-method/$', views.CreatePaymentView.as_view(), name='choose_method'),
    url(r'^completed/%s/$' % config.COMPLETE_VIEWS_REGEX,
        views.CompletedView.as_view(), name='complete'),
    # ...
]

settings.py

KNPAY_REDIRECTED_VIEW_NAME = 'checkout:complete'

knpay_plugin/payment_method.html

{% load knpay_tags %}

<form id="payment-form" action="{% url 'checkout:choose_method' %}" method="post">
    {% csrf_token %}

    {% show_gateway_choices %}

    <button type="submit" id="submit-payment-form">Submit</button>
</form>

checkout/payment_details.html

{% load knpay_tags %}

    <h2>{% trans 'Status' %}: {{ object.get_status_display }}</h2>
    {% prepare_payload object as payload %}
    <ul>
        {% for key, value in payload.iteritems %}
            <li>{{ key }}: {{ value }}</li>
        {% endfor %}
    </ul>

API

If PaymentForm and CreatePaymentMixin is a hassle for your needs, you can use a function to create the payment url.

from knpay_plugin.api import create_payment_url

result, created = create_payment_url('123.000', gateway_code='knet')
if created:
    # result is http://knpay3.mykuwaitnet.net/gw/dispatch/2kbEIQo1Z2hDll8/
    return JsonResponse(dict(redirect_url=result), status=200)
else:
    return JsonResponse(dict(errors=result), status=400)

result, created = create_payment_url('123.000')
# result is {'gateway_code': 'This field is required.'}

Still, creating direct payment urls can be controlled even more

result, created = create_payment_url('12',
                                     gateway_code='knet',
                                     request=None,
                                     currency_code='USD',
                                     extra={'booking_id': 'foo_bar'},
                                     language='ar',
                                     customer_email='foo@example.com',
                                     customer_address_country='QA'
                                     )

Additional information

Gateway choices display

Template which resides in knpay_plugin/gateway_choices.html must be overriden in your installation in order to reflect the style you need.

CONFIG

Prepend KNPAY_ for any variable which will be overriden.

Variable Description Default value
DEFAULT_CURRENCY Default currency code to be sent to KNPay when creating the payment request. Define it if your website uses single currency for checkout process. KWD
GATEWAY_CHOICES List of tuples containing gateway choices. First item of the tuple must match the codes defined in KNPay and the second the name to be displayed in the template. REQUIRED. knet / Knet
cybersource / Credit Card
BASE_URL KNPay base URL. For test, can be used the default value. For production, custom url has to be provided. Make sure it's ssl url. REQUIRED. http://knpay3.mykuwaitnet.net/
ADMIN_SHOW_OPTIONAL_FIELDS Show payment transaction customer optional fields in admin interface. False
ADMIN_SHOW_EXTRA_FIELD Show payment transaction extra field in admin interface. IE: basket_id: 34 False
DISCLOSURE_VIEW_NAME Name of the view which shall process the silent POST from KNPay. In 99.99% of the cases you don't need to override this. kp_disclosure
REDIRECTED_VIEW_NAME Name of view where customer will be redirected after payment stage completes. Can be namespace:view_name or just view_name. In 99.99% of the cases needs to be overriden. kp_complete
PROTOCOL Http protocol be used for URI generation if HttpRequest is not passed when PaymentForm is instantiated. http
MANDATORY_FORM_FIELDS Mandatory form fields for the payment form. (amount, currency_code)
VISIBLE_FORM_FIELDS Form fields which shall be visible in the template. []
RENDER_FORM If payment form will be rendered on the page and submitted via POST. If payment values are manually entered by the customer, you need this False
GENERATE_ORDER_FUNC A unique order id has to be assigned to each request to KNPay. If the default format of the generated order id does not match your needs, define the path to the custom function which generates a order id and returns it knpay_plugin.forms.uuid_url64
VAR_MAPPING Defines how the raw parameters from PSP shall be displayed in the payment details page. result / Result
trackid / Track ID
postdate / Post Date
tranid / Tran ID
paymentid / Payment ID
auth / Auth ID
ref / Reference ID
decision / Decision
transaction_id / Transaction ID
vpc_Message / Message
vpc_ReceiptNo / Receipt No
vpc_TransactionNo / Transaction No
GATEWAY_NAMES Name of the gateways as defined inside KNPay. For sure, you'll never need to override the keys of this dict. However, override the values for changing what to display in the template. migs / MiGS
knet / Knet
mpgs / MPGS
paypal / PayPal
cybersource / CyberSource
SHOW_ORDER_NO Define if the automatic generated order number shall be displayed on the confirmation page. False