forked from DearTanakorn/truemoney-webhook-gateway
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from parsilver/refactor-code
Add supported PHP 8.3
- Loading branch information
Showing
11 changed files
with
204 additions
and
90 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 |
---|---|---|
|
@@ -36,7 +36,7 @@ | |
"authors": [ | ||
{ | ||
"name": "PA", | ||
"role": "DevOps" | ||
"role": "Developer" | ||
} | ||
], | ||
"config": { | ||
|
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,13 @@ | ||
<?php | ||
|
||
namespace Farzai\TruemoneyWebhook\Contracts; | ||
|
||
interface MessageParser | ||
{ | ||
/** | ||
* Parse the request from Truemoney webhook. | ||
* | ||
* @return mixed | ||
*/ | ||
public function parse(array $data); | ||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace Farzai\TruemoneyWebhook\Contracts; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
interface ServerRequestFactory | ||
{ | ||
/** | ||
* Create a new server request. | ||
*/ | ||
public function create(): ServerRequestInterface; | ||
} |
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,44 @@ | ||
<?php | ||
|
||
namespace Farzai\TruemoneyWebhook; | ||
|
||
use Farzai\TruemoneyWebhook\Contracts\MessageParser as MessageParserContract; | ||
use Farzai\TruemoneyWebhook\Entity\Message; | ||
use Firebase\JWT\JWT; | ||
use Firebase\JWT\Key; | ||
use RuntimeException; | ||
|
||
class MessageParser implements MessageParserContract | ||
{ | ||
private array $config = [ | ||
// The key used to sign | ||
'secret' => null, | ||
|
||
// Algorithm used to sign the token | ||
'alg' => 'HS256', | ||
]; | ||
|
||
/** | ||
* Parser constructor. | ||
*/ | ||
public function __construct(array $config) | ||
{ | ||
$this->config = array_merge($this->config, $config); | ||
} | ||
|
||
/** | ||
* Parse the request from Truemoney webhook. | ||
* | ||
* @return mixed | ||
*/ | ||
public function parse(array $data) | ||
{ | ||
if (! $data || ! $data['message']) { | ||
throw new RuntimeException('Invalid request body.'); | ||
} | ||
|
||
$data = JWT::decode($data['message'], new Key($this->config['secret'], $this->config['alg'])); | ||
|
||
return new Message((array) $data); | ||
} | ||
} |
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,28 @@ | ||
<?php | ||
|
||
namespace Farzai\TruemoneyWebhook; | ||
|
||
use Farzai\TruemoneyWebhook\Contracts\ServerRequestFactory as ServerRequestFactoryContract; | ||
use Nyholm\Psr7\Factory\Psr17Factory; | ||
use Nyholm\Psr7Server\ServerRequestCreator; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
class ServerRequestFactory implements ServerRequestFactoryContract | ||
{ | ||
/** | ||
* Create a new server request. | ||
*/ | ||
public function create(): ServerRequestInterface | ||
{ | ||
$factory = new Psr17Factory(); | ||
|
||
$creator = new ServerRequestCreator( | ||
$factory, | ||
$factory, | ||
$factory, | ||
$factory | ||
); | ||
|
||
return $creator->fromGlobals(); | ||
} | ||
} |
Oops, something went wrong.