API changes for Zapier

This commit is contained in:
Hillel Coren 2016-06-23 12:39:07 +03:00
parent fc0a7cfbf0
commit e36d23fcdc
3 changed files with 35 additions and 26 deletions

View File

@ -225,4 +225,15 @@ class BaseAPIController extends Controller
return $data; return $data;
} }
protected function fileReponse($name, $data)
{
header('Content-Type: application/pdf');
header('Content-Length: ' . strlen($data));
header('Content-disposition: attachment; filename="' . $name . '"');
header('Cache-Control: public, must-revalidate, max-age=0');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
return $data;
}
} }

View File

@ -184,6 +184,10 @@ class InvoiceApiController extends BaseAPIController
->with('client', 'invoice_items', 'invitations') ->with('client', 'invoice_items', 'invitations')
->first(); ->first();
if (isset($data['download_invoice']) && boolval($data['download_invoice'])) {
return $this->fileReponse($invoice->getFileName(), $invoice->getPDFString());
}
return $this->itemResponse($invoice); return $this->itemResponse($invoice);
} }
@ -361,14 +365,7 @@ class InvoiceApiController extends BaseAPIController
public function download(InvoiceRequest $request) public function download(InvoiceRequest $request)
{ {
$invoice = $request->entity(); $invoice = $request->entity();
$pdfString = $invoice->getPDFString();
header('Content-Type: application/pdf'); return $this->fileReponse($invoice->getFileName(), $invoice->getPDFString());
header('Content-Length: ' . strlen($pdfString));
header('Content-disposition: attachment; filename="' . $invoice->getFileName() . '"');
header('Cache-Control: public, must-revalidate, max-age=0');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
return $pdfString;
} }
} }

View File

@ -57,6 +57,7 @@ class PaymentTransformer extends EntityTransformer
'is_deleted' => (bool) $payment->is_deleted, 'is_deleted' => (bool) $payment->is_deleted,
'payment_type_id' => (int) $payment->payment_type_id, 'payment_type_id' => (int) $payment->payment_type_id,
'invoice_id' => (int) ($this->invoice ? $this->invoice->public_id : $payment->invoice->public_id), 'invoice_id' => (int) ($this->invoice ? $this->invoice->public_id : $payment->invoice->public_id),
'invoice_number' => $this->invoice ? $this->invoice->invoice_number : $payment->invoice->invoice_number,
]); ]);
} }
} }