/** * 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 testMissingErrorFields() { $Exception1 = ErrorException::initializeByString('{ "error": { "code": 404, "message": "Not found" } }'); $this->assertInstanceOf(ErrorException::class, $Exception1); $this->assertEquals('404', $Exception1->getCode()); $this->assertEquals('Not found', $Exception1->getMessage()); $Exception2 = ErrorException::initializeByString('Not found'); $this->assertInstanceOf(ErrorException::class, $Exception2); $this->assertEquals('Not found', $Exception2->getMessage()); }