Website Integration
This source code of this tutorial is hosted at Github demo python.
You can try it here https://ksherpay-demo-store.herokuapp.com/
This is a poor man's ecommerce website with only two SKUs but with rich payment options.
#
Adding SKU to shopping cartClick add cart
for adding the related SKU to shopping card. You can add serveral.
#
CheckoutClick Checkout
to call payment gateway API and collect from customer.
#
Get the payment result via WebhookAs soon as the transaction is authorized by the payer, the order status change will be sent by webhook.
#
Outline- Requirement
- Installation
- Configuration
- Run The Demo
- system overview
- How The App Make a payment using Ksher Payment Gateway
- How The App Know that the payment has been done
#
RequirementPython 3.7
- other python3 version should also work, but python package version might cause some conflice and minor change might need to be done.
Ksher Payment API Account
- Requesting sandbox account please contact support@ksher.com
API_URL
- Along with a sandbox accout, you will be receiving a API_URL in this format: s[UNIQUE_NAME].vip.ksher.net
API_TOKEN
- Log in into API_URL using given sandbox account and get the token. see How to get API Token
#
InstallationIn this section we will install all the requirement and prepare our environment
#
step 1: clone this respository#
step 2: change directroy into cloned repository#
step 3: create virtual enviroment and activate itNOTE: You migh have to specified if you have multiple python install
#
step 4: install all the requirements#
Configurationbefore we can run there are parameters that need to be config. In this demo we use Environment variables. Which is a good way to prevent expose your secret keys to the source code and observe by other. especially in an opensoure softwar storing in github.
we already provide you with the template file
Your .env should now look like this:
Explanation on some configuation parameter
- FLASK_SECRET_KEY
- random string to be use as a secret key of your flask app
- KSHER_API_BASE
- ksher API_URL specified in requirement
- KSHER_API_TOKEN
- ksher API_TOKEN specified in requirement
- SERVER_NAME
- the url name of this flask app service. use for storing cookies
- in heroku or production deployment this will be matched with STROE_BASE_URL
- STORE_BASE_URL
- the url name of this flask app service
- in heroku or production deployment this will be matched with STROE_BASE_URL how ever in localhost run we will set it to ngrok for webhook testing
#
Config Objectthe configuration parameters sepecified here is that loard into config object here which will later be load into flask app's config
#
Run The DemoIn this section, we going to show you how to run this demo on you local machine. We also provide you on how to run this demo on Heroku on Deployment section
After you finnish all the previous sections(Installation, configuration), now it's time to run the demo
to run simply run below command
the service will be app and running and listen to port 5000
#
System Overviewthe system overview can be observed in the application factories inside app.py file locate in root directory of this demo.
The system is splited in to 3 modules (or 3 microservices);
- Store
- Dealing with Store and how to get product into Cart
- Checkout
- Creating order from the Cart being check out and redirect user to make a payment on Khser Payment Gateway and redirect after the payment has been finnish
- Webhook
- Dealing with the webhook notification send back from Ksher Payment gateay after the payment has been done
#
How The App Make a payment using Ksher Payment GatewayAfter the user click 'check out' in Checkout page the method below will be executed making payment request to Ksher Payment Gateway.
here are some explanation;
#
import the payment objectFirst need to import payment object from Ksher payment python sdk
which in this demo we already clone the sdk and put it in root directory here.
In future version this sdk will be installable with pip install, but for not will still need to clone it.
#
Init the Payment Objectwe then you paremters we've configured to init Payment Object
#
Create an OrderWe get all the requried data to make the order from Checkout page and Cart and create the Order
The order_id is not necessary need to make this way. you can use your own method, but make sure it will be a unique string.
#
Redirect user to make a paymentif the Order has been created successfully.(ksher will response with status code 200) then we can use the redirect_url send back along with the response body to redirect the user into Khser Payment Gateway Page
#
How The App Know that the payment has been doneAfter the payment is done khser will redirect the user back using redirect_url speicified in the Order Creation
which is this endpoint implente in checkout module
along with this redirect, Khser will send the notification to our webhook here