-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from amcintosh/issue-67-file-uploads
✨ Add file upload resources
- Loading branch information
Showing
14 changed files
with
551 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
File Uploads | ||
============ | ||
|
||
Some FreshBooks resource can include images and attachments. For example, invoices can have a company | ||
logo or banner image as part of the invoice presentation object as well as images or pdfs attachments. | ||
Expenses can also include copies or photos of receipts as attachments. | ||
|
||
All images and attachments first need to be uploaded to FreshBooks via the ``images`` or ``attachments`` | ||
endpoints. | ||
|
||
These will then return a path to your file with a JWT. This path will can then be passed as part of the | ||
data in a subsequent call. | ||
|
||
See FreshBooks' `invoice attachment <https://www.freshbooks.com/api/invoice_presentation_attachments>`_ | ||
and `expense attachment <https://www.freshbooks.com/api/https://www.freshbooks.com/api/expense-attachments>`_ | ||
documentation for more information. | ||
|
||
Invoice Images and Attachments | ||
------------------------------ | ||
|
||
See `FreshBooks' API Documentation <https://www.freshbooks.com/api/invoice_presentation_attachments>`_. | ||
|
||
The ``upload()`` function takes a `PHP resource <https://www.php.net/manual/en/language.types.resource.php>`_. | ||
Logo's and banners are added to the invoice presentation object. To include an uploaded attachment on | ||
an invoice, the invoice request must include an attachments object. | ||
|
||
.. code-block:: php | ||
$logo = $freshBooksClient->images()->upload($accountId, fopen('./sample_logo.png', 'r')); | ||
$attachment = $freshBooksClient->attachments()->upload($accountId, fopen('./sample_attachment.pdf', 'r')); | ||
$presentation = [ | ||
'image_logo_src' => "/uploads/images/{$logo->jwt}", | ||
'theme_primary_color' => '#1fab13', | ||
'theme_layout' => 'simple' | ||
]; | ||
$invoiceData = [ | ||
'customerid' => $clientId, | ||
'attachments' => [ | ||
[ | ||
'jwt' => $attachment->jwt, | ||
'media_type' => $attachment->mediaType | ||
] | ||
], | ||
'presentation' => presentation | ||
]; | ||
$invoice = $freshBooksClient->invoices()->create($accountId, $invoiceData); | ||
Expense Receipts | ||
---------------- | ||
|
||
See `FreshBooks' API Documentation <https://www.freshbooks.com/api/expense-attachments>`_. | ||
|
||
Expenses have have images or PDFs of the associated receipt attached. The expense request must include | ||
an attachments object. | ||
|
||
.. code-block:: php | ||
$attachment = $freshBooksClient->attachments()->upload($accountId, fopen('./sample_receipt.pdf', 'r')); | ||
$expense->amount = new Money("6.49", "CAD"); | ||
$expense->date = new DateTime(); | ||
$expense->staffId = 1; | ||
$expense->categoryId = 3436009; | ||
$expenseAttachment = new ExpenseAttachment(); | ||
$expenseAttachment->jwt = $attachment->jwt; | ||
$expenseAttachment->mediaType = $attachment->mediaType; | ||
$expense->attachment = $expenseAttachment; | ||
$includes = (new IncludesBuilder())->include('attachment'); | ||
$expense = $freshBooksClient->expenses()->create($accountId, model: $expense, includes: $includes); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
configuration | ||
authorization | ||
api-calls/index | ||
file-uploads | ||
webhooks | ||
examples | ||
|
||
|
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace amcintosh\FreshBooks\Model; | ||
|
||
use Psr\Http\Message\StreamInterface; | ||
|
||
/** | ||
* A file that has been uploaded to FreshBooks. | ||
* | ||
* @package amcintosh\FreshBooks\Model | ||
*/ | ||
class FileUpload | ||
{ | ||
/** | ||
* @var string The JWT used to fetch the file from FreshBooks. | ||
*/ | ||
public ?string $jwt; | ||
|
||
/** | ||
* @var string The name of the file uploaded to FreshBooks. | ||
* | ||
* This is returned from the API in the `X-filename` header. | ||
*/ | ||
public ?string $fileName; | ||
|
||
/** | ||
* @var string The media type (eg. `image/png`) of the file uploaded to FreshBooks. | ||
*/ | ||
public ?string $mediaType; | ||
|
||
/** | ||
* @var string The PSR StreamInterface steam of data from the request body. | ||
*/ | ||
public ?StreamInterface $responseBody; | ||
|
||
/** | ||
* @var string A fully qualified path the the file from FreshBooks. | ||
*/ | ||
public ?string $link; | ||
|
||
public function __construct(?string $fileName, ?string $mediaType, ?StreamInterface $responseBody) | ||
{ | ||
$this->fileName = $fileName; | ||
$this->mediaType = $mediaType; | ||
$this->responseBody = $responseBody; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace amcintosh\FreshBooks\Model; | ||
|
||
use Spatie\DataTransferObject\Attributes\MapFrom; | ||
use Spatie\DataTransferObject\Attributes\MapTo; | ||
use Spatie\DataTransferObject\Caster; | ||
use Spatie\DataTransferObject\DataTransferObject; | ||
use amcintosh\FreshBooks\Model\DataModel; | ||
|
||
/** | ||
* Attached files and images to include with an invoice. | ||
* | ||
* _Note:_ This data is not in the default response and will only be | ||
* present with the use of a corresponding "includes" filter. | ||
* | ||
* @package amcintosh\FreshBooks\Model | ||
* @link https://www.freshbooks.com/api/invoice_presentation_attachments | ||
*/ | ||
class InvoiceAttachment extends DataTransferObject implements DataModel | ||
{ | ||
public const RESPONSE_FIELD = 'expense'; | ||
|
||
/** | ||
* @var int The unique identifier of this expense attachment within this business. | ||
*/ | ||
public ?int $id; | ||
|
||
/** | ||
* @var int Duplicate of id | ||
*/ | ||
#[MapFrom('attachmentid')] | ||
public ?int $attachmentId; | ||
|
||
/** | ||
* @var int Id of the expense this attachment is associated with, if applicable. | ||
*/ | ||
#[MapFrom('expenseid')] | ||
#[MapTo('expenseid')] | ||
public ?int $expenseId; | ||
|
||
/** | ||
* @var string JWT link to the attachment. | ||
*/ | ||
public ?string $jwt; | ||
|
||
/** | ||
* @var string Type of the attachment. | ||
*/ | ||
#[MapFrom('media_type')] | ||
#[MapTo('media_type')] | ||
public ?string $mediaType; | ||
|
||
/** | ||
* Get the data as an array to POST or PUT to FreshBooks, removing any read-only fields. | ||
* | ||
* @return array | ||
*/ | ||
public function getContent(): array | ||
{ | ||
$data = $this | ||
->except('id') | ||
->except('attachmentId') | ||
->toArray(); | ||
foreach ($data as $key => $value) { | ||
if (is_null($value)) { | ||
unset($data[$key]); | ||
} | ||
} | ||
return $data; | ||
} | ||
} |
Oops, something went wrong.