Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,68 @@ if(response.success):

print("Payment Status: ", status.status)
```

# Paynow Button Generator
Create Paynow Buttons using your paynow instance by calling the method ```button_generator```

## Example
This generates the Paynow Image Button
```python
paynow_button = paynow.button_generator({
'email' : 'ignertic@icloud.com', # The Merchant's Email
'reference' : '123', # transaction reference
'amount' : 20.50, # transaction amount
'lock' : True, # Lock Form
'text' : 'donate', # Label for the button
'type' : 'image' # The type of button
})
```

### Button Type
There are 3 options available for button ```type``` which include:
*image*: Generate HTML code for Paynow Payment Button
*url*: Generate Paynow Payment URL
*text*: Generate HTML code for Text with link to Paynow Payment Page


## Rendering Buttons
You can render Paynow Buttons directly in your templates
```python
# Your other code
# ...............

paynow_button = paynow.button_generator({
'email' : 'ignertic@icloud.com', # The Merchant's Email
'reference' : '123', # transaction reference
'amount' : 20.50, # transaction amount
'lock' : True, # Lock Form
'text' : 'donate', # Label for the button
'type' : 'image' # The type of button
})

context = {
'paynow_button' : paynow_button
}

return render_template('index.html', **context)
```

For Django it's the same concept
```python
# Your fancy Django stuff here
# ....................
context = {
'paynow_button' : paynow_button
}
return render(request, 'index.html', context)
```

### Inside your HTML template file simply add the following line to render
```html
{{ paynow_button }}
```

## Result
<img src="https://www.paynow.co.zw/Content/Buttons/Medium_buttons/button_pay-now_medium.png"
alt="Paynow Button"
/>
4 changes: 2 additions & 2 deletions paynow/model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import requests
import hashlib
from six.moves.urllib_parse import quote_plus, parse_qs

from utils import ButtonGenerator

class HashMismatchException(Exception):
"""
Expand Down Expand Up @@ -192,7 +192,7 @@ def info(self):
return out


class Paynow:
class Paynow(ButtonGenerator):
"""Contains helper methods to interact with the Paynow API

Attributes:
Expand Down
91 changes: 91 additions & 0 deletions paynow/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from urllib.parse import quote_plus
import base64


class ButtonGenerator(object):
"""Paynow Button Generator"""

BUTTONS = {
'paynow' : 'https://www.paynow.co.zw/Content/Buttons/Medium_buttons/button_pay-now_medium.png',
'buynow' : 'https://www.paynow.co.zw/Content/Buttons/Medium_buttons/button_buy-now_medium.png',
'donate' : 'https://www.paynow.co.zw/Content/Buttons/Medium_buttons/button_donate_medium.png'
}



def __encode_payment(self,email, reference, amount, lock, **extra) -> str:
"""
Build base64 encoded payment payload

Args:
email (str) : Merchant's Paynow email address
reference (any): The unique identifier for the transaction
amount (float): Amount to charge
lock (bool): Lock form

Returns:
str: Base64 encoded payload
"""

strresult = f"search={email}&amount={amount}&reference={reference}&l={('2', '1')[lock]}"
return quote_plus(base64.b64encode(bytes(strresult, "utf8")))

def __image(self,data):
"""
Generate Image Button

Args:
data (dict): Payload

Returns:
str: HTML code for Paynow Button
"""
payment = self.__encode_payment(**data)

return f"""<a href='https://www.paynow.co.zw/Payment/Link/?q={payment}' target='_blank'>
<img src='{self.BUTTONS[data.get("text", "paynow")]}' style='border:0' /></a>"""

def __text(self, data):
"""
Generate Paynow HTML Text

Args:
data (dict): Payload

Returns:
str: HTML code for Paynow Text
"""
payment = self.__encode_payment(**data)
return f"<a href='https://www.paynow.co.zw/Payment/Link/?q={payment}' target='_blank'>{data.get('text', 'paynow').capitalize()}</a>"

def __url(self, data):
"""
Generate Paynow Url

Args:
data (dict): Payload

Returns:
str: Paynow payment URL
"""
payment = self.__encode_payment(**data)
return f"https://www.paynow.co.zw/Payment/Link/?q={payment}"


def button_generator(self, data):
"""
Paynow Button Generator

Args:
data (dict): Payload

Returns:
str: Paynow Button
"""
BUTTON_TYPE = {
'image' : self.__image,
'text' : self.__text,
'url' : self.__url
}

return BUTTON_TYPE[data['type']](data)