Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cria clase para ter acesso ao servidor do PagSeguro
Adiciona tambem uma classe para gerar uma sessao valida
- Loading branch information
1 parent
b6b5a8e
commit a269772
Showing
4 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace ValdeirPsr\PagSeguro\Request; | ||
|
||
use Curl\Curl; | ||
use ValdeirPsr\PagSeguro\Domains\Environment; | ||
|
||
class Factory | ||
{ | ||
private const URL_SANDBOX = 'https://ws.sandbox.pagseguro.uol.com.br/'; | ||
private const URL_PRODUCTION = 'https://ws.pagseguro.uol.com.br/'; | ||
|
||
public const USER_AGENT = 'PagSeguro SDK for OpenCart v1.0.0'; | ||
|
||
/** | ||
* Cria classe para requisição | ||
* | ||
* @param Environment $env | ||
* | ||
* @return Curl | ||
*/ | ||
public static function request(Environment $env): Curl | ||
{ | ||
$instance = new Curl(); | ||
$instance->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) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace ValdeirPsr\PagSeguro\Request; | ||
|
||
use DOMDocument; | ||
use ValdeirPsr\PagSeguro\Domains\Environment; | ||
use ValdeirPsr\PagSeguro\Exception\Auth as AuthException; | ||
|
||
class Session | ||
{ | ||
private $env; | ||
|
||
public function __construct(Environment $env) | ||
{ | ||
$this->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() | ||
]); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
upload/system/library/PagSeguro/tests/unit/Request/FactoryTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use ValdeirPsr\PagSeguro\Domains\Environment; | ||
use ValdeirPsr\PagSeguro\Request\Factory; | ||
use Curl\Curl; | ||
|
||
class FactoryTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function newInstance() | ||
{ | ||
$env = Environment::sandbox('pagseguro@valdeir.dev', '1234567890'); | ||
$instance = Factory::request($env); | ||
|
||
$this->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); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
upload/system/library/PagSeguro/tests/unit/Request/SessionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use ValdeirPsr\PagSeguro\Domains\Environment; | ||
use ValdeirPsr\PagSeguro\Request\Factory; | ||
use ValdeirPsr\PagSeguro\Request\Session; | ||
use ValdeirPsr\PagSeguro\Exception\Auth as AuthException; | ||
|
||
class SessionTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function newInstance() | ||
{ | ||
$env = Environment::sandbox('pagseguro@valdeir.dev', '1234567890'); | ||
$instance = new Session($env); | ||
|
||
$this->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(); | ||
} | ||
} |