Compare commits

...
Sign in to create a new pull request.

1 commit

Author SHA1 Message Date
Jérémy Munsch
c9cd3b4676
init mailjet integration 2021-11-05 16:26:59 +01:00
8 changed files with 198 additions and 2 deletions

View file

@ -0,0 +1,15 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\MailjetUnsubscribeRequest;
class MailjetController extends Controller
{
public function unsubscribe(MailjetUnsubscribeRequest $request)
{
\print_r($request->all());
die;
return \response()->json($request->all());
}
}

View file

@ -46,6 +46,12 @@ class Kernel extends HttpKernel
\App\Http\Middleware\ApiCheckTimeStamps::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class
],
'mailjet' => [
'throttle:api',
\App\Http\Middleware\JsonOnly::class,
\App\Http\Middleware\MailjetChecker::class
]
];
/**

View file

@ -0,0 +1,32 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class MailjetChecker
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string[] ...$guards
* @return mixed
*
* @throws \Illuminate\Auth\AuthenticationException
*/
public function handle($request, Closure $next)
{
$auth = $request->header('Authorization');
$authDecode = base64_decode(\str_replace('Basic ', '', $auth));
$authCreds = \explode(':', $authDecode);
$check = ($authCreds[0] ?? '') === config('mailjet.login');
$check = $check && (($authCreds[1] ?? '') === config('mailjet.password'));
if (!$check) {
throw new BadRequestHttpException();
}
return $next($request);
}
}

View file

@ -0,0 +1,60 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class MailjetUnsubscribeRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* event: the event type
* time: Unix timestamp of event
* email: email address of recipient triggering the event
* mj_campaign_id: internal Mailjet campaign ID associated to the message
* mj_contact_id: internal Mailjet contact ID
* customcampaign: value of the X-Mailjet-Campaign header when provided
* MessageID: The unique message ID
* CustomID: the custom ID, when provided at send time
* Payload: the event payload, when provided at send time
*
* {
* "event": "unsub",
* "time": 1433334941,
* "MessageID": 20547674933128000,
* "Message_GUID": "1ab23cd4-e567-8901-2345-6789f0gh1i2j",
* "email": "api@mailjet.com",
* "mj_campaign_id": 7276,
* "mj_contact_id": 126,
* "customcampaign": "",
* "CustomID": "helloworld",
* "Payload": "",
* "mj_list_id": 1,
* "ip": "127.0.0.1",
* "geo": "FR",
* "agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36"
* }
*
* @return array
*/
public function rules()
{
return [
// accept unsub request only
'event' => 'required|in:unsub',
// accept a valid email format
'email' => 'required|email',
];
}
}

View file

@ -37,8 +37,12 @@ class RouteServiceProvider extends ServiceProvider
$this->configureRateLimiting();
$this->routes(function () {
Route::prefix('api')->middleware('api')->namespace($this->namespace)->group(base_path('routes/api.php'));
Route::middleware('web')->namespace($this->namespace)->group(base_path('routes/web.php'));
Route::prefix('api')->middleware('api')->namespace($this->namespace)
->group(base_path('routes/api.php'));
Route::prefix('mailjet')->middleware('mailjet')->namespace($this->namespace)
->group(base_path('routes/mailjet.php'));
Route::middleware('web')->namespace($this->namespace)
->group(base_path('routes/web.php'));
});
Route::model('brand', Brand::class);

6
config/mailjet.php Normal file
View file

@ -0,0 +1,6 @@
<?php
return [
'login' => env('MAILJET_HT_LOGIN'),
'password' => env('MAILJET_HT_PASSWORD')
];

18
routes/mailjet.php Normal file
View file

@ -0,0 +1,18 @@
<?php
use App\Http\Controllers\MailjetController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Mailjet Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "mailjet" middleware group. Enjoy building your API!
|
*/
Route::post('/unsubscribe-recipient', [MailjetController::class, 'unsubscribe'])
->name('unsubscribe');

View file

@ -0,0 +1,55 @@
<?php
namespace Tests\Unit;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request as Psr7Request;
use LDC\LDCCRM\Enums\ApiError;
use LDC\LDCCRM\Enums\Country;
use LDC\LDCCRM\Enums\HttpMethods;
use LDC\LDCCRM\Exceptions\ApiErrorException;
use LDC\LDCCRM\Models\Customer;
use LDC\LDCCRM\Models\SingleOptin;
use Psr\Http\Message\ResponseInterface;
class MailjetTest extends CrmClientTester
{
/** @var \GuzzleHttp\Client */
private $client;
public function testMailjetHtaccess()
{
dd($this->callHttp(HttpMethods::post(), route('mailjet.unsubscribe')));
}
/**
* Make HTTP Calls
*
* @param HttpMethods $method
* @param string $uri
* @param array $headers
* @param array $data
* @return ResponseInterface
*/
private function callHttp(
HttpMethods $method,
string $uri,
array $headers = [],
array $data = []
): ResponseInterface {
$request = new Psr7Request(
\strtoupper($method),
$uri,
$headers,
$method->equals(HttpMethods::get()) ? null : \json_encode($data)
);
if (!$this->client) {
$this->client = new Client();
}
return $this->client->send($request, [
'http_errors' => false // do not throw http errors
]);
}
}