-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
254 additions
and
26 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace Webfox\Xero; | ||
|
||
use Illuminate\Support\Collection; | ||
use XeroAPI\XeroPHP\Api\AccountingApi; | ||
|
||
class Webhook | ||
{ | ||
|
||
protected $signingKey; | ||
|
||
protected $payload; | ||
|
||
protected $properties; | ||
|
||
protected $events; | ||
|
||
protected $accountingApi; | ||
|
||
protected OauthCredentialManager $credentialManager; | ||
|
||
public function __construct(OauthCredentialManager $credentialManager, AccountingApi $accountingApi, string $payload, string $signingKey) | ||
{ | ||
$this->accountingApi = $accountingApi; | ||
$this->payload = $payload; | ||
$this->signingKey = $signingKey; | ||
$this->properties = new Collection(json_decode($payload, true)); | ||
|
||
// bail if json_decode fails | ||
if ($this->properties->isEmpty()) { | ||
throw new \Exception('The webhook payload could not be decoded: ' . json_last_error_msg()); | ||
} | ||
|
||
// bail if we don't have all the fields we are expecting | ||
if (!$this->properties->has(['events', 'firstEventSequence', 'lastEventSequence'])) { | ||
throw new \Exception('The webhook payload was malformed'); | ||
} | ||
|
||
$this->events = new Collection(array_map(function($event) { | ||
return new WebhookEvent($this->credentialManager, $this->accountingApi, $event); | ||
}, $this->properties->get('events'))); | ||
$this->credentialManager = $credentialManager; | ||
} | ||
|
||
public function getSignature() | ||
{ | ||
return base64_encode(hash_hmac('sha256', $this->payload, $this->signingKey, true)); | ||
} | ||
|
||
public function validate($signature) | ||
{ | ||
return hash_equals($this->getSignature(), $signature); | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getFirstEventSequence() | ||
{ | ||
return $this->properties->get('firstEventSequence'); | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getLastEventSequence() | ||
{ | ||
return $this->properties->get('lastEventSequence'); | ||
} | ||
|
||
/** | ||
* @return \Webfox\Xero\WebhookEvent[]|\Illuminate\Support\Collection | ||
*/ | ||
public function getEvents() | ||
{ | ||
return $this->events; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
|
||
namespace Webfox\Xero; | ||
|
||
|
||
use Illuminate\Support\Collection; | ||
use XeroAPI\XeroPHP\Api\AccountingApi; | ||
|
||
class WebhookEvent | ||
{ | ||
|
||
protected $properties; | ||
|
||
protected AccountingApi $accountingApi; | ||
|
||
protected OauthCredentialManager $credentialManager; | ||
|
||
public function __construct(OauthCredentialManager $credentialManager, AccountingApi $accountingApi, $event) | ||
{ | ||
$this->accountingApi = $accountingApi; | ||
$this->properties = new Collection($event); | ||
|
||
if (!$this->properties->has(['resourceUrl', 'resourceId', 'eventDateUtc', 'eventType', 'eventCategory', 'tenantId', 'tenantType',])) { | ||
throw new \Exception("The event payload was malformed; missing required field"); | ||
} | ||
$this->credentialManager = $credentialManager; | ||
} | ||
|
||
public function getResourceUrl() | ||
{ | ||
return $this->properties->get('resourceUrl'); | ||
} | ||
|
||
public function getResourceId() | ||
{ | ||
return $this->properties->get('resourceId'); | ||
} | ||
|
||
public function getEventDateUtc() | ||
{ | ||
return $this->properties->get('eventDateUtc'); | ||
} | ||
|
||
public function getEventDate() | ||
{ | ||
return new \DateTime($this->getEventDateUtc()); | ||
} | ||
|
||
public function getEventType() | ||
{ | ||
return $this->properties->get('eventType'); | ||
} | ||
|
||
public function getEventCategory() | ||
{ | ||
return $this->properties->get('eventCategory'); | ||
} | ||
|
||
public function getEventClass() | ||
{ | ||
if ($this->getEventCategory() === 'INVOICE') { | ||
return \XeroApi\XeroPHP\Models\Accounting\Invoice::class; | ||
} | ||
if ($this->getEventCategory() === 'CONTACT') { | ||
return \XeroApi\XeroPHP\Models\Accounting\Contact::class; | ||
} | ||
|
||
} | ||
|
||
public function getTenantId() | ||
{ | ||
return $this->properties->get('tenantId'); | ||
} | ||
|
||
public function getTenantType() | ||
{ | ||
return $this->properties->get('tenantType'); | ||
} | ||
|
||
public function getResource() | ||
{ | ||
if ($this->getEventCategory() === 'INVOICE') { | ||
return $this->accountingApi | ||
->getInvoice($this->credentialManager->getTenantId(), $this->getResourceId()) | ||
->getInvoices()[0]; | ||
} | ||
if ($this->getEventCategory() === 'CONTACT') { | ||
return $this->accountingApi | ||
->getContact($this->credentialManager->getTenantId(), $this->getResourceId()) | ||
->getContacts()[0]; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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