Skip to content

Commit

Permalink
✨ Handle webhook callback verification, resend
Browse files Browse the repository at this point in the history
See Issue#44
  • Loading branch information
amcintosh committed Oct 12, 2023
1 parent 9dbb830 commit c8119e7
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,12 @@ To run all tests:
make test
```

Run a specific test:

```shell
./vendor/bin/phpunit --filter testCreateValidationError
```

### Documentations

You can generate the documentation via:
Expand Down
2 changes: 1 addition & 1 deletion src/FreshBooksClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public function taxes(): AccountingResource

/**
* FreshBooks callbacks (webhook callbacks) resource with calls to
* get, list, create, update, delete, resend_verification, verify
* get, list, create, update, delete, verify, resendVerification
*
* @return EventsResource
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Resource/AccountingResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected function getUrl(string $accountId, int $resourceId = null): string
* @param array $data
* @return array
*/
private function makeRequest(string $method, string $url, array $data = null): array
protected function makeRequest(string $method, string $url, array $data = null): array
{
if (!is_null($data)) {
$data = json_encode($data);
Expand Down
31 changes: 31 additions & 0 deletions src/Resource/EventsResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,35 @@ protected function handleError(int $statusCode, array $responseData, string $raw
var_dump($details);
throw new FreshBooksException($message, $statusCode, null, $rawRespone, $errorCode, $details);
}

/**
* Tell FreshBooks to resend the verification webhook for the callback
*
* @param string $accountId The alpha-numeric account id
* @param int $resourceId Id of the resource to update
* @return DataTransferObject Model of the updated resource's response data.
*/
public function resendVerification(string $accountId, int $resourceId): DataTransferObject {
$data = array($this->singleModel::RESPONSE_FIELD => array("resend" => true));
$result = $this->makeRequest(self::PUT, $this->getUrl($accountId, $resourceId), $data);
return new $this->singleModel($result[$this->singleModel::RESPONSE_FIELD]);
}

/**
* Verify webhook callback by making a put request
*
* @param string $accountId The alpha-numeric account id
* @param int $resourceId Id of the resource to update
* @param string The string verifier received by the webhook callback URI
* @return DataTransferObject Model of the updated resource's response data.
*/
public function verify(
string $accountId,
int $resourceId,
string $verifier,
): DataTransferObject {
$data = array($this->singleModel::RESPONSE_FIELD => array("verifier" => $verifier));
$result = $this->makeRequest(self::PUT, $this->getUrl($accountId, $resourceId), $data);
return new $this->singleModel($result[$this->singleModel::RESPONSE_FIELD]);
}
}
42 changes: 42 additions & 0 deletions tests/Resource/EventsResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,46 @@ public function testCreateValidationError(): void
]], $e->getErrorDetails());
}
}

public function testVerify(): void
{
$callbackId = 12345;
$mockHttpClient = $this->getMockHttpClient(
200,
['response' => ['result' => ['callback' => [
'callbackid' => $callbackId,
'verified' => true
]]]]
);

$resource = new EventsResource($mockHttpClient, 'events/callbacks', Callback::class, CallbackList::class);
$callback = $resource->verify($this->accountId, $callbackId, 'some_verifier');

$this->assertSame($callbackId, $callback->callbackId);

$request = $mockHttpClient->getLastRequest();
$this->assertSame('PUT', $request->getMethod());
$this->assertSame('/events/account/ACM123/events/callbacks/12345', $request->getRequestTarget());
}

public function testResend(): void
{
$callbackId = 12345;
$mockHttpClient = $this->getMockHttpClient(
200,
['response' => ['result' => ['callback' => [
'callbackid' => $callbackId,
'verified' => true
]]]]
);

$resource = new EventsResource($mockHttpClient, 'events/callbacks', Callback::class, CallbackList::class);
$callback = $resource->resendVerification($this->accountId, $callbackId);

$this->assertSame($callbackId, $callback->callbackId);

$request = $mockHttpClient->getLastRequest();
$this->assertSame('PUT', $request->getMethod());
$this->assertSame('/events/account/ACM123/events/callbacks/12345', $request->getRequestTarget());
}
}

0 comments on commit c8119e7

Please sign in to comment.