Created
August 23, 2019 01:32
-
-
Save lukecurtis93/b991c9dc54357726f1f7071a7984ba4f to your computer and use it in GitHub Desktop.
Webhooks with Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
XERO_CONSUMER_KEY=YOUR-APP-KEY | |
XERO_CONSUMER_SECRET=YOUR-SECRET-KEY | |
XERO_RSA_PRIVATE_KEY='file:///home/vagrant/code/test-web-app/storage/privatekey.pem' | |
XERO_RSA_PUBLIC_KEY='file:///home/vagrant/code/test-web-app/storage/publickey.cer' | |
XERO_WEBHOOK_KEY=YOUR-WEBHOOK-KEY |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http; | |
class Kernel extends HttpKernel | |
{ | |
/** More code above */ | |
protected $routeMiddleware = [ | |
'auth' => \App\Http\Middleware\Authenticate::class, | |
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, | |
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, | |
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class, | |
'can' => \Illuminate\Auth\Middleware\Authorize::class, | |
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, | |
'password_expires' => \App\Http\Middleware\PasswordExpires::class, | |
'permission' => \Spatie\Permission\Middlewares\PermissionMiddleware::class, | |
'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class, | |
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, | |
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, | |
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, | |
'verify_xero' => \App\Http\Middleware\VerifyXeroToken::class, | |
]; | |
/** More code below */ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Middleware; | |
use Closure; | |
use XeroPHP\Webhook; | |
use XeroPHP\Application\PrivateApplication; | |
/** | |
* Class VerifyXeroToken. | |
*/ | |
class VerifyXeroToken | |
{ | |
/** | |
* Handle an incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure $next | |
* @param string|null $guard | |
* @return mixed | |
*/ | |
public function handle($request, Closure $next, $guard = null) | |
{ | |
$application = new PrivateApplication(config('xero')); | |
$webhook = new Webhook($application, $request->getContent()); | |
if (! $webhook->validate($request->headers->get('x-xero-signature'))) { | |
$response = response()->json(null, 401); | |
$response->setContent(null); | |
return $response; | |
} | |
return $next($request); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Route::group(['namespace' => 'Api\v1\Webhooks', 'prefix' => 'webhooks', 'as' => 'webhooks.'], function () { | |
// | |
Route::post('acuity', 'AcuitySchedulingWebhookController@handle'); | |
Route::post('xero', 'XeroWebhookController@handle')->middleware('verify_xero'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\Api\v1\Webhooks; | |
use XeroPHP\Webhook; | |
use App\Models\Invoice; | |
use Illuminate\Http\Request; | |
use App\Jobs\Xero\SyncXeroInvoice; | |
use App\Http\Controllers\Controller; | |
use XeroPHP\Application\PrivateApplication; | |
use App\Repositories\Backend\InvoiceRepository; | |
class XeroWebhookController extends Controller | |
{ | |
public function handle(Request $request) | |
{ | |
$webhook = new Webhook(new PrivateApplication(config('xero')), $request->getContent()); | |
foreach ($webhook->getEvents() as $event) { | |
$invoice = Invoice::whereXeroGuid($event->getResourceId())->first(); | |
if ($invoice) { | |
dispatch_now(new SyncXeroInvoice($this->invoiceRepository, $invoice)); | |
} | |
} | |
$response = response()->json(null, 200); | |
$response->setContent(null); | |
return $response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment