Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
domkrm committed Feb 5, 2021
0 parents commit 1de2440
Show file tree
Hide file tree
Showing 11 changed files with 810 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.idea
/vendor
composer.lock
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) CTSoft

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Laravel LetterXpress

This package connects Laravel to the LetterXpress API.

## Install

Install this package via Composer:

```
composer require ctsoft/laravel-letter-xpress
```

Add the following lines to your .env file and insert your own API credentials:

```
LETTER_XPRESS_API_USER=[username]
LETTER_XPRESS_API_KEY=[apikey]
```

If you want to use the sandbox add this line to your .env file:

```
LETTER_XPRESS_API_URL=https://sandbox.letterxpress.de/v1/
```

## Usage

To set a new job:

```php
use CTSoft\Laravel\LetterXpress\Facades\LetterXpress;
use CTSoft\Laravel\LetterXpress\Models\Letter;

$letter = (new Letter())
->setFile('document.pdf') // Use one of this functions
->setDocument('PDF BINARY STRING') // to set the document
->setColor(true)
->setDuplex(true);
...

$letter = LetterXpress::setJob($letter);
echo $letter->getJobId();
echo $letter->getPrice();
...
```

## Notes

- Currently only set of a new job is supported
- Feel free to make a PR for additional features or ask for it via an issue

## Security

If you discover any security related issues, please email [[email protected]](mailto:[email protected]) instead of using the issue tracker.

## License

This package is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).
43 changes: 43 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "ctsoft/laravel-letter-xpress",
"description": "LetterXpress connection for Laravel.",
"keywords": [
"laravel",
"letterxpress"
],
"type": "library",
"license": "MIT",
"authors": [
{
"name": "CTSoft",
"email": "[email protected]"
}
],
"require": {
"php": "^7.2",
"illuminate/config": "^7.0|^8.0",
"illuminate/contracts": "^7.0|^8.0",
"illuminate/http": "^7.0|^8.0",
"illuminate/support": "^7.0|^8.0"
},
"autoload": {
"psr-4": {
"CTSoft\\Laravel\\LetterXpress\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"CTSoft\\Laravel\\LetterXpress\\Providers\\LetterXpressProvider"
],
"aliases": {
"LetterXpress": "CTSoft\\Laravel\\LetterXpress\\Facades\\LetterXpress"
}
}
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
46 changes: 46 additions & 0 deletions config/letterxpress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| API URL
|--------------------------------------------------------------------------
|
| This is the endpoint URL to the LetterXpress API.
|
| Production: https://api.letterxpress.de/v1/
| Sandbox: https://sandbox.letterxpress.de/v1/
|
*/

'api_url' => env('LETTER_XPRESS_API_URL', 'https://api.letterxpress.de/v1/'),

/*
|--------------------------------------------------------------------------
| API username
|--------------------------------------------------------------------------
|
| This is the username of your LetterXpress account.
|
| Production: https://api.letterxpress.de/v1/
| Sandbox: https://sandbox.letterxpress.de/v1/
|
*/

'api_user' => env('LETTER_XPRESS_API_USER'),

/*
|--------------------------------------------------------------------------
| API key
|--------------------------------------------------------------------------
|
| This is your personal API key.
|
| Refer to: https://www.letterxpress.de/account/api
|
*/

'api_key' => env('LETTER_XPRESS_API_KEY'),

];
100 changes: 100 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace CTSoft\Laravel\LetterXpress;

use CTSoft\Laravel\LetterXpress\Exceptions\LxpException;
use CTSoft\Laravel\LetterXpress\Models\Letter;
use CTSoft\Laravel\LetterXpress\Requests\SetJob;
use Illuminate\Http\Client\HttpClientException;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;

class Client
{
/**
* The API url.
*
* @var string
*/
protected $apiUrl;

/**
* The API user.
*
* @var string
*/
protected $apiUser;

/**
* The API key.
*
* @var string
*/
protected $apiKey;

/**
* Client constructor.
*
* @param string $apiUrl
* @param string $apiUser
* @param string $apiKey
*/
public function __construct(string $apiUrl, string $apiUser, string $apiKey)
{
$this->apiUrl = $apiUrl;
$this->apiUser = $apiUser;
$this->apiKey = $apiKey;
}

/**
* Set a new job.
*
* @param Letter $letter
* @return Letter
* @throws LxpException
*/
public function setJob(Letter $letter): Letter
{
return SetJob::getLetter(
$this->request('setJob',
SetJob::getPayload($letter)
)
);
}

/**
* Perform a request to the LetterXpress API.
*
* @param string $function
* @param array $payload
* @return array
* @throws LxpException
*/
protected function request(string $function, array $payload): array
{
$url = Str::finish($this->apiUrl, '/') . $function;

$payload = array_merge([
'auth' => [
'username' => $this->apiUser,
'apikey' => $this->apiKey,
],
], $payload);

try {
$response = Http::post($url, $payload)->throw()->json();
} catch (HttpClientException $exception) {
throw new LxpException('HTTP request failed.', 0, $exception);
}

if (is_null($response)) {
throw new LxpException('Invalid JSON response.');
}

if ($response['status'] !== 200) {
throw new LxpException($response['message'], $response['status']);
}

return $response;
}
}
10 changes: 10 additions & 0 deletions src/Exceptions/LxpException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace CTSoft\Laravel\LetterXpress\Exceptions;

use Exception;

class LxpException extends Exception
{
//
}
19 changes: 19 additions & 0 deletions src/Facades/LetterXpress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace CTSoft\Laravel\LetterXpress\Facades;

use CTSoft\Laravel\LetterXpress\Client;
use Illuminate\Support\Facades\Facade;

class LetterXpress extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return Client::class;
}
}
Loading

0 comments on commit 1de2440

Please sign in to comment.