|
| 1 | +import base64 |
| 2 | +import json |
| 3 | +import os |
| 4 | +import requests |
| 5 | +from fpdf import FPDF, XPos, YPos |
| 6 | + |
| 7 | + |
| 8 | +def handler(request): |
| 9 | + PRINTNODE_API_KEY = os.getenv('PRINTNODE_API_KEY') |
| 10 | + PRINTNODE_PRINTER_ID = os.getenv('PRINTNODE_PRINTER_ID') |
| 11 | + |
| 12 | + payload = request['body'] |
| 13 | + |
| 14 | + source_name = payload.get('source_name') |
| 15 | + if source_name.lower() == 'pos': |
| 16 | + return { |
| 17 | + 'statusCode': 200, |
| 18 | + 'body': json.dumps({'message': 'POS order, skipping print job.'}) |
| 19 | + } |
| 20 | + |
| 21 | + # Extract order information from Shopify webhook payload |
| 22 | + order_id = payload.get('id') |
| 23 | + order_total = payload.get('total_price') |
| 24 | + customer = payload.get('customer', {}) |
| 25 | + customer_name = customer.get('first_name') + ' ' + customer.get('last_name') |
| 26 | + shipping_address = customer.get('default_address', {}) |
| 27 | + |
| 28 | + # Extracting shipping address details |
| 29 | + shipping_details = f"{shipping_address.get('address1', '')}, {shipping_address.get('city', '')}, {shipping_address.get('province', '')}, {shipping_address.get('country', '')}, {shipping_address.get('zip', '')}" |
| 30 | + |
| 31 | + order_items = payload.get('line_items', []) |
| 32 | + |
| 33 | + # Create a PDF document using fpdf2 |
| 34 | + pdf = FPDF() |
| 35 | + pdf.add_page() |
| 36 | + pdf.set_font("Helvetica", size=12) |
| 37 | + |
| 38 | + # Add content to PDF |
| 39 | + pdf.cell(200, 10, text=f"Order ID: {order_id}", new_x=XPos.LMARGIN, new_y=YPos.NEXT) |
| 40 | + pdf.cell(200, 10, text=f"Order Total: ${order_total}", new_x=XPos.LMARGIN, new_y=YPos.NEXT) |
| 41 | + pdf.cell(200, 10, text=f"Customer: {customer_name}", new_x=XPos.LMARGIN, new_y=YPos.NEXT) |
| 42 | + pdf.cell(200, 10, text=f"Shipping Address: {shipping_details}", new_x=XPos.LMARGIN, new_y=YPos.NEXT) |
| 43 | + pdf.cell(200, 10, text="Items:", new_x=XPos.LMARGIN, new_y=YPos.NEXT) |
| 44 | + |
| 45 | + # Items List |
| 46 | + for item in order_items: |
| 47 | + pdf.cell(200, 10, text=f"Item: {item.get('name')}", new_x=XPos.LMARGIN, new_y=YPos.NEXT) |
| 48 | + pdf.cell(200, 10, text=f" - SKU: {item.get('sku')}", new_x=XPos.LMARGIN, new_y=YPos.NEXT) |
| 49 | + pdf.cell(200, 10, text=f" - Quantity: {item.get('quantity')}", new_x=XPos.LMARGIN, new_y=YPos.NEXT) |
| 50 | + pdf.cell(200, 10, text=f" - Price: ${item.get('price')}", new_x=XPos.LMARGIN, new_y=YPos.NEXT) |
| 51 | + |
| 52 | + if 'variant_id' in item and item['variant_id']: |
| 53 | + pdf.cell(200, 10, text=f" - Variant ID: {item.get('variant_id')}", new_x=XPos.LMARGIN, new_y=YPos.NEXT) |
| 54 | + if 'variant_title' in item and item['variant_title']: |
| 55 | + pdf.cell(200, 10, text=f" - Variant Name: {item.get('variant_title')}", new_x=XPos.LMARGIN, |
| 56 | + new_y=YPos.NEXT) |
| 57 | + |
| 58 | + # Save the PDF to a variable |
| 59 | + pdf_output = pdf.output() |
| 60 | + pdf_base64 = base64.b64encode(pdf_output).decode('utf-8') |
| 61 | + |
| 62 | + print_job_payload = { |
| 63 | + "printerId": PRINTNODE_PRINTER_ID, |
| 64 | + "title": "Shopify Order", |
| 65 | + "contentType": "pdf_base64", |
| 66 | + "content": pdf_base64, |
| 67 | + "source": "Shopify Webhook" |
| 68 | + } |
| 69 | + |
| 70 | + response = requests.post( |
| 71 | + 'https://api.printnode.com/printjobs', |
| 72 | + auth=(PRINTNODE_API_KEY, ''), |
| 73 | + headers={'Content-Type': 'application/json'}, |
| 74 | + data=json.dumps(print_job_payload) |
| 75 | + ) |
| 76 | + |
| 77 | + if response.status_code == 201: |
| 78 | + return { |
| 79 | + 'statusCode': 200, |
| 80 | + 'body': json.dumps({'message': 'Print job sent successfully!'}) |
| 81 | + } |
| 82 | + else: |
| 83 | + return { |
| 84 | + 'statusCode': 500, |
| 85 | + 'body': json.dumps({'message': 'Failed to send print job.', 'error': response.text}) |
| 86 | + } |
0 commit comments