from datetime import datetime def create_payment_ref(email, method, country): """ Creates a unique payment reference that will be sent to the payment provider. Examples of returned references: - TENDA-TZ-CARD-20230519-181549-acustomer@somedomain.com - TENDA-TZ-MOBILE-20230519-181549-acustomer@somedomain.com - TENDA-KE-CARD-20230519-181549-acustomer@somedomain.com :param email: Customer's email :param method: Payment method - CARD, MOBILE, 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