Skip to content
项目

samber/invoice-as-a-service

在 GitHub 查看

关于

💰 Simple invoicing service (REST API): from JSON to PDF

商业与金融

这些数字如何统计

实测星标增长

24小时
0
7天
0
15天
-1
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

Invoice as a service

tests License: MIT

Create professional PDF invoices from a simple web request. Use your own template, sign every PDF with your own certificate (CA chain and RFC 3161 timestamp supported), and send it to remote storage (S3, FTP, Zapier, email, webhook).

Run it on your own server, or use the hosted version.

Example invoice

Example PDF

Quick start

Send the smallest valid payload to the hosted endpoint:

curl "https://invoice-as-a-service.cleverapps.io/api/invoice/generate" \
  -X POST -H "content-type: application/json" \
  -o invoice.pdf \
  -d '{
    "id": "42",
    "currency": "€",
    "lang": "en",
    "date": 1520852472,
    "due_date": 1521457272,
    "items": [
      {
        "title": "Growth plan",
        "price": 42,
        "tax": 20
      }
    ],
    "customer": {
      "summary": "John Doe",
      "address_line_1": "1 Main Street"
    },
    "company": {
      "summary": "Acme",
      "address_line_1": "2 High Street"
    }
  }'

The response is the PDF (201, application/pdf). Open invoice.pdf.

Every other property (items, taxes, logo, extra fields, destinations) is in the full API reference.

The hosted endpoint is run best effort, without an SLA. It receives your invoice data and any FTP or webhook credentials you put in the payload. Self-host when that matters.

Self-host

Requirements: PHP 8.5, Composer, and these PHP extensions: dom, fileinfo, ftp, gd, libxml, mbstring, openssl.

composer install fails when one is missing, except gd, which dompdf needs to draw logos.

composer install
php artisan serve

Then run the same request against http://localhost:8000/api/invoice/generate.

Optional features read their settings from environment variables. Copy .env.dist to .env and set the MAIL_* variables to enable email. Set PDF_SIGN_CERT and PDF_SIGN_KEY to sign every PDF. Add PDF_SIGN_CHAIN (your CA's intermediate certificates) so readers can validate it, and PDF_SIGN_TIMESTAMP_URL (an RFC 3161 authority) so it stays valid after the certificate expires. See PDF signature.

Authentication

Authentication is optional and off by default. Set API_TOKEN in .env to require a token. Clients then send it in the Authorization header, and a missing or wrong token gets a 401:

curl "http://localhost:8000/api/invoice/generate" \
  -X POST -H "content-type: application/json" \
  -H "Authorization: Bearer change-me" \
  -o invoice.pdf -d @payload.json

Details in Authentication.

Where the PDF goes

The PDF is always returned in the HTTP response. Add an object to the payload to also send it to:

  • AWS S3, or any S3-compatible storage, through a presigned URL (s3)
  • an FTP server (ftp)
  • a webhook (webhook)
  • an email, with the PDF attached, through your SMTP server (email)
  • Zapier, through a "Catch hook" trigger, to reach Drive, Dropbox and others (zapier)

Deploy safely

The service makes network requests to addresses the caller chooses: the destinations in the request body (s3.presigned_url, ftp.host and ftp.port, webhook.url, zapier.zap_url, email) and the remote logo (company.logo_url). Anyone who can call the API can use your instance to reach hosts it can reach from where it runs (internal network, cloud metadata endpoint, other services on the same host), and sometimes read an error that shows what answered.

Install it in a dedicated sub-network, sandbox or DMZ:

  • no route to your internal services
  • an egress firewall that allows only the destinations you need (S3 endpoint, FTP host, webhook host, SMTP server)
  • the cloud metadata address blocked
  • no cloud credentials attached to the machine or container, and no secrets other than the ones the service needs
  • kept apart from the systems that hold customer data

Other levers:

  • PER_REQUEST_STORAGE_ENABLED=false rejects any request that carries s3, ftp, webhook, zapier or email (HTTP 422). Only the destinations you set with DELIVERY_* run. It does not cover the remote logo: company.logo_url is still fetched. See instance-wide delivery settings.
  • API_TOKEN limits who can call the API: see Authentication.
  • Requests are limited to 120 per minute per client, and the other limits apply. Behind a reverse proxy, set TRUSTED_PROXIES so the limit sees the real client address.

What is checked today for URLs in the request body:

  • s3.presigned_url: the host must resolve only to public addresses, the connection is pinned to the checked addresses, and redirects are not followed.
  • company.logo_url: must be an http(s) URL whose host resolves only to public addresses. The service downloads it itself, pinned to the checked addresses, without following redirects, and keeps it only when the bytes are a PNG, JPEG, GIF or BMP image within the limits. The PDF renderer never fetches a URL.
  • webhook.url: must be https and its host must resolve. Private addresses are not refused. zapier.zap_url must start with https://hooks.zapier.com/hooks/catch/.
  • ftp.host and ftp.port: no restriction.

The hosted version accepts the same input. Do not send it secrets and do not use it as a trusted internal service. Self-host when that matters, as said in Quick start.

What it is not

It renders one invoice per request from the data you send. It stores no invoices, keeps no customers or invoice numbers, and computes no payments. Use an accounting tool for those.

Reference

  • API reference: every property, destination option, instance-wide delivery settings, PDF metadata and PDF signature.

Contribute

Issues and pull requests are welcome. Where to start:

  • template: resources/views/invoices/default.blade.php
  • controller and input validation: app/Http/Controllers/InvoiceController.php
  • PDF build: app/Helpers/PDF.php
  • file storage: app/Helpers/Storage.php
  • instance-wide delivery settings: config/delivery.php and app/Helpers/Delivery.php

Run the tests with vendor/bin/phpunit.

Generate a presigned S3 upload URL for testing:

aws configure
node ./scripts/presign-upload-url.js <region> <my-bucket> invoices/[email protected]

Show your support

Give a ⭐️ if this project helped you!

GitHub Sponsors

License

MIT

探索相关项目

加载中…