Skip to content

Commit f9e344d

Browse files
authored
Merge pull request #156 from TwistedBytes/feature/addattachment
Feature/addattachment
2 parents 5b93cb1 + d03c4f6 commit f9e344d

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

src/Picqer/Financials/Moneybird/Connection.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,35 @@ private function createRequest($method = 'GET', $endpoint, $body = null, array $
174174
return $request;
175175
}
176176

177+
/**
178+
* @param string $method
179+
* @param $endpoint
180+
* @param null $body
181+
* @param array $params
182+
* @param array $headers
183+
*
184+
* @return \GuzzleHttp\Psr7\Request
185+
* @throws \Picqer\Financials\Moneybird\Exceptions\ApiException
186+
*/
187+
private function createRequestNoJson($method = 'GET', $endpoint, $body = null, array $params = [], array $headers = [])
188+
{
189+
// If access token is not set or token has expired, acquire new token
190+
if (empty($this->accessToken)) {
191+
$this->acquireAccessToken();
192+
}
193+
// If we have a token, sign the request
194+
if (! empty($this->accessToken)) {
195+
$headers['Authorization'] = 'Bearer ' . $this->accessToken;
196+
}
197+
// Create param string
198+
if (!empty($params)) {
199+
$endpoint .= '?' . http_build_query($params);
200+
}
201+
// Create the request
202+
$request = new Request($method, $endpoint, $headers, $body);
203+
return $request;
204+
}
205+
177206
/**
178207
* @param string $url
179208
* @param array $params
@@ -256,6 +285,25 @@ public function delete($url, $body)
256285
}
257286
}
258287

288+
/**
289+
* @param string $url
290+
* @param array $options
291+
* @return mixed
292+
* @throws ApiException
293+
*/
294+
public function upload($url, $options)
295+
{
296+
try {
297+
$request = $this->createRequestNoJson('POST', $this->formatUrl($url, 'post'), null);
298+
299+
$response = $this->client()->send($request, $options);
300+
301+
return $this->parseResponse($response);
302+
} catch (Exception $e) {
303+
$this->parseExceptionForErrorMessages($e);
304+
}
305+
}
306+
259307
/**
260308
* @return string
261309
*/

src/Picqer/Financials/Moneybird/Entities/SalesInvoice.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,31 @@ public function duplicateToCreditInvoice()
203203

204204
return $this->makeFromResponse($response);
205205
}
206+
207+
/**
208+
* Add Attachment to this invoice
209+
*
210+
* You can use fopen('/path/to/file', 'r') in $resource.
211+
*
212+
* @param string $filename The filename of the attachment
213+
* @param resource $contents A StreamInterface/resource/string, @see http://docs.guzzlephp.org/en/stable/request-options.html?highlight=multipart#multipart
214+
*
215+
* @return \Picqer\Financials\Moneybird\Entities\SalesInvoice
216+
*
217+
* @throws \Picqer\Financials\Moneybird\Exceptions\ApiException
218+
*/
219+
public function addAttachment($filename, $contents)
220+
{
221+
$this->connection()->upload($this->endpoint . '/' . $this->id . '/attachments', [
222+
'multipart' => [
223+
[
224+
'name' => 'file',
225+
'contents' => $contents,
226+
'filename' => $filename,
227+
],
228+
]
229+
]);
230+
231+
return $this;
232+
}
206233
}

0 commit comments

Comments
 (0)