Compare commits
1 commit
master
...
mailjet-in
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c9cd3b4676 |
8 changed files with 198 additions and 2 deletions
15
app/Http/Controllers/MailjetController.php
Normal file
15
app/Http/Controllers/MailjetController.php
Normal 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());
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
]
|
||||
];
|
||||
|
||||
/**
|
||||
|
|
|
|||
32
app/Http/Middleware/MailjetChecker.php
Normal file
32
app/Http/Middleware/MailjetChecker.php
Normal 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);
|
||||
}
|
||||
}
|
||||
60
app/Http/Requests/MailjetUnsubscribeRequest.php
Normal file
60
app/Http/Requests/MailjetUnsubscribeRequest.php
Normal 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',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -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
6
config/mailjet.php
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
<?php
|
||||
|
||||
return [
|
||||
'login' => env('MAILJET_HT_LOGIN'),
|
||||
'password' => env('MAILJET_HT_PASSWORD')
|
||||
];
|
||||
18
routes/mailjet.php
Normal file
18
routes/mailjet.php
Normal 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');
|
||||
55
tests/Unit/MailjetTest.php
Normal file
55
tests/Unit/MailjetTest.php
Normal 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
|
||||
]);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue