diff --git a/README.md b/README.md index d252e33..5a4f938 100644 --- a/README.md +++ b/README.md @@ -27,13 +27,13 @@ this is just passing an array in the following format: 'access_id' => 'xxxxxxxx', 'secure_key' => 'xxxxxxxx', ... - ] + ], + 'live' => [ + 'access_id' => 'xxxxxxxx', + 'secure_key' => 'xxxxxxxx', + ... + ], ], - 'override_sub_resource_environments' => [ - 'address' => 'env_name', - 'application' => 'env_name', - ... - ] ] ``` @@ -64,18 +64,6 @@ parameters: sandbox: "%forte_api_livetest_sandbox%" base_uri: ~ debug: false - - override_sub_resource_environments: - address: livetest - application: livetest - customer: livetest - dispute: livetest - document: livetest - funding: livetest - pay_method: livetest - schedule_item: livetest - settlement: livetest - transaction: ~ ``` ### Production Environments @@ -94,8 +82,6 @@ parameters: sandbox: "%forte_api_default_sandbox%" base_uri: ~ debug: false - - override_sub_resource_environments: ~ ``` ## Usage @@ -147,13 +133,13 @@ $settings = [ 'access_id' => 'xxxxxxxx', 'secure_key' => 'xxxxxxxx', ... + ], + 'live' => [ + 'access_id' => 'xxxxxxxx', + 'secure_key' => 'xxxxxxxx', + ... ] ], - 'override_sub_resource_environments' => [ - 'address' => 'env_name', - 'application' => 'env_name', - ... - ] ]; $logger = new FileLogger(); diff --git a/src/Client/Factory.php b/src/Client/Factory.php index 6466d9b..011c9fa 100644 --- a/src/Client/Factory.php +++ b/src/Client/Factory.php @@ -25,8 +25,7 @@ class Factory */ public static function make( array $settings, - LoggerInterface $logger, - string $defaultEnvironmentKey + LoggerInterface $logger ): ForteClient { if (empty($settings['environments'])) { @@ -74,8 +73,6 @@ public static function make( ); } - $overrideSubResourceEnvironments = $settings['override_sub_resource_environments'] ?? []; - - return new ForteClient($environments, $defaultEnvironmentKey, $overrideSubResourceEnvironments); + return new ForteClient($environments); } } diff --git a/src/Client/ForteClient.php b/src/Client/ForteClient.php index 32d7936..d3a827c 100644 --- a/src/Client/ForteClient.php +++ b/src/Client/ForteClient.php @@ -4,94 +4,70 @@ namespace Rentpost\ForteApi\Client; -use Rentpost\ForteApi\Client\SubResource\AbstractSubResource; +use Rentpost\ForteApi\Client\SubResource\AddressSubResource; +use Rentpost\ForteApi\Client\SubResource\ApplicationSubResource; +use Rentpost\ForteApi\Client\SubResource\CustomerSubResource; +use Rentpost\ForteApi\Client\SubResource\DisputeSubResource; +use Rentpost\ForteApi\Client\SubResource\DocumentSubResource; +use Rentpost\ForteApi\Client\SubResource\FundingSubResource; +use Rentpost\ForteApi\Client\SubResource\LocationSubResource; +use Rentpost\ForteApi\Client\SubResource\PayMethodSubResource; +use Rentpost\ForteApi\Client\SubResource\ScheduleItemSubResource; +use Rentpost\ForteApi\Client\SubResource\ScheduleSubResource; +use Rentpost\ForteApi\Client\SubResource\SettlementSubResource; +use Rentpost\ForteApi\Client\SubResource\TransactionSubResource; use Rentpost\ForteApi\Exception\LibraryGenericException; use Rentpost\ForteApi\ForteEnvironment; -use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; /** * This is the main entry class/client for accessing this library * - * @author Sam Anthony * @author Jacob Thomason + * @author Sam Anthony */ class ForteClient { - /** @var AbstractSubResource[] */ - protected array $subResources; - /** @var ForteEnvironment[] */ protected array $environments; - protected ?ForteEnvironment $currentEnvironment = null; - /** * Constructor + * + * @param ForteEnvironment[] $environments List of environments by name/key identifiers + * to make it easier to identify when using sub-resources */ - public function __construct( - array $environments, - string $defaultEnvironment, - array $overrideSubResourceEnvironments = [] - ) { + public function __construct(array $environments) + { foreach ($environments as $name => $environment) { $this->addForteEnvironment($name, $environment); } - - $this->setCurrentForteEnvironment($defaultEnvironment); - $this->initSubResources($overrideSubResourceEnvironments); } /** * Adds an environment to support */ - private function addForteEnvironment(string $name, ForteEnvironment $environment): void + protected function addForteEnvironment(string $name, ForteEnvironment $environment): void { $this->environments[$name] = $environment; } /** - * Gets a particular ForteEnvironment based on the key identifier - */ - protected function getForteEnvironment(string $name): ForteEnvironment - { - if (!isset($this->environments[$name])) { - throw new LibraryGenericException('Cannot find forte environment with name `' . $name . '`'); - } - - return $this->environments[$name]; - } - - - /** - * Gets a given environment by name - */ - protected function getCurrentForteEnvironment(): ForteEnvironment - { - if ($this->currentEnvironment === null) { - throw new LibraryGenericException('Current Forte environemnt has not been set'); - } - - return $this->currentEnvironment; - } - - - /** - * Sets the current environment to be used + * Gets the ForteEnvironment + * + * @param string #env The name/key/identifier passed in when instantiating the ForteClient */ - public function setCurrentForteEnvironment(string $name): self + protected function getForteEnvironment(string $env): ForteEnvironment { - if (!isset($this->environments[$name])) { - throw new LibraryGenericException('Cannot find forte environment with name `' . $name . '`'); + if (!isset($this->environments[$env])) { + throw new LibraryGenericException('Unable to locate a ForteEnvironment identified by: ' . $env); } - $this->currentEnvironment = $this->environments[$name]; - - return $this; + return $this->environments[$env]; } @@ -115,164 +91,134 @@ public function getRiskSessionJavascriptUrl(string $riskSessionId): string } - /** - * Initializes all the necessary sub resources - * - * @param string[] $overrideSubResourceEnvironments - */ - protected function initSubResources(array $overrideSubResourceEnvironments): void - { - $subResourceList = [ - 'address', - 'application', - 'customer', - 'dispute', - 'document', - 'funding', - 'location', - 'pay_method', - 'schedule_item', - 'schedule', - 'settlement', - 'transaction', - ]; - - foreach ($overrideSubResourceEnvironments as $subResource => $environment) { - if (!in_array($subResource, $subResourceList)) { - throw new LibraryGenericException('You have passed a sub resource which does not exist'); - } - } - - foreach ($subResourceList as $subResource) { - $subResourceFqns = $this->inferSubResourceClassName($subResource); - - if (!empty($overrideSubResourceEnvironments[$subResource])) { - $environment = $this->getForteEnvironment($overrideSubResourceEnvironments[$subResource]); - } else { - $environment = $this->getCurrentForteEnvironment(); - } - - $this->subResources[$subResource] = new $subResourceFqns($environment); - } - } - - - /** - * Determines the sub resource FQCN - */ - protected function inferSubResourceClassName(string $subResource): string - { - $converter = new CamelCaseToSnakeCaseNameConverter(null, false); - $part = $converter->denormalize($subResource); - - return 'Rentpost\\ForteApi\\Client\\SubResource\\' . $part . 'SubResource'; - } - - /** * Uses the transactions sub resource + * + * @param string $env ForteEnvironment name/key/identifier */ - public function useTransactions(): SubResource\TransactionSubResource + public function useTransactions(string $env): TransactionSubResource { - return $this->subResources['transaction']; + return new TransactionSubResource($this->getForteEnvironment($env)); } /** * Uses the schedule sub resource + * + * @param string $env ForteEnvironment name/key/identifier */ - public function useSchedules(): SubResource\ScheduleSubResource + public function useSchedules(string $env): ScheduleSubResource { - return $this->subResources['schedule']; + return new ScheduleSubResource($this->getForteEnvironment($env)); } /** * Uses the schedule item sub resource + * + * @param string $env ForteEnvironment name/key/identifier */ - public function useScheduleItems(): SubResource\ScheduleItemSubResource + public function useScheduleItems(string $env): ScheduleItemSubResource { - return $this->subResources['schedule_item']; + return new ScheduleItemSubResource($this->getForteEnvironment($env)); } /** * Uses the settlement sub resource + * + * @param string $env ForteEnvironment name/key/identifier */ - public function useSettlements(): SubResource\SettlementSubResource + public function useSettlements(string $env): SettlementSubResource { - return $this->subResources['settlement']; + return new SettlementSubResource($this->getForteEnvironment($env)); } /** * Uses the funding sub resource + * + * @param string $env ForteEnvironment name/key/identifier */ - public function useFundings(): SubResource\FundingSubResource + public function useFundings(string $env): FundingSubResource { - return $this->subResources['funding']; + return new FundingSubResource($this->getForteEnvironment($env)); } /** * Uses the location sub resource + * + * @param string $env ForteEnvironment name/key/identifier */ - public function useLocations(): SubResource\LocationSubResource + public function useLocations(string $env): LocationSubResource { - return $this->subResources['location']; + return new LocationSubResource($this->getForteEnvironment($env)); } /** * Uses the customer sub resource + * + * @param string $env ForteEnvironment name/key/identifier */ - public function useCustomers(): SubResource\CustomerSubResource + public function useCustomers(string $env): CustomerSubResource { - return $this->subResources['customer']; + return new CustomerSubResource($this->getForteEnvironment($env)); } /** * Uses the address sub resource + * + * @param string $env ForteEnvironment name/key/identifier */ - public function useAddresses(): SubResource\AddressSubResource + public function useAddresses(string $env): AddressSubResource { - return $this->subResources['address']; + return new AddressSubResource($this->getForteEnvironment($env)); } /** * Uses the pay method sub resource + * + * @param string $env ForteEnvironment name/key/identifier */ - public function usePayMethods(): SubResource\PayMethodSubResource + public function usePayMethods(string $env): PayMethodSubResource { - return $this->subResources['pay_method']; + return new PayMethodSubResource($this->getForteEnvironment($env)); } /** * Uses the dispute sub resource + * + * @param string $env ForteEnvironment name/key/identifier */ - public function useDisputes(): SubResource\DisputeSubResource + public function useDisputes(string $env): DisputeSubResource { - return $this->subResources['dispute']; + return new DisputeSubResource($this->getForteEnvironment($env)); } /** * Uses the application sub resource + * + * @param string $env ForteEnvironment name/key/identifier */ - public function useApplications(): SubResource\ApplicationSubResource + public function useApplications(string $env): ApplicationSubResource { - return $this->subResources['application']; + return new ApplicationSubResource($this->getForteEnvironment($env)); } /** * Uses the document sub resource + * + * @param string $env ForteEnvironment name/key/identifier */ - public function useDocuments(): SubResource\DocumentSubResource + public function useDocuments(string $env): DocumentSubResource { - return $this->subResources['document']; + return new DocumentSubResource($this->getForteEnvironment($env)); } } diff --git a/test/Integration/AbstractIntegrationTest.php b/test/Integration/AbstractIntegrationTest.php index 3a4ee8a..b6ed9a5 100644 --- a/test/Integration/AbstractIntegrationTest.php +++ b/test/Integration/AbstractIntegrationTest.php @@ -37,7 +37,7 @@ protected function getForteClient(): ForteClient $allSettings = $this->getAllSettingsFromFile(); - return (new \Rentpost\ForteApi\Client\Factory())->make($allSettings, $logger, 'sandbox'); + return (new \Rentpost\ForteApi\Client\Factory())->make($allSettings, $logger); } diff --git a/test/Integration/Client/ApplicationsTest.php b/test/Integration/Client/ApplicationsTest.php index 51fcc12..5357830 100644 --- a/test/Integration/Client/ApplicationsTest.php +++ b/test/Integration/Client/ApplicationsTest.php @@ -104,7 +104,7 @@ public function testCreate() $client = $this->getForteClient(); - $returnedApplication = $client->useApplications()->create($application); + $returnedApplication = $client->useApplications('livetest')->create($application); $this->assertInstanceOf(Model\Application::class, $returnedApplication); $this->assertInstanceOf(Attribute\Id\ApplicationId::class, $returnedApplication->getApplicationId()); @@ -122,7 +122,7 @@ function testFindAll() { $client = $this->getForteClient(); - $applications = $client->useApplications()->find(); + $applications = $client->useApplications('livetest')->find(); $this->assertInstanceOf(Model\ApplicationCollection::class, $applications); diff --git a/test/Integration/Client/DisputesTest.php b/test/Integration/Client/DisputesTest.php index 0c42840..2398bb0 100644 --- a/test/Integration/Client/DisputesTest.php +++ b/test/Integration/Client/DisputesTest.php @@ -22,7 +22,7 @@ public function testFindListNoFilter() $organizationId = new Attribute\Id\OrganizationId(UserSettings::getLivetestAuthenticatingOrganizationId()); // WONTFIX hard coded - $disputeCollection = $client->useDisputes()->find($organizationId); + $disputeCollection = $client->useDisputes('livetest')->find($organizationId); $this->assertInstanceOf(Model\DisputeCollection::class, $disputeCollection); $this->assertInstanceOf(Model\Response::class, $disputeCollection->getResponse()); diff --git a/test/Integration/Client/DocumentTest.php b/test/Integration/Client/DocumentTest.php index d2426a2..2cef60e 100644 --- a/test/Integration/Client/DocumentTest.php +++ b/test/Integration/Client/DocumentTest.php @@ -42,7 +42,7 @@ public function testCreate() ->setHttpFileName('test-document-' . rand() . '.png') // Note: if same file name is used to many times, forte API will complain ->setContentType('image/png'); - $returnedDocument = $client->useDocuments()->create($document, $attachment); + $returnedDocument = $client->useDocuments('livetest')->create($document, $attachment); $this->assertDocument($returnedDocument); @@ -57,7 +57,7 @@ public function testFindOne(Attribute\Id\DocumentId $documentId) { $client = $this->getForteClient(); - $client->useDocuments()->findOne($documentId); + $client->useDocuments('livetest')->findOne($documentId); $returnedDocument = null; @@ -66,7 +66,7 @@ public function testFindOne(Attribute\Id\DocumentId $documentId) // repeatly attempt to get it for a few seconds for ($attempt = 0; $attempt < 10; $attempt++) { try { - $returnedDocument = $client->useDocuments()->findOne($documentId); + $returnedDocument = $client->useDocuments('livetest')->findOne($documentId); } catch (AbstractRequestException $e) { // Ignore this exception as we want to attempt to this a few times. } diff --git a/test/Integration/Client/FundingsTest.php b/test/Integration/Client/FundingsTest.php index 720cade..b0427d5 100644 --- a/test/Integration/Client/FundingsTest.php +++ b/test/Integration/Client/FundingsTest.php @@ -27,7 +27,7 @@ public function testFindAll() ->setStartEffectiveDate(new Attribute\Date('2017-09-01')) ->setEndEffectiveDate(new Attribute\Date('2018-01-01')); - $fundingsCollection = $client->useFundings()->find(UserSettings::getLivetestMerchantOrganizationId(), $filter); + $fundingsCollection = $client->useFundings('livetest')->find(UserSettings::getLivetestMerchantOrganizationId(), $filter); $this->assertInstanceOf(Model\FundingCollection::class, $fundingsCollection); $this->assertInstanceOf(Model\Response::class, $fundingsCollection->getResponse()); diff --git a/test/Integration/Client/LocationsTest.php b/test/Integration/Client/LocationsTest.php index abecae7..3779897 100644 --- a/test/Integration/Client/LocationsTest.php +++ b/test/Integration/Client/LocationsTest.php @@ -54,7 +54,7 @@ public function testFindOne(): void $organizationId = UserSettings::getSandboxMerchantOrganizationId(); $locationId = UserSettings::getSandboxMerchantLocationId(); - $location = $client->useLocations()->findOne($organizationId, $locationId); + $location = $client->useLocations('sandbox')->findOne($organizationId, $locationId); $this->assertNotEmpty($location, 'Attmpting to find a Location resource'); @@ -71,7 +71,7 @@ public function testFind() $organizationId = UserSettings::getSandboxMerchantOrganizationId(); - $locationCollection = $client->useLocations()->find($organizationId); + $locationCollection = $client->useLocations('sandbox')->find($organizationId); $this->assertInstanceOf(Model\LocationCollection::class, $locationCollection); @@ -113,7 +113,7 @@ public function testUpdateLimits(): void ->setPerTransDebit(5000) ->setPerTransCredit(5000); - $returnedLocation = $client->useLocations()->updateLimits( + $returnedLocation = $client->useLocations('sandbox')->updateLimits( $organizationId, $locationId, $echeckSetting, diff --git a/test/Integration/Client/SettlementsTest.php b/test/Integration/Client/SettlementsTest.php index d3195f7..b1cddee 100644 --- a/test/Integration/Client/SettlementsTest.php +++ b/test/Integration/Client/SettlementsTest.php @@ -18,6 +18,7 @@ */ class SettlementsTest extends AbstractIntegrationTest { + public function testFindListNoFilter() { $client = $this->getForteClient(); @@ -27,7 +28,7 @@ public function testFindListNoFilter() ->setStartSettleDate(new Attribute\Date('2010-01-01')) ->setEndSettleDate(new Attribute\Date('2018-02-01')); - $settlementCollection = $client->useSettlements()->findForEntireOrganization($filter); + $settlementCollection = $client->useSettlements('livetest')->findForEntireOrganization($filter); $this->assertInstanceOf(Model\SettlementCollection::class, $settlementCollection); } diff --git a/test/Integration/Client/TransactionsTest.php b/test/Integration/Client/TransactionsTest.php index c1d886e..b236569 100644 --- a/test/Integration/Client/TransactionsTest.php +++ b/test/Integration/Client/TransactionsTest.php @@ -5,7 +5,6 @@ namespace Rentpost\ForteApi\Test\Integration\Client; use Rentpost\ForteApi\Attribute; -use Rentpost\ForteApi\Exception\Request\AbstractRequestException; use Rentpost\ForteApi\Exception\Request\TimeoutException; use Rentpost\ForteApi\Exception\ValidationException; use Rentpost\ForteApi\Filter\TransactionFilter; @@ -27,7 +26,6 @@ class TransactionsTest extends AbstractIntegrationTest public function testModelValidation() { - $this->expectException(ValidationException::class); $client = $this->getForteClient(); @@ -38,7 +36,7 @@ public function testModelValidation() $transaction = new Model\Transaction(); // Expecting an exception - $client->useTransactions()->create($organizationId, $locationId, $transaction); + $client->useTransactions('sandbox')->create($organizationId, $locationId, $transaction); } @@ -86,7 +84,7 @@ public function testCreate() $transaction->setAuthorizationAmount(new Attribute\Money('109.49')); - $returnedTransaction = $client->useTransactions()->create($organizationId, $locationId, $transaction); + $returnedTransaction = $client->useTransactions('sandbox')->create($organizationId, $locationId, $transaction); $this->assertTransactionModel($returnedTransaction, true); @@ -136,7 +134,7 @@ public function testVerify() $transaction->setAuthorizationAmount(new Attribute\Money('00.01')); // Need to send at least 1 cent to verify (person will not be chargd this amount) - $returnedTransaction = $client->useTransactions()->create($organizationId, $locationId, $transaction); + $returnedTransaction = $client->useTransactions('sandbox')->create($organizationId, $locationId, $transaction); $this->assertInstanceOf('Rentpost\ForteApi\Attribute\Id\TransactionId', $returnedTransaction->getTransactionId()); @@ -154,7 +152,7 @@ public function testFindOneWaitFail() $locationId = UserSettings::getSandboxMerchantLocationId(); $invalidTransactionId = new Attribute\Id\TransactionId('trn_aaaabbbb-aaaa-1111-86a9-aaaabbbbcccc'); - $transaction = $client->useTransactions()->findOneWait($organizationId, $locationId, $invalidTransactionId); + $transaction = $client->useTransactions('sandbox')->findOneWait($organizationId, $locationId, $invalidTransactionId); } @@ -168,7 +166,7 @@ public function testFindOne(Attribute\Id\TransactionId $transactionId): Model\Tr $organizationId = UserSettings::getSandboxMerchantOrganizationId(); $locationId = UserSettings::getSandboxMerchantLocationId(); - $transaction = $client->useTransactions()->findOneWait($organizationId, $locationId, $transactionId); + $transaction = $client->useTransactions('sandbox')->findOneWait($organizationId, $locationId, $transactionId); $this->assertNotEmpty($transaction, 'Attempted a few times to get transaction from API, it should have succeeded by now'); @@ -196,7 +194,7 @@ public function testFindListAll() $filter->setStartReceivedDate(new Attribute\Date(self::START_RECEIVED_DATE)); $filter->setEndReceivedDate(new Attribute\Date($tomorrow)); - $transactionCollection = $client->useTransactions()->find($organizationId, $locationId, $filter); + $transactionCollection = $client->useTransactions('sandbox')->find($organizationId, $locationId, $filter); $this->assertInstanceOf(Model\TransactionCollection::class, $transactionCollection); @@ -231,7 +229,7 @@ public function testFindListFilterValid(int $countEntireList) $filter->setStartReceivedDate(new Attribute\Date(self::START_RECEIVED_DATE)); $filter->setEndReceivedDate(new Attribute\Date($yestarday)); - $transactionCollection = $client->useTransactions()->find($organizationId, $locationId, $filter, $pagination); + $transactionCollection = $client->useTransactions('sandbox')->find($organizationId, $locationId, $filter, $pagination); $this->assertInstanceOf(Model\TransactionCollection::class, $transactionCollection); @@ -257,10 +255,10 @@ public function testVoid(Model\Transaction $transaction) $organizationId = UserSettings::getSandboxMerchantOrganizationId(); $locationId = UserSettings::getSandboxMerchantLocationId(); - $returnedTransaction = $client->useTransactions()->void( + $returnedTransaction = $client->useTransactions('sandbox')->void( $organizationId, $locationId, - $transaction + $transaction, ); $this->assertInstanceOf(Model\Transaction::class, $returnedTransaction); diff --git a/test/settings.php.dist b/test/settings.php.dist index e7e4e1c..352c177 100644 --- a/test/settings.php.dist +++ b/test/settings.php.dist @@ -26,17 +26,5 @@ function get_forte_settings(): array 'debug' => false, ], ], - 'override_sub_resource_environments' => [ - 'address' => 'livetest', - 'application' => 'livetest', - 'customer' => 'livetest', - 'dispute' => 'livetest', - 'document' => 'livetest', - 'funding' => 'livetest', - 'pay_method' => 'livetest', - 'schedule_item' => 'livetest', - 'settlement' => 'livetest', - 'transaction' => null, - ], ]; }