diff --git a/upload/system/library/PagSeguro/src/Request/Factory.php b/upload/system/library/PagSeguro/src/Request/Factory.php new file mode 100644 index 0000000..0a6373c --- /dev/null +++ b/upload/system/library/PagSeguro/src/Request/Factory.php @@ -0,0 +1,43 @@ +setUserAgent(self::USER_AGENT); + $instance->setOpt(CURLOPT_SSL_VERIFYPEER, !$env->isSandbox()); + + return $instance; + } + + /** + * Cria uma URL + */ + public static function url(Environment $env, string $path, array $parameters = []): string + { + return sprintf( + '%s%s?%s', + $env->isSandbox() ? self::URL_SANDBOX : self::URL_PRODUCTION, + ltrim($path, '/'), + http_build_query($parameters) + ); + } +} diff --git a/upload/system/library/PagSeguro/src/Request/Session.php b/upload/system/library/PagSeguro/src/Request/Session.php new file mode 100644 index 0000000..bad66c1 --- /dev/null +++ b/upload/system/library/PagSeguro/src/Request/Session.php @@ -0,0 +1,58 @@ +env = $env; + } + + /** + * Gera uma sessão + * + * @throws AuthException Caso as credenciais sejam inválidas + * + * @return string + */ + public function generate(): string + { + $url = $this->buildUrl(); + + $request = Factory::request($this->env); + $request->post($url); + $request->close(); + + if ($request->isSuccess()) { + $xml = $request->getResponse(); + $dom = new DOMDocument(); + $dom->loadXml($xml); + + $session = $dom->getElementsByTagName('id'); + + if ($session->count() > 0) { + return trim($session->item(0)->textContent); + } + } elseif ($request->getHttpStatus() === 401) { + throw new AuthException('Check your credentials', 1000); + } + } + + /** + * {@inheritDoc} + */ + protected function buildUrl(): string + { + return Factory::url($this->env, 'v2/sessions', [ + 'email' => $this->env->getEmail(), + 'token' => $this->env->getToken() + ]); + } +} diff --git a/upload/system/library/PagSeguro/tests/unit/Request/FactoryTest.php b/upload/system/library/PagSeguro/tests/unit/Request/FactoryTest.php new file mode 100644 index 0000000..f040574 --- /dev/null +++ b/upload/system/library/PagSeguro/tests/unit/Request/FactoryTest.php @@ -0,0 +1,38 @@ +assertInstanceOf(Curl::class, $instance); + $this->assertEquals('PagSeguro SDK for OpenCart v1.0.0', Factory::USER_AGENT); + } + + /** + * @test + */ + public function checkUrlBuilder() + { + $expected = 'https://ws.sandbox.pagseguro.uol.com.br/checkout/v2/installments.json?sessionId=0123456789&amount=1000.00&creditCardBrand=master'; + + $env = Environment::sandbox('pagseguro@valdeir.dev', '1234567890'); + $url = Factory::url($env, 'checkout/v2/installments.json', [ + 'sessionId' => '0123456789', + 'amount' => '1000.00', + 'creditCardBrand' => 'master' + ]); + + $this->assertEquals($expected, $url); + } +} diff --git a/upload/system/library/PagSeguro/tests/unit/Request/SessionTest.php b/upload/system/library/PagSeguro/tests/unit/Request/SessionTest.php new file mode 100644 index 0000000..66175a2 --- /dev/null +++ b/upload/system/library/PagSeguro/tests/unit/Request/SessionTest.php @@ -0,0 +1,62 @@ +assertInstanceOf(Session::class, $instance); + } + + /** + * @test + */ + public function generatingANewSessionWithValidDataShouldNotReturnAnError() + { + $env = Environment::sandbox('pagseguro@valdeir.dev', ''); + + $stub = $this->getMockBuilder(Session::class) + ->setConstructorArgs([$env]) + ->setMethods(['buildUrl']) + ->getMock(); + + $stub->expects($this->any()) + ->method('buildUrl') + ->willReturn('https://f3528d51-6219-4b80-8bd3-3ab112b8094f.mock.pstmn.io/v2/sessions/valid'); + + $result = $stub->generate(); + $this->assertNotNull($result); + } + + /** + * @test + */ + public function whenEnteringInvalidCredentialsShouldReturnAnError() + { + $this->expectException(AuthException::class); + + $env = Environment::sandbox('pagseguro@valdeir.dev', ''); + + $stub = $this->getMockBuilder(Session::class) + ->setConstructorArgs([$env]) + ->setMethods(['buildUrl']) + ->getMock(); + + $stub->expects($this->any()) + ->method('buildUrl') + ->willReturn('https://f3528d51-6219-4b80-8bd3-3ab112b8094f.mock.pstmn.io/v2/sessions/invalid'); + + $stub->generate(); + } +}