Created
November 25, 2022 21:46
-
-
Save gwbischof/455d07fbb373dea7d531554cacbbbdf7 to your computer and use it in GitHub Desktop.
Generate and Email receipts for Venmo transactions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Checks your Vemmo transactions, generates a receipt, and emails it to email in venmo note. | |
| 1. Make a PDF of you receipt template. | |
| 2. https://www.sejda.com/pdf-forms, this is useful to add fields to an existing PDF. | |
| 3. Use fillpdfs.get_form_fields to find all of the fields in the PDF. | |
| 4. Get gmail and venmo api credentials. | |
| 5. Follow instructions here to get venmo token https://github.com/mmohades/Venmo | |
| 6. Update the GLOBALS. | |
| 7. Execute this script. | |
| """ | |
| import yagmail | |
| from cleantext import clean | |
| from datetime import datetime | |
| from fillpdf import fillpdfs | |
| from os.path import exists | |
| from pathlib import Path | |
| from venmo_api import Client as VenmoClient | |
| OUTPUT_DIRECTORY = Path("/Users/gbischof/auction_receipts") | |
| RECEIPT_TEMPLATE = Path("/Users/gbischof/auction_receipts/BSA_UW_Receipt.pdf") | |
| # Venmo credentials. | |
| VENMO_TOKEN = "Venmo token" | |
| VENMO_USER_ID = "venmo id" | |
| # Gmail credentials. | |
| MY_EMAIL = "email to send from" | |
| OAUTH_FILE = "gmail oauth json" | |
| def receipt_exists(transaction_id, output_directory=OUTPUT_DIRECTORY): | |
| return exists(output_directory / f"{transaction_id}.pdf") | |
| def create_receipt(transaction, email): | |
| """ | |
| Make a pdf receipt for Venmo transaction. | |
| """ | |
| receipt_data = { | |
| "company1": transaction.actor.display_name, | |
| "company2": "", | |
| "contact": f"{transaction.actor.first_name} {transaction.actor.last_name}", | |
| "address": "", | |
| "city_state_zip": None, | |
| "phone": transaction.actor.phone, | |
| "email": email, | |
| "donation": "money", | |
| "description": "BNL - United Way of Long Island Holiday Auction", | |
| "restrictions": "none", | |
| "value": transaction.amount, | |
| "date": datetime.utcfromtimestamp(t.date_created) \ | |
| .strftime('%Y-%m-%d %H:%M:%S'), | |
| } | |
| receipt_new = OUTPUT_DIRECTORY / f"{transaction.id}.pdf" | |
| fillpdfs.write_fillable_pdf(RECEIPT_TEMPLATE, receipt_new, receipt_data) | |
| return receipt_new | |
| def email_receipt(email, receipt): | |
| """ | |
| Use gmail to email the receipt. | |
| """ | |
| content = ['BNL-United Way of Long Island Holiday Auction Donation Receipt', | |
| str(receipt)] | |
| yag = yagmail.SMTP(MY_EMAIL, oauth2_file=OAUTH_FILE) | |
| yag.send(email, subject="Donation Receipt", contents=content) | |
| print('Sent email successfully') | |
| def topoff_receipts(description_filter): | |
| """ | |
| Generate and email receipts for Venmo transactions that have the | |
| description_filter in the Venmo transaction note. | |
| This looks at Venmo transactions for VENMO_USER_ID. | |
| It outputs the generated receipts to OUTPUT_DIRECTORY. | |
| It will look to see if the receipt already exists in the OUTPUT_DIRECTORY | |
| to determine if the transaction has already been processed. | |
| The venmo transaction note must be have a description followed by an email, | |
| separated by a comma. | |
| """ | |
| venmo_client = VenmoClient(access_token=VENMO_TOKEN) | |
| transactions = venmo_client.user.get_user_transactions(user_id=VENMO_USER_ID) | |
| while transactions: | |
| for transaction in transactions: | |
| note = clean(transaction.note, no_emoji=True) | |
| if ',' in note: | |
| description = note.split(",")[0].strip() | |
| email = note.split(",")[1].strip() | |
| if description == description_filter and not receipt_exists(transaction.id): | |
| receipt = create_receipt(transaction, email) | |
| email_receipt(receipt, email) | |
| transactions = transactions.get_next_page() | |
| if __name__ == "__main__": | |
| topoff_receipts("holiday auction") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment