diff --git a/README.md b/README.md index c247712..2a68a42 100644 --- a/README.md +++ b/README.md @@ -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 +Paynow Button diff --git a/paynow/model.py b/paynow/model.py index 8c66f29..86cc526 100644 --- a/paynow/model.py +++ b/paynow/model.py @@ -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): """ @@ -192,7 +192,7 @@ def info(self): return out -class Paynow: +class Paynow(ButtonGenerator): """Contains helper methods to interact with the Paynow API Attributes: diff --git a/paynow/utils.py b/paynow/utils.py new file mode 100644 index 0000000..cf95503 --- /dev/null +++ b/paynow/utils.py @@ -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""" + """ + + 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"{data.get('text', 'paynow').capitalize()}" + + 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)