├── .gitignore
├── .scrutinizer.yml
├── .travis.yml
├── CONTRIBUTING.md
├── README.md
├── bootstrap.php
├── composer.json
├── composer.lock
├── phpunit.xml.dist
├── src
├── Client
│ ├── Client.php
│ └── PagSeguroException.php
├── Credentials.php
├── Customer
│ ├── Address.php
│ ├── Customer.php
│ └── Phone.php
├── Environment.php
├── Environments
│ ├── Production.php
│ └── Sandbox.php
├── Items
│ ├── Item.php
│ ├── ItemCollection.php
│ └── Items.php
├── Purchases
│ ├── ChargeBuilder.php
│ ├── Decoder.php
│ ├── Details.php
│ ├── NotificationService.php
│ ├── SearchService.php
│ ├── SubscriptionService.php
│ ├── Subscriptions
│ │ ├── CancellationResponse.php
│ │ ├── Charge.php
│ │ ├── ChargeBuilder.php
│ │ ├── ChargeResponse.php
│ │ ├── Decoder.php
│ │ ├── Locator.php
│ │ ├── Subscription.php
│ │ └── SubscriptionService.php
│ └── Transactions
│ │ ├── Decoder.php
│ │ ├── Locator.php
│ │ ├── Payment.php
│ │ ├── PaymentMethod.php
│ │ └── Transaction.php
├── Requests
│ ├── Checkout
│ │ ├── Checkout.php
│ │ ├── CheckoutBuilder.php
│ │ ├── CheckoutService.php
│ │ └── Order.php
│ ├── CheckoutBuilder.php
│ ├── CheckoutService.php
│ ├── PreApprovalService.php
│ ├── PreApprovals
│ │ ├── ChargeType.php
│ │ ├── Period.php
│ │ ├── PreApproval.php
│ │ ├── PreApprovalService.php
│ │ ├── Request.php
│ │ └── RequestBuilder.php
│ ├── Redirection.php
│ ├── RequestBuilder.php
│ └── Service.php
├── SerializerTrait.php
├── Service.php
└── Shipping
│ ├── Shipping.php
│ └── Type.php
└── tests
├── Client
├── ClientTest.php
└── PagSeguroExceptionTest.php
├── CredentialsTest.php
├── Customer
├── AddressTest.php
├── CustomerTest.php
└── PhoneTest.php
├── EnvironmentTest.php
├── Environments
├── ProductionTest.php
└── SandboxTest.php
├── Items
├── ItemTest.php
└── ItemsTest.php
├── Purchases
├── DetailsTest.php
├── Subscriptions
│ ├── CancellationResponseTest.php
│ ├── ChargeBuilderTest.php
│ ├── ChargeResponseTest.php
│ ├── ChargeTest.php
│ ├── DecoderTest.php
│ ├── LocatorTest.php
│ ├── SubscriptionServiceTest.php
│ ├── SubscriptionTest.php
│ └── xml
│ │ ├── chargeFull.xml
│ │ └── preApproval.xml
└── Transactions
│ ├── DecoderTest.php
│ ├── LocatorTest.php
│ ├── PaymentMethodTest.php
│ ├── PaymentTest.php
│ ├── TransactionTest.php
│ └── xml
│ └── transactionFull.xml
├── Requests
├── Checkout
│ ├── CheckoutBuilderTest.php
│ ├── CheckoutServiceTest.php
│ ├── CheckoutTest.php
│ ├── OrderTest.php
│ └── xml
│ │ ├── checkoutCustomer.xml
│ │ ├── checkoutEmpty.xml
│ │ └── checkoutFull.xml
├── PreApprovals
│ ├── ChargeTypeTest.php
│ ├── PeriodTypeTest.php
│ ├── PreApprovalServiceTest.php
│ ├── PreApprovalTest.php
│ ├── RequestBuilderTest.php
│ ├── RequestTest.php
│ └── xml
│ │ └── preAprovalsRequestFull.xml
└── RedirectionTest.php
├── ServiceTest.php
└── Shipping
├── ShippingTest.php
└── TypeTest.php
/.gitignore:
--------------------------------------------------------------------------------
1 | /vendor
2 | /composer.phar
3 |
--------------------------------------------------------------------------------
/.scrutinizer.yml:
--------------------------------------------------------------------------------
1 | build:
2 | environment:
3 | mysql: false
4 | redis: false
5 | postgresql: false
6 | php:
7 | version: 7.1
8 | tools:
9 | external_code_coverage:
10 | timeout: 300
11 | php_sim: true
12 | php_pdepend: true
13 | php_analyzer: true
14 | php_changetracking: true
15 | php_code_sniffer:
16 | enabled: true
17 | filter:
18 | paths: ["src", "tests"]
19 | config:
20 | standard: PSR2
21 | php_mess_detector:
22 | enabled: true
23 | filter:
24 | paths: ["src", "tests"]
25 | config:
26 | code_size_rules:
27 | cyclomatic_complexity: true
28 | npath_complexity: true
29 | excessive_method_length: true
30 | excessive_class_length: true
31 | excessive_parameter_list: true
32 | excessive_public_count: true
33 | too_many_fields: true
34 | too_many_methods: true
35 | excessive_class_complexity: true
36 | design_rules:
37 | exit_expression: true
38 | eval_expression: true
39 | goto_statement: true
40 | number_of_class_children: true
41 | depth_of_inheritance: true
42 | coupling_between_objects: true
43 | unused_code_rules:
44 | unused_private_field: true
45 | unused_local_variable: true
46 | unused_private_method: true
47 | unused_formal_parameter: true
48 | naming_rules:
49 | short_variable:
50 | minimum: 2
51 | constructor_conflict: true
52 | constant_naming: true
53 | boolean_method_name: true
54 | controversial_rules:
55 | superglobals: true
56 | camel_case_class_name: true
57 | camel_case_property_name: true
58 | camel_case_method_name: true
59 | camel_case_parameter_name: true
60 | camel_case_variable_name: true
61 | checks:
62 | php:
63 | argument_type_checks: true
64 | assignment_of_null_return: true
65 | avoid_conflicting_incrementers: true
66 | avoid_useless_overridden_methods: true
67 | catch_class_exists: true
68 | closure_use_modifiable: true
69 | closure_use_not_conflicting: true
70 | code_rating: true
71 | deprecated_code_usage: true
72 | duplication: true
73 | method_calls_on_non_object: true
74 | missing_arguments: true
75 | no_duplicate_arguments: true
76 | no_non_implemented_abstract_methods: true
77 | no_property_on_interface: true
78 | parameter_non_unique: true
79 | precedence_in_conditions: true
80 | precedence_mistakes: true
81 | require_php_tag_first: true
82 | security_vulnerabilities: true
83 | sql_injection_vulnerabilities: true
84 | too_many_arguments: true
85 | unreachable_code: true
86 | unused_methods: true
87 | unused_parameters: true
88 | unused_properties: true
89 | unused_variables: true
90 | use_statement_alias_conflict: true
91 | useless_calls: true
92 | variable_existence: true
93 | verify_access_scope_valid: true
94 | verify_argument_usable_as_reference: true
95 | verify_property_names: true
96 |
97 | filter:
98 | excluded_paths:
99 | - tests/*
100 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: php
2 | sudo: false
3 |
4 | php:
5 | - 5.6
6 | - 7.3
7 |
8 | matrix:
9 | fast_finish: true
10 | allow_failures:
11 | - php: hhvm
12 |
13 | before_script:
14 | - composer self-update
15 | - composer install
16 | - if [[ $TRAVIS_PHP_VERSION = '7.3' ]]; then PHPUNIT_FLAGS="--coverage-clover ./clover.xml"; else PHPUNIT_FLAGS=""; fi
17 |
18 | script:
19 | - ./vendor/bin/phpunit $PHPUNIT_FLAGS
20 |
21 | after_script:
22 | - wget https://scrutinizer-ci.com/ocular.phar
23 | - if [ -f clover.xml ]; then php ocular.phar code-coverage:upload --format=php-clover ./clover.xml; fi
24 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # Contribuindo
2 |
3 | Você que chegou aqui com o objetivo de contribuir com este projeto, nós do PHP Santa Catarina agradecemos!
4 |
5 | Testes de unidades, documentação, novas funcionalidades, sugestões... temos muitas
6 | áreas que com certeza você tem plenas possibilidades de nos ajudar! Aliás, você
7 | já conferiu nossa lista de [tarefas](https://github.com/PHPSC/pagseguro/issues)?
8 |
9 | ## Ferramentas básicas
10 |
11 | Antes de explicarmos a estrutura básica do projeto é necessario conhecermos
12 | as ferramentas fundamentais que utilizamos para organizar o desenvolvimento.
13 |
14 | ### git-flow
15 |
16 | A partir [deste post](http://nvie.com/posts/a-successful-git-branching-model)
17 | surgiu uma extensão para o git, o [git-flow](https://github.com/nvie/gitflow).
18 |
19 | Esta ferramenta é extremamente útil para agilizar o desenvolvimento usando
20 | branchs.
21 |
22 | Leia o [post](http://nvie.com/posts/a-successful-git-branching-model) e instale
23 | a ferramenta para poder acompanhar as explicações que virão mais abaixo. Utilize também o [cheatsheet do git-flow](http://danielkummer.github.io/git-flow-cheatsheet/index.pt_BR.html) como material de auxílio.
24 |
25 | ### Composer
26 |
27 | O [composer](http://getcomposer.org) é uma ferramenta de gerenciamento de
28 | dependências de projetos PHP. Com ele é possível realizar a instalação de
29 | todas as bibliotecas que o software necessita utilizando um único comando.
30 |
31 | Recomendamos também a leitura dos slides [desta palestra](http://www.slideshare.net/rdohms/composer-for-busy-developers-dpc13)
32 | do nosso amigo [Rafael Dohms](https://github.com/rdohms).
33 |
34 | ## Code style
35 |
36 | Todas as alterações DEVEM seguir a [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md).
37 |
38 | ## Preparando o terreno
39 |
40 | Você quer realizar alterações no código então? Muito bem, para
41 | começar a brincadeira você deve seguir os passos abaixo:
42 |
43 | 1. Crie um [fork](https://help.github.com/articles/fork-a-repo) e clone o projeto;
44 | 1. Instale as dependências utilizando o composer (```composer install```);
45 | 1. Defina as permissões de diretórios com o phing (```vendor/bin/phing post-install```);
46 | 1. Aponte o branch local master para origin/master (```git branch master origin/master```);
47 | 1. Inicialize o git-flow (```git flow init -d```);
48 | 1. Crie seu branch
49 | * Caso seja uma nova funcionalidade você deve executar o
50 | comando ```git flow feature start $feature```, onde $feature
51 | é um identificador para a funcionalidade a ser implementada;
52 | * Caso você opte por corrigir um bug você deverá inicializar
53 | um hotfix branch ```git flow hotfix start $hotfix```, onde
54 | $hotfix deverá ser a versão atual, incrementando o número
55 | da última posição (PATCH).
56 |
57 | Se tiver dúvidas a respeito do versionamento, leia [esta especificação](https://github.com/wendtecnologia/semver/blob/2.0.0/semver.pt-br.md) ([ou sua versão original, em inglês](http://semver.org/spec/v2.0.0.html)).
58 |
--------------------------------------------------------------------------------
/bootstrap.php:
--------------------------------------------------------------------------------
1 | =5.6",
26 | "guzzlehttp/guzzle": "~6.2",
27 | "doctrine/collections": "~1.3",
28 | "jms/serializer":"~1.1"
29 | },
30 | "require-dev": {
31 | "phpunit/phpunit": "~5.5",
32 | "squizlabs/php_codesniffer": "~3.0",
33 | "pdepend/pdepend": "~2.2",
34 | "phpmd/phpmd": "~2.4",
35 | "sebastian/phpcpd": "~3.0"
36 | },
37 | "autoload": {
38 | "psr-4": {
39 | "PHPSC\\PagSeguro\\": "src"
40 | }
41 | },
42 | "autoload-dev": {
43 | "psr-4": {
44 | "PHPSC\\PagSeguro\\": "test"
45 | }
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/phpunit.xml.dist:
--------------------------------------------------------------------------------
1 |
2 |
13 |
14 |
15 | ./tests
16 |
17 |
18 |
19 |
20 |
21 | src
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/src/Client/Client.php:
--------------------------------------------------------------------------------
1 |
11 | */
12 | class Client
13 | {
14 | private $client;
15 |
16 | /**
17 | * @param ClientInterface $client
18 | */
19 | public function __construct(ClientInterface $client = null)
20 | {
21 | $this->client = $client ?: new HttpClient();
22 | }
23 |
24 | /**
25 | * @param string $url
26 | * @param SimpleXMLElement $body
27 | *
28 | * @return SimpleXMLElement
29 | */
30 | public function post($url, SimpleXMLElement $body)
31 | {
32 | try {
33 | $response = $this->client->request(
34 | 'POST',
35 | $url,
36 | [
37 | 'headers' => ['Content-Type' => 'application/xml; charset=UTF-8'],
38 | 'body' => $body->asXML(),
39 | 'verify' => false
40 | ]
41 | );
42 |
43 | return new SimpleXMLElement($response->getBody());
44 | } catch (RequestException $e) {
45 | throw PagSeguroException::create($e->getResponse(), $e);
46 | }
47 | }
48 |
49 | /**
50 | * @param string $url
51 | *
52 | * @return SimpleXMLElement
53 | */
54 | public function get($url)
55 | {
56 | try {
57 | $response = $this->client->request('GET', $url, ['verify' => false]);
58 |
59 | return new SimpleXMLElement($response->getBody());
60 | } catch (RequestException $e) {
61 | throw PagSeguroException::create($e->getResponse(), $e);
62 | }
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/src/Client/PagSeguroException.php:
--------------------------------------------------------------------------------
1 | getStatusCode() !== 400) {
27 | return '[' . $response->getStatusCode() . '] A HTTP error has occurred: ' . $response->getBody();
28 | }
29 |
30 | $message = 'Some errors occurred:';
31 | $content = new \SimpleXMLElement($response->getBody()->getContents());
32 |
33 | foreach ($content->error as $error) {
34 | $message .= PHP_EOL . '[' . (string) $error->code . '] ' . (string) $error->message;
35 | }
36 |
37 | return $message;
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/Credentials.php:
--------------------------------------------------------------------------------
1 |
9 | */
10 | class Credentials
11 | {
12 | /**
13 | * @var string
14 | */
15 | private $email;
16 |
17 | /**
18 | * @var string
19 | */
20 | private $token;
21 |
22 | /**
23 | * @var Environment
24 | */
25 | private $environment;
26 |
27 | /**
28 | * @param string $email
29 | * @param string $token
30 | * @param Environment $environment
31 | */
32 | public function __construct($email, $token, Environment $environment = null)
33 | {
34 | $this->email = substr($email, 0, 60);
35 | $this->token = substr($token, 0, 32);
36 | $this->environment = $environment ?: new Production();
37 | }
38 |
39 | /**
40 | * @param string $resource
41 | *
42 | * @return string
43 | */
44 | public function getUrl($resource)
45 | {
46 | return $this->environment->getUrl($resource);
47 | }
48 |
49 | /**
50 | * @param string $resource
51 | * @param array $params
52 | *
53 | * @return string
54 | */
55 | public function getWsUrl($resource, array $params = [])
56 | {
57 | $params = array_merge(
58 | $params,
59 | ['email' => $this->email, 'token' => $this->token]
60 | );
61 |
62 | return sprintf(
63 | '%s?%s',
64 | $this->environment->getWsUrl($resource),
65 | http_build_query($params)
66 | );
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/src/Customer/Address.php:
--------------------------------------------------------------------------------
1 |
13 | */
14 | class Address
15 | {
16 | use SerializerTrait;
17 |
18 | /**
19 | * @var string
20 | */
21 | private $country;
22 |
23 | /**
24 | * @var string
25 | */
26 | private $state;
27 |
28 | /**
29 | * @var string
30 | */
31 | private $city;
32 |
33 | /**
34 | * @var string
35 | */
36 | private $postalCode;
37 |
38 | /**
39 | * @var string
40 | */
41 | private $district;
42 |
43 | /**
44 | * @var string
45 | */
46 | private $street;
47 |
48 | /**
49 | * @var string
50 | */
51 | private $number;
52 |
53 | /**
54 | * @var string
55 | */
56 | private $complement;
57 |
58 | /**
59 | * @param string $state
60 | * @param string $city
61 | * @param string $postalCode
62 | * @param string $district
63 | * @param string $street
64 | * @param string $number
65 | * @param string $complement
66 | */
67 | public function __construct(
68 | $state,
69 | $city,
70 | $postalCode,
71 | $district,
72 | $street,
73 | $number,
74 | $complement = null
75 | ) {
76 | $this->country = 'BRA';
77 | $this->state = (string) $state;
78 | $this->city = (string) $city;
79 | $this->postalCode = preg_replace('/[^0-9]/', '', (string) $postalCode);
80 | $this->district = (string) $district;
81 | $this->street = (string) $street;
82 | $this->number = (string) $number;
83 |
84 | if (!empty($complement)) {
85 | $this->complement = (string) $complement;
86 | }
87 | }
88 |
89 | /**
90 | * @return string
91 | */
92 | public function getCountry()
93 | {
94 | return $this->country;
95 | }
96 |
97 | /**
98 | * @return string
99 | */
100 | public function getState()
101 | {
102 | return $this->state;
103 | }
104 |
105 | /**
106 | * @return string
107 | */
108 | public function getCity()
109 | {
110 | return $this->city;
111 | }
112 |
113 | /**
114 | * @return string
115 | */
116 | public function getPostalCode()
117 | {
118 | return $this->postalCode;
119 | }
120 |
121 | /**
122 | * @return string
123 | */
124 | public function getDistrict()
125 | {
126 | return $this->district;
127 | }
128 |
129 | /**
130 | * @return string
131 | */
132 | public function getStreet()
133 | {
134 | return $this->street;
135 | }
136 |
137 | /**
138 | * @return string
139 | */
140 | public function getNumber()
141 | {
142 | return $this->number;
143 | }
144 |
145 | /**
146 | * @return string
147 | */
148 | public function getComplement()
149 | {
150 | return $this->complement;
151 | }
152 | }
153 |
--------------------------------------------------------------------------------
/src/Customer/Customer.php:
--------------------------------------------------------------------------------
1 |
13 | */
14 | class Customer
15 | {
16 | use SerializerTrait;
17 |
18 | /**
19 | * @Serializer\XmlElement(cdata=false)
20 | *
21 | * @var string
22 | */
23 | private $email;
24 |
25 | /**
26 | * @Serializer\XmlElement(cdata=false)
27 | *
28 | * @var string
29 | */
30 | private $name;
31 |
32 | /**
33 | * @Serializer\Type("PHPSC\PagSeguro\Customer\Phone")
34 | *
35 | * @var Phone
36 | */
37 | private $phone;
38 |
39 | /**
40 | * @Serializer\Type("PHPSC\PagSeguro\Customer\Address")
41 | *
42 | * @var Address
43 | */
44 | private $address;
45 |
46 | /**
47 | * @param string $email
48 | * @param string $name
49 | * @param Phone $phone
50 | * @param Address $address
51 | */
52 | public function __construct(
53 | $email,
54 | $name = null,
55 | Phone $phone = null,
56 | Address $address = null
57 | ) {
58 | $this->email = $email;
59 | $this->name = $name;
60 | $this->phone = $phone;
61 | $this->address = $address;
62 | }
63 |
64 | /**
65 | * @return string
66 | */
67 | public function getEmail()
68 | {
69 | return $this->email;
70 | }
71 |
72 | /**
73 | * @return string
74 | */
75 | public function getName()
76 | {
77 | return $this->name;
78 | }
79 |
80 | /**
81 | * @return Phone
82 | */
83 | public function getPhone()
84 | {
85 | return $this->phone;
86 | }
87 |
88 | /**
89 | * @return Address
90 | */
91 | public function getAddress()
92 | {
93 | return $this->address;
94 | }
95 | }
96 |
--------------------------------------------------------------------------------
/src/Customer/Phone.php:
--------------------------------------------------------------------------------
1 |
13 | */
14 | class Phone
15 | {
16 | use SerializerTrait;
17 |
18 | /**
19 | * @var string
20 | */
21 | private $areaCode;
22 |
23 | /**
24 | * @var string
25 | */
26 | private $number;
27 |
28 | /**
29 | * @param string $areaCode
30 | * @param string $number
31 | */
32 | public function __construct($areaCode, $number)
33 | {
34 | $this->areaCode = $areaCode;
35 | $this->number = $number;
36 | }
37 |
38 | /**
39 | * @return string
40 | */
41 | public function getAreaCode()
42 | {
43 | return $this->areaCode;
44 | }
45 |
46 | /**
47 | * @return string
48 | */
49 | public function getNumber()
50 | {
51 | return $this->number;
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/Environment.php:
--------------------------------------------------------------------------------
1 |
9 | */
10 | abstract class Environment
11 | {
12 | /**
13 | * @param string $host
14 | *
15 | * @return boolean
16 | */
17 | public static function isValid($host)
18 | {
19 | return in_array($host, [Production::WS_HOST, Sandbox::WS_HOST]);
20 | }
21 |
22 | /**
23 | * @param string $resource
24 | *
25 | * @return string
26 | */
27 | public function getWsUrl($resource)
28 | {
29 | return 'https://' . $this->getWsHost() . $resource;
30 | }
31 |
32 | /**
33 | * @param string $resource
34 | *
35 | * @return string
36 | */
37 | public function getUrl($resource)
38 | {
39 | return 'https://' . $this->getHost() . $resource;
40 | }
41 |
42 | /**
43 | * @return string
44 | */
45 | abstract public function getWsHost();
46 |
47 | /**
48 | * @return string
49 | */
50 | abstract public function getHost();
51 | }
52 |
--------------------------------------------------------------------------------
/src/Environments/Production.php:
--------------------------------------------------------------------------------
1 |
8 | */
9 | class Production extends Environment
10 | {
11 | const HOST = 'pagseguro.uol.com.br';
12 |
13 | const WS_HOST = 'ws.pagseguro.uol.com.br';
14 |
15 | /**
16 | * {@inheritdoc}
17 | */
18 | public function getHost()
19 | {
20 | return static::HOST;
21 | }
22 |
23 | /**
24 | * {@inheritdoc}
25 | */
26 | public function getWsHost()
27 | {
28 | return static::WS_HOST;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/Environments/Sandbox.php:
--------------------------------------------------------------------------------
1 |
8 | */
9 | class Sandbox extends Environment
10 | {
11 | const HOST = 'sandbox.pagseguro.uol.com.br';
12 |
13 | const WS_HOST = 'ws.sandbox.pagseguro.uol.com.br';
14 |
15 | /**
16 | * {@inheritdoc}
17 | */
18 | public function getHost()
19 | {
20 | return static::HOST;
21 | }
22 |
23 | /**
24 | * {@inheritdoc}
25 | */
26 | public function getWsHost()
27 | {
28 | return static::WS_HOST;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/Items/Item.php:
--------------------------------------------------------------------------------
1 |
13 | */
14 | class Item
15 | {
16 | use SerializerTrait;
17 |
18 | /**
19 | * @Serializer\XmlElement(cdata=false)
20 | *
21 | * @var string
22 | */
23 | private $id;
24 |
25 | /**
26 | * @Serializer\XmlElement(cdata=false)
27 | *
28 | * @var string
29 | */
30 | private $description;
31 |
32 | /**
33 | * @Serializer\XmlElement(cdata=false)
34 | *
35 | * @var float
36 | */
37 | private $amount;
38 |
39 | /**
40 | * @Serializer\Type("integer")
41 | *
42 | * @var int
43 | */
44 | private $quantity;
45 |
46 | /**
47 | * @Serializer\XmlElement(cdata=false)
48 | *
49 | * @var float
50 | */
51 | private $shippingCost;
52 |
53 | /**
54 | * @Serializer\Type("integer")
55 | *
56 | * @var int
57 | */
58 | private $weight;
59 |
60 | /**
61 | * @param string $id
62 | * @param string $description
63 | * @param float $amount
64 | * @param int $quantity
65 | * @param float $shippingCost
66 | * @param int $weight
67 | */
68 | public function __construct(
69 | $id,
70 | $description,
71 | $amount,
72 | $quantity = 1,
73 | $shippingCost = null,
74 | $weight = null
75 | ) {
76 | $this->id = $id;
77 | $this->description = $description;
78 | $this->amount = $amount;
79 | $this->quantity = $quantity;
80 | $this->shippingCost = $shippingCost;
81 | $this->weight = $weight;
82 | }
83 |
84 | /**
85 | * @return string
86 | */
87 | public function getId()
88 | {
89 | return $this->id;
90 | }
91 |
92 | /**
93 | * @return string
94 | */
95 | public function getDescription()
96 | {
97 | return $this->description;
98 | }
99 |
100 | /**
101 | * @return string
102 | */
103 | public function getAmount()
104 | {
105 | return $this->formatAmount($this->amount);
106 | }
107 |
108 | /**
109 | * @return number
110 | */
111 | public function getQuantity()
112 | {
113 | return $this->quantity;
114 | }
115 |
116 | /**
117 | * @return string
118 | */
119 | public function getShippingCost()
120 | {
121 | return $this->formatAmount($this->shippingCost);
122 | }
123 |
124 | /**
125 | * @return number
126 | */
127 | public function getWeight()
128 | {
129 | return $this->weight;
130 | }
131 | }
132 |
--------------------------------------------------------------------------------
/src/Items/ItemCollection.php:
--------------------------------------------------------------------------------
1 |
8 | */
9 | interface ItemCollection extends Collection
10 | {
11 | }
12 |
--------------------------------------------------------------------------------
/src/Items/Items.php:
--------------------------------------------------------------------------------
1 |
8 | */
9 | class Items extends ArrayCollection implements ItemCollection
10 | {
11 | }
12 |
--------------------------------------------------------------------------------
/src/Purchases/ChargeBuilder.php:
--------------------------------------------------------------------------------
1 |
9 | */
10 | interface ChargeBuilder
11 | {
12 | /**
13 | * @param Item $item
14 | *
15 | * @return self
16 | */
17 | public function addItem(Item $item);
18 |
19 | /**
20 | * @param string $reference
21 | *
22 | * @return self
23 | */
24 | public function setReference($reference);
25 |
26 | /**
27 | * @return Charge
28 | */
29 | public function getCharge();
30 | }
31 |
--------------------------------------------------------------------------------
/src/Purchases/Decoder.php:
--------------------------------------------------------------------------------
1 |
12 | */
13 | abstract class Decoder
14 | {
15 | /**
16 | * @param SimpleXMLElement $obj
17 | *
18 | * @return Details
19 | */
20 | protected function createDetails(SimpleXMLElement $obj)
21 | {
22 | return new Details(
23 | (string) $obj->code,
24 | isset($obj->reference) ? (string) $obj->reference : null,
25 | (string) $obj->status,
26 | new DateTime((string) $obj->date),
27 | new DateTime((string) $obj->lastEventDate),
28 | $this->createCustomer($obj->sender)
29 | );
30 | }
31 |
32 | /**
33 | * @param SimpleXMLElement $address
34 | *
35 | * @return Address
36 | */
37 | protected function createAddress(SimpleXMLElement $address)
38 | {
39 | return new Address(
40 | (string) $address->state,
41 | (string) $address->city,
42 | (string) $address->postalCode,
43 | (string) $address->district,
44 | (string) $address->street,
45 | (string) $address->number,
46 | (string) $address->complement
47 | );
48 | }
49 |
50 | /**
51 | * @param SimpleXMLElement $customer
52 | *
53 | * @return Customer
54 | */
55 | protected function createCustomer(SimpleXMLElement $customer)
56 | {
57 | $phone = null;
58 |
59 | if (isset($customer->phone)) {
60 | $phone = new Phone(
61 | (string) $customer->phone->areaCode,
62 | (string) $customer->phone->number
63 | );
64 | }
65 |
66 | return new Customer(
67 | (string) $customer->email,
68 | isset($customer->name) ? (string) $customer->name : null,
69 | $phone,
70 | isset($customer->address) ? $this->createAddress($customer->address) : null
71 | );
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/src/Purchases/Details.php:
--------------------------------------------------------------------------------
1 |
9 | */
10 | class Details
11 | {
12 | /**
13 | * @var string
14 | */
15 | protected $code;
16 |
17 | /**
18 | * @var string
19 | */
20 | protected $reference;
21 |
22 | /**
23 | * @var mixed
24 | */
25 | protected $status;
26 |
27 | /**
28 | * @var DateTime
29 | */
30 | protected $date;
31 |
32 | /**
33 | * @var DateTime
34 | */
35 | protected $lastEventDate;
36 |
37 | /**
38 | * @var Customer
39 | */
40 | protected $customer;
41 |
42 | /**
43 | * @param string $code
44 | * @param string $reference
45 | * @param int $status
46 | * @param DateTime $date
47 | * @param DateTime $lastEventDate
48 | * @param Customer $customer
49 | */
50 | public function __construct(
51 | $code,
52 | $reference,
53 | $status,
54 | DateTime $date,
55 | DateTime $lastEventDate,
56 | Customer $customer
57 | ) {
58 | $this->code = $code;
59 | $this->reference = $reference;
60 | $this->status = $status;
61 | $this->date = $date;
62 | $this->lastEventDate = $lastEventDate;
63 | $this->customer = $customer;
64 | }
65 |
66 | /**
67 | * @return string
68 | */
69 | public function getCode()
70 | {
71 | return $this->code;
72 | }
73 |
74 | /**
75 | * @return string
76 | */
77 | public function getReference()
78 | {
79 | return $this->reference;
80 | }
81 |
82 | /**
83 | * @return mixed
84 | */
85 | public function getStatus()
86 | {
87 | return $this->status;
88 | }
89 |
90 | /**
91 | * @return DateTime
92 | */
93 | public function getDate()
94 | {
95 | return $this->date;
96 | }
97 |
98 | /**
99 | * @return DateTime
100 | */
101 | public function getLastEventDate()
102 | {
103 | return $this->lastEventDate;
104 | }
105 |
106 | /**
107 | * @return Customer
108 | */
109 | public function getCustomer()
110 | {
111 | return $this->customer;
112 | }
113 | }
114 |
--------------------------------------------------------------------------------
/src/Purchases/NotificationService.php:
--------------------------------------------------------------------------------
1 |
9 | */
10 | interface NotificationService
11 | {
12 | /**
13 | * @param string $code
14 | *
15 | * @return Transaction|Subscription
16 | */
17 | public function getByNotification($code);
18 | }
19 |
--------------------------------------------------------------------------------
/src/Purchases/SearchService.php:
--------------------------------------------------------------------------------
1 |
8 | */
9 | interface SearchService
10 | {
11 | /**
12 | * @param string $code
13 | *
14 | * @return Transaction
15 | */
16 | public function getByCode($code);
17 | }
18 |
--------------------------------------------------------------------------------
/src/Purchases/SubscriptionService.php:
--------------------------------------------------------------------------------
1 |
10 | */
11 | interface SubscriptionService
12 | {
13 | /**
14 | * @var string
15 | */
16 | const CHARGE_ENDPOINT = '/v2/pre-approvals/payment';
17 |
18 | /**
19 | * @var string
20 | */
21 | const CANCEL_ENDPOINT = '/v2/pre-approvals/cancel';
22 |
23 | /**
24 | * @return ChargeBuilder
25 | */
26 | public function createChargeBuilder($code);
27 |
28 | /**
29 | * @param string $code
30 | *
31 | * @return CancellationResponse
32 | */
33 | public function cancel($code);
34 |
35 | /**
36 | * @param Charge $charge
37 | *
38 | * @return ChargeResponse
39 | */
40 | public function charge(Charge $charge);
41 | }
42 |
--------------------------------------------------------------------------------
/src/Purchases/Subscriptions/CancellationResponse.php:
--------------------------------------------------------------------------------
1 |
8 | */
9 | class CancellationResponse
10 | {
11 | /**
12 | * @var string
13 | */
14 | private $status;
15 |
16 | /**
17 | * @var DateTime
18 | */
19 | private $date;
20 |
21 | /**
22 | * @param string $status
23 | * @param DateTime $date
24 | */
25 | public function __construct($status, DateTime $date)
26 | {
27 | $this->status = $status;
28 | $this->date = $date;
29 | }
30 |
31 | /**
32 | * @return string
33 | */
34 | public function getStatus()
35 | {
36 | return $this->status;
37 | }
38 |
39 | /**
40 | * @return DateTime
41 | */
42 | public function getDate()
43 | {
44 | return $this->date;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/Purchases/Subscriptions/Charge.php:
--------------------------------------------------------------------------------
1 |
15 | */
16 | class Charge
17 | {
18 | use SerializerTrait;
19 |
20 | /**
21 | * @Serializer\SerializedName("preApprovalCode")
22 | * @Serializer\XmlElement(cdata=false)
23 | *
24 | * @var string
25 | */
26 | private $subscriptionCode;
27 |
28 | /**
29 | * @Serializer\Type("ArrayCollection")
30 | * @Serializer\XmlList(entry="item")
31 | * @Serializer\SerializedName("items")
32 | *
33 | * @var ItemCollection
34 | */
35 | private $items;
36 |
37 | /**
38 | * @Serializer\XmlElement(cdata=false)
39 | *
40 | * @var string
41 | */
42 | private $reference;
43 |
44 | /**
45 | * @param ItemCollection $items
46 | */
47 | public function __construct(ItemCollection $items = null)
48 | {
49 | $this->items = $items ?: new Items();
50 | }
51 |
52 | /**
53 | * @return string
54 | */
55 | public function getSubscriptionCode()
56 | {
57 | return $this->subscriptionCode;
58 | }
59 |
60 | /**
61 | * @param string $subscriptionCode
62 | */
63 | public function setSubscriptionCode($subscriptionCode)
64 | {
65 | $this->subscriptionCode = $subscriptionCode;
66 | }
67 |
68 | /**
69 | * @return ItemCollection
70 | */
71 | public function getItems()
72 | {
73 | return $this->items;
74 | }
75 |
76 | /**
77 | * @return string
78 | */
79 | public function getReference()
80 | {
81 | return $this->reference;
82 | }
83 |
84 | /**
85 | * @param string $reference
86 | */
87 | public function setReference($reference)
88 | {
89 | $this->reference = $reference;
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/src/Purchases/Subscriptions/ChargeBuilder.php:
--------------------------------------------------------------------------------
1 |
9 | */
10 | class ChargeBuilder implements ChargeBuilderInterface
11 | {
12 | /**
13 | * @var Charge
14 | */
15 | private $charge;
16 |
17 | /**
18 | * @param string $code
19 | * @param Charge $charge
20 | */
21 | public function __construct($code, Charge $charge = null)
22 | {
23 | $this->charge = $charge ?: new Charge();
24 | $this->charge->setSubscriptionCode($code);
25 | }
26 |
27 | /**
28 | * {@inheritdoc}
29 | */
30 | public function addItem(Item $item)
31 | {
32 | $this->charge->getItems()->add($item);
33 |
34 | return $this;
35 | }
36 |
37 | /**
38 | * {@inheritdoc}
39 | */
40 | public function setReference($reference)
41 | {
42 | $this->charge->setReference($reference);
43 |
44 | return $this;
45 | }
46 |
47 | /**
48 | * {@inheritdoc}
49 | */
50 | public function getCharge()
51 | {
52 | return $this->charge;
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/Purchases/Subscriptions/ChargeResponse.php:
--------------------------------------------------------------------------------
1 |
8 | */
9 | class ChargeResponse
10 | {
11 | /**
12 | * @var string
13 | */
14 | private $transactionCode;
15 |
16 | /**
17 | * @var DateTime
18 | */
19 | private $date;
20 |
21 | /**
22 | * @param string $transactionCode
23 | * @param DateTime $date
24 | */
25 | public function __construct($transactionCode, DateTime $date)
26 | {
27 | $this->transactionCode = $transactionCode;
28 | $this->date = $date;
29 | }
30 |
31 | /**
32 | * @return string
33 | */
34 | public function getTransactionCode()
35 | {
36 | return $this->transactionCode;
37 | }
38 |
39 | /**
40 | * @return DateTime
41 | */
42 | public function getDate()
43 | {
44 | return $this->date;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/Purchases/Subscriptions/Decoder.php:
--------------------------------------------------------------------------------
1 |
9 | */
10 | class Decoder extends BaseDecoder
11 | {
12 | /**
13 | * @param SimpleXMLElement $obj
14 | *
15 | * @return Transaction
16 | */
17 | public function decode(SimpleXMLElement $obj)
18 | {
19 | return new Subscription(
20 | (string) $obj->name,
21 | $this->createDetails($obj),
22 | (string) $obj->tracker,
23 | (string) $obj->charge
24 | );
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/Purchases/Subscriptions/Locator.php:
--------------------------------------------------------------------------------
1 |
12 | */
13 | class Locator extends Service implements SearchService, NotificationService
14 | {
15 | /**
16 | * @var string
17 | */
18 | const ENDPOINT = '/v2/pre-approvals';
19 |
20 | /**
21 | * @var Decoder
22 | */
23 | private $decoder;
24 |
25 | /**
26 | * @param Credentials $credentials
27 | * @param Client $client
28 | * @param Decoder $decoder
29 | */
30 | public function __construct(
31 | Credentials $credentials,
32 | Client $client = null,
33 | Decoder $decoder = null
34 | ) {
35 | parent::__construct($credentials, $client);
36 |
37 | $this->decoder = $decoder ?: new Decoder();
38 | }
39 |
40 | /**
41 | * {@inheritdoc}
42 | */
43 | public function getByCode($code)
44 | {
45 | return $this->decoder->decode($this->get(static::ENDPOINT . '/' . $code));
46 | }
47 |
48 | /**
49 | * {@inheritdoc}
50 | */
51 | public function getByNotification($code)
52 | {
53 | return $this->decoder->decode($this->get(static::ENDPOINT . '/notifications/' . $code));
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/Purchases/Subscriptions/Subscription.php:
--------------------------------------------------------------------------------
1 |
9 | */
10 | class Subscription
11 | {
12 | const INITIATED = 'INITIATED';
13 | const PENDING = 'PENDING';
14 | const ACTIVE = 'ACTIVE';
15 | const CANCELLED = 'CANCELLED';
16 | const CANCELLED_BY_RECEIVER = 'CANCELLED_BY_RECEIVER';
17 | const CANCELLED_BY_SENDER = 'CANCELLED_BY_SENDER';
18 | const EXPIRED = 'EXPIRED';
19 |
20 | /**
21 | * @var string
22 | */
23 | private $name;
24 |
25 | /**
26 | * @var Details
27 | */
28 | private $details;
29 |
30 | /**
31 | * @var string
32 | */
33 | private $tracker;
34 |
35 | /**
36 | * @var string
37 | */
38 | private $chargeType;
39 |
40 | /**
41 | * @param string $name
42 | * @param Details $details
43 | * @param string $tracker
44 | * @param string $chargeType
45 | */
46 | public function __construct(
47 | $name,
48 | Details $details,
49 | $tracker,
50 | $chargeType
51 | ) {
52 | $this->name = $name;
53 | $this->details = $details;
54 | $this->tracker = $tracker;
55 | $this->chargeType = $chargeType;
56 | }
57 |
58 | /**
59 | * @return string
60 | */
61 | public function getName()
62 | {
63 | return $this->name;
64 | }
65 |
66 | /**
67 | * @return Details
68 | */
69 | public function getDetails()
70 | {
71 | return $this->details;
72 | }
73 |
74 | /**
75 | * @return string
76 | */
77 | public function getTracker()
78 | {
79 | return $this->tracker;
80 | }
81 |
82 | /**
83 | * @return boolean
84 | */
85 | public function isAutomatic()
86 | {
87 | return $this->chargeType == ChargeType::AUTOMATIC;
88 | }
89 |
90 | /**
91 | * @return boolean
92 | */
93 | public function isManual()
94 | {
95 | return $this->chargeType == ChargeType::MANUAL;
96 | }
97 |
98 | /**
99 | * @return boolean
100 | */
101 | public function isInitiated()
102 | {
103 | return $this->getDetails()->getStatus() === static::INITIATED;
104 | }
105 |
106 | /**
107 | * @return boolean
108 | */
109 | public function isPending()
110 | {
111 | return $this->getDetails()->getStatus() === static::PENDING;
112 | }
113 |
114 | /**
115 | * @return boolean
116 | */
117 | public function isActive()
118 | {
119 | return $this->getDetails()->getStatus() === static::ACTIVE;
120 | }
121 |
122 | /**
123 | * @return boolean
124 | */
125 | public function isCancelledByAcquirer()
126 | {
127 | return $this->getDetails()->getStatus() === static::CANCELLED;
128 | }
129 |
130 | /**
131 | * @return boolean
132 | */
133 | public function isCancelledByReceiver()
134 | {
135 | return $this->getDetails()->getStatus() === static::CANCELLED_BY_RECEIVER;
136 | }
137 |
138 | /**
139 | * @return boolean
140 | */
141 | public function isCancelledByCustomer()
142 | {
143 | return $this->getDetails()->getStatus() === static::CANCELLED_BY_SENDER;
144 | }
145 |
146 | /**
147 | * @return boolean
148 | */
149 | public function isExpired()
150 | {
151 | return $this->getDetails()->getStatus() === static::EXPIRED;
152 | }
153 | }
154 |
--------------------------------------------------------------------------------
/src/Purchases/Subscriptions/SubscriptionService.php:
--------------------------------------------------------------------------------
1 |
10 | */
11 | class SubscriptionService extends Service implements SubscriptionServiceInterface
12 | {
13 | /**
14 | * {@inheritdoc}
15 | */
16 | public function createChargeBuilder($code)
17 | {
18 | return new ChargeBuilder($code);
19 | }
20 |
21 | /**
22 | * {@inheritdoc}
23 | */
24 | public function cancel($code)
25 | {
26 | $response = $this->get(static::CANCEL_ENDPOINT . '/' . $code);
27 |
28 | return new CancellationResponse(
29 | (string) $response->status,
30 | new DateTime((string) $response->date)
31 | );
32 | }
33 |
34 | /**
35 | * {@inheritdoc}
36 | */
37 | public function charge(Charge $charge)
38 | {
39 | $response = $this->post(static::CHARGE_ENDPOINT, $charge->xmlSerialize());
40 |
41 | return new ChargeResponse(
42 | (string) $response->transactionCode,
43 | new DateTime((string) $response->date)
44 | );
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/Purchases/Transactions/Decoder.php:
--------------------------------------------------------------------------------
1 |
13 | */
14 | class Decoder extends BaseDecoder
15 | {
16 | /**
17 | * @param SimpleXMLElement $obj
18 | *
19 | * @return Transaction
20 | */
21 | public function decode(SimpleXMLElement $obj)
22 | {
23 | return new Transaction(
24 | $this->createDetails($obj),
25 | $this->createPayment($obj),
26 | (int) $obj->type,
27 | $this->createItems($obj->items),
28 | $this->createShipping($obj->shipping)
29 | );
30 | }
31 |
32 | /**
33 | * @param SimpleXMLElement $obj
34 | * @return Payment
35 | */
36 | protected function createPayment(SimpleXMLElement $obj)
37 | {
38 | return new Payment(
39 | new PaymentMethod(
40 | (int) $obj->paymentMethod->type,
41 | (int) $obj->paymentMethod->code
42 | ),
43 | (float) $obj->grossAmount,
44 | (float) $obj->discountAmount,
45 | (float) $obj->feeAmount,
46 | (float) $obj->netAmount,
47 | (float) $obj->extraAmount,
48 | (int) $obj->installmentCount,
49 | isset($obj->escrowEndDate) ? new DateTime((string) $obj->escrowEndDate) : null
50 | );
51 | }
52 |
53 | /**
54 | * @param SimpleXMLElement $itemsNode
55 | *
56 | * @return Items
57 | */
58 | protected function createItems(SimpleXMLElement $itemsNode)
59 | {
60 | $items = new Items();
61 |
62 | foreach ($itemsNode->item as $item) {
63 | $items->add(
64 | new Item(
65 | (string) $item->id,
66 | (string) $item->description,
67 | (float) $item->amount,
68 | (int) $item->quantity,
69 | isset($item->shippingCost) ? (float) $item->shippingCost : null,
70 | isset($item->weight) ? (int) $item->weight : null
71 | )
72 | );
73 | }
74 |
75 | return $items;
76 | }
77 |
78 | /**
79 | * @param SimpleXMLElement $shipping
80 | * @return Shipping
81 | */
82 | protected function createShipping(SimpleXMLElement $shipping)
83 | {
84 | return new Shipping(
85 | (int) $shipping->type,
86 | isset($shipping->address) ? $this->createAddress($shipping->address) : null,
87 | isset($shipping->cost) ? (float) $shipping->cost : null
88 | );
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/src/Purchases/Transactions/Locator.php:
--------------------------------------------------------------------------------
1 |
12 | */
13 | class Locator extends Service implements SearchService, NotificationService
14 | {
15 | /**
16 | * @var string
17 | */
18 | const ENDPOINT = '/v2/transactions';
19 |
20 | /**
21 | * @var Decoder
22 | */
23 | private $decoder;
24 |
25 | /**
26 | * @param Credentials $credentials
27 | * @param Client $client
28 | * @param Decoder $decoder
29 | */
30 | public function __construct(
31 | Credentials $credentials,
32 | Client $client = null,
33 | Decoder $decoder = null
34 | ) {
35 | parent::__construct($credentials, $client);
36 |
37 | $this->decoder = $decoder ?: new Decoder();
38 | }
39 |
40 | /**
41 | * @param string $code
42 | *
43 | * @return Transaction
44 | */
45 | public function getByCode($code)
46 | {
47 | return $this->decoder->decode($this->get(static::ENDPOINT . '/' . $code));
48 | }
49 |
50 | /**
51 | * {@inheritdoc}
52 | */
53 | public function getByNotification($code)
54 | {
55 | return $this->decoder->decode($this->get(static::ENDPOINT . '/notifications/' . $code));
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/Purchases/Transactions/Payment.php:
--------------------------------------------------------------------------------
1 | escrowEndDate = $escrowEndDate;
69 | $this->paymentMethod = $paymentMethod;
70 | $this->grossAmount = $grossAmount;
71 | $this->discountAmount = $discountAmount;
72 | $this->feeAmount = $feeAmount;
73 | $this->netAmount = $netAmount;
74 | $this->extraAmount = $extraAmount;
75 | $this->installmentCount = $installmentCount;
76 | }
77 |
78 | /**
79 | * @return DateTime
80 | */
81 | public function getEscrowEndDate()
82 | {
83 | return $this->escrowEndDate;
84 | }
85 |
86 | /**
87 | * @return PaymentMethod
88 | */
89 | public function getPaymentMethod()
90 | {
91 | return $this->paymentMethod;
92 | }
93 |
94 | /**
95 | * @return number
96 | */
97 | public function getGrossAmount()
98 | {
99 | return $this->grossAmount;
100 | }
101 |
102 | /**
103 | * @return number
104 | */
105 | public function getDiscountAmount()
106 | {
107 | return $this->discountAmount;
108 | }
109 |
110 | /**
111 | * @return number
112 | */
113 | public function getFeeAmount()
114 | {
115 | return $this->feeAmount;
116 | }
117 |
118 | /**
119 | * @return number
120 | */
121 | public function getNetAmount()
122 | {
123 | return $this->netAmount;
124 | }
125 |
126 | /**
127 | * @return number
128 | */
129 | public function getExtraAmount()
130 | {
131 | return $this->extraAmount;
132 | }
133 |
134 | /**
135 | * @return number
136 | */
137 | public function getInstallmentCount()
138 | {
139 | return $this->installmentCount;
140 | }
141 | }
142 |
--------------------------------------------------------------------------------
/src/Purchases/Transactions/PaymentMethod.php:
--------------------------------------------------------------------------------
1 | type = (int) $type;
23 | $this->code = (int) $code;
24 | }
25 |
26 | /**
27 | * @return number
28 | */
29 | public function getType()
30 | {
31 | return $this->type;
32 | }
33 |
34 | /**
35 | * @return number
36 | */
37 | public function getCode()
38 | {
39 | return $this->code;
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/Purchases/Transactions/Transaction.php:
--------------------------------------------------------------------------------
1 |
11 | */
12 | class Transaction
13 | {
14 | /**
15 | * @var int
16 | */
17 | const WAITING_PAYMENT = '1';
18 |
19 | /**
20 | * @var int
21 | */
22 | const UNDER_ANALYSIS = '2';
23 |
24 | /**
25 | * @var int
26 | */
27 | const PAID = '3';
28 |
29 | /**
30 | * @var int
31 | */
32 | const AVAILABLE = '4';
33 |
34 | /**
35 | * @var int
36 | */
37 | const UNDER_CONTEST = '5';
38 |
39 | /**
40 | * @var int
41 | */
42 | const RETURNED = '6';
43 |
44 | /**
45 | * @var int
46 | */
47 | const CANCELLED = '7';
48 |
49 | /**
50 | * @var Details
51 | */
52 | private $details;
53 |
54 | /**
55 | * @var Payment
56 | */
57 | private $payment;
58 |
59 | /**
60 | * @var int
61 | */
62 | private $type;
63 |
64 | /**
65 | * @var ItemCollection
66 | */
67 | private $items;
68 |
69 | /**
70 | * @var Shipping
71 | */
72 | private $shipping;
73 |
74 | /**
75 | * @param Details $details
76 | * @param Payment $payment
77 | * @param int $type
78 | * @param ItemCollection $items
79 | * @param Shipping $shipping
80 | */
81 | public function __construct(
82 | Details $details,
83 | Payment $payment,
84 | $type,
85 | ItemCollection $items,
86 | Shipping $shipping = null
87 | ) {
88 | $this->details = $details;
89 | $this->payment = $payment;
90 | $this->type = $type;
91 | $this->items = $items;
92 | $this->shipping = $shipping;
93 | }
94 |
95 | /**
96 | * @return Details
97 | */
98 | public function getDetails()
99 | {
100 | return $this->details;
101 | }
102 |
103 | /**
104 | * @return number
105 | */
106 | public function getType()
107 | {
108 | return $this->type;
109 | }
110 |
111 | /**
112 | * @return Payment
113 | */
114 | public function getPayment()
115 | {
116 | return $this->payment;
117 | }
118 |
119 | /**
120 | * @return ItemCollection
121 | */
122 | public function getItems()
123 | {
124 | return $this->items;
125 | }
126 |
127 | /**
128 | * @return Shipping
129 | */
130 | public function getShipping()
131 | {
132 | return $this->shipping;
133 | }
134 |
135 | /**
136 | * @return boolean
137 | */
138 | public function isWaitingPayment()
139 | {
140 | return $this->details->getStatus() === static::WAITING_PAYMENT;
141 | }
142 |
143 | /**
144 | * @return boolean
145 | */
146 | public function isUnderAnalysis()
147 | {
148 | return $this->details->getStatus() === static::UNDER_ANALYSIS;
149 | }
150 |
151 | /**
152 | * @return boolean
153 | */
154 | public function isPaid()
155 | {
156 | return $this->details->getStatus() === static::PAID;
157 | }
158 |
159 | /**
160 | * @return boolean
161 | */
162 | public function isAvailable()
163 | {
164 | return $this->details->getStatus() === static::AVAILABLE;
165 | }
166 |
167 | /**
168 | * @return boolean
169 | */
170 | public function isUnderContest()
171 | {
172 | return $this->details->getStatus() === static::UNDER_CONTEST;
173 | }
174 |
175 | /**
176 | * @return boolean
177 | */
178 | public function isReturned()
179 | {
180 | return $this->details->getStatus() === static::RETURNED;
181 | }
182 |
183 | /**
184 | * @return boolean
185 | */
186 | public function isCancelled()
187 | {
188 | return $this->details->getStatus() === static::CANCELLED;
189 | }
190 | }
191 |
--------------------------------------------------------------------------------
/src/Requests/Checkout/Checkout.php:
--------------------------------------------------------------------------------
1 |
14 | */
15 | class Checkout
16 | {
17 | use SerializerTrait;
18 |
19 | /**
20 | * @Serializer\Inline
21 | * @Serializer\Type("PHPSC\PagSeguro\Requests\Checkout\Order")
22 | *
23 | * @var Order
24 | */
25 | private $order;
26 |
27 | /**
28 | * @Serializer\SerializedName("sender")
29 | * @Serializer\Type("PHPSC\PagSeguro\Customer\Customer")
30 | *
31 | * @var Customer
32 | */
33 | private $customer;
34 |
35 | /**
36 | * @Serializer\XmlElement(cdata=false)
37 | * @Serializer\SerializedName("redirectURL")
38 | *
39 | * @var string
40 | */
41 | private $redirectTo;
42 |
43 | /**
44 | * @Serializer\Type("integer")
45 | *
46 | * @var int
47 | */
48 | private $maxUses;
49 |
50 | /**
51 | * @Serializer\Type("integer")
52 | *
53 | * @var int
54 | */
55 | private $maxAge;
56 |
57 | /**
58 | * @Serializer\XmlElement(cdata=false)
59 | *
60 | * @var string
61 | */
62 | private $notificationURL;
63 |
64 | /**
65 | * @param Order $order
66 | */
67 | public function __construct(Order $order = null)
68 | {
69 | $this->order = $order ?: new Order();
70 | }
71 |
72 | /**
73 | * @return Order
74 | */
75 | public function getOrder()
76 | {
77 | return $this->order;
78 | }
79 |
80 | /**
81 | * @return Customer
82 | */
83 | public function getCustomer()
84 | {
85 | return $this->customer;
86 | }
87 |
88 | /**
89 | * @param Customer $customer
90 | */
91 | public function setCustomer(Customer $customer)
92 | {
93 | $this->customer = $customer;
94 | }
95 |
96 | /**
97 | * @return string
98 | */
99 | public function getRedirectTo()
100 | {
101 | return $this->redirectTo;
102 | }
103 |
104 | /**
105 | * @param string $redirectTo
106 | */
107 | public function setRedirectTo($redirectTo)
108 | {
109 | $this->redirectTo = $redirectTo;
110 | }
111 |
112 | /**
113 | * @return int
114 | */
115 | public function getMaxUses()
116 | {
117 | return $this->maxUses;
118 | }
119 |
120 | /**
121 | * @param int $maxUses
122 | */
123 | public function setMaxUses($maxUses)
124 | {
125 | $this->maxUses = $maxUses;
126 | }
127 |
128 | /**
129 | * @return int
130 | */
131 | public function getMaxAge()
132 | {
133 | return $this->maxAge;
134 | }
135 |
136 | /**
137 | * @param int $maxAge
138 | */
139 | public function setMaxAge($maxAge)
140 | {
141 | $this->maxAge = $maxAge;
142 | }
143 |
144 | /**
145 | * @return string
146 | */
147 | public function getNotificationURL()
148 | {
149 | return $this->notificationURL;
150 | }
151 |
152 | /**
153 | * @param string $notificationURL
154 | */
155 | public function setNotificationURL($notificationURL)
156 | {
157 | $this->notificationURL = $notificationURL;
158 | }
159 | }
160 |
--------------------------------------------------------------------------------
/src/Requests/Checkout/CheckoutBuilder.php:
--------------------------------------------------------------------------------
1 |
11 | */
12 | class CheckoutBuilder implements CheckoutBuilderInterface
13 | {
14 | /**
15 | * @var Checkout
16 | */
17 | private $checkout;
18 |
19 | /**
20 | * @param Checkout $checkout
21 | */
22 | public function __construct(Checkout $checkout = null)
23 | {
24 | $this->checkout = $checkout ?: new Checkout();
25 | }
26 |
27 | /**
28 | * {@inheritdoc}
29 | */
30 | public function addItem(Item $item)
31 | {
32 | $this->checkout->getOrder()->getItems()->add($item);
33 |
34 | return $this;
35 | }
36 |
37 | /**
38 | * {@inheritdoc}
39 | */
40 | public function setShipping(Shipping $shipping)
41 | {
42 | $this->checkout->getOrder()->setShipping($shipping);
43 |
44 | return $this;
45 | }
46 |
47 | /**
48 | * {@inheritdoc}
49 | */
50 | public function setReference($reference)
51 | {
52 | $this->checkout->getOrder()->setReference($reference);
53 |
54 | return $this;
55 | }
56 |
57 | /**
58 | * {@inheritdoc}
59 | */
60 | public function setCustomer(Customer $customer)
61 | {
62 | $this->checkout->setCustomer($customer);
63 |
64 | return $this;
65 | }
66 |
67 | /**
68 | * {@inheritdoc}
69 | */
70 | public function setRedirectTo($redirectTo)
71 | {
72 | $this->checkout->setRedirectTo($redirectTo);
73 |
74 | return $this;
75 | }
76 |
77 | /**
78 | * {@inheritdoc}
79 | */
80 | public function setMaxAge($maxAge)
81 | {
82 | $this->checkout->setMaxAge($maxAge);
83 |
84 | return $this;
85 | }
86 |
87 | /**
88 | * {@inheritdoc}
89 | */
90 | public function setMaxUses($maxUses)
91 | {
92 | $this->checkout->setMaxUses($maxUses);
93 |
94 | return $this;
95 | }
96 |
97 | /**
98 | * {@inheritdoc}
99 | */
100 | public function setExtraAmount($extraAmount)
101 | {
102 | $this->checkout->getOrder()->setExtraAmount($extraAmount);
103 |
104 | return $this;
105 | }
106 |
107 | /**
108 | * {@inheritdoc}
109 | */
110 | public function getCheckout()
111 | {
112 | return $this->checkout;
113 | }
114 | }
115 |
--------------------------------------------------------------------------------
/src/Requests/Checkout/CheckoutService.php:
--------------------------------------------------------------------------------
1 |
9 | */
10 | class CheckoutService extends Service implements CheckoutServiceInterface
11 | {
12 | /**
13 | * {@inheritdoc}
14 | */
15 | public function createCheckoutBuilder()
16 | {
17 | return new CheckoutBuilder();
18 | }
19 |
20 | /**
21 | * {@inheritdoc}
22 | */
23 | public function checkout(Checkout $checkout)
24 | {
25 | $response = $this->post(
26 | static::ENDPOINT,
27 | $checkout->xmlSerialize()
28 | );
29 |
30 | return $this->getRedirection($response);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/Requests/Checkout/Order.php:
--------------------------------------------------------------------------------
1 |
16 | */
17 | class Order
18 | {
19 | use SerializerTrait;
20 |
21 | /**
22 | * @Serializer\XmlElement(cdata=false)
23 | *
24 | * @var string
25 | */
26 | private $currency;
27 |
28 | /**
29 | * @Serializer\SerializedName("items")
30 | * @Serializer\Type("ArrayCollection")
31 | * @Serializer\XmlList(entry="item")
32 | *
33 | * @var ItemCollection
34 | */
35 | private $items;
36 |
37 | /**
38 | * @Serializer\XmlElement(cdata=false)
39 | *
40 | * @var string
41 | */
42 | private $reference;
43 |
44 | /**
45 | * @Serializer\Type("PHPSC\PagSeguro\Shipping\Shipping")
46 | *
47 | * @var Shipping
48 | */
49 | private $shipping;
50 |
51 | /**
52 | * @Serializer\XmlElement(cdata=false)
53 | *
54 | * @var float
55 | */
56 | private $extraAmount;
57 |
58 | /**
59 | * @param ItemCollection $items
60 | */
61 | public function __construct(ItemCollection $items = null)
62 | {
63 | $this->items = $items ? : new Items();
64 | $this->currency = 'BRL';
65 | }
66 |
67 | /**
68 | * @return string
69 | */
70 | public function getCurrency()
71 | {
72 | return $this->currency;
73 | }
74 |
75 | /**
76 | * @return ItemCollection
77 | */
78 | public function getItems()
79 | {
80 | return $this->items;
81 | }
82 |
83 | /**
84 | * @return string
85 | */
86 | public function getReference()
87 | {
88 | return $this->reference;
89 | }
90 |
91 | /**
92 | * @param string $reference
93 | */
94 | public function setReference($reference)
95 | {
96 | $this->reference = $reference;
97 | }
98 |
99 | /**
100 | * @return Shipping
101 | */
102 | public function getShipping()
103 | {
104 | return $this->shipping;
105 | }
106 |
107 | /**
108 | * @param Shipping $shipping
109 | */
110 | public function setShipping(Shipping $shipping)
111 | {
112 | $this->shipping = $shipping;
113 | }
114 |
115 | /**
116 | * @return float
117 | */
118 | public function getExtraAmount()
119 | {
120 | return $this->formatAmount($this->extraAmount);
121 | }
122 |
123 | /**
124 | * @param float $extraAmount
125 | */
126 | public function setExtraAmount($extraAmount)
127 | {
128 | $this->extraAmount = $extraAmount;
129 | }
130 | }
131 |
--------------------------------------------------------------------------------
/src/Requests/CheckoutBuilder.php:
--------------------------------------------------------------------------------
1 |
11 | */
12 | interface CheckoutBuilder
13 | {
14 | /**
15 | * @param Item $item
16 | *
17 | * @return self
18 | */
19 | public function addItem(Item $item);
20 |
21 | /**
22 | * @param Shipping $shipping
23 | *
24 | * @return self
25 | */
26 | public function setShipping(Shipping $shipping);
27 |
28 | /**
29 | * @param string $reference
30 | *
31 | * @return self
32 | */
33 | public function setReference($reference);
34 |
35 | /**
36 | * @param Customer $customer
37 | *
38 | * @return self
39 | */
40 | public function setCustomer(Customer $customer);
41 |
42 | /**
43 | * @param string $redirectTo
44 | *
45 | * @return self
46 | */
47 | public function setRedirectTo($redirectTo);
48 |
49 | /**
50 | * @param int $maxAge
51 | *
52 | * @return self
53 | */
54 | public function setMaxAge($maxAge);
55 |
56 | /**
57 | * @param int $maxUses
58 | *
59 | * @return self
60 | */
61 | public function setMaxUses($maxUses);
62 |
63 | /**
64 | * @param float $extraAmount
65 | *
66 | * @return self
67 | */
68 | public function setExtraAmount($extraAmount);
69 |
70 | /**
71 | * @return Checkout
72 | */
73 | public function getCheckout();
74 | }
75 |
--------------------------------------------------------------------------------
/src/Requests/CheckoutService.php:
--------------------------------------------------------------------------------
1 |
8 | */
9 | interface CheckoutService
10 | {
11 | /**
12 | * @var string
13 | */
14 | const REDIRECT_TO = '/v2/checkout/payment.html';
15 |
16 | /**
17 | * @var string
18 | */
19 | const ENDPOINT = '/v2/checkout';
20 |
21 | /**
22 | * @return CheckoutBuilder
23 | */
24 | public function createCheckoutBuilder();
25 |
26 | /**
27 | * @param Checkout $checkout
28 | *
29 | * @return Redirection
30 | */
31 | public function checkout(Checkout $checkout);
32 | }
33 |
--------------------------------------------------------------------------------
/src/Requests/PreApprovalService.php:
--------------------------------------------------------------------------------
1 |
8 | */
9 | interface PreApprovalService
10 | {
11 | /**
12 | * @var string
13 | */
14 | const ENDPOINT = '/v2/pre-approvals/request';
15 |
16 | /**
17 | * @var string
18 | */
19 | const REDIRECT_TO = '/v2/pre-approvals/request.html';
20 |
21 | /**
22 | * @param boolean $manualCharge
23 | *
24 | * @return RequestBuilder
25 | */
26 | public function createRequestBuilder($manualCharge = true);
27 |
28 | /**
29 | * @param Request $request
30 | *
31 | * @return Redirection
32 | */
33 | public function approve(Request $request);
34 | }
35 |
--------------------------------------------------------------------------------
/src/Requests/PreApprovals/ChargeType.php:
--------------------------------------------------------------------------------
1 |
6 | */
7 | class ChargeType
8 | {
9 | /**
10 | * @var string
11 | */
12 | const AUTOMATIC = 'auto';
13 |
14 | /**
15 | * @var string
16 | */
17 | const MANUAL = 'manual';
18 |
19 | /**
20 | * @param string $type
21 | *
22 | * @return boolean
23 | */
24 | public static function isValid($type)
25 | {
26 | return in_array($type, [static::AUTOMATIC, static::MANUAL]);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/Requests/PreApprovals/Period.php:
--------------------------------------------------------------------------------
1 |
6 | */
7 | class Period
8 | {
9 | /**
10 | * @var string
11 | */
12 | const WEEKLY = 'WEEKLY';
13 |
14 | /**
15 | * @var string
16 | */
17 | const MONTHLY = 'MONTHLY';
18 |
19 | /**
20 | * @var string
21 | */
22 | const BIMONTHLY = 'BIMONTHLY';
23 |
24 | /**
25 | * @var string
26 | */
27 | const TRIMONTHLY = 'TRIMONTHLY';
28 |
29 | /**
30 | * @var string
31 | */
32 | const SEMESTRALLY = 'SEMIANNUALLY';
33 |
34 | /**
35 | * @var string
36 | */
37 | const YEARLY = 'YEARLY';
38 |
39 | /**
40 | * @return array
41 | */
42 | protected static function getPeriods()
43 | {
44 | return [
45 | static::WEEKLY,
46 | static::MONTHLY,
47 | static::BIMONTHLY,
48 | static::TRIMONTHLY,
49 | static::SEMESTRALLY,
50 | static::YEARLY
51 | ];
52 | }
53 |
54 | /**
55 | * @param string $period
56 | *
57 | * @return boolean
58 | */
59 | public static function isValid($period)
60 | {
61 | return in_array($period, static::getPeriods());
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/Requests/PreApprovals/PreApproval.php:
--------------------------------------------------------------------------------
1 |
15 | */
16 | class PreApproval
17 | {
18 | use SerializerTrait;
19 |
20 | /**
21 | * @Serializer\XmlElement(cdata=false)
22 | *
23 | * @var string
24 | */
25 | private $name;
26 |
27 | /**
28 | * @Serializer\SerializedName("charge")
29 | * @Serializer\XmlElement(cdata=false)
30 | *
31 | * @var string
32 | */
33 | private $chargeType;
34 |
35 | /**
36 | * @Serializer\XmlElement(cdata=false)
37 | *
38 | * @var string
39 | */
40 | private $details;
41 |
42 | /**
43 | * @Serializer\XmlElement(cdata=false)
44 | *
45 | * @var string
46 | */
47 | private $period;
48 |
49 | /**
50 | * @Serializer\Type("DateTime<'Y-m-d\TH:i:sP'>")
51 | * @Serializer\XmlElement(cdata=false)
52 | *
53 | * @var DateTime
54 | */
55 | private $finalDate;
56 |
57 | /**
58 | * @Serializer\XmlElement(cdata=false)
59 | *
60 | * @var float
61 | */
62 | private $maxTotalAmount;
63 |
64 | /**
65 | * @Serializer\XmlElement(cdata=false)
66 | *
67 | * @var float
68 | */
69 | private $amountPerPayment;
70 |
71 | /**
72 | * @Serializer\XmlElement(cdata=false)
73 | *
74 | * @var float
75 | */
76 | private $maxAmountPerPayment;
77 |
78 | /**
79 | * @Serializer\Type("integer")
80 | *
81 | * @var int
82 | */
83 | private $maxPaymentsPerPeriod;
84 |
85 | /**
86 | * @Serializer\XmlElement(cdata=false)
87 | *
88 | * @var float
89 | */
90 | private $maxAmountPerPeriod;
91 |
92 | /**
93 | * @Serializer\Type("DateTime<'Y-m-d\TH:i:sP','00:00'>")
94 | * @Serializer\XmlElement(cdata=false)
95 | *
96 | * @var DateTime
97 | */
98 | private $initialDate;
99 |
100 | /**
101 | * @return string
102 | */
103 | public function getName()
104 | {
105 | return $this->name;
106 | }
107 |
108 | /**
109 | * @param string $name
110 | */
111 | public function setName($name)
112 | {
113 | $this->name = $name;
114 | }
115 |
116 | /**
117 | * @return string
118 | */
119 | public function getChargeType()
120 | {
121 | return $this->chargeType;
122 | }
123 |
124 | /**
125 | * @param string $chargeType
126 | *
127 | * @throws InvalidArgumentException
128 | */
129 | public function setChargeType($chargeType)
130 | {
131 | if (!ChargeType::isValid($chargeType)) {
132 | throw new InvalidArgumentException('You should inform a valid charge type');
133 | }
134 |
135 | $this->chargeType = $chargeType;
136 | }
137 |
138 | /**
139 | * @return string
140 | */
141 | public function getDetails()
142 | {
143 | return $this->details;
144 | }
145 |
146 | /**
147 | * @param string $details
148 | */
149 | public function setDetails($details)
150 | {
151 | $this->details = $details;
152 | }
153 |
154 | /**
155 | * @return string
156 | */
157 | public function getPeriod()
158 | {
159 | return $this->period;
160 | }
161 |
162 | /**
163 | * @param string $period
164 | *
165 | * @throws InvalidArgumentException
166 | */
167 | public function setPeriod($period)
168 | {
169 | if (!Period::isValid($period)) {
170 | throw new InvalidArgumentException('You should inform a valid period');
171 | }
172 |
173 | $this->period = $period;
174 | }
175 |
176 | /**
177 | * @return DateTime
178 | */
179 | public function getFinalDate()
180 | {
181 | return $this->finalDate;
182 | }
183 |
184 | /**
185 | * @param DateTime $finalDate
186 | */
187 | public function setFinalDate(DateTime $finalDate)
188 | {
189 | $this->finalDate = $finalDate;
190 | }
191 |
192 | /**
193 | * @return string
194 | */
195 | public function getMaxTotalAmount()
196 | {
197 | return $this->formatAmount($this->maxTotalAmount);
198 | }
199 |
200 | /**
201 | * @param float $maxTotalAmount
202 | */
203 | public function setMaxTotalAmount($maxTotalAmount)
204 | {
205 | $this->maxTotalAmount = $maxTotalAmount;
206 | }
207 |
208 | /**
209 | * @return string
210 | */
211 | public function getAmountPerPayment()
212 | {
213 | return $this->formatAmount($this->amountPerPayment);
214 | }
215 |
216 | /**
217 | * @param float $amountPerPayment
218 | */
219 | public function setAmountPerPayment($amountPerPayment)
220 | {
221 | $this->amountPerPayment = $amountPerPayment;
222 | }
223 | /**
224 | * @return string
225 | */
226 | public function getMaxAmountPerPayment()
227 | {
228 | return $this->formatAmount($this->maxAmountPerPayment);
229 | }
230 |
231 | /**
232 | * @param float $maxAmountPerPayment
233 | */
234 | public function setMaxAmountPerPayment($maxAmountPerPayment)
235 | {
236 | $this->maxAmountPerPayment = $maxAmountPerPayment;
237 | }
238 |
239 | /**
240 | * @return int
241 | */
242 | public function getMaxPaymentsPerPeriod()
243 | {
244 | return $this->maxPaymentsPerPeriod;
245 | }
246 |
247 | /**
248 | * @param int $maxPaymentsPerPeriod
249 | */
250 | public function setMaxPaymentsPerPeriod($maxPaymentsPerPeriod)
251 | {
252 | $this->maxPaymentsPerPeriod = $maxPaymentsPerPeriod;
253 | }
254 |
255 | /**
256 | * @return string
257 | */
258 | public function getMaxAmountPerPeriod()
259 | {
260 | return $this->formatAmount($this->maxAmountPerPeriod);
261 | }
262 |
263 | /**
264 | * @param float $maxAmountPerPeriod
265 | */
266 | public function setMaxAmountPerPeriod($maxAmountPerPeriod)
267 | {
268 | $this->maxAmountPerPeriod = $maxAmountPerPeriod;
269 | }
270 |
271 | /**
272 | * @return DateTime
273 | */
274 | public function getInitialDate()
275 | {
276 | return $this->initialDate;
277 | }
278 |
279 | /**
280 | * @param DateTime $initialDate
281 | */
282 | public function setInitialDate(DateTime $initialDate)
283 | {
284 | $this->initialDate = $initialDate;
285 | }
286 | }
287 |
--------------------------------------------------------------------------------
/src/Requests/PreApprovals/PreApprovalService.php:
--------------------------------------------------------------------------------
1 |
9 | */
10 | class PreApprovalService extends Service implements PreApprovalServiceInterface
11 | {
12 | /**
13 | * {@inheritdoc}
14 | */
15 | public function createRequestBuilder($manualCharge = true)
16 | {
17 | return new RequestBuilder($manualCharge);
18 | }
19 |
20 | /**
21 | * {@inheritdoc}
22 | */
23 | public function approve(Request $request)
24 | {
25 | $response = $this->post(
26 | static::ENDPOINT,
27 | $request->xmlSerialize()
28 | );
29 |
30 | return $this->getRedirection($response);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/Requests/PreApprovals/Request.php:
--------------------------------------------------------------------------------
1 |
14 | */
15 | class Request
16 | {
17 | use SerializerTrait;
18 |
19 | /**
20 | * @Serializer\Type("PHPSC\PagSeguro\Requests\PreApprovals\PreApproval")
21 | *
22 | * @var PreApproval
23 | */
24 | private $preApproval;
25 |
26 | /**
27 | * @Serializer\XmlElement(cdata=false)
28 | *
29 | * @var string
30 | */
31 | private $reference;
32 |
33 | /**
34 | * @Serializer\Type("PHPSC\PagSeguro\Customer\Customer")
35 | * @Serializer\SerializedName("sender")
36 | *
37 | * @var Customer
38 | */
39 | private $customer;
40 |
41 | /**
42 | * @Serializer\XmlElement(cdata=false)
43 | * @Serializer\SerializedName("redirectURL")
44 | *
45 | * @var string
46 | */
47 | private $redirectTo;
48 |
49 | /**
50 | * @Serializer\XmlElement(cdata=false)
51 | * @Serializer\SerializedName("reviewURL")
52 | *
53 | * @var string
54 | */
55 | private $reviewOn;
56 |
57 | /**
58 | * @param PreApproval $preApproval
59 | */
60 | public function __construct(PreApproval $preApproval = null)
61 | {
62 | $this->preApproval = $preApproval ?: new PreApproval();
63 | }
64 |
65 | /**
66 | * @return PreApproval
67 | */
68 | public function getPreApproval()
69 | {
70 | return $this->preApproval;
71 | }
72 |
73 | /**
74 | * @return string
75 | */
76 | public function getReference()
77 | {
78 | return $this->reference;
79 | }
80 |
81 | /**
82 | * @param string $reference
83 | */
84 | public function setReference($reference)
85 | {
86 | $this->reference = $reference;
87 | }
88 |
89 | /**
90 | * @return Customer
91 | */
92 | public function getCustomer()
93 | {
94 | return $this->customer;
95 | }
96 |
97 | /**
98 | * @param Customer $customer
99 | */
100 | public function setCustomer(Customer $customer)
101 | {
102 | $this->customer = $customer;
103 | }
104 |
105 | /**
106 | * @return string
107 | */
108 | public function getRedirectTo()
109 | {
110 | return $this->redirectTo;
111 | }
112 |
113 | /**
114 | * @param string $redirectTo
115 | */
116 | public function setRedirectTo($redirectTo)
117 | {
118 | $this->redirectTo = $redirectTo;
119 | }
120 |
121 | /**
122 | * @return string
123 | */
124 | public function getReviewOn()
125 | {
126 | return $this->reviewOn;
127 | }
128 |
129 | /**
130 | * @param string $reviewOn
131 | */
132 | public function setReviewOn($reviewOn)
133 | {
134 | $this->reviewOn = $reviewOn;
135 | }
136 | }
137 |
--------------------------------------------------------------------------------
/src/Requests/PreApprovals/RequestBuilder.php:
--------------------------------------------------------------------------------
1 |
10 | */
11 | class RequestBuilder implements RequestBuilderInterface
12 | {
13 | /**
14 | * @var Request
15 | */
16 | private $request;
17 |
18 | /**
19 | * @param Request $request
20 | * @param boolean $manualCharge
21 | */
22 | public function __construct($manualCharge, Request $request = null)
23 | {
24 | $this->request = $request ?: new Request();
25 |
26 | $this->request->getPreApproval()->setChargeType(
27 | $manualCharge ? ChargeType::MANUAL : ChargeType::AUTOMATIC
28 | );
29 | }
30 |
31 | /**
32 | * {@inheritdoc}
33 | */
34 | public function setCustomer(Customer $customer)
35 | {
36 | $this->request->setCustomer($customer);
37 |
38 | return $this;
39 | }
40 |
41 | /**
42 | * {@inheritdoc}
43 | */
44 | public function setName($name)
45 | {
46 | $this->request->getPreApproval()->setName($name);
47 |
48 | return $this;
49 | }
50 |
51 | /**
52 | * {@inheritdoc}
53 | */
54 | public function setDetails($details)
55 | {
56 | $this->request->getPreApproval()->setDetails($details);
57 |
58 | return $this;
59 | }
60 |
61 | /**
62 | * {@inheritdoc}
63 | */
64 | public function setFinalDate(DateTime $finalDate)
65 | {
66 | $this->request->getPreApproval()->setFinalDate($finalDate);
67 |
68 | return $this;
69 | }
70 |
71 | /**
72 | * {@inheritdoc}
73 | */
74 | public function setMaxTotalAmount($maxTotalAmount)
75 | {
76 | $this->request->getPreApproval()->setMaxTotalAmount($maxTotalAmount);
77 |
78 | return $this;
79 | }
80 |
81 | /**
82 | * {@inheritdoc}
83 | */
84 | public function setPeriod($period)
85 | {
86 | $this->request->getPreApproval()->setPeriod($period);
87 |
88 | return $this;
89 | }
90 |
91 | /**
92 | * {@inheritdoc}
93 | */
94 | public function setRedirectTo($redirectTo)
95 | {
96 | $this->request->setRedirectTo($redirectTo);
97 |
98 | return $this;
99 | }
100 |
101 | /**
102 | * {@inheritdoc}
103 | */
104 | public function setReference($reference)
105 | {
106 | $this->request->setReference($reference);
107 |
108 | return $this;
109 | }
110 |
111 | /**
112 | * {@inheritdoc}
113 | */
114 | public function setReviewOn($reviewOn)
115 | {
116 | $this->request->setReviewOn($reviewOn);
117 |
118 | return $this;
119 | }
120 |
121 | /**
122 | * {@inheritdoc}
123 | */
124 | public function setAmountPerPayment($amountPerPayment)
125 | {
126 | $this->request->getPreApproval()->setAmountPerPayment($amountPerPayment);
127 |
128 | return $this;
129 | }
130 |
131 | /**
132 | * {@inheritdoc}
133 | */
134 | public function setMaxAmountPerPayment($maxAmountPerPayment)
135 | {
136 | $this->request->getPreApproval()->setMaxAmountPerPayment($maxAmountPerPayment);
137 |
138 | return $this;
139 | }
140 |
141 | /**
142 | * {@inheritdoc}
143 | */
144 | public function setMaxPaymentsPerPeriod($maxPaymentsPerPeriod)
145 | {
146 | $this->request->getPreApproval()->setMaxPaymentsPerPeriod($maxPaymentsPerPeriod);
147 |
148 | return $this;
149 | }
150 |
151 | /**
152 | * {@inheritdoc}
153 | */
154 | public function setMaxAmountPerPeriod($maxAmountPerPeriod)
155 | {
156 | $this->request->getPreApproval()->setMaxAmountPerPeriod($maxAmountPerPeriod);
157 |
158 | return $this;
159 | }
160 |
161 | /**
162 | * {@inheritdoc}
163 | */
164 | public function setInitialDate(DateTime $initialDate)
165 | {
166 | $this->request->getPreApproval()->setInitialDate($initialDate);
167 |
168 | return $this;
169 | }
170 |
171 | /**
172 | * {@inheritdoc}
173 | */
174 | public function getRequest()
175 | {
176 | return $this->request;
177 | }
178 | }
179 |
--------------------------------------------------------------------------------
/src/Requests/Redirection.php:
--------------------------------------------------------------------------------
1 | code = $code;
31 | $this->date = $date;
32 | $this->uri = $uri;
33 | }
34 |
35 | /**
36 | * @return string
37 | */
38 | public function getCode()
39 | {
40 | return $this->code;
41 | }
42 |
43 | /**
44 | * @return DateTime
45 | */
46 | public function getDate()
47 | {
48 | return $this->date;
49 | }
50 |
51 | /**
52 | * @return string
53 | */
54 | public function getRedirectionUrl()
55 | {
56 | return $this->uri . '?code=' . $this->getCode();
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/Requests/RequestBuilder.php:
--------------------------------------------------------------------------------
1 |
10 | */
11 | interface RequestBuilder
12 | {
13 | /**
14 | * @param string $reference
15 | *
16 | * @return self
17 | */
18 | public function setReference($reference);
19 |
20 | /**
21 | * @param Customer $customer
22 | *
23 | * @return self
24 | */
25 | public function setCustomer(Customer $customer);
26 |
27 | /**
28 | * @param string $redirectTo
29 | *
30 | * @return self
31 | */
32 | public function setRedirectTo($redirectTo);
33 |
34 | /**
35 | * @param string $reviewOn
36 | *
37 | * @return self
38 | */
39 | public function setReviewOn($reviewOn);
40 |
41 | /**
42 | * @param string $name
43 | *
44 | * @return self
45 | */
46 | public function setName($name);
47 |
48 | /**
49 | * @param string $details
50 | *
51 | * @return self
52 | */
53 | public function setDetails($details);
54 |
55 | /**
56 | * @param string $period
57 | *
58 | * @return self
59 | */
60 | public function setPeriod($period);
61 |
62 | /**
63 | * @param DateTime $finalDate
64 | *
65 | * @return self
66 | */
67 | public function setFinalDate(DateTime $finalDate);
68 |
69 | /**
70 | * @param float $maxTotalAmount
71 | *
72 | * @return self
73 | */
74 | public function setMaxTotalAmount($maxTotalAmount);
75 |
76 | /**
77 | * @param float $amountPerPayment
78 | *
79 | * @return self
80 | */
81 | public function setAmountPerPayment($amountPerPayment);
82 |
83 | /**
84 | * @param float $maxAmountPerPayment
85 | *
86 | * @return self
87 | */
88 | public function setMaxAmountPerPayment($maxAmountPerPayment);
89 |
90 | /**
91 | * @param int $maxPaymentsPerPeriod
92 | *
93 | * @return self
94 | */
95 | public function setMaxPaymentsPerPeriod($maxPaymentsPerPeriod);
96 |
97 | /**
98 | * @param float $maxAmountPerPeriod
99 | *
100 | * @return self
101 | */
102 | public function setMaxAmountPerPeriod($maxAmountPerPeriod);
103 |
104 | /**
105 | * @param DateTime $initialDate
106 | *
107 | * @return self
108 | */
109 | public function setInitialDate(DateTime $initialDate);
110 |
111 | /**
112 | * @return Request
113 | */
114 | public function getRequest();
115 | }
116 |
--------------------------------------------------------------------------------
/src/Requests/Service.php:
--------------------------------------------------------------------------------
1 |
10 | */
11 | abstract class Service extends BaseService
12 | {
13 | /**
14 | * @param SimpleXMLElement $obj
15 | *
16 | * @return Response
17 | */
18 | protected function getRedirection(SimpleXMLElement $obj)
19 | {
20 | return new Redirection(
21 | (string) $obj->code,
22 | new DateTime((string) $obj->date),
23 | $this->credentials->getUrl(static::REDIRECT_TO)
24 | );
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/SerializerTrait.php:
--------------------------------------------------------------------------------
1 |
13 | */
14 | trait SerializerTrait
15 | {
16 | /**
17 | * @Serializer\Exclude
18 | *
19 | * @var \JMS\Serializer\Serializer
20 | */
21 | private $serializer;
22 |
23 | /**
24 | * @return \JMS\Serializer\Serializer
25 | */
26 | public function getSerializer()
27 | {
28 | if ($this->serializer === null) {
29 | $this->serializer = SerializerBuilder::create()
30 | ->setPropertyNamingStrategy(new SerializedNameAnnotationStrategy(new IdenticalPropertyNamingStrategy()))
31 | ->build();
32 | }
33 |
34 | return $this->serializer;
35 | }
36 |
37 | /**
38 | * @param SimpleXMLElement|null $xmlRoot
39 | *
40 | * @return SimpleXMLElement
41 | */
42 | public function xmlSerialize(SimpleXMLElement $xmlRoot = null)
43 | {
44 | $xmlString = $this->getSerializer()->serialize($this, 'xml');
45 | $xmlObject = new SimpleXMLElement($xmlString);
46 |
47 | if ($xmlRoot === null) {
48 | return $xmlObject;
49 | }
50 |
51 | $domRoot = dom_import_simplexml($xmlRoot);
52 | $domObject = dom_import_simplexml($xmlObject);
53 |
54 | $domRoot->appendChild($domRoot->ownerDocument->importNode($domObject, true));
55 |
56 | return $xmlRoot;
57 | }
58 |
59 | protected final function formatAmount($amount)
60 | {
61 | if ($amount === null) {
62 | return null;
63 | }
64 |
65 | return number_format($amount, 2, '.', '');
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/src/Service.php:
--------------------------------------------------------------------------------
1 |
9 | */
10 | abstract class Service
11 | {
12 | /**
13 | * @var Credentials
14 | */
15 | protected $credentials;
16 |
17 | /**
18 | * @var Client
19 | */
20 | private $client;
21 |
22 | /**
23 | * @param Credentials $credentials
24 | * @param Client $client
25 | */
26 | public function __construct(Credentials $credentials, Client $client = null)
27 | {
28 | $this->credentials = $credentials;
29 | $this->client = $client ?: new Client();
30 | }
31 |
32 | /**
33 | * @param string $resource
34 | * @param array $params
35 | *
36 | * @return SimpleXMLElement
37 | */
38 | protected function get($resource, array $params = [])
39 | {
40 | return $this->client->get($this->credentials->getWsUrl($resource, $params));
41 | }
42 |
43 | /**
44 | * @param string $resource
45 | * @param SimpleXMLElement $request
46 | *
47 | * @return SimpleXMLElement
48 | */
49 | protected function post($resource, SimpleXMLElement $request)
50 | {
51 | return $this->client->post($this->credentials->getWsUrl($resource), $request);
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/Shipping/Shipping.php:
--------------------------------------------------------------------------------
1 |
15 | */
16 | class Shipping
17 | {
18 | use SerializerTrait;
19 |
20 | /**
21 | * @Serializer\Type("integer")
22 | *
23 | * @var int
24 | */
25 | private $type;
26 |
27 | /**
28 | * @Serializer\Type("PHPSC\PagSeguro\Customer\Address")
29 | *
30 | * @var Address
31 | */
32 | private $address;
33 |
34 | /**
35 | * @Serializer\XmlElement(cdata=false)
36 | *
37 | * @var float
38 | */
39 | private $cost;
40 |
41 | /**
42 | * @param int $type
43 | * @param Address $address
44 | * @param float $cost
45 | */
46 | public function __construct($type, Address $address = null, $cost = null)
47 | {
48 | $this->setType($type);
49 | $this->address = $address;
50 |
51 | if ($cost !== null) {
52 | $this->cost = (float) $cost;
53 | }
54 | }
55 |
56 | /**
57 | * @return number
58 | */
59 | public function getType()
60 | {
61 | return $this->type;
62 | }
63 |
64 | protected function setType($type)
65 | {
66 | if (!Type::isValid($type)) {
67 | throw new InvalidArgumentException('Invalid shipping type informed');
68 | }
69 |
70 | $this->type = (int) $type;
71 | }
72 |
73 | /**
74 | * @return Address
75 | */
76 | public function getAddress()
77 | {
78 | return $this->address;
79 | }
80 |
81 | /**
82 | * @return string
83 | */
84 | public function getCost()
85 | {
86 | return $this->formatAmount($this->cost);
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/src/Shipping/Type.php:
--------------------------------------------------------------------------------
1 |
6 | */
7 | class Type
8 | {
9 | /**
10 | * @var int
11 | */
12 | const TYPE_PAC = 1;
13 |
14 | /**
15 | * @var int
16 | */
17 | const TYPE_SEDEX = 2;
18 |
19 | /**
20 | * @var int
21 | */
22 | const TYPE_UNKNOWN = 3;
23 |
24 | /**
25 | * @return array
26 | */
27 | public static function getTypes()
28 | {
29 | return [static::TYPE_PAC, static::TYPE_SEDEX, static::TYPE_UNKNOWN];
30 | }
31 |
32 | /**
33 | * @param int $type
34 | * @return boolean
35 | */
36 | public static function isValid($type)
37 | {
38 | return in_array($type, static::getTypes());
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/tests/Client/ClientTest.php:
--------------------------------------------------------------------------------
1 | httpClient = $this->createMock(ClientInterface::class);
25 | $this->request = $this->createMock(RequestInterface::class);
26 | $this->response = $this->createMock(ResponseInterface::class);
27 |
28 | $xml = <<<'XML'
29 |
30 |
31 | 8CF4BE7DCECEF0F004A6DFA0A8243412
32 | 2010-12-02T10:11:28.000-02:00
33 |
34 | XML;
35 |
36 | $this->response->expects($this->once())
37 | ->method('getBody')
38 | ->willReturn($xml);
39 | }
40 |
41 | /**
42 | * @test
43 | */
44 | public function postShouldSendTheBodyAsXml()
45 | {
46 | $client = new Client($this->httpClient);
47 | $xml = simplexml_load_string('');
48 |
49 | $this->httpClient->expects($this->once())
50 | ->method('request')
51 | ->with(
52 | 'POST',
53 | '/test',
54 | [
55 | 'headers' => ['Content-Type' => 'application/xml; charset=UTF-8'],
56 | 'body' => $xml->asXML(),
57 | 'verify' => false
58 | ]
59 | )->willReturn($this->response);
60 |
61 | $this->assertInstanceOf('SimpleXMLElement', $client->post('/test', $xml));
62 | }
63 |
64 | /**
65 | * @test
66 | */
67 | public function getShouldConfigureHeaders()
68 | {
69 | $client = new Client($this->httpClient);
70 |
71 | $this->httpClient->expects($this->once())
72 | ->method('request')
73 | ->with('GET', '/test?name=Test', ['verify' => false])
74 | ->willReturn($this->response);
75 |
76 | $this->assertInstanceOf('SimpleXMLElement', $client->get('/test?name=Test'));
77 | }
78 |
79 |
80 | }
81 |
--------------------------------------------------------------------------------
/tests/Client/PagSeguroExceptionTest.php:
--------------------------------------------------------------------------------
1 | assertInstanceOf(PagSeguroException::class, $exception);
17 | $this->assertEquals('[500] A HTTP error has occurred: Server gone mad', $exception->getMessage());
18 | }
19 |
20 | /**
21 | * @test
22 | */
23 | public function createShouldParseAsXmlWhenStatusCodeIs400()
24 | {
25 | $xml = <<<'XML'
26 |
27 |
28 |
29 | 11004
30 | Currency is required.
31 |
32 |
33 | 11005
34 | Currency invalid value: 100
35 |
36 |
37 | XML;
38 |
39 | $message = <<<'MESSAGE'
40 | Some errors occurred:
41 | [11004] Currency is required.
42 | [11005] Currency invalid value: 100
43 | MESSAGE;
44 |
45 | $response = new Response(400, [], $xml);
46 | $exception = PagSeguroException::create($response);
47 |
48 | $this->assertInstanceOf(PagSeguroException::class, $exception);
49 | $this->assertEquals($message, $exception->getMessage());
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/tests/CredentialsTest.php:
--------------------------------------------------------------------------------
1 |
8 | */
9 | class CredentialsTest extends \PHPUnit_Framework_TestCase
10 | {
11 | /**
12 | * @var Environment
13 | */
14 | private $environment;
15 |
16 | /**
17 | * {@inheritdoc}
18 | */
19 | protected function setUp()
20 | {
21 | $this->environment = $this->getMockForAbstractClass(Environment::class);
22 |
23 | $this->environment->expects($this->any())
24 | ->method('getHost')
25 | ->willReturn('test.com');
26 |
27 | $this->environment->expects($this->any())
28 | ->method('getWsHost')
29 | ->willReturn('ws.test.com');
30 | }
31 |
32 | /**
33 | * @test
34 | */
35 | public function constructShouldConfigureTheAttributes()
36 | {
37 | $credentials = new Credentials('contato@phpsc.com.br', 'testing', $this->environment);
38 |
39 | $this->assertAttributeEquals('contato@phpsc.com.br', 'email', $credentials);
40 | $this->assertAttributeEquals('testing', 'token', $credentials);
41 | $this->assertAttributeSame($this->environment, 'environment', $credentials);
42 | }
43 |
44 | /**
45 | * @test
46 | */
47 | public function constructShouldTruncateEmailAndToken()
48 | {
49 | $credentials = new Credentials(str_repeat('a', 80), str_repeat('a', 40), $this->environment);
50 |
51 | $this->assertAttributeEquals(str_repeat('a', 60), 'email', $credentials);
52 | $this->assertAttributeEquals(str_repeat('a', 32), 'token', $credentials);
53 | }
54 |
55 | /**
56 | * @test
57 | */
58 | public function constructShouldUseProductionAsDefaultEnvironment()
59 | {
60 | $credentials = new Credentials('contato@phpsc.com.br', 'testing');
61 |
62 | $this->assertAttributeInstanceOf(Production::class, 'environment', $credentials);
63 | }
64 |
65 | /**
66 | * @test
67 | */
68 | public function getUrlShouldGetTheUrlFromTheEnvironment()
69 | {
70 | $credentials = new Credentials(
71 | 'contato@phpsc.com.br',
72 | 'testing',
73 | $this->environment
74 | );
75 |
76 | $this->assertEquals('https://test.com/test', $credentials->getUrl('/test'));
77 | }
78 |
79 | /**
80 | * @test
81 | */
82 | public function getWsUrlShouldGetTheWsUrlFromTheEnvironmentAppendingEmailAndTokenAsGetParams()
83 | {
84 | $credentials = new Credentials(
85 | 'contato@phpsc.com.br',
86 | 'testing',
87 | $this->environment
88 | );
89 |
90 | $this->assertEquals(
91 | 'https://ws.test.com/test?page=1&email=contato%40phpsc.com.br&token=testing',
92 | $credentials->getWsUrl('/test', ['page' => '1'])
93 | );
94 | }
95 | }
96 |
--------------------------------------------------------------------------------
/tests/Customer/AddressTest.php:
--------------------------------------------------------------------------------
1 | address = new Address(
14 | 'sca',
15 | 'Florianopolis',
16 | '12345-1231',
17 | 'Centro',
18 | 'Avenida Mauro Ramos Euripedes da Silva Santos Oliveira Carlos Henrique Salvador das Palmeiras',
19 | 123,
20 | 'Apto 200'
21 | );
22 | }
23 |
24 | /**
25 | * @test
26 | */
27 | public function constructorShouldConfigureTheAttributes()
28 | {
29 | $this->assertAttributeEquals('BRA', 'country', $this->address);
30 | $this->assertAttributeEquals('sca', 'state', $this->address);
31 | $this->assertAttributeEquals('Florianopolis', 'city', $this->address);
32 | $this->assertAttributeEquals('123451231', 'postalCode', $this->address);
33 | $this->assertAttributeEquals('Centro', 'district', $this->address);
34 | $this->assertAttributeEquals('123', 'number', $this->address);
35 | $this->assertAttributeEquals('Apto 200', 'complement', $this->address);
36 |
37 | $this->assertAttributeEquals(
38 | 'Avenida Mauro Ramos Euripedes da Silva Santos Oliveira Carlos Henrique Salvador das Palmeiras',
39 | 'street',
40 | $this->address
41 | );
42 | }
43 |
44 | /**
45 | * @test
46 | */
47 | public function gettersShouldReturnTheAttributeValue()
48 | {
49 | $this->assertAttributeEquals($this->address->getCountry(), 'country', $this->address);
50 | $this->assertAttributeEquals($this->address->getState(), 'state', $this->address);
51 | $this->assertAttributeEquals($this->address->getCity(), 'city', $this->address);
52 | $this->assertAttributeEquals($this->address->getPostalCode(), 'postalCode', $this->address);
53 | $this->assertAttributeEquals($this->address->getDistrict(), 'district', $this->address);
54 | $this->assertAttributeEquals($this->address->getStreet(), 'street', $this->address);
55 | $this->assertAttributeEquals($this->address->getNumber(), 'number', $this->address);
56 | $this->assertAttributeEquals($this->address->getComplement(), 'complement', $this->address);
57 | }
58 |
59 | /**
60 | * @testAppendFormattedValuesOnAChildNode
61 | */
62 | public function xmlSerializeShouldAppendFormattedValuesOnAChildNode()
63 | {
64 | $data = simplexml_load_string('');
65 | $xml = $this->address->xmlSerialize($data);
66 |
67 | $this->assertSame($data, $xml);
68 | $this->assertEquals('BRA', (string) $xml->address->country);
69 | $this->assertEquals('sca', (string) $xml->address->state);
70 | $this->assertEquals('Florianopolis', (string) $xml->address->city);
71 | $this->assertEquals('123451231', (string) $xml->address->postalCode);
72 | $this->assertEquals('Centro', (string) $xml->address->district);
73 | $this->assertEquals('123', (string) $xml->address->number);
74 | $this->assertEquals('Apto 200', (string) $xml->address->complement);
75 |
76 | $this->assertEquals(
77 | 'Avenida Mauro Ramos Euripedes da Silva Santos Oliveira Carlos Henrique Salvador das Palmeiras',
78 | (string) $xml->address->street
79 | );
80 | }
81 |
82 | /**
83 | * @test
84 | */
85 | public function xmlSerializeShouldNotAppendComplementIfItWasntInformed()
86 | {
87 | $address = new Address(
88 | 'SC',
89 | 'Florianopolis',
90 | '12345-1231',
91 | 'Centro',
92 | 'Avenida Mauro Ramos Euripedes da Silva Santos Oliveira Carlos Henrique Salvador',
93 | 123
94 | );
95 |
96 | $data = simplexml_load_string('');
97 | $xml = $address->xmlSerialize($data);
98 |
99 | $this->assertSame($data, $xml);
100 | $this->assertEquals('BRA', (string) $xml->address->country);
101 | $this->assertEquals('SC', (string) $xml->address->state);
102 | $this->assertEquals('Florianopolis', (string) $xml->address->city);
103 | $this->assertEquals('123451231', (string) $xml->address->postalCode);
104 | $this->assertEquals('Centro', (string) $xml->address->district);
105 | $this->assertEquals('123', (string) $xml->address->number);
106 | $this->assertEmpty($xml->xpath('//address/complement'));
107 |
108 |
109 | $this->assertEquals(
110 | 'Avenida Mauro Ramos Euripedes da Silva Santos Oliveira Carlos Henrique Salvador',
111 | (string) $xml->address->street
112 | );
113 | }
114 | }
115 |
--------------------------------------------------------------------------------
/tests/Customer/CustomerTest.php:
--------------------------------------------------------------------------------
1 | assertAttributeEquals('aa@test.com', 'email', $customer);
16 | $this->assertAttributeEquals('aa', 'name', $customer);
17 | $this->assertAttributeSame($phone, 'phone', $customer);
18 | $this->assertAttributeSame($address, 'address', $customer);
19 |
20 | return $customer;
21 | }
22 |
23 | /**
24 | * @test
25 | */
26 | public function gettersShouldRetrieveConfiguredData()
27 | {
28 | $phone = new Phone(11, 999999999);
29 | $address = new Address('aa', 'aa', '2123', 'aa', 'asdad', 12312);
30 | $customer = new Customer('aa@test.com', 'aa', $phone, $address);
31 |
32 | $this->assertEquals('aa@test.com', $customer->getEmail());
33 | $this->assertEquals('aa', $customer->getName());
34 | $this->assertSame($phone, $customer->getPhone());
35 | $this->assertSame($address, $customer->getAddress());
36 | }
37 |
38 | /**
39 | * @test
40 | */
41 | public function xmlSerializeMustAppendFormattedCustomerData()
42 | {
43 | $name = str_repeat('a', 55);
44 | $data = simplexml_load_string('');
45 |
46 | $phone = new Phone(12, 999999989);
47 | $address = new Address('DF', 'Brasília', '70999-999', 'Plano Piloto', 'SQWN 17', 12, 'Apto 507');
48 | $customer = new Customer($name . '@test.com', $name, $phone, $address);
49 |
50 | $xml = $customer->xmlSerialize($data);
51 |
52 | $this->assertEquals($name . '@test.com', (string) $xml->sender->email);
53 | $this->assertEquals(str_repeat('a', 55), (string) $xml->sender->name);
54 |
55 | $this->assertEquals('BRA', (string) $xml->sender->address->country);
56 | $this->assertEquals('DF', (string) $xml->sender->address->state);
57 | $this->assertEquals('Brasília', (string) $xml->sender->address->city);
58 | $this->assertEquals('70999999', (string) $xml->sender->address->postalCode);
59 | $this->assertEquals('Plano Piloto', (string) $xml->sender->address->district);
60 | $this->assertEquals('SQWN 17', (string) $xml->sender->address->street);
61 | $this->assertEquals('12', (string) $xml->sender->address->number);
62 | $this->assertEquals('Apto 507', (string) $xml->sender->address->complement);
63 |
64 | $this->assertEquals(12, (string) $xml->sender->phone->areaCode);
65 | $this->assertEquals(999999989, (string) $xml->sender->phone->number);
66 | }
67 |
68 | /**
69 | * @test
70 | */
71 | public function xmlSerializeShouldNotAppendAddressIfItWasntInformed()
72 | {
73 | $name = str_repeat('a', 55);
74 | $data = simplexml_load_string('');
75 |
76 | $phone = new Phone(12, 999999989);
77 | $customer = new Customer($name . '@test.com', $name, $phone);
78 |
79 | $xml = $customer->xmlSerialize($data);
80 |
81 | $this->assertEquals($name . '@test.com', (string) $xml->sender->email);
82 | $this->assertEquals(str_repeat('a', 55), (string) $xml->sender->name);
83 |
84 | $this->assertEquals(12, (string) $xml->sender->phone->areaCode);
85 | $this->assertEquals(999999989, (string) $xml->sender->phone->number);
86 |
87 | $this->assertEmpty($xml->xpath('//sender/address'));
88 | }
89 |
90 | /**
91 | * @test
92 | */
93 | public function xmlSerializeShouldNotAppendPhoneIfItWasntInformed()
94 | {
95 | $name = str_repeat('a', 55);
96 | $data = simplexml_load_string('');
97 |
98 | $address = new Address('DF', 'Brasília', '70999-999', 'Plano Piloto', 'SQWN 17', 12, 'Apto 507');
99 | $customer = new Customer($name . '@test.com', $name, null, $address);
100 |
101 | $xml = $customer->xmlSerialize($data);
102 |
103 | $this->assertEquals($name . '@test.com', (string) $xml->sender->email);
104 | $this->assertEquals(str_repeat('a', 55), (string) $xml->sender->name);
105 |
106 | $this->assertEquals('BRA', (string) $xml->sender->address->country);
107 | $this->assertEquals('DF', (string) $xml->sender->address->state);
108 | $this->assertEquals('Brasília', (string) $xml->sender->address->city);
109 | $this->assertEquals('70999999', (string) $xml->sender->address->postalCode);
110 | $this->assertEquals('Plano Piloto', (string) $xml->sender->address->district);
111 | $this->assertEquals('SQWN 17', (string) $xml->sender->address->street);
112 | $this->assertEquals('12', (string) $xml->sender->address->number);
113 | $this->assertEquals('Apto 507', (string) $xml->sender->address->complement);
114 |
115 | $this->assertEmpty($xml->xpath('//sender/phone'));
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/tests/Customer/PhoneTest.php:
--------------------------------------------------------------------------------
1 | assertAttributeEquals(479, 'areaCode', $phone);
14 | $this->assertAttributeEquals(1234567890, 'number', $phone);
15 | }
16 |
17 | /**
18 | * @test
19 | */
20 | public function gettersShouldReturnConfiguredData()
21 | {
22 | $phone = new Phone(47, 98761234);
23 |
24 | $this->assertEquals(47, $phone->getAreaCode());
25 | $this->assertEquals(98761234, $phone->getNumber());
26 | }
27 |
28 | /**
29 | * @test
30 | */
31 | public function xmlSerializeMustAppendFormattedPhoneData()
32 | {
33 |
34 | $data = simplexml_load_string('');
35 |
36 | $phone = new Phone(47, 1234567890);
37 | $xml = $phone->xmlSerialize($data);
38 |
39 | $this->assertSame($data, $xml);
40 | $this->assertEquals(47, (string) $xml->phone->areaCode);
41 | $this->assertEquals(1234567890, (string) $xml->phone->number);
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/tests/EnvironmentTest.php:
--------------------------------------------------------------------------------
1 |
9 | */
10 | class EnvironmentTest extends \PHPUnit_Framework_TestCase
11 | {
12 | /**
13 | * @var Environment
14 | */
15 | private $environment;
16 |
17 | /**
18 | * {@inheritdoc}
19 | */
20 | protected function setUp()
21 | {
22 | $this->environment = $this->getMockForAbstractClass(Environment::class);
23 |
24 | $this->environment->expects($this->any())
25 | ->method('getHost')
26 | ->willReturn('test.com');
27 |
28 | $this->environment->expects($this->any())
29 | ->method('getWsHost')
30 | ->willReturn('ws.test.com');
31 | }
32 |
33 | /**
34 | * @test
35 | */
36 | public function isValidShouldReturnTrueWhenHostIsProduction()
37 | {
38 | $this->assertTrue(Environment::isValid(Production::WS_HOST));
39 | }
40 |
41 | /**
42 | * @test
43 | */
44 | public function isValidShouldReturnFalseWhenHostIsSandbox()
45 | {
46 | $this->assertTrue(Environment::isValid(Sandbox::WS_HOST));
47 | }
48 |
49 | /**
50 | * @test
51 | */
52 | public function isValidShouldReturnFalseWhenHostNotProductionOrSandbox()
53 | {
54 | $this->assertFalse(Environment::isValid('example.org'));
55 | }
56 |
57 | /**
58 | * @test
59 | */
60 | public function getWsUrlShouldAppendProtocolAndWsHostToResource()
61 | {
62 | $this->assertEquals('https://ws.test.com/test', $this->environment->getWsUrl('/test'));
63 | }
64 |
65 | /**
66 | * @test
67 | */
68 | public function getUrlShouldAppendProtocolAndHostToResource()
69 | {
70 | $this->assertEquals('https://test.com/test', $this->environment->getUrl('/test'));
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/tests/Environments/ProductionTest.php:
--------------------------------------------------------------------------------
1 |
6 | */
7 | class ProductionTest extends \PHPUnit_Framework_TestCase
8 | {
9 | /**
10 | * @test
11 | */
12 | public function getHostShouldReturnTheConstantValue()
13 | {
14 | $env = new Production();
15 |
16 | $this->assertEquals(Production::HOST, $env->getHost());
17 | }
18 |
19 | /**
20 | * @test
21 | */
22 | public function getWsHostShouldReturnTheConstantValue()
23 | {
24 | $env = new Production();
25 |
26 | $this->assertEquals(Production::WS_HOST, $env->getWsHost());
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/tests/Environments/SandboxTest.php:
--------------------------------------------------------------------------------
1 |
6 | */
7 | class SandboxTest extends \PHPUnit_Framework_TestCase
8 | {
9 | /**
10 | * @test
11 | */
12 | public function getHostShouldReturnTheConstantValue()
13 | {
14 | $env = new Sandbox();
15 |
16 | $this->assertEquals(Sandbox::HOST, $env->getHost());
17 | }
18 |
19 | /**
20 | * @test
21 | */
22 | public function getWsHostShouldReturnTheConstantValue()
23 | {
24 | $env = new Sandbox();
25 |
26 | $this->assertEquals(Sandbox::WS_HOST, $env->getWsHost());
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/tests/Items/ItemTest.php:
--------------------------------------------------------------------------------
1 | item = new Item(
14 | str_repeat('01', 51),
15 | str_repeat('a very long description', 100),
16 | 150.23,
17 | 3,
18 | 10.30,
19 | 123
20 | );
21 | }
22 |
23 | /**
24 | * @test
25 | */
26 | public function constructorShouldConfigureTheAttributes()
27 | {
28 | $this->assertAttributeSame(str_repeat('01', 51), 'id', $this->item);
29 | $this->assertAttributeSame(str_repeat('a very long description', 100), 'description', $this->item);
30 | $this->assertAttributeSame(150.23, 'amount', $this->item);
31 | $this->assertAttributeSame(3, 'quantity', $this->item);
32 | $this->assertAttributeSame(10.30, 'shippingCost', $this->item);
33 | $this->assertAttributeSame(123, 'weight', $this->item);
34 | }
35 |
36 | /**
37 | * @test
38 | */
39 | public function gettersShouldReturnTheAttributeValue()
40 | {
41 | $this->assertAttributeSame($this->item->getId(), 'id', $this->item);
42 | $this->assertAttributeSame($this->item->getDescription(), 'description', $this->item);
43 | $this->assertAttributeEquals($this->item->getAmount(), 'amount', $this->item);
44 | $this->assertAttributeSame($this->item->getQuantity(), 'quantity', $this->item);
45 | $this->assertAttributeEquals($this->item->getShippingCost(), 'shippingCost', $this->item);
46 | $this->assertAttributeSame($this->item->getWeight(), 'weight', $this->item);
47 | }
48 |
49 | /**
50 | * @test
51 | */
52 | public function xmlSerializeMustAppendFormattedItemData()
53 | {
54 | $data = simplexml_load_string('');
55 | $xml = $this->item->xmlSerialize($data);
56 |
57 | $this->assertEquals(str_repeat('01', 51), (string) $xml->item->id);
58 | $this->assertEquals(str_repeat('a very long description', 100), (string) $xml->item->description);
59 | $this->assertEquals(150.23, (string) $xml->item->amount);
60 | $this->assertEquals(3, (string) $xml->item->quantity);
61 | $this->assertEquals(10.30, (string) $xml->item->shippingCost);
62 | $this->assertEquals('123', (string) $xml->item->weight);
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/tests/Items/ItemsTest.php:
--------------------------------------------------------------------------------
1 |
8 | */
9 | class ItemsTest extends \PHPUnit_Framework_TestCase
10 | {
11 | /**
12 | * @test
13 | */
14 | public function constructorShouldInstanceOf()
15 | {
16 | $items = new Items;
17 |
18 | $this->assertInstanceOf(ArrayCollection::class, $items);
19 | $this->assertInstanceOf(ItemCollection::class, $items);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/tests/Purchases/DetailsTest.php:
--------------------------------------------------------------------------------
1 |
9 | */
10 | class DetailsTest extends \PHPUnit_Framework_TestCase
11 | {
12 | /**
13 | * @var Details
14 | */
15 | private $details;
16 |
17 | /**
18 | * @var DateTime
19 | */
20 | private $date;
21 |
22 | /**
23 | * @var DateTime
24 | */
25 | private $lastEventDate;
26 |
27 | /**
28 | * @var Customer
29 | */
30 | private $customer;
31 |
32 | protected function setUp()
33 | {
34 | $this->date = new DateTime("2015-01-01");
35 | $this->lastEventDate = new DateTime("2015-01-01");
36 | $this->customer = $this->createMock(Customer::class);
37 | $this->details = new Details(
38 | '1',
39 | 'SomeRef',
40 | 2,
41 | $this->date,
42 | $this->lastEventDate,
43 | $this->customer
44 | );
45 | }
46 |
47 | /**
48 | * @test
49 | */
50 | public function constructShouldConfigureTheAttributes()
51 | {
52 | $this->assertAttributeEquals('1', 'code', $this->details);
53 | $this->assertAttributeEquals('SomeRef', 'reference', $this->details);
54 | $this->assertAttributeEquals(2, 'status', $this->details);
55 | $this->assertAttributeSame($this->date, 'date', $this->details);
56 | $this->assertAttributeSame($this->lastEventDate, 'lastEventDate', $this->details);
57 | $this->assertAttributeSame($this->customer, 'customer', $this->details);
58 | }
59 |
60 | /**
61 | * @test
62 | * @depends constructShouldConfigureTheAttributes
63 | */
64 | public function getCodeShouldReturnTheConfiguredCode()
65 | {
66 | $this->assertEquals("1", $this->details->getCode());
67 | }
68 |
69 | /**
70 | * @test
71 | * @depends constructShouldConfigureTheAttributes
72 | */
73 | public function getReferenceShouldReturnTheConfiguredReference()
74 | {
75 | $this->assertEquals("SomeRef", $this->details->getReference());
76 | }
77 |
78 | /**
79 | * @test
80 | * @depends constructShouldConfigureTheAttributes
81 | */
82 | public function getStatusShouldReturnTheConfiguredStatus()
83 | {
84 | $this->assertEquals(2, $this->details->getStatus());
85 | }
86 |
87 | /**
88 | * @test
89 | * @depends constructShouldConfigureTheAttributes
90 | */
91 | public function getDateShouldReturnTheConfiredDate()
92 | {
93 | $this->assertSame($this->date, $this->details->getDate());
94 | }
95 |
96 | /**
97 | * @test
98 | * @depends constructShouldConfigureTheAttributes
99 | */
100 | public function getLastEventDateShouldReturnTheConfiredLastEventDate()
101 | {
102 | $this->assertSame($this->lastEventDate, $this->details->getLastEventDate());
103 | }
104 |
105 | /**
106 | * @test
107 | * @depends constructShouldConfigureTheAttributes
108 | */
109 | public function getCustomerShouldReturnTheConfiredCustomer()
110 | {
111 | $this->assertSame($this->customer, $this->details->getCustomer());
112 | }
113 | }
114 |
--------------------------------------------------------------------------------
/tests/Purchases/Subscriptions/CancellationResponseTest.php:
--------------------------------------------------------------------------------
1 |
8 | */
9 | class CancellationResponseTest extends \PHPUnit_Framework_TestCase
10 | {
11 | /**
12 | * @var CancellationResponse
13 | */
14 | private $cancellationResponse;
15 |
16 | /**
17 | * @var DateTime
18 | */
19 | private $date;
20 |
21 | public function setUp()
22 | {
23 | $this->date = new DateTime('2015-01-01');
24 | $this->cancellationResponse = new CancellationResponse('someStatus', $this->date);
25 | }
26 |
27 | /**
28 | * @test
29 | */
30 | public function constructShouldConfigureTheAttributes()
31 | {
32 | $this->assertAttributeEquals('someStatus', 'status', $this->cancellationResponse);
33 | $this->assertAttributeSame($this->date, 'date', $this->cancellationResponse);
34 | }
35 |
36 | /**
37 | * @test
38 | * @depends constructShouldConfigureTheAttributes
39 | */
40 | public function getStatusShouldReturnTheConfiguredStatus()
41 | {
42 | $this->assertEquals('someStatus', $this->cancellationResponse->getStatus());
43 | }
44 |
45 | /**
46 | * @test
47 | * @depends constructShouldConfigureTheAttributes
48 | */
49 | public function getDateShouldReturnTheConfiredDate()
50 | {
51 | $this->assertSame($this->date, $this->cancellationResponse->getDate());
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/tests/Purchases/Subscriptions/ChargeBuilderTest.php:
--------------------------------------------------------------------------------
1 |
10 | */
11 | class ChargeBuilderTest extends \PHPUnit_Framework_TestCase
12 | {
13 | private $charge;
14 |
15 | protected function setUp()
16 | {
17 | $this->charge = $this->createMock(Charge::class);
18 |
19 | $this->charge->expects($this->once())
20 | ->method('setSubscriptionCode')
21 | ->with('123456');
22 | }
23 |
24 | public function testConstructShouldDoCallSetSubscription()
25 | {
26 | $builder = new ChargeBuilder('123456', $this->charge);
27 |
28 | $this->assertInstanceOf(ChargeBuilderInterface::class, $builder);
29 | $this->assertEquals($this->charge, $builder->getCharge());
30 |
31 | }
32 |
33 | public function testAddItemShouldDoCallAddInChargeAndReturnSelfObject()
34 | {
35 | $item = new Item(99, 'Produto 03', 1.77, 8, 12.9, 360);
36 | $items = $this->createMock(Items::class);
37 | $items->expects($this->once())->method('add')->with($item);
38 | $this->charge->expects($this->once())
39 | ->method('getItems')
40 | ->willReturn($items);
41 |
42 | $builder = new ChargeBuilder('123456', $this->charge);
43 |
44 | $this->assertEquals($builder, $builder->addItem($item));
45 | }
46 |
47 | public function testsetReferenceShouldDoCallInChargeAndReturnSelfObject()
48 | {
49 | $this->charge->expects($this->once())
50 | ->method('setReference')
51 | ->with('ABCDE');
52 |
53 | $builder = new ChargeBuilder('123456', $this->charge);
54 |
55 | $this->assertEquals($builder, $builder->setReference('ABCDE'));
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/tests/Purchases/Subscriptions/ChargeResponseTest.php:
--------------------------------------------------------------------------------
1 |
8 | */
9 | class ChargeResponseTest extends \PHPUnit_Framework_TestCase
10 | {
11 | /**
12 | * @var ChargeResponse
13 | */
14 | private $chargeResponse;
15 |
16 | /**
17 | * @var DateTime
18 | */
19 | private $date;
20 |
21 | public function setUp()
22 | {
23 | $this->date = new DateTime('2015-01-01');
24 | $this->chargeResponse = new ChargeResponse('someTransactionCode', $this->date);
25 | }
26 |
27 | /**
28 | * @test
29 | */
30 | public function constructShouldConfigureTheAttributes()
31 | {
32 | $this->assertAttributeEquals('someTransactionCode', 'transactionCode', $this->chargeResponse);
33 | $this->assertAttributeSame($this->date, 'date', $this->chargeResponse);
34 | }
35 |
36 | /**
37 | * @test
38 | * @depends constructShouldConfigureTheAttributes
39 | */
40 | public function getTransactionCodeShouldReturnTheConfiguredTransactionCode()
41 | {
42 | $this->assertEquals('someTransactionCode', $this->chargeResponse->getTransactionCode());
43 | }
44 |
45 | /**
46 | * @test
47 | * @depends constructShouldConfigureTheAttributes
48 | */
49 | public function getDateShouldReturnTheConfiredDate()
50 | {
51 | $this->assertSame($this->date, $this->chargeResponse->getDate());
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/tests/Purchases/Subscriptions/ChargeTest.php:
--------------------------------------------------------------------------------
1 |
11 | */
12 | class ChargeTest extends \PHPUnit_Framework_TestCase
13 | {
14 | /**
15 | * @var Charge
16 | */
17 | private $charge;
18 |
19 | protected function setUp()
20 | {
21 | $this->charge = new Charge();
22 | }
23 |
24 | /**
25 | * @test
26 | */
27 | public function constructShouldConfigureTheAttributes()
28 | {
29 | $this->assertInstanceOf(ItemCollection::class, $this->charge->getItems());
30 | }
31 |
32 | /**
33 | * @test
34 | */
35 | public function setReferenceShouldConfigureTheReference()
36 | {
37 | $charge = new Charge();
38 | $charge->setReference('SomeRef');
39 |
40 | $this->assertAttributeEquals('SomeRef', 'reference', $charge);
41 | $this->assertEquals('SomeRef', $charge->getReference());
42 | }
43 |
44 | /**
45 | * @test
46 | */
47 | public function setSubscriptionCodeShouldConfigureTheSubscriptionCode()
48 | {
49 | $charge = new Charge();
50 | $charge->setSubscriptionCode('SomeSubscription');
51 |
52 | $this->assertAttributeEquals('SomeSubscription', 'subscriptionCode', $charge);
53 | $this->assertEquals('SomeSubscription', $charge->getSubscriptionCode());
54 | }
55 |
56 | public function testSerializeShouldXMLFull()
57 | {
58 | $items = new Items;
59 | $items->add(new Item(99, 'Produto 03', 1.77, 8, 12.9, 360));
60 | $items->add(new Item(97, 'Produto 04', 43.67, 3, 134.98, 1100));
61 |
62 | $charge = new Charge($items);
63 | $charge->setSubscriptionCode(4556788);
64 | $charge->setReference('abcdef');
65 |
66 | $xml = $charge->xmlSerialize();
67 |
68 | $this->assertInstanceOf(SimpleXMLElement::class, $xml);
69 | $expected = simplexml_load_file(__DIR__.'/xml/chargeFull.xml');
70 | $this->assertEquals($expected, $xml);
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/tests/Purchases/Subscriptions/DecoderTest.php:
--------------------------------------------------------------------------------
1 |
12 | */
13 | class DecoderTest extends \PHPUnit_Framework_TestCase
14 | {
15 | public function testDecodeShouldDoReturningObjectSubscription()
16 | {
17 | $detailsDate = new DateTime('2011-11-23T13:40:00.000-02:00');
18 | $detailsLastEventDate = new DateTime('2011-11-25T20:04:00.000-02:00');
19 | $detailsAddress = new Address('SP', 'SAO PAULO', '01421000', 'J Paulista', 'ALAMEDAITU', '78', 'ap.2601');
20 | $customer = new Customer('comprador@uol.com', 'Nome Comprador', new Phone('11', '30389678'), $detailsAddress);
21 | $details = new Details('C08984', 'REF1234', 'CANCELLED', $detailsDate, $detailsLastEventDate, $customer);
22 |
23 | $subscription = new Subscription('Seguro Notebook', $details, '538C53', 'auto');
24 |
25 | $obj = simplexml_load_file(__DIR__.'/xml/preApproval.xml');
26 |
27 | $decoder = new Decoder;
28 | $this->assertEquals($subscription, $decoder->decode($obj));
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/tests/Purchases/Subscriptions/LocatorTest.php:
--------------------------------------------------------------------------------
1 |
14 | */
15 | class LocatorTest extends \PHPUnit_Framework_TestCase
16 | {
17 | public function testConstructShouldSettersDecoder()
18 | {
19 | $credentials = $this->createMock(Credentials::class);
20 | $client = $this->createMock(Client::class);
21 | $decoder = $this->createMock(Decoder::class);
22 |
23 | $locator = new Locator($credentials, $client, $decoder);
24 |
25 | $this->assertInstanceOf(NotificationService::class, $locator);
26 | $this->assertInstanceOf(SearchService::class, $locator);
27 | $this->assertInstanceOf(Service::class, $locator);
28 | $this->assertAttributeSame($decoder, 'decoder', $locator);
29 | }
30 |
31 | public function testGetByCodeShouldDoAGetRequestAddingCredentialsData()
32 | {
33 | $credentials = $this->createMock(Credentials::class);
34 | $client = $this->createMock(CLient::class);
35 | $decoder = $this->createMock(Decoder::class);
36 |
37 | $locator = new Locator($credentials, $client, $decoder);
38 |
39 | $wsUrl = 'https://ws.test.com/v2/transactions?token=zzzzz';
40 |
41 | $credentials->expects($this->once())
42 | ->method('getWsUrl')
43 | ->with('/v2/pre-approvals/123456', [])
44 | ->willReturn($wsUrl);
45 |
46 | $xml = new SimpleXMLElement('');
47 | $client->expects($this->once())->method('get')->with($wsUrl)->willReturn($xml);
48 |
49 | $transaction = $this->createMock(Transaction::class);
50 | $decoder->expects($this->once())->method('decode')->with($xml)->willReturn($transaction);
51 |
52 | $this->assertEquals($transaction, $locator->getByCode('123456'));
53 | }
54 |
55 | public function testGetByNotificationShouldDoAGetRequestAddingCredentialsData()
56 | {
57 | $credentials = $this->createMock(Credentials::class);
58 | $client = $this->createMock(Client::class);
59 | $decoder = $this->createMock(Decoder::class);
60 |
61 | $locator = new Locator($credentials, $client, $decoder);
62 |
63 | $wsUrl = 'https://ws.test.com/v2/transactions?token=xxxxx';
64 | $credentials->expects($this->once())
65 | ->method('getWsUrl')
66 | ->with('/v2/pre-approvals/notifications/abcd', [])
67 | ->willReturn($wsUrl);
68 |
69 | $xml = new SimpleXMLElement('');
70 | $client->expects($this->once())->method('get')->with($wsUrl)->willReturn($xml);
71 |
72 | $transaction = $this->createMock(Transaction::class);
73 |
74 | $decoder->expects($this->once())
75 | ->method('decode')
76 | ->with($xml)
77 | ->willReturn($transaction);
78 |
79 | $this->assertEquals($transaction, $locator->getByNotification('abcd'));
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/tests/Purchases/Subscriptions/SubscriptionServiceTest.php:
--------------------------------------------------------------------------------
1 |
13 | */
14 | class SubscriptionServiceTest extends \PHPUnit_Framework_TestCase
15 | {
16 | /**
17 | * @var Credentials|\PHPUnit_Framework_MockObject_MockObject
18 | */
19 | private $credentials;
20 |
21 | /**
22 | * @var Client|\PHPUnit_Framework_MockObject_MockObject
23 | */
24 | private $client;
25 |
26 | /**
27 | * @var ChargeSerializer|\PHPUnit_Framework_MockObject_MockObject
28 | */
29 | private $serializer;
30 |
31 | protected function setUp()
32 | {
33 | $this->credentials = $this->createMock(Credentials::class);
34 | $this->client = $this->createMock(Client::class);
35 | }
36 |
37 | public function testConstructShouldSettersDecoder()
38 | {
39 | $service = new SubscriptionService($this->credentials, $this->client);
40 |
41 | $this->assertInstanceOf(SubscriptionServiceInterface::class, $service);
42 | $this->assertInstanceOf(Service::class, $service);
43 | }
44 |
45 | public function testCreateChargeBuilderShouldReturnObjectBuilder()
46 | {
47 | $service = new SubscriptionService($this->credentials, $this->client);
48 |
49 | $this->assertEquals(new ChargeBuilder('ABCDEF'), $service->createChargeBuilder('ABCDEF'));
50 | }
51 |
52 | public function testCancelShouldDoReturnCancellationResponse()
53 | {
54 | $wsUrl = 'https://ws.test.com/v2/transactions?token=zzzzz';
55 | $this->credentials
56 | ->expects($this->once())
57 | ->method('getWsUrl')
58 | ->with('/v2/pre-approvals/cancel/ABCDEF', [])
59 | ->willReturn($wsUrl);
60 |
61 | $response = '';
62 | $response .= '62015-11-19T11:33:54.000-03:00';
63 | $response .= '';
64 | $xmlResponse = new SimpleXMLElement($response);
65 | $this->client
66 | ->expects($this->once())
67 | ->method('get')
68 | ->with($wsUrl)
69 | ->willReturn($xmlResponse);
70 |
71 | $service = new SubscriptionService($this->credentials, $this->client);
72 |
73 | $expected = new CancellationResponse('6', new DateTime('2015-11-19T11:33:54.000-03:00'));
74 | $this->assertEquals($expected, $service->cancel('ABCDEF'));
75 | }
76 |
77 | public function testChargeShouldDoReturnChargeResponse()
78 | {
79 | $charge = $this->createMock(Charge::class);
80 |
81 | $request = '';
82 | $xmlRequest = new SimpleXMLElement($request);
83 | $charge
84 | ->expects($this->once())
85 | ->method('xmlSerialize')
86 | ->willReturn($xmlRequest);
87 |
88 | $wsUrl = 'https://ws.test.com/v2/transactions?token=zzzzz';
89 | $this->credentials
90 | ->expects($this->once())
91 | ->method('getWsUrl')
92 | ->with('/v2/pre-approvals/payment')
93 | ->willReturn($wsUrl);
94 |
95 | $response = '';
96 | $response .= '1234562015-11-19T11:33:54.000-03:00';
97 | $response .= '';
98 | $xmlResponse = new SimpleXMLElement($response);
99 | $this->client
100 | ->expects($this->once())
101 | ->method('post')
102 | ->with($wsUrl, $xmlRequest)
103 | ->willReturn($xmlResponse);
104 |
105 | $service = new SubscriptionService($this->credentials, $this->client);
106 |
107 | $expected = new ChargeResponse('123456', new DateTime('2015-11-19T11:33:54.000-03:00'));
108 | $this->assertEquals($expected, $service->charge($charge));
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/tests/Purchases/Subscriptions/SubscriptionTest.php:
--------------------------------------------------------------------------------
1 |
12 | */
13 | class SubscriptionTest extends \PHPUnit_Framework_TestCase
14 | {
15 | protected function setUp()
16 | {
17 | $this->dDate = new DateTime('2011-11-23T13:40:00.000-02:00');
18 | $this->eventDate = new DateTime('2011-11-25T20:04:00.000-02:00');
19 | $this->detailsAddress = new Address('SP', 'SAO PAULO', '01421000', 'J Paulista', 'ALAMEDAITU', '78', 'ap.2601');
20 | $this->customer = new Customer('a@uol.com', 'Comprador', new Phone('11', '30389678'), $this->detailsAddress);
21 | }
22 |
23 | public function testConstructShouldReturnOfGetters()
24 | {
25 | $details = new Details('C89', 'R12', '', $this->dDate, $this->eventDate, $this->customer);
26 | $subscription = new Subscription('FooBar', $details, 'TRACKER-AAAA', '');
27 |
28 | $this->assertEquals('FooBar', $subscription->getName());
29 | $this->assertEquals($details, $subscription->getDetails());
30 | $this->assertEquals('TRACKER-AAAA', $subscription->getTracker());
31 |
32 | $this->assertFalse($subscription->isAutomatic());
33 | $this->assertFalse($subscription->isManual());
34 | $this->assertFalse($subscription->isInitiated());
35 | $this->assertFalse($subscription->isPending());
36 | $this->assertFalse($subscription->isActive());
37 | $this->assertFalse($subscription->isCancelledByAcquirer());
38 | $this->assertFalse($subscription->isCancelledByReceiver());
39 | $this->assertFalse($subscription->isCancelledByCustomer());
40 | $this->assertFalse($subscription->isExpired());
41 | }
42 |
43 | public function testIsAutomaticShouldReturnTrue()
44 | {
45 | $details = new Details('C89', 'R12', '', $this->dDate, $this->eventDate, $this->customer);
46 | $subscription = new Subscription('FooBar', $details, 'TRACKER-AAAA', 'auto');
47 |
48 | $this->assertTrue($subscription->isAutomatic());
49 | $this->assertFalse($subscription->isManual());
50 | }
51 |
52 | public function testIsManualShouldReturnTrue()
53 | {
54 | $details = new Details('C89', 'R12', '', $this->dDate, $this->eventDate, $this->customer);
55 | $subscription = new Subscription('FooBar', $details, 'TRACKER-AAAA', 'manual');
56 |
57 | $this->assertTrue($subscription->isManual());
58 | $this->assertFalse($subscription->isAutomatic());
59 | }
60 |
61 | public function testIsInitiatedShouldReturnTrue()
62 | {
63 | $details = new Details('C89', 'R12', 'INITIATED', $this->dDate, $this->eventDate, $this->customer);
64 |
65 | $subscription = new Subscription('FooBar', $details, 'TRACKER-AAAA', 'manual');
66 |
67 | $this->assertTrue($subscription->isInitiated());
68 | }
69 |
70 | public function testIsPendingShouldReturnTrue()
71 | {
72 | $details = new Details('C89', 'R12', 'PENDING', $this->dDate, $this->eventDate, $this->customer);
73 |
74 | $subscription = new Subscription('FooBar', $details, 'TRACKER-AAAA', 'manual');
75 |
76 | $this->assertTrue($subscription->isPending());
77 | }
78 |
79 | public function testIsActiveShouldReturnTrue()
80 | {
81 | $details = new Details('C89', 'R12', 'ACTIVE', $this->dDate, $this->eventDate, $this->customer);
82 |
83 | $subscription = new Subscription('FooBar', $details, 'TRACKER-AAAA', 'manual');
84 |
85 | $this->assertTrue($subscription->isActive());
86 | }
87 |
88 | public function testIsCancelledByAcquirerShouldReturnTrue()
89 | {
90 | $details = new Details('C89', 'R12', 'CANCELLED', $this->dDate, $this->eventDate, $this->customer);
91 |
92 | $subscription = new Subscription('FooBar', $details, 'TRACKER-AAAA', 'manual');
93 |
94 | $this->assertTrue($subscription->isCancelledByAcquirer());
95 | }
96 |
97 | public function testIsCancelledByReceiverShouldReturnTrue()
98 | {
99 | $details = new Details('C89', 'R12', 'CANCELLED_BY_RECEIVER', $this->dDate, $this->eventDate, $this->customer);
100 |
101 | $subscription = new Subscription('FooBar', $details, 'TRACKER-AAAA', 'manual');
102 |
103 | $this->assertTrue($subscription->isCancelledByReceiver());
104 | }
105 |
106 | public function testIsCancelledByCustomerShouldReturnTrue()
107 | {
108 | $details = new Details('C89', 'R12', 'CANCELLED_BY_SENDER', $this->dDate, $this->eventDate, $this->customer);
109 |
110 | $subscription = new Subscription('FooBar', $details, 'TRACKER-AAAA', 'manual');
111 |
112 | $this->assertTrue($subscription->isCancelledByCustomer());
113 | }
114 |
115 | public function testIsExpiredShouldReturnTrue()
116 | {
117 | $details = new Details('C89', 'R12', 'EXPIRED', $this->dDate, $this->eventDate, $this->customer);
118 |
119 | $subscription = new Subscription('FooBar', $details, 'TRACKER-AAAA', 'manual');
120 |
121 | $this->assertTrue($subscription->isExpired());
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/tests/Purchases/Subscriptions/xml/chargeFull.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4556788
4 |
5 | -
6 | 99
7 | Produto 03
8 | 1.77
9 | 8
10 | 360
11 | 12.90
12 |
13 | -
14 | 97
15 | Produto 04
16 | 43.67
17 | 3
18 | 1100
19 | 134.98
20 |
21 |
22 | abcdef
23 |
--------------------------------------------------------------------------------
/tests/Purchases/Subscriptions/xml/preApproval.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | Seguro Notebook
4 | C08984
5 | 2011-11-23T13:40:00.000-02:00
6 | 538C53
7 | CANCELLED
8 | REF1234
9 | 2011-11-25T20:04:00.000-02:00
10 | auto
11 |
12 | Nome Comprador
13 | comprador@uol.com
14 |
15 | 11
16 | 30389678
17 |
18 |
19 | ALAMEDAITU
20 | 78
21 | ap.2601
22 | J Paulista
23 | SAO PAULO
24 | SP
25 | BRA
26 | 01421000
27 |
28 |
29 |
--------------------------------------------------------------------------------
/tests/Purchases/Transactions/DecoderTest.php:
--------------------------------------------------------------------------------
1 |
15 | */
16 | class DecoderTest extends \PHPUnit_Framework_TestCase
17 | {
18 | public function testDecodeShouldDoReturningObjectTransaction()
19 | {
20 | $detailsDate = new DateTime('2011-02-10T16:13:41.000-03:00');
21 | $detailsLastEventDate = new DateTime('2011-02-10T19:15:11.000-03:00');
22 | $detailsAddress = new Address('AC', 'Sao Maite', '99500079', 'Centro', 'R Delgado', '55', 'Fundos');
23 | $customer = new Customer('usuario@site.com', 'FooBar', new Phone('11', '99999999'), $detailsAddress);
24 | $details = new Details('9E884542', 'REF1234', '6', $detailsDate, $detailsLastEventDate, $customer);
25 |
26 | $paymentMethod = new PaymentMethod(1, 101);
27 | $escrowEndDate = new DateTime('2011-03-10T08:00:00.000-03:00');
28 | $payment = new Payment($paymentMethod, 49900.00, 0.01, 0.04, 49900.03, 0.02, 1, $escrowEndDate);
29 |
30 | $shippingAddress = new Address('CE', 'Ortega', '40610912', 'Ipe', 'R. Regina', '36', 'Bl.A');
31 | $shipping = new Shipping(2, $shippingAddress, 23.45);
32 |
33 | $items = new Items;
34 | $items->add(new Item(77, 'Produto 01', 2.5, 4, 20, 300));
35 | $items->add(new Item(88, 'Produto 02', 342.51, 3, 134.98, 1000));
36 |
37 | $transaction = new Transaction($details, $payment, 2, $items, $shipping);
38 |
39 | $obj = simplexml_load_file(__DIR__.'/xml/transactionFull.xml');
40 | $decoder = new Decoder;
41 | $this->assertEquals($transaction, $decoder->decode($obj));
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/tests/Purchases/Transactions/LocatorTest.php:
--------------------------------------------------------------------------------
1 |
10 | */
11 | class LocatorTest extends \PHPUnit_Framework_TestCase
12 | {
13 | /**
14 | * @var Credentials
15 | */
16 | protected $credentials;
17 |
18 | /**
19 | * @var Client|\PHPUnit_Framework_MockObject_MockObject
20 | */
21 | protected $client;
22 |
23 | /**
24 | * @var Decoder|\PHPUnit_Framework_MockObject_MockObject
25 | */
26 | protected $decoder;
27 |
28 | /**
29 | * @var Transaction|\PHPUnit_Framework_MockObject_MockObject
30 | */
31 | private $transaction;
32 |
33 | protected function setUp()
34 | {
35 | $environment = $this->getMockForAbstractClass(Environment::class);
36 |
37 | $environment->expects($this->any())
38 | ->method('getHost')
39 | ->willReturn('test.com');
40 |
41 | $environment->expects($this->any())
42 | ->method('getWsHost')
43 | ->willReturn('ws.test.com');
44 |
45 | $this->credentials = new Credentials('a@a.com', 't', $environment);
46 |
47 | $this->client = $this->createMock(Client::class);
48 | $this->decoder = $this->createMock(Decoder::class);
49 | $this->transaction = $this->createMock(Transaction::class);
50 | }
51 |
52 | /**
53 | * @test
54 | */
55 | public function getByCodeShouldDoAGetRequestAddingCredentialsData()
56 | {
57 | $xml = simplexml_load_string('');
58 |
59 | $this->client->expects($this->once())
60 | ->method('get')
61 | ->with('https://ws.test.com/v2/transactions/1?email=a%40a.com&token=t')
62 | ->willReturn($xml);
63 |
64 | $this->decoder->expects($this->once())
65 | ->method('decode')
66 | ->with($xml)
67 | ->willReturn($this->transaction);
68 |
69 | $service = new Locator($this->credentials, $this->client, $this->decoder);
70 |
71 | $this->assertSame($this->transaction, $service->getByCode(1));
72 | }
73 |
74 | /**
75 | * @test
76 | */
77 | public function getByNotificationShouldDoAGetRequestAddingCredentialsData()
78 | {
79 | $xml = simplexml_load_string('');
80 |
81 | $this->client->expects($this->once())
82 | ->method('get')
83 | ->with('https://ws.test.com/v2/transactions/notifications/1?email=a%40a.com&token=t')
84 | ->willReturn($xml);
85 |
86 | $this->decoder->expects($this->once())
87 | ->method('decode')
88 | ->with($xml)
89 | ->willReturn($this->transaction);
90 |
91 | $service = new Locator($this->credentials, $this->client, $this->decoder);
92 |
93 | $this->assertSame($this->transaction, $service->getByNotification(1));
94 | }
95 | }
96 |
--------------------------------------------------------------------------------
/tests/Purchases/Transactions/PaymentMethodTest.php:
--------------------------------------------------------------------------------
1 | assertEquals(1, $paymentMethod->getType());
11 | $this->assertEquals(2, $paymentMethod->getCode());
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/tests/Purchases/Transactions/PaymentTest.php:
--------------------------------------------------------------------------------
1 |
8 | */
9 | class PaymentTest extends \PHPUnit_Framework_TestCase
10 | {
11 | /**
12 | * @var Payment
13 | */
14 | private $payment;
15 |
16 | /**
17 | * @var DateTime
18 | */
19 | private $escrowEndDate;
20 |
21 | /**
22 | * @var PaymentMethod
23 | */
24 | private $paymentMethod;
25 |
26 | protected function setUp()
27 | {
28 | $this->escrowEndDate = new DateTime("2015-01-01");
29 | $this->paymentMethod = $this->createMock(PaymentMethod::class);
30 | $this->payment = new Payment($this->paymentMethod, 123, 2, 3, 4, 5, 1, $this->escrowEndDate);
31 | }
32 |
33 | /**
34 | * @test
35 | */
36 | public function constructShouldConfigureTheAttributes()
37 | {
38 | $this->assertAttributeSame($this->paymentMethod, 'paymentMethod', $this->payment);
39 | $this->assertAttributeEquals(123, 'grossAmount', $this->payment);
40 | $this->assertAttributeEquals(2, 'discountAmount', $this->payment);
41 | $this->assertAttributeEquals(3, 'feeAmount', $this->payment);
42 | $this->assertAttributeEquals(4, 'netAmount', $this->payment);
43 | $this->assertAttributeEquals(5, 'extraAmount', $this->payment);
44 | $this->assertAttributeEquals(1, 'installmentCount', $this->payment);
45 | }
46 |
47 | /**
48 | * @test
49 | */
50 | public function getPaymentMethodShouldReturnTheConfiredPaymentMethod()
51 | {
52 | $this->assertSame($this->paymentMethod, $this->payment->getPaymentMethod());
53 | }
54 |
55 | /**
56 | * @test
57 | */
58 | public function getGrossAmountShouldReturnTheConfiredGrossAmount()
59 | {
60 | $this->assertEquals(123, $this->payment->getGrossAmount());
61 | }
62 |
63 | /**
64 | * @test
65 | */
66 | public function getDiscountAmountShouldReturnTheConfiredDiscountAmount()
67 | {
68 | $this->assertEquals(2, $this->payment->getDiscountAmount());
69 | }
70 |
71 | /**
72 | * @test
73 | */
74 | public function getFeeAmountShouldReturnTheConfiredFeeAmount()
75 | {
76 | $this->assertEquals(3, $this->payment->getFeeAmount());
77 | }
78 |
79 | /**
80 | * @test
81 | */
82 | public function getNetAmountShouldReturnTheConfiredNetAmount()
83 | {
84 | $this->assertEquals(4, $this->payment->getNetAmount());
85 | }
86 |
87 | /**
88 | * @test
89 | */
90 | public function getExtraAmountShouldReturnTheConfiredExtraAmount()
91 | {
92 | $this->assertEquals(5, $this->payment->getExtraAmount());
93 | }
94 |
95 | /**
96 | * @test
97 | */
98 | public function getInstallmentCountShouldReturnTheConfiredInstallmentCount()
99 | {
100 | $this->assertEquals(1, $this->payment->getInstallmentCount());
101 | }
102 |
103 | /**
104 | * @test
105 | */
106 | public function getEscrowEndDateShouldReturnTheConfiredEscrowEndDate()
107 | {
108 | $this->assertSame($this->escrowEndDate, $this->payment->getEscrowEndDate());
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/tests/Purchases/Transactions/TransactionTest.php:
--------------------------------------------------------------------------------
1 |
11 | */
12 | class TransactionTest extends \PHPUnit_Framework_TestCase
13 | {
14 | /**
15 | * @var Transaction
16 | */
17 | private $transaction;
18 |
19 | /**
20 | * @var Details
21 | */
22 | private $details;
23 |
24 | /**
25 | * @var Payment
26 | */
27 | private $payment;
28 |
29 | /**
30 | * @var ItemCollection
31 | */
32 | private $itemCollection;
33 |
34 | /**
35 | * @var Shipping
36 | */
37 | private $shipping;
38 |
39 | protected function setUp()
40 | {
41 | $this->details = $this->createMock(Details::class);
42 | $this->payment = $this->createMock(Payment::class);
43 | $this->itemCollection = $this->createMock(ItemCollection::class);
44 | $this->shipping = $this->createMock(Shipping::class);
45 |
46 | $this->transaction = new Transaction(
47 | $this->details,
48 | $this->payment,
49 | 1,
50 | $this->itemCollection,
51 | $this->shipping
52 | );
53 | }
54 |
55 | /**
56 | * @test
57 | */
58 | public function constructShouldConfigureTheAttributes()
59 | {
60 | $this->assertAttributeSame($this->details, 'details', $this->transaction);
61 | $this->assertAttributeSame($this->payment, 'payment', $this->transaction);
62 | $this->assertAttributeEquals(1, 'type', $this->transaction);
63 | $this->assertAttributeSame($this->itemCollection, 'items', $this->transaction);
64 | $this->assertAttributeSame($this->shipping, 'shipping', $this->transaction);
65 | }
66 |
67 | /**
68 | * @test
69 | */
70 | public function getDetailsShouldReturnTheConfiguredDetails()
71 | {
72 | $this->assertSame($this->details, $this->transaction->getDetails());
73 | }
74 |
75 | /**
76 | * @test
77 | */
78 | public function getTypeShouldReturnTheConfiguredType()
79 | {
80 | $this->assertEquals(1, $this->transaction->getType());
81 | }
82 |
83 | /**
84 | * @test
85 | */
86 | public function getPaymentShouldReturnTheConfiguredPayment()
87 | {
88 | $this->assertSame($this->payment, $this->transaction->getPayment());
89 | }
90 |
91 | /**
92 | * @test
93 | */
94 | public function getItemsShouldReturnTheConfiguredItems()
95 | {
96 | $this->assertSame($this->itemCollection, $this->transaction->getItems());
97 | }
98 |
99 | /**
100 | * @test
101 | */
102 | public function getShippingShouldReturnTheConfiguredShipping()
103 | {
104 | $this->assertSame($this->shipping, $this->transaction->getShipping());
105 | }
106 |
107 | /**
108 | * @test
109 | */
110 | public function getIsWaitingPaymentShouldReturnTheConfiguredIsWaitingPayment()
111 | {
112 | $this->assertFalse($this->transaction->isWaitingPayment());
113 | }
114 |
115 | /**
116 | * @test
117 | */
118 | public function getIsUnderAnalysisShouldReturnTheConfiguredIsUnderAnalysis()
119 | {
120 | $this->assertFalse($this->transaction->isUnderAnalysis());
121 | }
122 |
123 | /**
124 | * @test
125 | */
126 | public function getIsPaidShouldReturnTheConfiguredIsPaid()
127 | {
128 | $this->assertFalse($this->transaction->isPaid());
129 | }
130 |
131 | /**
132 | * @test
133 | */
134 | public function getIsAvailableShouldReturnTheConfiguredIsAvailable()
135 | {
136 | $this->assertFalse($this->transaction->isAvailable());
137 | }
138 |
139 | /**
140 | * @test
141 | */
142 | public function getIsUnderContestShouldReturnTheConfiguredIsUnderContest()
143 | {
144 | $this->assertFalse($this->transaction->isUnderContest());
145 | }
146 |
147 | /**
148 | * @test
149 | */
150 | public function getIsReturnedShouldReturnTheConfiguredIsReturned()
151 | {
152 | $this->assertFalse($this->transaction->isReturned());
153 | }
154 |
155 | /**
156 | * @test
157 | */
158 | public function getIsCancelledShouldReturnTheConfiguredIsCancelled()
159 | {
160 | $this->assertFalse($this->transaction->isCancelled());
161 | }
162 | }
163 |
--------------------------------------------------------------------------------
/tests/Purchases/Transactions/xml/transactionFull.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 2011-02-10T16:13:41.000-03:00
4 | 9E884542
5 | REF1234
6 | 2
7 | 6
8 | 2011-02-10T19:15:11.000-03:00
9 | 2011-03-10T08:00:00.000-03:00
10 |
11 |
12 | usuario@site.com
13 | FooBar
14 |
15 | 11
16 | 99999999
17 |
18 |
19 | BRA
20 | AC
21 | Sao Maite
22 | 99500079
23 | Centro
24 | R Delgado
25 | 55
26 | Fundos
27 |
28 |
29 |
30 |
31 | 1
32 | 101
33 |
34 | 49900.00
35 | 0.01
36 | 0.04
37 | 49900.03
38 | 0.02
39 | 1
40 | 2
41 |
42 |
43 | -
44 | 77
45 | Produto 01
46 | 2.50
47 | 4
48 | 300
49 | 20.00
50 |
51 | -
52 | 88
53 | Produto 02
54 | 342.51
55 | 3
56 | 1000
57 | 134.98
58 |
59 |
60 |
61 |
62 |
63 |
64 | 2
65 | 23.45
66 |
67 | BRA
68 | CE
69 | Ortega
70 | 40610912
71 | Ipe
72 | R. Regina
73 | 36
74 | Bl.A
75 |
76 |
77 |
--------------------------------------------------------------------------------
/tests/Requests/Checkout/CheckoutBuilderTest.php:
--------------------------------------------------------------------------------
1 |
10 | */
11 | class CheckoutBuilderTest extends \PHPUnit_Framework_TestCase
12 | {
13 | /**
14 | * @var Checkout
15 | */
16 | private $checkout;
17 |
18 | /**
19 | * @var CheckoutBuilder
20 | */
21 | private $builder;
22 |
23 | /**
24 | * {@inheritdoc}
25 | */
26 | protected function setUp()
27 | {
28 | $this->checkout = new Checkout();
29 | $this->builder = new CheckoutBuilder($this->checkout);
30 | }
31 |
32 | /**
33 | * @test
34 | */
35 | public function constructShouldConfigureAttributes()
36 | {
37 | $this->assertAttributeSame($this->checkout, 'checkout', $this->builder);
38 | }
39 |
40 | /**
41 | * @test
42 | */
43 | public function constructShouldCreateANewCheckoutWhenItWasntInformed()
44 | {
45 | $this->assertAttributeInstanceOf(
46 | Checkout::class,
47 | 'checkout',
48 | new CheckoutBuilder()
49 | );
50 | }
51 |
52 | /**
53 | * @test
54 | */
55 | public function addItemShouldAppendTheGivenItem()
56 | {
57 | $item = new Item(1, 'testing', 10);
58 | $this->builder->addItem($item);
59 |
60 | $this->assertTrue($this->checkout->getOrder()->getItems()->contains($item));
61 | }
62 |
63 | /**
64 | * @test
65 | */
66 | public function setShippingShouldConfigureTheShipping()
67 | {
68 | $shipping = new Shipping(1);
69 | $this->builder->setShipping($shipping);
70 |
71 | $this->assertAttributeSame($shipping, 'shipping', $this->checkout->getOrder());
72 | }
73 |
74 | /**
75 | * @test
76 | */
77 | public function setReferenceShouldConfigureTheReference()
78 | {
79 | $this->builder->setReference('testing');
80 |
81 | $this->assertAttributeEquals('testing', 'reference', $this->checkout->getOrder());
82 | }
83 |
84 | /**
85 | * @test
86 | */
87 | public function setCustomerShouldConfigureTheReference()
88 | {
89 | $customer = new Customer('test@test.com');
90 | $this->builder->setCustomer($customer);
91 |
92 | $this->assertAttributeSame($customer, 'customer', $this->checkout);
93 | }
94 |
95 | /**
96 | * @test
97 | */
98 | public function setRedirectToShouldConfigureTheRedirectionUri()
99 | {
100 | $this->builder->setRedirectTo('http://test.com');
101 |
102 | $this->assertAttributeEquals('http://test.com', 'redirectTo', $this->checkout);
103 | }
104 |
105 | /**
106 | * @test
107 | */
108 | public function setMaxAgeShouldConfigureTheMaximumAge()
109 | {
110 | $this->builder->setMaxAge(1);
111 |
112 | $this->assertAttributeEquals(1, 'maxAge', $this->checkout);
113 | }
114 |
115 | /**
116 | * @test
117 | */
118 | public function setMaxUsesShouldTheNumberOfUses()
119 | {
120 | $this->builder->setMaxUses(1);
121 |
122 | $this->assertAttributeEquals(1, 'maxUses', $this->checkout);
123 | }
124 |
125 | /**
126 | * @test
127 | */
128 | public function getCheckoutShouldReturnTheConfiguredCheckout()
129 | {
130 | $this->assertSame($this->checkout, $this->builder->getCheckout());
131 | }
132 | }
133 |
--------------------------------------------------------------------------------
/tests/Requests/Checkout/CheckoutServiceTest.php:
--------------------------------------------------------------------------------
1 |
12 | */
13 | class CheckoutServiceTest extends \PHPUnit_Framework_TestCase
14 | {
15 | /**
16 | * @var Client|\PHPUnit_Framework_MockObject_MockObject
17 | */
18 | private $client;
19 |
20 | /**
21 | * @var Credentials
22 | */
23 | private $credentials;
24 |
25 | protected function setUp()
26 | {
27 | $environment = $this->getMockForAbstractClass(Environment::class);
28 |
29 | $environment->expects($this->any())
30 | ->method('getHost')
31 | ->willReturn('test.com');
32 |
33 | $environment->expects($this->any())
34 | ->method('getWsHost')
35 | ->willReturn('ws.test.com');
36 |
37 | $this->credentials = new Credentials('test@test.com', 'test', $environment);
38 | $this->client = $this->createMock(Client::class);
39 | }
40 |
41 | /**
42 | * @test
43 | */
44 | public function checkoutShouldDoAPostRequestReturningTheRedirection()
45 | {
46 | $checkout = new Checkout();
47 |
48 | $wsUri = 'https://ws.test.com/v2/checkout?email=test%40test.com&token=test';
49 | $request = simplexml_load_string(
50 | ''
51 | . 'BRL'
52 | );
53 |
54 | $response = simplexml_load_string(
55 | ''
56 | . '123
2010-12-02T10:11:28.000-02:00'
57 | );
58 |
59 | $this->client->expects($this->once())
60 | ->method('post')
61 | ->with($wsUri, $request)
62 | ->willReturn($response);
63 |
64 | $service = new CheckoutService($this->credentials, $this->client);
65 | $redirection = $service->checkout($checkout);
66 |
67 | $redirectUri = 'https://test.com/v2/checkout/payment.html';
68 |
69 | $this->assertInstanceOf(Redirection::class, $redirection);
70 | $this->assertAttributeEquals($redirectUri, 'uri', $redirection);
71 | $this->assertAttributeEquals('123', 'code', $redirection);
72 | $this->assertAttributeEquals(new DateTime('2010-12-02T10:11:28.000-02:00'), 'date', $redirection);
73 | }
74 |
75 | /**
76 | * @test
77 | */
78 | public function createCheckoutBuilderShouldReturnANewBuilderInstance()
79 | {
80 | $service = new CheckoutService($this->credentials, $this->client);
81 |
82 | $this->assertInstanceOf(CheckoutBuilder::class, $service->createCheckoutBuilder());
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/tests/Requests/Checkout/CheckoutTest.php:
--------------------------------------------------------------------------------
1 |
15 | */
16 | class CheckoutTest extends \PHPUnit_Framework_TestCase
17 | {
18 | /**
19 | * @var Checkout
20 | */
21 | private $checkout;
22 |
23 | /**
24 | * @var Order
25 | */
26 | private $order;
27 |
28 | /**
29 | * {@inheritdoc}
30 | */
31 | protected function setUp()
32 | {
33 | $this->order = new Order();
34 | $this->checkout = new Checkout($this->order);
35 | }
36 |
37 | /**
38 | * @test
39 | */
40 | public function constructShouldConfigureAttributes()
41 | {
42 | $this->assertAttributeSame($this->order, 'order', $this->checkout);
43 | }
44 |
45 | /**
46 | * @test
47 | */
48 | public function constructShouldCreateANewOrderWhenItWasntInformed()
49 | {
50 | $this->assertAttributeInstanceOf(
51 | Order::class,
52 | 'order',
53 | new Checkout()
54 | );
55 | }
56 |
57 | public function getOrderShouldReturnConfiguredOrder()
58 | {
59 | $this->assertSame($this->order, $this->checkout->getOrder());
60 | }
61 |
62 | /**
63 | * @test
64 | */
65 | public function setCustomerShouldConfigureTheReference()
66 | {
67 | $customer = new Customer('test@test.com');
68 | $this->checkout->setCustomer($customer);
69 |
70 | $this->assertAttributeSame($customer, 'customer', $this->checkout);
71 | }
72 |
73 | /**
74 | * @test
75 | */
76 | public function setRedirectToShouldConfigureTheRedirectionUri()
77 | {
78 | $this->checkout->setRedirectTo('http://test.com');
79 |
80 | $this->assertAttributeEquals('http://test.com', 'redirectTo', $this->checkout);
81 | }
82 |
83 | /**
84 | * @test
85 | */
86 | public function setMaxAgeShouldConfigureTheMaximumAge()
87 | {
88 | $this->checkout->setMaxAge(1);
89 |
90 | $this->assertAttributeEquals(1, 'maxAge', $this->checkout);
91 | }
92 |
93 | /**
94 | * @test
95 | */
96 | public function setMaxUsesShouldTheNumberOfUses()
97 | {
98 | $this->checkout->setMaxUses(1);
99 |
100 | $this->assertAttributeEquals(1, 'maxUses', $this->checkout);
101 | }
102 |
103 | /**
104 | * @test
105 | */
106 | public function getMaxAgeShouldReturnConfiguredMaxAge()
107 | {
108 | $this->checkout->setMaxAge(12);
109 |
110 | $this->assertEquals(12, $this->checkout->getMaxAge());
111 | }
112 |
113 | /**
114 | * @test
115 | */
116 | public function getMaxUsesShouldReturnConfiguredMaxUses()
117 | {
118 | $this->checkout->setMaxUses(7);
119 |
120 | $this->assertEquals(7, $this->checkout->getMaxUses());
121 | }
122 |
123 | /**
124 | * @test
125 | */
126 | public function getRedirectToShouldReturnConfiguredRedirectTo()
127 | {
128 | $this->checkout->setRedirectTo('someRedirect');
129 |
130 | $this->assertEquals('someRedirect', $this->checkout->getRedirectTo());
131 | }
132 |
133 | /**
134 | * @test
135 | */
136 | public function getCustomerShouldReturnConfiguredCustomer()
137 | {
138 | $customer = $this->createMock(Customer::class);
139 | $this->checkout->setCustomer($customer);
140 |
141 | $this->assertSame($customer, $this->checkout->getCustomer());
142 | }
143 |
144 | /**
145 | * @test
146 | */
147 | public function setNotificationURLShouldConfigureTheNotificationUri()
148 | {
149 | $uri = 'http://chibungo.com';
150 | $this->checkout->setNotificationURL($uri);
151 | $this->assertAttributeEquals($uri, 'notificationURL', $this->checkout);
152 | }
153 |
154 | /**
155 | * @test
156 | */
157 | public function serializeShouldXMLEmpty()
158 | {
159 | $checkout = new Checkout;
160 | $xml = $checkout->xmlSerialize();
161 |
162 | $this->assertInstanceOf(SimpleXMLElement::class, $xml);
163 |
164 | $expected = simplexml_load_file(__DIR__.'/xml/checkoutEmpty.xml');
165 | $this->assertEquals($expected, $xml);
166 | }
167 |
168 | /**
169 | * @test
170 | */
171 | public function serializeShouldReturnXMLCustomer()
172 | {
173 | $customer = new Customer('usuario@site.com');
174 | $checkout = new Checkout;
175 | $checkout->setCustomer($customer);
176 |
177 | $xml = $checkout->xmlSerialize();
178 |
179 | $this->assertInstanceOf(SimpleXMLElement::class, $xml);
180 | $expected = simplexml_load_file(__DIR__.'/xml/checkoutCustomer.xml');
181 | $this->assertEquals($expected, $xml);
182 | }
183 |
184 | /**
185 | * @test
186 | */
187 | public function serializeShouldReturnXMLFull()
188 | {
189 | $items = new Items;
190 | $items->add(new Item(77, 'Produto 01', 2.5, 4, 20, 300));
191 | $items->add(new Item(88, 'Produto 02', 342.51, 3, 134.98, 1000));
192 |
193 | $shippingAddress = new Address('CE', 'Ortega do Norte', '40610-912', 'Ipe', 'R. Regina Salas', '3601', 'Bl.A');
194 | $shipping = new Shipping(1, $shippingAddress, 23.45);
195 |
196 | $order = new Order($items);
197 | $order->setReference('REF1234');
198 | $order->setExtraAmount(-10.30);
199 | $order->setShipping($shipping);
200 |
201 | $customerAddress = new Address('AC', 'Sao Maite', '99500-079', 'Centro', 'Rua David Delgado', '55', 'Fundos');
202 | $customerPhone = new Phone('11', '99999999');
203 | $customer = new Customer('usuario@site.com', 'FooBar', $customerPhone, $customerAddress);
204 |
205 | $checkout = new Checkout($order);
206 | $checkout->setCustomer($customer);
207 | $checkout->setRedirectTo('http://localhost/return.php');
208 | $checkout->setMaxUses(5);
209 | $checkout->setMaxAge(60);
210 |
211 | $xml = $checkout->xmlSerialize();
212 |
213 | $this->assertInstanceOf(SimpleXMLElement::class, $xml);
214 | $expected = simplexml_load_file(__DIR__ . '/xml/checkoutFull.xml');
215 | $this->assertEquals($expected, $xml);
216 | }
217 | }
218 |
--------------------------------------------------------------------------------
/tests/Requests/Checkout/OrderTest.php:
--------------------------------------------------------------------------------
1 |
9 | */
10 | class OrderTest extends \PHPUnit_Framework_TestCase
11 | {
12 | /**
13 | * @var ItemCollection
14 | */
15 | private $items;
16 |
17 | /**
18 | * @var Order
19 | */
20 | private $order;
21 |
22 | /**
23 | * {@inheritdoc}
24 | */
25 | protected function setUp()
26 | {
27 | $this->items = $this->createMock(ItemCollection::class);
28 | $this->order = new Order($this->items);
29 | }
30 |
31 | /**
32 | * @test
33 | */
34 | public function constructShouldConfigureItemsAndCurrency()
35 | {
36 | $this->assertAttributeSame($this->items, 'items', $this->order);
37 | $this->assertAttributeEquals('BRL', 'currency', $this->order);
38 | }
39 |
40 | /**
41 | * @test
42 | */
43 | public function constructShouldCreateAnItemCollectionWhenItWasntInformed()
44 | {
45 | $this->assertAttributeInstanceOf(ItemCollection::class, 'items', new Order());
46 | }
47 |
48 | /**
49 | * @test
50 | */
51 | public function getItemsShouldReturnConfiguredItemCollection()
52 | {
53 | $this->assertSame($this->items, $this->order->getItems());
54 | }
55 |
56 | /**
57 | * @test
58 | */
59 | public function getCurrencyShouldReturnConfiguredCurrency()
60 | {
61 | $this->assertEquals('BRL', $this->order->getCurrency());
62 | }
63 |
64 | /**
65 | * @test
66 | */
67 | public function getReferenceShouldReturnConfiguredReference()
68 | {
69 | $this->order->setReference('someRef');
70 |
71 | $this->assertEquals('someRef', $this->order->getReference());
72 | }
73 |
74 | /**
75 | * @test
76 | */
77 | public function getExtraAmountShouldReturnConfiguredExtraAmount()
78 | {
79 | $this->order->setExtraAmount(123);
80 |
81 | $this->assertEquals(123, $this->order->getExtraAmount());
82 | }
83 |
84 | /**
85 | * @test
86 | */
87 | public function getShippingShouldReturnConfiguredShipping()
88 | {
89 | $shipping = new Shipping(1);
90 | $this->order->setShipping($shipping);
91 |
92 | $this->assertSame($shipping, $this->order->getShipping());
93 | }
94 |
95 | /**
96 | * @test
97 | */
98 | public function setReferenceShouldChangeTheAttribute()
99 | {
100 | $this->order->setReference('test');
101 |
102 | $this->assertAttributeEquals('test', 'reference', $this->order);
103 | }
104 |
105 | /**
106 | * @test
107 | */
108 | public function setShippingShouldChangeTheAttribute()
109 | {
110 | $shipping = new Shipping(1);
111 | $this->order->setShipping($shipping);
112 |
113 | $this->assertAttributeSame($shipping, 'shipping', $this->order);
114 | }
115 |
116 | /**
117 | * @test
118 | */
119 | public function setExtraAmountShouldChangeTheAttribute()
120 | {
121 | $this->order->setExtraAmount(10);
122 |
123 | $this->assertAttributeEquals(10, 'extraAmount', $this->order);
124 | }
125 | }
126 |
--------------------------------------------------------------------------------
/tests/Requests/Checkout/xml/checkoutCustomer.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | BRL
4 |
5 |
6 | usuario@site.com
7 |
8 |
--------------------------------------------------------------------------------
/tests/Requests/Checkout/xml/checkoutEmpty.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | BRL
4 |
5 |
--------------------------------------------------------------------------------
/tests/Requests/Checkout/xml/checkoutFull.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | BRL
4 |
5 |
6 | -
7 | 77
8 | Produto 01
9 | 2.50
10 | 4
11 | 300
12 | 20.00
13 |
14 | -
15 | 88
16 | Produto 02
17 | 342.51
18 | 3
19 | 1000
20 | 134.98
21 |
22 |
23 |
24 | REF1234
25 | -10.30
26 |
27 |
28 | 1
29 | 23.45
30 |
31 | BRA
32 | CE
33 | Ortega do Norte
34 | 40610912
35 | Ipe
36 | R. Regina Salas
37 | 3601
38 | Bl.A
39 |
40 |
41 |
42 |
43 | usuario@site.com
44 | FooBar
45 |
46 | 11
47 | 99999999
48 |
49 |
50 | BRA
51 | AC
52 | Sao Maite
53 | 99500079
54 | Centro
55 | Rua David Delgado
56 | 55
57 | Fundos
58 |
59 |
60 |
61 | http://localhost/return.php
62 | 5
63 | 60
64 |
65 |
--------------------------------------------------------------------------------
/tests/Requests/PreApprovals/ChargeTypeTest.php:
--------------------------------------------------------------------------------
1 |
6 | */
7 | class ChargeTypeTest extends \PHPUnit_Framework_TestCase
8 | {
9 | public function testValidShouldReturnTrue()
10 | {
11 | $this->assertTrue(ChargeType::isValid('auto'));
12 | $this->assertTrue(ChargeType::isValid('manual'));
13 | }
14 |
15 | public function testValuesInvalidShouldReturnFalse()
16 | {
17 | $this->assertFalse(ChargeType::isValid('AUTO'));
18 | $this->assertFalse(ChargeType::isValid('other'));
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/tests/Requests/PreApprovals/PeriodTypeTest.php:
--------------------------------------------------------------------------------
1 |
6 | */
7 | class PeriodTest extends \PHPUnit_Framework_TestCase
8 | {
9 | public function testValidShouldReturnTrue()
10 | {
11 | $this->assertTrue(Period::isValid('WEEKLY'));
12 | $this->assertTrue(Period::isValid('MONTHLY'));
13 | $this->assertTrue(Period::isValid('BIMONTHLY'));
14 | $this->assertTrue(Period::isValid('TRIMONTHLY'));
15 | $this->assertTrue(Period::isValid('SEMIANNUALLY'));
16 | $this->assertTrue(Period::isValid('YEARLY'));
17 | }
18 |
19 | public function testValuesInvalidShouldReturnFalse()
20 | {
21 | $this->assertFalse(Period::isValid('Weekly'));
22 | $this->assertFalse(Period::isValid('other'));
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/tests/Requests/PreApprovals/PreApprovalServiceTest.php:
--------------------------------------------------------------------------------
1 |
11 | */
12 | class PreApprovalServiceTest extends \PHPUnit_Framework_TestCase
13 | {
14 | /**
15 | * @test
16 | */
17 | public function createRequestBuilderShouldDoReturnObject()
18 | {
19 | $credentials = $this->createMock(Credentials::class);
20 | $client = $this->createMock(Client::class);
21 |
22 | $service = new PreApprovalService($credentials, $client);
23 |
24 | $this->assertAttributeEquals($credentials, 'credentials', $service);
25 | $this->assertAttributeEquals($client, 'client', $service);
26 |
27 | $this->assertEquals(new RequestBuilder(true), $service->createRequestBuilder());
28 | $this->assertEquals(new RequestBuilder(false), $service->createRequestBuilder(false));
29 | }
30 |
31 | /**
32 | * @test
33 | */
34 | public function aproveShouldReturningTheRedirection()
35 | {
36 | $request = $this->createMock(Request::class);
37 | $response = new SimpleXMLElement('');
38 | $xmlSerialize = new SimpleXMLElement('');
39 | $redirect = $this->createMock(Redirection::class);
40 |
41 | $credentials = $this->createMock(Credentials::class);
42 | $client = $this->createMock(Client::class);
43 |
44 | $request->expects($this->once())
45 | ->method('xmlSerialize')
46 | ->willReturn($xmlSerialize);
47 |
48 | $service = $this->getMockBuilder(PreApprovalService::class)
49 | ->setMethods(['post', 'getRedirection'])
50 | ->setConstructorArgs([$credentials, $client])
51 | ->disableOriginalClone()
52 | ->getMock();
53 |
54 | $service->expects($this->once())
55 | ->method('post')
56 | ->with(PreApprovalService::ENDPOINT, $xmlSerialize)
57 | ->willReturn($response);
58 |
59 | $service->expects($this->once())
60 | ->method('getRedirection')
61 | ->with($response)
62 | ->willReturn($redirect);
63 |
64 | $this->assertEquals($redirect, $service->approve($request));
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/tests/Requests/PreApprovals/PreApprovalTest.php:
--------------------------------------------------------------------------------
1 |
8 | */
9 | class PreApprovalTest extends \PHPUnit_Framework_TestCase
10 | {
11 | public function testAttributesShouldValuesEmpty()
12 | {
13 | $preApproval = new PreApproval;
14 |
15 | $this->assertAttributeEmpty('name', $preApproval);
16 | $this->assertAttributeEmpty('chargeType', $preApproval);
17 | $this->assertAttributeEmpty('details', $preApproval);
18 | $this->assertAttributeEmpty('period', $preApproval);
19 | $this->assertAttributeEmpty('finalDate', $preApproval);
20 | $this->assertAttributeEmpty('maxTotalAmount', $preApproval);
21 | $this->assertAttributeEmpty('amountPerPayment', $preApproval);
22 | $this->assertAttributeEmpty('maxAmountPerPayment', $preApproval);
23 | $this->assertAttributeEmpty('maxPaymentsPerPeriod', $preApproval);
24 | $this->assertAttributeEmpty('maxAmountPerPeriod', $preApproval);
25 | $this->assertAttributeEmpty('initialDate', $preApproval);
26 | }
27 |
28 | public function testSetChargeTypeShouldThrowInvalidArgumentException()
29 | {
30 | $this->setExpectedException('InvalidArgumentException', 'You should inform a valid charge type');
31 |
32 | $preApproval = new PreApproval;
33 | $preApproval->setChargeType('other');
34 | }
35 |
36 | public function testSetPeriodShouldThrowInvalidArgumentException()
37 | {
38 | $this->setExpectedException('InvalidArgumentException', 'You should inform a valid period');
39 |
40 | $preApproval = new PreApproval;
41 | $preApproval->setPeriod('other');
42 | }
43 |
44 | public function testSettersAndGettersAttributes()
45 | {
46 | $preApproval = new PreApproval;
47 |
48 | $preApproval->setName('Name Assinatura');
49 | $preApproval->setChargeType('auto');
50 | $preApproval->setDetails('Cobranca Mensal');
51 | $preApproval->setPeriod('MONTHLY');
52 | $preApproval->setFinalDate(new DateTime('2016-11-18'));
53 | $preApproval->setMaxTotalAmount(3000.50);
54 | $preApproval->setAmountPerPayment(100.50);
55 | $preApproval->setMaxAmountPerPayment(150.50);
56 | $preApproval->setMaxPaymentsPerPeriod(12);
57 | $preApproval->setMaxAmountPerPeriod(1200.50);
58 | $preApproval->setInitialDate(new DateTime('2015-11-18'));
59 |
60 | $this->assertAttributeEquals('Name Assinatura', 'name', $preApproval);
61 | $this->assertAttributeEquals('auto', 'chargeType', $preApproval);
62 | $this->assertAttributeEquals('Cobranca Mensal', 'details', $preApproval);
63 | $this->assertAttributeEquals('MONTHLY', 'period', $preApproval);
64 | $this->assertAttributeEquals(new DateTime('2016-11-18'), 'finalDate', $preApproval);
65 | $this->assertAttributeSame(3000.50, 'maxTotalAmount', $preApproval);
66 | $this->assertAttributeSame(100.50, 'amountPerPayment', $preApproval);
67 | $this->assertAttributeSame(150.50, 'maxAmountPerPayment', $preApproval);
68 | $this->assertAttributeSame(12, 'maxPaymentsPerPeriod', $preApproval);
69 | $this->assertAttributeSame(1200.50, 'maxAmountPerPeriod', $preApproval);
70 | $this->assertAttributeEquals(new DateTime('2015-11-18'), 'initialDate', $preApproval);
71 |
72 | $this->assertEquals('Name Assinatura', $preApproval->getName());
73 | $this->assertEquals('auto', $preApproval->getChargeType());
74 | $this->assertEquals('Cobranca Mensal', $preApproval->getDetails());
75 | $this->assertEquals('MONTHLY', $preApproval->getPeriod());
76 | $this->assertEquals(new DateTime('2016-11-18'), $preApproval->getFinalDate());
77 | $this->assertSame('3000.50', $preApproval->getMaxTotalAmount());
78 | $this->assertSame('100.50', $preApproval->getAmountPerPayment());
79 | $this->assertSame('150.50', $preApproval->getMaxAmountPerPayment());
80 | $this->assertSame(12, $preApproval->getMaxPaymentsPerPeriod());
81 | $this->assertSame('1200.50', $preApproval->getMaxAmountPerPeriod());
82 | $this->assertEquals(new DateTime('2015-11-18'), $preApproval->getInitialDate());
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/tests/Requests/PreApprovals/RequestBuilderTest.php:
--------------------------------------------------------------------------------
1 |
10 | */
11 | class RequestBuilderTest extends \PHPUnit_Framework_TestCase
12 | {
13 | /**
14 | * @var PreApproval|\PHPUnit_Framework_MockObject_MockObject
15 | */
16 | private $approval;
17 |
18 | protected function setUp()
19 | {
20 | $this->approval = $this->createMock(PreApproval::class);
21 |
22 | $this->approval->expects($this->once())
23 | ->method('setChargeType')
24 | ->with(ChargeType::AUTOMATIC);
25 |
26 | $this->mockRequest = $this->createMock(Request::class);
27 | }
28 |
29 | public function testContructShouldSetterChargeTypeAndInstanceofInterface()
30 | {
31 | $this->mockRequest->expects($this->once())->method('getPreApproval')->willReturn($this->approval);
32 |
33 | $builder = new RequestBuilder(false, $this->mockRequest);
34 |
35 | $this->assertInstanceOf(RequestBuilderInterface::class, $builder);
36 | $this->assertAttributeEquals($this->mockRequest, 'request', $builder);
37 | $this->assertEquals($this->mockRequest, $builder->getRequest());
38 | }
39 |
40 | public function testSetterRequestShouldReturnSelfObject()
41 | {
42 | $customer = $this->createMock(Customer::class);
43 |
44 | $this->mockRequest->expects($this->once())->method('getPreApproval')->willReturn($this->approval);
45 | $this->mockRequest->expects($this->once())->method('setCustomer')->with($customer);
46 | $this->mockRequest->expects($this->once())->method('setRedirectTo')->with('http://redirect');
47 | $this->mockRequest->expects($this->once())->method('setReference')->with('ABCDEF');
48 | $this->mockRequest->expects($this->once())->method('setReviewOn')->with('http://review');
49 |
50 | $builder = new RequestBuilder(false, $this->mockRequest);
51 |
52 | $this->assertEquals($builder, $builder->setCustomer($customer));
53 | $this->assertEquals($builder, $builder->setRedirectTo('http://redirect'));
54 | $this->assertEquals($builder, $builder->setReference('ABCDEF'));
55 | $this->assertEquals($builder, $builder->setReviewOn('http://review'));
56 | }
57 |
58 | public function testSetterPreApprovalShouldReturnSelfObject()
59 | {
60 | $this->mockRequest->expects($this->exactly(11))->method('getPreApproval')->willReturn($this->approval);
61 |
62 | $this->approval->expects($this->once())->method('setName')->with('FooBar');
63 | $this->approval->expects($this->once())->method('setDetails')->with('Details Foo Bar');
64 | $this->approval->expects($this->once())->method('setFinalDate')->with(new DateTime('2016-11-18'));
65 | $this->approval->expects($this->once())->method('setMaxTotalAmount')->with(2000);
66 | $this->approval->expects($this->once())->method('setPeriod')->with('WEEKLY');
67 | $this->approval->expects($this->once())->method('setAmountPerPayment')->with(123.56);
68 | $this->approval->expects($this->once())->method('setMaxAmountPerPayment')->with(97);
69 | $this->approval->expects($this->once())->method('setMaxPaymentsPerPeriod')->with(544.87);
70 | $this->approval->expects($this->once())->method('setMaxAmountPerPeriod')->with(5432.90);
71 | $this->approval->expects($this->once())->method('setInitialDate')->with(new DateTime('2015-11-18'));
72 |
73 | $builder = new RequestBuilder(false, $this->mockRequest);
74 |
75 | $this->assertEquals($builder, $builder->setName('FooBar'));
76 | $this->assertEquals($builder, $builder->setDetails('Details Foo Bar'));
77 | $this->assertEquals($builder, $builder->setFinalDate(new DateTime('2016-11-18')));
78 | $this->assertEquals($builder, $builder->setMaxTotalAmount(2000));
79 | $this->assertEquals($builder, $builder->setPeriod('WEEKLY'));
80 | $this->assertEquals($builder, $builder->setAmountPerPayment(123.56));
81 | $this->assertEquals($builder, $builder->setMaxAmountPerPayment(97));
82 | $this->assertEquals($builder, $builder->setMaxPaymentsPerPeriod(544.87));
83 | $this->assertEquals($builder, $builder->setMaxAmountPerPeriod(5432.90));
84 | $this->assertEquals($builder, $builder->setInitialDate(new DateTime('2015-11-18')));
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/tests/Requests/PreApprovals/RequestTest.php:
--------------------------------------------------------------------------------
1 |
12 | */
13 | class RequestTest extends \PHPUnit_Framework_TestCase
14 | {
15 | /**
16 | * @var Request
17 | */
18 | private $request;
19 |
20 | /**
21 | * @var PreApproval
22 | */
23 | private $preApproval;
24 |
25 | protected function setUp()
26 | {
27 | $this->preApproval = $this->createMock(PreApproval::class);
28 | $this->request = new Request($this->preApproval);
29 | }
30 |
31 | /**
32 | * @test
33 | */
34 | public function constructShouldConfigureTheAttributes()
35 | {
36 | $this->assertAttributeInstanceOf(PreApproval::class, 'preApproval', $this->request);
37 | }
38 |
39 | /**
40 | * @test
41 | */
42 | public function getPreApprovalShouldReturnConfiguredPreApproval()
43 | {
44 | $this->assertInstanceOf(PreApproval::class, $this->request->getPreApproval());
45 | }
46 |
47 | /**
48 | * @test
49 | */
50 | public function getReferenceShouldReturnConfiguredReference()
51 | {
52 | $this->request->setReference('someRef');
53 |
54 | $this->assertEquals('someRef', $this->request->getReference());
55 | }
56 |
57 | /**
58 | * @test
59 | */
60 | public function setReferenceShouldChangeTheAttribute()
61 | {
62 | $this->request->setReference('test');
63 |
64 | $this->assertAttributeEquals('test', 'reference', $this->request);
65 | }
66 |
67 | /**
68 | * @test
69 | */
70 | public function getRedirectToShouldReturnConfiguredRedirectTo()
71 | {
72 | $this->request->setRedirectTo('someRedirect');
73 |
74 | $this->assertEquals('someRedirect', $this->request->getRedirectTo());
75 | }
76 |
77 | /**
78 | * @test
79 | */
80 | public function setRedirectToShouldChangeTheAttribute()
81 | {
82 | $this->request->setRedirectTo('otherRedirect');
83 |
84 | $this->assertAttributeEquals('otherRedirect', 'redirectTo', $this->request);
85 | }
86 |
87 | /**
88 | * @test
89 | */
90 | public function getReviewOnShouldReturnConfiguredReviewOn()
91 | {
92 | $this->request->setReviewOn('someReview');
93 |
94 | $this->assertEquals('someReview', $this->request->getReviewOn());
95 | }
96 |
97 | /**
98 | * @test
99 | */
100 | public function setReviewOnToShouldChangeTheAttribute()
101 | {
102 | $this->request->setReviewOn('otherReview');
103 |
104 | $this->assertAttributeEquals('otherReview', 'reviewOn', $this->request);
105 | }
106 |
107 | /**
108 | * @test
109 | */
110 | public function getCustomerShouldReturnConfiguredCustomer()
111 | {
112 | $customer = $this->createMock(Customer::class);
113 | $this->request->setCustomer($customer);
114 |
115 | $this->assertSame($customer, $this->request->getCustomer());
116 | }
117 |
118 | /**
119 | * @test
120 | */
121 | public function setCustomerToShouldChangeTheAttribute()
122 | {
123 | $customer = $this->createMock(Customer::class);
124 | $this->request->setCustomer($customer);
125 |
126 | $this->assertAttributeSame($customer, 'customer', $this->request);
127 | }
128 |
129 | public function testSerializeShouldXMLFull()
130 | {
131 | $preApproval = new PreApproval();
132 | $preApproval->setChargeType('auto');
133 | $preApproval->setName('Assinatura Revista');
134 | $preApproval->setPeriod('MONTHLY');
135 | $preApproval->setFinalDate(new DateTime('2016-11-18', new \DateTimeZone('UTC')));
136 | $preApproval->setMaxTotalAmount(3000);
137 | $preApproval->setDetails('Cobranca Mensal da Revista');
138 | $preApproval->setAmountPerPayment(100);
139 | $preApproval->setMaxAmountPerPayment(150);
140 | $preApproval->setInitialDate(new DateTime('2015-11-18', new \DateTimeZone('UTC')));
141 | $preApproval->setMaxPaymentsPerPeriod(12);
142 | $preApproval->setMaxAmountPerPeriod(1200);
143 |
144 | $customerAddress = new Address('AC', 'Sao Maite', '99500-079', 'Centro', 'Rua David Delgado', '55', 'Fundos');
145 | $customerPhone = new Phone('11', '99999999');
146 | $customer = new Customer('usuario@site.com', 'FooBar', $customerPhone, $customerAddress);
147 |
148 | $request = new Request($preApproval);
149 | $request->setCustomer($customer);
150 | $request->setReference('abcdef');
151 | $request->setReviewOn('http://localhost/return.php');
152 | $request->setRedirectTo('http://localhost/success.php');
153 |
154 | $xml = $request->xmlSerialize();
155 |
156 | $this->assertInstanceOf(SimpleXMLElement::class, $xml);
157 | $expected = simplexml_load_file(__DIR__ . '/xml/preAprovalsRequestFull.xml');
158 | $this->assertEquals($expected, $xml);
159 | }
160 | }
161 |
--------------------------------------------------------------------------------
/tests/Requests/PreApprovals/xml/preAprovalsRequestFull.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | usuario@site.com
5 | FooBar
6 |
7 | 11
8 | 99999999
9 |
10 |
11 | BRA
12 | AC
13 | Sao Maite
14 | 99500079
15 | Centro
16 | Rua David Delgado
17 | 55
18 | Fundos
19 |
20 |
21 |
22 |
23 | auto
24 | Assinatura Revista
25 | MONTHLY
26 | 2016-11-18T00:00:00+00:00
27 | 3000.00
28 | Cobranca Mensal da Revista
29 | 100.00
30 | 150.00
31 | 2015-11-18T00:00:00+00:00
32 | 12
33 | 1200.00
34 |
35 |
36 | abcdef
37 | http://localhost/return.php
38 | http://localhost/success.php
39 |
40 |
--------------------------------------------------------------------------------
/tests/Requests/RedirectionTest.php:
--------------------------------------------------------------------------------
1 | date = new DateTime('2014-05-30');
21 | $this->redirection = new Redirection(1, $this->date, 'http://example.org');
22 | }
23 |
24 | /**
25 | * @test
26 | */
27 | public function constructShouldConfigureTheAttributes()
28 | {
29 | $this->assertAttributeEquals(1, 'code', $this->redirection);
30 | $this->assertAttributeSame($this->date, 'date', $this->redirection);
31 | $this->assertAttributeEquals('http://example.org', 'uri', $this->redirection);
32 | }
33 |
34 | /**
35 | * @test
36 | * @depends constructShouldConfigureTheAttributes
37 | */
38 | public function getCodeShouldReturnTheConfiguredCode()
39 | {
40 | $this->assertEquals(1, $this->redirection->getCode());
41 | }
42 |
43 | /**
44 | * @test
45 | * @depends constructShouldConfigureTheAttributes
46 | */
47 | public function getDateShouldReturnTheConfiredDate()
48 | {
49 | $this->assertSame($this->date, $this->redirection->getDate());
50 | }
51 |
52 | /**
53 | * @test
54 | * @depends constructShouldConfigureTheAttributes
55 | */
56 | public function getRedirectionUrlShouldReturnTheUriWithCodeAsQueryString()
57 | {
58 | $this->assertSame('http://example.org?code=1', $this->redirection->getRedirectionUrl());
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/tests/ServiceTest.php:
--------------------------------------------------------------------------------
1 |
8 | */
9 | class ServiceTest extends \PHPUnit_Framework_TestCase
10 | {
11 | /**
12 | * @var Credentials
13 | */
14 | protected $credentials;
15 |
16 | /**
17 | * @var Client|\PHPUnit_Framework_MockObject_MockObject
18 | */
19 | protected $client;
20 |
21 | protected function setUp()
22 | {
23 | $this->credentials = new Credentials('a@a.com', 't');
24 | $this->client = $this->createMock(Client::class);
25 | }
26 |
27 | /**
28 | * @test
29 | */
30 | public function constructorShouldConfigureAttributes()
31 | {
32 | $service = $this->getMockForAbstractClass(
33 | Service::class,
34 | [$this->credentials, $this->client]
35 | );
36 |
37 | $this->assertAttributeSame($this->credentials, 'credentials', $service);
38 | $this->assertAttributeSame($this->client, 'client', $service);
39 | }
40 |
41 | /**
42 | * @test
43 | */
44 | public function constructorShouldCreateAClientWhenItWasntInformed()
45 | {
46 | $service = $this->getMockForAbstractClass(
47 | Service::class,
48 | [$this->credentials]
49 | );
50 |
51 | $this->assertAttributeInstanceOf(Client::class, 'client', $service);
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/tests/Shipping/ShippingTest.php:
--------------------------------------------------------------------------------
1 | assertAttributeEquals(Type::TYPE_PAC, 'type', $shipping);
25 | $this->assertAttributeEquals(null, 'address', $shipping);
26 | $this->assertAttributeEquals(null, 'cost', $shipping);
27 | }
28 |
29 | /**
30 | * @test
31 | */
32 | public function constructorMustBeAbleToReceiveTypeAndCost()
33 | {
34 | $shipping = new Shipping(Type::TYPE_PAC, null, '10.31');
35 |
36 | $this->assertAttributeEquals(Type::TYPE_PAC, 'type', $shipping);
37 | $this->assertAttributeEquals(null, 'address', $shipping);
38 | $this->assertAttributeEquals(10.31, 'cost', $shipping);
39 | }
40 |
41 | /**
42 | * @test
43 | */
44 | public function constructorMustBeAbleToReceiveTypeAndAddress()
45 | {
46 | $address = $this->createMock(Address::class);
47 | $shipping = new Shipping(Type::TYPE_PAC, $address);
48 |
49 | $this->assertAttributeEquals(Type::TYPE_PAC, 'type', $shipping);
50 | $this->assertAttributeSame($address, 'address', $shipping);
51 | $this->assertAttributeEquals(null, 'cost', $shipping);
52 | }
53 |
54 | /**
55 | * @test
56 | */
57 | public function constructorMustBeAbleToReceiveAllArguments()
58 | {
59 | $address = $this->createMock(Address::class);
60 | $shipping = new Shipping(Type::TYPE_PAC, $address, '10.31');
61 |
62 | $this->assertAttributeEquals(Type::TYPE_PAC, 'type', $shipping);
63 | $this->assertAttributeSame($address, 'address', $shipping);
64 | $this->assertAttributeEquals(10.31, 'cost', $shipping);
65 | }
66 |
67 | /**
68 | * @test
69 | */
70 | public function getterShouldReturnConfiguredData()
71 | {
72 | $address = $this->createMock(Address::class);
73 | $shipping = new Shipping(Type::TYPE_PAC, $address, '10.31');
74 |
75 | $this->assertEquals(Type::TYPE_PAC, $shipping->getType());
76 | $this->assertSame($address, $shipping->getAddress());
77 | $this->assertSame('10.31', $shipping->getCost());
78 | }
79 |
80 | /**
81 | * @test
82 | */
83 | public function xmlSerializeMustAppendFormattedShippingData()
84 | {
85 | $data = simplexml_load_string('');
86 |
87 | $address = new Address('BA', 'Salvador', '40999-999', 'Red River', 'Beco Sem Nome', 25, 'Buteco do França');
88 | $shipping = new Shipping(Type::TYPE_PAC, $address, '10.31');
89 |
90 | $xml = $shipping->xmlSerialize($data);
91 |
92 | $this->assertSame('1', (string) $xml->shipping->type);
93 | $this->assertSame('10.31', (string) $xml->shipping->cost);
94 | $this->assertSame('BRA', (string) $xml->shipping->address->country);
95 | $this->assertSame('BA', (string) $xml->shipping->address->state);
96 | $this->assertSame('Salvador', (string) $xml->shipping->address->city);
97 | $this->assertSame('40999999', (string) $xml->shipping->address->postalCode);
98 | $this->assertSame('Red River', (string) $xml->shipping->address->district);
99 | $this->assertSame('Beco Sem Nome', (string) $xml->shipping->address->street);
100 | $this->assertSame('25', (string) $xml->shipping->address->number);
101 | $this->assertSame('Buteco do França', (string) $xml->shipping->address->complement);
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/tests/Shipping/TypeTest.php:
--------------------------------------------------------------------------------
1 |
6 | */
7 | class TypeTest extends \PHPUnit_Framework_TestCase
8 | {
9 | public function testGetTypesShouldDoReturnArray()
10 | {
11 | $expected = [1, 2, 3];
12 | $this->assertEquals($expected, Type::getTypes());
13 | }
14 |
15 | public function testValidShouldReturnTrue()
16 | {
17 | $this->assertTrue(Type::isValid(1));
18 | $this->assertTrue(Type::isValid(2));
19 | $this->assertTrue(Type::isValid(3));
20 | }
21 |
22 | public function testValuesInvalidShouldReturnFalse()
23 | {
24 | $this->assertFalse(Type::isValid(0));
25 | $this->assertFalse(Type::isValid(4));
26 | }
27 | }
28 |
--------------------------------------------------------------------------------