From abdf89f071488ce9baa7f45b49c937f1f94cd184 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dezs=C5=91=20Bicz=C3=B3?= Date: Wed, 23 Jan 2019 08:37:11 +0100 Subject: [PATCH] Send developer email instead of UUID. Fixes #36. --- .../Controller/AcceptedRatePlanController.php | 11 +++++++++++ .../DeveloperAcceptedRatePlanController.php | 16 ++++++++++++++++ .../AcceptedRatePlanControllerTestBase.php | 11 +++++++---- .../CompanyAcceptedRatePlanControllerTest.php | 15 +++++++++++++++ .../DeveloperAcceptedRatePlanControllerTest.php | 15 +++++++++++++++ 5 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/Api/Monetization/Controller/AcceptedRatePlanController.php b/src/Api/Monetization/Controller/AcceptedRatePlanController.php index f9257bfc..62d4e091 100644 --- a/src/Api/Monetization/Controller/AcceptedRatePlanController.php +++ b/src/Api/Monetization/Controller/AcceptedRatePlanController.php @@ -121,6 +121,7 @@ public function updateSubscription(AcceptedRatePlanInterface $acceptedRatePlan, if (null !== $waveTerminationCharge) { $tmp['waveTerminationCharge'] = $waveTerminationCharge ? 'true' : 'false'; } + $this->alterRequestPayload($tmp, $acceptedRatePlan); $payload = json_encode($tmp); // Update an existing entity. $response = $this->client->put($this->getEntityEndpointUri($acceptedRatePlan->id()), $payload); @@ -147,6 +148,16 @@ abstract protected function buildContextForEntityTransformerInCreate(): array; */ abstract protected function getAcceptedRatePlansEndpoint(): UriInterface; + /** + * Allows to alter payload before it gets sent to the API. + * + * @param array $payload + * API request payload. + */ + protected function alterRequestPayload(array &$payload, AcceptedRatePlanInterface $acceptedRatePlan): void + { + } + /** * Helper function for listing accepted rate plans. * diff --git a/src/Api/Monetization/Controller/DeveloperAcceptedRatePlanController.php b/src/Api/Monetization/Controller/DeveloperAcceptedRatePlanController.php index e169ec3f..48489ef6 100644 --- a/src/Api/Monetization/Controller/DeveloperAcceptedRatePlanController.php +++ b/src/Api/Monetization/Controller/DeveloperAcceptedRatePlanController.php @@ -18,6 +18,7 @@ namespace Apigee\Edge\Api\Monetization\Controller; +use Apigee\Edge\Api\Monetization\Entity\AcceptedRatePlanInterface; use Apigee\Edge\Api\Monetization\Entity\DeveloperAcceptedRatePlan; use Apigee\Edge\Api\Monetization\Normalizer\EntityNormalizer; use Apigee\Edge\ClientInterface; @@ -87,4 +88,19 @@ protected function getAcceptedRatePlansEndpoint(): UriInterface // https://apidocs.apigee.com/monetize/apis/get/organizations/%7Borg_name%7D/developers/%7Bdeveloper_id%7D/developer-accepted-rateplans return $this->client->getUriFactory()->createUri("/mint/organizations/{$this->organization}/developers/{$this->developer}/developer-accepted-rateplans"); } + + /** + * @inheritdoc + * + * @psalm-suppress UndefinedMethod - getDeveloper() exists on the annotated + * interface. + */ + protected function alterRequestPayload(array &$payload, AcceptedRatePlanInterface $acceptedRatePlan): void + { + /* @var \Apigee\Edge\Api\Monetization\Entity\DeveloperAcceptedRatePlanInterface $acceptedRatePlan */ + // We should prefer developer email addresses over developer ids + // (UUIDs) when we are communicating with the Monetization API. + // @see https://github.com/apigee/apigee-client-php/issues/36 + $payload['developer']['id'] = $acceptedRatePlan->getDeveloper()->getEmail(); + } } diff --git a/tests/Api/Monetization/Controller/AcceptedRatePlanControllerTestBase.php b/tests/Api/Monetization/Controller/AcceptedRatePlanControllerTestBase.php index 2c06d416..3e67ffc2 100644 --- a/tests/Api/Monetization/Controller/AcceptedRatePlanControllerTestBase.php +++ b/tests/Api/Monetization/Controller/AcceptedRatePlanControllerTestBase.php @@ -18,7 +18,7 @@ namespace Apigee\Edge\Tests\Api\Monetization\Controller; -use Apigee\Edge\Api\Monetization\Controller\RatePlanController; +use Apigee\Edge\Api\Monetization\Entity\RatePlanInterface; use Apigee\Edge\Tests\Api\Monetization\EntitySerializer\AcceptedRatePlanSerializerValidator; use Apigee\Edge\Tests\Test\Controller\MockClientAwareTrait; use Apigee\Edge\Tests\Test\EntitySerializer\EntitySerializerValidatorInterface; @@ -61,12 +61,10 @@ public function testGetPaginatedAcceptedRatePlanList(): void public function testAcceptRatePlan(): void { $httpClient = static::mockApiClient()->getMockHttpClient(); - /** @var \Apigee\Edge\Api\Monetization\Controller\RatePlanControllerInterface $ratePlanController */ - $ratePlanController = new RatePlanController('phpunit', static::defaultTestOrganization(static::defaultAPIClient()), static::defaultAPIClient()); /** @var \Apigee\Edge\Api\Monetization\Controller\AcceptedRatePlanControllerInterface $acceptedController */ $acceptedController = static::entityController(static::mockApiClient()); /** @var \Apigee\Edge\Api\Monetization\Entity\RatePlanInterface $ratePlan */ - $ratePlan = $ratePlanController->load('standard-rev'); + $ratePlan = $this->getRatePlanToAccept(); $startDate = new \DateTimeImmutable('now'); $response = $this->getAcceptRatePlanResponse(); $httpClient->addResponse($response); @@ -145,6 +143,11 @@ public function testUpdateSubscription(): void */ abstract protected function getAcceptRatePlanResponse(): ResponseInterface; + /** + * @return \Apigee\Edge\Api\Monetization\Entity\RatePlanInterface + */ + abstract protected function getRatePlanToAccept(): RatePlanInterface; + /** * @inheritdoc */ diff --git a/tests/Api/Monetization/Controller/CompanyAcceptedRatePlanControllerTest.php b/tests/Api/Monetization/Controller/CompanyAcceptedRatePlanControllerTest.php index e4d10e7e..65ceb9ed 100644 --- a/tests/Api/Monetization/Controller/CompanyAcceptedRatePlanControllerTest.php +++ b/tests/Api/Monetization/Controller/CompanyAcceptedRatePlanControllerTest.php @@ -19,6 +19,8 @@ namespace Apigee\Edge\Tests\Api\Monetization\Controller; use Apigee\Edge\Api\Monetization\Controller\CompanyAcceptedRatePlanController; +use Apigee\Edge\Api\Monetization\Controller\RatePlanController; +use Apigee\Edge\Api\Monetization\Entity\RatePlanInterface; use Apigee\Edge\ClientInterface; use Apigee\Edge\Tests\Test\Controller\EntityControllerTester; use Apigee\Edge\Tests\Test\Controller\EntityControllerTesterInterface; @@ -55,4 +57,17 @@ protected function getAcceptRatePlanResponse(): ResponseInterface return (new FileSystemResponseFactory())->createResponseForRequest(new Request('GET', "v1/mint/organizations/phpunit/companies/{$id}/developer-rateplans/phpunit")); } + + /** + * @inheritdoc + */ + protected function getRatePlanToAccept(): RatePlanInterface + { + /** @var \Apigee\Edge\Api\Monetization\Controller\RatePlanControllerInterface $ratePlanController */ + $ratePlanController = new RatePlanController('phpunit', static::defaultTestOrganization(static::defaultAPIClient()), static::defaultAPIClient()); + /** @var \Apigee\Edge\Api\Monetization\Entity\CompanyRatePlanInterface $ratePlan */ + $ratePlan = $ratePlanController->load('company-rev'); + + return $ratePlan; + } } diff --git a/tests/Api/Monetization/Controller/DeveloperAcceptedRatePlanControllerTest.php b/tests/Api/Monetization/Controller/DeveloperAcceptedRatePlanControllerTest.php index 86dc5edc..bbf8ec39 100644 --- a/tests/Api/Monetization/Controller/DeveloperAcceptedRatePlanControllerTest.php +++ b/tests/Api/Monetization/Controller/DeveloperAcceptedRatePlanControllerTest.php @@ -19,6 +19,8 @@ namespace Apigee\Edge\Tests\Api\Monetization\Controller; use Apigee\Edge\Api\Monetization\Controller\DeveloperAcceptedRatePlanController; +use Apigee\Edge\Api\Monetization\Controller\RatePlanController; +use Apigee\Edge\Api\Monetization\Entity\RatePlanInterface; use Apigee\Edge\ClientInterface; use Apigee\Edge\Tests\Test\Controller\EntityControllerTester; use Apigee\Edge\Tests\Test\Controller\EntityControllerTesterInterface; @@ -55,4 +57,17 @@ protected function getAcceptRatePlanResponse(): ResponseInterface return (new FileSystemResponseFactory())->createResponseForRequest(new Request('GET', "v1/mint/organizations/phpunit/developers/{$id}/developer-rateplans/phpunit")); } + + /** + * @inheritdoc + */ + protected function getRatePlanToAccept(): RatePlanInterface + { + /** @var \Apigee\Edge\Api\Monetization\Controller\RatePlanControllerInterface $ratePlanController */ + $ratePlanController = new RatePlanController('phpunit', static::defaultTestOrganization(static::defaultAPIClient()), static::defaultAPIClient()); + /** @var \Apigee\Edge\Api\Monetization\Entity\DeveloperRatePlanInterface $ratePlan */ + $ratePlan = $ratePlanController->load('developer-rev'); + + return $ratePlan; + } }