From 9067cdc8e07b29d54d0171791f175e6dfd1d54bd Mon Sep 17 00:00:00 2001 From: Daniel Subiabre Date: Wed, 13 Dec 2023 16:02:06 +0100 Subject: [PATCH] Add basic gateways --- config/services.yaml | 6 +++ src/ApiResource/Gateway.php | 24 +++++++++ src/Entity/Accounting.php | 1 - src/Entity/Transaction.php | 6 ++- src/Library/Economy/AbstractMonetizable.php | 3 ++ .../Economy/Payment/GatewayInterface.php | 11 +++++ .../Economy/Payment/GatewayLocator.php | 49 +++++++++++++++++++ src/Library/Economy/Payment/StripeGateway.php | 11 +++++ src/State/GatewayStateProvider.php | 49 +++++++++++++++++++ 9 files changed, 157 insertions(+), 3 deletions(-) create mode 100644 src/ApiResource/Gateway.php create mode 100644 src/Library/Economy/Payment/GatewayInterface.php create mode 100644 src/Library/Economy/Payment/GatewayLocator.php create mode 100644 src/Library/Economy/Payment/StripeGateway.php create mode 100644 src/State/GatewayStateProvider.php diff --git a/config/services.yaml b/config/services.yaml index a5fe3ab..3a11d20 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -14,6 +14,8 @@ services: _instanceof: App\Library\Economy\Currency\ExchangeInterface: tags: ['app.lib.economy.currency.exchange'] + App\Library\Economy\Payment\GatewayInterface: + tags: ['app.lib.economy.payment.gateway'] # makes classes in src/ available to be used as services # this creates a service per class whose id is the fully-qualified class name @@ -34,5 +36,9 @@ services: arguments: - !tagged 'app.lib.economy.currency.exchange' + App\Library\Economy\Payment\GatewayLocator: + arguments: + - !tagged 'app.lib.economy.payment.gateway' + # add more service definitions when explicit configuration is needed # please note that last definitions always *replace* previous ones diff --git a/src/ApiResource/Gateway.php b/src/ApiResource/Gateway.php new file mode 100644 index 0000000..9a23680 --- /dev/null +++ b/src/ApiResource/Gateway.php @@ -0,0 +1,24 @@ +gateway->getName(); + } +} diff --git a/src/Entity/Accounting.php b/src/Entity/Accounting.php index 44cf2b8..ab38eb8 100644 --- a/src/Entity/Accounting.php +++ b/src/Entity/Accounting.php @@ -15,7 +15,6 @@ use Symfony\Component\Validator\Constraints as Assert; * When making relations between Transactions and Accountings you are effectively altering the platform's internal economy. */ #[ORM\Entity(repositoryClass: AccountingRepository::class)] -#[API\ApiResource()] #[API\GetCollection()] #[API\Get()] #[API\Put()] diff --git a/src/Entity/Transaction.php b/src/Entity/Transaction.php index 0196042..31feeda 100644 --- a/src/Entity/Transaction.php +++ b/src/Entity/Transaction.php @@ -11,8 +11,10 @@ use Symfony\Component\Validator\Constraints as Assert; /** * Transactions represent a movement of funds inside the platform.\ - * When making relations between Transactions and Accountings you are effectively altering the platform's internal economy.\ - * Transactions cannot be mutated after they are stored. + * When making relations between Transactions and Accountings you are effectively altering the platform's internal economy. + * \ + * \ + * Transactions cannot be mutated after they are stored.\ * If in need to reverse a Transaction generate a new Transaction reversing the to-be-cancelled one. */ #[ORM\Entity(repositoryClass: TransactionRepository::class)] diff --git a/src/Library/Economy/AbstractMonetizable.php b/src/Library/Economy/AbstractMonetizable.php index 135be9c..373002a 100644 --- a/src/Library/Economy/AbstractMonetizable.php +++ b/src/Library/Economy/AbstractMonetizable.php @@ -2,6 +2,8 @@ namespace App\Library\Economy; +use ApiPlatform\Metadata as API; + abstract class AbstractMonetizable { abstract public function getAmount(): int; @@ -25,6 +27,7 @@ abstract class AbstractMonetizable ); } + #[API\ApiProperty(readable: false)] public function isZero(): bool { return $this->toBrickMoney()->isZero(); diff --git a/src/Library/Economy/Payment/GatewayInterface.php b/src/Library/Economy/Payment/GatewayInterface.php new file mode 100644 index 0000000..e00e41f --- /dev/null +++ b/src/Library/Economy/Payment/GatewayInterface.php @@ -0,0 +1,11 @@ +gateways[$gateway->getName()] = $gateway; + } + } + + /** + * @return array List of the available Gateway names + */ + public function getNames(): array + { + return \array_keys($this->gateways); + } + + /** + * @return GatewayInterface[] + */ + public function getGateways(): array + { + return $this->gateways; + } + + /** + * @param string $name Name of the Gateway interface implementation + * @return GatewayInterface + * @throws \Exception When the $name does not match to that of an implemented Gateway + */ + public function getGateway(string $name): GatewayInterface + { + if (!\array_key_exists($name, $this->gateways)) { + throw new \Exception("No such Gateway with the name $name"); + } + + return $this->gateways[$name]; + } +} diff --git a/src/Library/Economy/Payment/StripeGateway.php b/src/Library/Economy/Payment/StripeGateway.php new file mode 100644 index 0000000..8ee9c49 --- /dev/null +++ b/src/Library/Economy/Payment/StripeGateway.php @@ -0,0 +1,11 @@ +gateways->getGateways() as $gateway) { + $gateways[] = new Gateway($gateway); + } + + return $gateways; + } + + private function getGateway(string $name): Gateway + { + try { + $gateway = $this->gateways->getGateway($name); + + return new Gateway($gateway); + } catch (\Exception $e) { + throw new NotFoundHttpException("Not Found"); + } + } + + public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null + { + switch ($operation::class) { + case API\GetCollection::class: + return $this->getGateways(); + case API\Get::class: + return $this->getGateway($uriVariables['name']); + } + } +}