/** * Returns information about the in-app product specified * @param string $productId product identifier * @return Resource product purchase resource instance * @throws ErrorException when API error acquired * @throws RuntimeException when type code for client is not supported now */ public function get($productId) { $accessToken = $this->getAccessToken(); if (!empty($accessToken)) { $Request = $this->getRequest(self::ENDPOINT_PURCHASES)->addUrlField('applications', $this->getPackage())->addUrlField('inappproducts', $productId)->addGetField('access_token', $accessToken); try { return Resource::initializeByString($Request->send()); } catch (HttpClientErrorCodeException $Ex) { switch ($Ex->getCode()) { case 401: throw InvalidCredentialsException::initializeByString($Ex->getMessage()); default: throw ErrorException::initializeByString($Ex->getMessage()); } } catch (HttpServerErrorCodeException $Ex) { throw ErrorException::initializeByString($Ex->getMessage()); } } else { throw new UnexpectedValueException('access token is empty'); } }
/** * @inheritdoc */ public function get($productId, $token) { $accessToken = $this->getAccessToken(); if (!empty($accessToken)) { $Request = $this->getRequest(self::ENDPOINT_PURCHASES)->addUrlField('applications', $this->getPackage())->addUrlField('purchases', '')->addUrlField($this->getPurchasesUri(), $productId)->addUrlField('tokens', $token)->addGetField('access_token', $accessToken); try { switch ($this->typeCode) { case self::TYPE_PRODUCTS: return ProductResource::initializeByString($Request->send()); case self::TYPE_SUBSCRIPTIONS: return SubscriptionResource::initializeByString($Request->send()); default: throw new RuntimeException(sprintf('type code %s not supported now', $this->typeCode)); } } catch (HttpClientErrorCodeException $Ex) { switch ($Ex->getCode()) { case 401: throw InvalidCredentialsException::initializeByString($Ex->getMessage()); case 404: throw NotFoundException::initializeByString($Ex->getMessage()); default: throw ErrorException::initializeByString($Ex->getMessage()); } } catch (HttpServerErrorCodeException $Ex) { throw ErrorException::initializeByString($Ex->getMessage()); } } else { throw new UnexpectedValueException('access token is empty'); } }
/** * Send command for Purchases Subscriptions API * @param Request $Request request instance * @return string API response body * @throws InvalidCredentialsException when access grants is not granted * @throws ErrorException when API error acquired * @throws TransportException when HTTP transport error occurred * @throws UnexpectedValueException when access token is empty for client */ private function makeRequest(Request $Request) { $accessToken = $this->getAccessToken(); if (!empty($accessToken)) { try { return $Request->send(); } catch (HttpClientErrorCodeException $Ex) { switch ($Ex->getCode()) { case 401: throw InvalidCredentialsException::initializeByString($Ex->getMessage()); default: throw ErrorException::initializeByString($Ex->getMessage()); } } catch (HttpServerErrorCodeException $Ex) { throw ErrorException::initializeByString($Ex->getMessage()); } } else { throw new UnexpectedValueException('access token is empty'); } }
public function testInheritedExceptions() { $Exception1 = InvalidCredentialsException::initializeByString('{ "error": { "errors": [ { "domain": "global", "reason": "authError", "message": "Invalid Credentials", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Invalid Credentials" } }'); $this->assertInstanceOf(InvalidCredentialsException::class, $Exception1); $this->assertCount(1, $Exception1->getErrors()); $this->assertInstanceOf(stdClass::class, $Exception1->getErrors()[0]); $this->assertEquals('401', $Exception1->getCode()); $this->assertEquals('Invalid Credentials', $Exception1->getMessage()); $Error1 = $Exception1->getErrors()[0]; $this->assertObjectHasAttribute('domain', $Error1); $this->assertObjectHasAttribute('location', $Error1); $this->assertObjectHasAttribute('locationType', $Error1); $this->assertObjectHasAttribute('message', $Error1); $this->assertObjectHasAttribute('reason', $Error1); $this->assertEquals('global', $Error1->domain); $this->assertEquals('Authorization', $Error1->location); $this->assertEquals('header', $Error1->locationType); $this->assertEquals('Invalid Credentials', $Error1->message); $this->assertEquals('authError', $Error1->reason); $Exception2 = NotFoundException::initializeByString('{ "error": { "errors": [ { "domain": "global", "reason": "applicationNotFound", "message": "No application was found for the given package name.", "locationType": "parameter", "location": "packageName" } ], "code": 404, "message": "No application was found for the given package name." } }'); $this->assertInstanceOf(NotFoundException::class, $Exception2); $this->assertCount(1, $Exception2->getErrors()); $this->assertInstanceOf(stdClass::class, $Exception2->getErrors()[0]); $this->assertEquals('404', $Exception2->getCode()); $this->assertEquals('No application was found for the given package name.', $Exception2->getMessage()); $Error2 = $Exception2->getErrors()[0]; $this->assertObjectHasAttribute('domain', $Error2); $this->assertObjectHasAttribute('location', $Error2); $this->assertObjectHasAttribute('locationType', $Error2); $this->assertObjectHasAttribute('reason', $Error2); $this->assertObjectHasAttribute('message', $Error2); $this->assertEquals('global', $Error2->domain); $this->assertEquals('packageName', $Error2->location); $this->assertEquals('parameter', $Error2->locationType); $this->assertEquals('No application was found for the given package name.', $Error2->message); $this->assertEquals('applicationNotFound', $Error2->reason); $Exception3 = NotFoundException::initializeByString('Not Found'); $this->assertInstanceOf(NotFoundException::class, $Exception3); $this->assertCount(0, $Exception3->getErrors()); $this->assertEquals('0', $Exception3->getCode()); $this->assertEquals('Not Found', $Exception3->getMessage()); $Exception3 = NotFoundException::initializeByString('{ "error": { "errors": [ { "domain": "global", "reason": "purchaseTokenNotFound", "message": "The purchase token was not found.", "locationType": "parameter", "location": "token" } ], "code": 404, "message": "The purchase token was not found." } }'); $this->assertInstanceOf(NotFoundException::class, $Exception3); $this->assertCount(1, $Exception3->getErrors()); $this->assertInstanceOf(stdClass::class, $Exception3->getErrors()[0]); $this->assertEquals('404', $Exception3->getCode()); $this->assertEquals('The purchase token was not found.', $Exception3->getMessage()); $Error3 = $Exception3->getErrors()[0]; $this->assertObjectHasAttribute('domain', $Error3); $this->assertObjectHasAttribute('location', $Error3); $this->assertObjectHasAttribute('locationType', $Error3); $this->assertObjectHasAttribute('reason', $Error3); $this->assertObjectHasAttribute('message', $Error3); $this->assertEquals('global', $Error3->domain); $this->assertEquals('token', $Error3->location); $this->assertEquals('parameter', $Error3->locationType); $this->assertEquals('The purchase token was not found.', $Error3->message); $this->assertEquals('purchaseTokenNotFound', $Error3->reason); $Exception3 = NotFoundException::initializeByString('Not Found'); $this->assertInstanceOf(NotFoundException::class, $Exception3); $this->assertCount(0, $Exception3->getErrors()); $this->assertEquals('0', $Exception3->getCode()); $this->assertEquals('Not Found', $Exception3->getMessage()); }