Skip to content

Commit

Permalink
Merge pull request #156 from TwistedBytes/feature/addattachment
Browse files Browse the repository at this point in the history
Feature/addattachment
  • Loading branch information
stephangroen authored Jul 11, 2018
2 parents 5b93cb1 + d03c4f6 commit f9e344d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/Picqer/Financials/Moneybird/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,35 @@ private function createRequest($method = 'GET', $endpoint, $body = null, array $
return $request;
}

/**
* @param string $method
* @param $endpoint
* @param null $body
* @param array $params
* @param array $headers
*
* @return \GuzzleHttp\Psr7\Request
* @throws \Picqer\Financials\Moneybird\Exceptions\ApiException
*/
private function createRequestNoJson($method = 'GET', $endpoint, $body = null, array $params = [], array $headers = [])
{
// If access token is not set or token has expired, acquire new token
if (empty($this->accessToken)) {
$this->acquireAccessToken();
}
// If we have a token, sign the request
if (! empty($this->accessToken)) {
$headers['Authorization'] = 'Bearer ' . $this->accessToken;
}
// Create param string
if (!empty($params)) {
$endpoint .= '?' . http_build_query($params);
}
// Create the request
$request = new Request($method, $endpoint, $headers, $body);
return $request;
}

/**
* @param string $url
* @param array $params
Expand Down Expand Up @@ -256,6 +285,25 @@ public function delete($url, $body)
}
}

/**
* @param string $url
* @param array $options
* @return mixed
* @throws ApiException
*/
public function upload($url, $options)
{
try {
$request = $this->createRequestNoJson('POST', $this->formatUrl($url, 'post'), null);

$response = $this->client()->send($request, $options);

return $this->parseResponse($response);
} catch (Exception $e) {
$this->parseExceptionForErrorMessages($e);
}
}

/**
* @return string
*/
Expand Down
27 changes: 27 additions & 0 deletions src/Picqer/Financials/Moneybird/Entities/SalesInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,31 @@ public function duplicateToCreditInvoice()

return $this->makeFromResponse($response);
}

/**
* Add Attachment to this invoice
*
* You can use fopen('/path/to/file', 'r') in $resource.
*
* @param string $filename The filename of the attachment
* @param resource $contents A StreamInterface/resource/string, @see http://docs.guzzlephp.org/en/stable/request-options.html?highlight=multipart#multipart
*
* @return \Picqer\Financials\Moneybird\Entities\SalesInvoice
*
* @throws \Picqer\Financials\Moneybird\Exceptions\ApiException
*/
public function addAttachment($filename, $contents)
{
$this->connection()->upload($this->endpoint . '/' . $this->id . '/attachments', [
'multipart' => [
[
'name' => 'file',
'contents' => $contents,
'filename' => $filename,
],
]
]);

return $this;
}
}

0 comments on commit f9e344d

Please sign in to comment.