Skip to content

Instantly share code, notes, and snippets.

@muendelezaji
Last active May 19, 2023 18:22
Show Gist options
  • Save muendelezaji/924591f2388a4d2c293b95609a56e6c2 to your computer and use it in GitHub Desktop.
Save muendelezaji/924591f2388a4d2c293b95609a56e6c2 to your computer and use it in GitHub Desktop.
Create a simple unique payment reference to send to a provider
from datetime import datetime
def generate_payment_ref(email, method, country):
"""
Generates a unique payment reference that will be sent to the payment provider.
Examples of returned references:
- [email protected]
- [email protected]
- [email protected]
- [email protected]
:param email: Customer's email
:param method: Payment method - CARD, PUSH, BANK, USSD etc.
:param country: Country of service - TZ, KE etc.
:return: Unique payment reference to be sent to the provider
"""
# Generate date and time strings in the right format
date = datetime.now().strftime("%Y%m%d")
time = datetime.now().strftime("%H%M%S")
# Combine it with the customer's email, payment method and country
ref = f"TENDA-{country}-{method}-{date}-{time}-{email}"
return ref
def get_email_from_payment_ref(pay_ref):
"""
Extracts the customer's email from payment reference sent to the provider.
:param pay_ref: Payment reference
:return: Email of the customer that attempted payment
"""
# Split the payment reference string into groups by "-"
groups = pay_ref.split("-")
# Rejoin all groups after the 5th "-" (i.e. where email starts)
# This ensures any emails with "-" will be extracted correctly
email = "-".join(groups[5:])
return email
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment