Integration of Wepay Payment Gateway into the Django Application Using Python

tudip-logo

Tudip

30 May 2019

What is Wepay Payment Gateway?

WePay is a fantastic online payment service provider which provides an integrated and customizable payment solution through its APIs to platform businesses such as crowdfunding sites, marketplaces, and small business software companies.

There are two sites (environment) to work on WePay integration. One for testing transactions and one for live transactions.

  1. Staging site: https://stage-go.wepay.com
  2. Live site: https://go.wepay.com/

Begin integration by creating an account in the stage environment. Obtain the Client ID and Client Secret from the API Keys section of the site. These keys will be required in the Django application to connect with your WePay account.

Official developer’s Documentation: https://developer.wepay.com/

Installation

  • Install Pip:
    It is used to install packages listed in the Python Package Index (PyPI).

    sudo apt install python-pip (For Python2) 
    
    sudo apt install python3-pip (For Python3)
  • Install WePay:
    We need to install a Python SDK for our WePay API call in the django application. pip install wepay

Django Settings

Add the Client ID and Client secret key obtained from the WePay account in the Django settings file. WEPAY_CLIENT_ID = ‘XXXXXX’

WEPAY_CLIENT_SECRET = ‘XXXXXXXXXX’

PRODUCTION_MODE = (True or False) Boolean Value

Note: ‘False’ for staging environment and ‘True’ for live environment.

Package Usage and setup

Import the package

from wepay import WePay

Create an instance of WePay :

wepay = WePay()

We need to create a merchant account in which the checkout amount will be received. A merchant needs to confirm its account by accepting the terms and condition sent over the mail.

Types of account creation in WePay

  1. Custom account
  2. Oauth2 account

Steps to create a custom account

  • User register API call: (/user/register)

    • This API call is used to register a new user. It gives a access_token in response which can be used to create a payment account for the user.
      user_register_response = wepay.call('/user/register', { 
      'Client_id': <client_ID of the account>, 
      'client_secret':<client_secret of the account>,
       'email': <email>, 
      'scope': WEPAY_USER_REGISTER_SCOPES, 
      'first_name': <first_name>,
       'last_name': <last_name>, 
      'original_ip': <IP address of the user device>,
       'original_device': <user-agent (for web) or the IMEI (for mobile)>, 
      'tos_acceptance_time': <Unix timestamp> })
  • User account create API call: (/account/create)

    • This API is called with the access_token obtained from the user register API call to get an account_id.
      account_create_response = wepay.call('/account/create', {
       'name': <account name>, 
      'Description': <account description>,
       'callback_uri': <URI that will receive IPNs for this account>, }
  • User account confirmation API call: (/user/send_confirmation)

    • This API is called to send the user an email asking them to confirm.
    • The merchant clicks on a link which he has received in the email confirm its new account and sets a password.

Types of checkout in WePay

  1. Custom checkout
  2. Embedded checkout

Steps to create an embedded checkout

  1. Obtain the access token of the WePay account to which the checkout amount needs to be credited. (Note: store this access token in the database to access a certain WePay account)
  2. Create an instance of WePay to perform a checkout with the access_token of the WePay Account:
    wepay = WePay(<production value>, <wepay_account access_token>)
  3. Custom checkout API call: (/checkout/create)
    • This API call is to get a checkout_id and checkout_uri. A checkout object represents a single payment. Provide account ID, checkout amount.
    • In order to receive payment status of individual checkout, please provide callback URI. This URI will receive an instant payment notification. It needs to be a full URI and must not be localhost or 127.0.0.1.
    • Redirect_uri is the URI which the payer will be redirected after the complete payment is done.
      checkout_response = wepay.call('/checkout/create', { 
      'account_id': <wepay_account account_id>, 
      'amount': '', 
      'currency': 'USD',
       'short_description': '',
       'type': 'donation',
       'callback_uri': ‘’, 
      'fee': { 'app_fee': cut_amount, 'fee_payer': '’ },
       'hosted_checkout': { 'redirect_uri': '' 'mode': 'iframe' } })
  4. In the response of this above API call, checkout_id and checkout_uri is provided. (Note: save the checkout_id in the database for future reference)
  5. Now, embed this checkout_uri provided in the response of checkout API to an iframe on your site.
  6. After the payer has confirmed its payment, the payment method (either credit card or bank account) will be charged and then they will be sent to the URI which has been provided in redirect_uri of checkout API. You need to design a page in that redirect_uri. Checkout_id of the checkout will be appended as a GET parameter so you can get the details of the payment details.

Request a quote