Skip to content

Commit 30e85a2

Browse files
committed
expanded comments, added additional params to Phaxio
1 parent cb984c4 commit 30e85a2

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

service.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,31 @@
44
from xero import Xero
55
from xero.auth import PrivateCredentials
66

7-
# Setup Xero Private App
7+
# Setup the Xero Private App
8+
# Ensure your private key is listed in your .gitignore, so you doesn't end up in your repo.
9+
# Xero API Consumer Key is retrieved via an environmental variable
10+
# Ensure you add the environment variable on your local machine AND in your AWS Lambda Settings (via config.yaml)
811
with open('./keys/lambdaprivatekey.pem') as keyfile:
912
rsa_key = keyfile.read()
1013
credentials = PrivateCredentials(os.getenv("XERO_CONSUMER_KEY_PHAXIO"), rsa_key)
1114
xero = Xero(credentials)
1215

1316
# Setup PhaxioAPI
17+
# Phaxio API keys are set via environment variables
18+
# Ensure you add the environment variables on your local machine AND in your AWS Lambda Settings (via config.yaml)
1419
phaxio = PhaxioApi(os.getenv('PHAXIO_API_KEY'), os.getenv('PHAXIO_API_SECRET'))
1520

1621
fax_number = None
1722

1823
def handler(event, context):
1924

25+
# invoice_id is the only param sent to the lambda function
2026
invoice_id = event.get('invoice_id')
2127

22-
# find the invoice
28+
# Use the invoice_id to retrieve the invoice details from the Xero API
2329
invoice = xero.invoices.get(invoice_id)
2430

25-
# get the fax number
31+
# Included in the invoice response is the Contact and their Fax Number, lets parse it out.
2632
for phone in invoice[0]['Contact']['Phones']:
2733
if phone['PhoneType'] == "FAX":
2834
# Check to see if the fax number is complete, if not, return an error
@@ -31,23 +37,27 @@ def handler(event, context):
3137
else:
3238
fax_number = phone['PhoneCountryCode']+phone['PhoneAreaCode']+phone['PhoneNumber']
3339

34-
# didn't find any fax number, return an error
40+
# Sanity check, did the contact have a fax number? If not, return with relevant error.
3541
if fax_number == None:
3642
return { "statusCode": 400, "headers": { "Content-Type": "application/json", "Access-Control-Allow-Origin" : "*", "Access-Control-Allow-Credentials" : true }, "body": 'Contact does not have a Fax Number, add Fax Number to Contact in Xero and try again.'}
3743

38-
# get the invoice pdf
39-
invoice_pdf = xero.invoices.get(invoice_number, headers={'Accept': 'application/pdf'})
44+
# Now lets prepare to send a fax with Phaxio
45+
# Frist, retrieve the PDF version of the invoice from the Xero API
46+
invoice_pdf = xero.invoices.get(invoice_id, headers={'Accept': 'application/pdf'})
4047

48+
# Save the PDF retrieved to disk temporarily. This is probably unnecessary, but I'm a Python Noob.
4149
pdf_file = open('tmp/tmp_fax.pdf', 'wb')
4250
pdf_file.write(invoice_pdf)
4351
pdf_file.close()
4452

45-
# fax the pdf
46-
response = phaxio.Fax.send(fax_number, files='tmp/tmp_fax.pdf')
53+
# Send the PDF invoice as a fax with Phaxio
54+
response = phaxio.Fax.send(fax_number, files='tmp/tmp_fax.pdf', header_text='Xero Invoice Faxed with Phaxio', tags_dict={'invoice_id': invoice_id})
4755

48-
# upload a fax receipt (attach to invoice)
49-
56+
# Upload a confirmation of the Fax Receipt
57+
# As the Fax is not sent immediately, this needs to be done via a second Lambda function, invoked via the callback url.
58+
# Skipping this feature for now.
5059

60+
# Return with a status code 200, adding the relevant CORS headers to handle cross domain scripting.
5161
return {
5262
"statusCode": 200,
5363
"headers": {

0 commit comments

Comments
 (0)