├── .gitignore
├── .travis.yml
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── composer.json
├── phpunit.xml.dist
├── src
├── Gateway.php
├── Item.php
├── Message
│ ├── AbstractRequest.php
│ ├── CompletePurchaseRequest.php
│ ├── CompletePurchaseResponse.php
│ ├── FetchNotificationRequest.php
│ ├── FetchNotificationResponse.php
│ ├── FindTransactionRequest.php
│ ├── FindTransactionResponse.php
│ ├── PurchaseRequest.php
│ ├── PurchaseResponse.php
│ ├── RefundRequest.php
│ ├── RefundResponse.php
│ ├── TransactionSearchRequest.php
│ └── TransactionSearchResponse.php
└── Support
│ └── Customer
│ ├── Address.php
│ ├── Customer.php
│ └── Phone.php
└── tests
├── GatewayTest.php
└── Mock
├── CompletePurchaseSuccess.txt
└── PurchaseSuccess.txt
/.gitignore:
--------------------------------------------------------------------------------
1 | composer.phar
2 | composer.lock
3 | phpunit.xml
4 | vendor/
5 |
6 | !**/.gitkeep
7 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: php
2 |
3 | php:
4 | - 7.2
5 | - 7.3
6 | - 7.4
7 |
8 | before_script:
9 | - composer selfupdate
10 | - composer install --prefer-dist
11 |
12 | notifications:
13 | email: false
14 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # Contributing Guidelines
2 |
3 | * Fork the project.
4 | * Make your feature addition or bug fix.
5 | * Add tests for it. This is important so I don't break it in a future version unintentionally.
6 | * Commit just the modifications, do not mess with the composer.json or CHANGELOG.md files.
7 | * Ensure your code is nicely formatted in the [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)
8 | style and that all tests pass.
9 | * Send the pull request.
10 | * Check that the Travis CI build passed. If not, rinse and repeat.
11 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 Abdala Cerqueira
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](https://travis-ci.org/abdala/omnipay-pagseguro)
2 | # Omnipay: PagSeguro
3 |
4 | **PagSeguro driver for the Omnipay PHP payment processing library**
5 |
6 | [Omnipay](https://github.com/thephpleague/omnipay) is a framework agnostic, multi-gateway payment
7 | processing library for PHP. This package implements PagSeguro support for Omnipay.
8 |
9 | ## Installation
10 |
11 | Omnipay is installed via [Composer](http://getcomposer.org/). To install, simply add it
12 | to your `composer.json` file:
13 |
14 | ```json
15 | {
16 | "require": {
17 | "abdala/omnipay-pagseguro": "^1.3.0"
18 | }
19 | }
20 | ```
21 |
22 | And run composer to update your dependencies:
23 |
24 | $ curl -s http://getcomposer.org/installer | php
25 | $ php composer.phar update
26 |
27 | ## Basic Usage
28 |
29 | The following gateways are provided by this package:
30 |
31 | * PagSeguro
32 |
33 | For general usage instructions, please see the main [Omnipay](https://github.com/thephpleague/omnipay)
34 | repository.
35 |
36 | ## Support
37 |
38 | If you are having general issues with Omnipay, we suggest posting on
39 | [Stack Overflow](http://stackoverflow.com/). Be sure to add the
40 | [omnipay tag](http://stackoverflow.com/questions/tagged/omnipay) so it can be easily found.
41 |
42 | If you want to keep up to date with release anouncements, discuss ideas for the project,
43 | or ask more detailed questions, there is also a [mailing list](https://groups.google.com/forum/#!forum/omnipay) which
44 | you can subscribe to.
45 |
46 | If you believe you have found a bug, please report it using the [GitHub issue tracker](https://github.com/abdala/omnipay-pagseguro/issues),
47 | or better yet, fork the library and submit a pull request.
48 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "abdala/omnipay-pagseguro",
3 | "type": "library",
4 | "description": "PagSeguro gateway for OmniPay",
5 | "keywords": [
6 | "pagseguro",
7 | "mp",
8 | "gateway",
9 | "merchant",
10 | "omnipay",
11 | "pay",
12 | "payment",
13 | "PHPeste"
14 | ],
15 | "homepage": "https://github.com/abdala/omnipay-pagseguro",
16 | "license": "MIT",
17 | "authors": [
18 | {
19 | "name": "Abdala Cerqueira",
20 | "email": "abdala.cerqueira@gmail.com"
21 | }
22 | ],
23 | "autoload": {
24 | "psr-4": { "Omnipay\\PagSeguro\\" : "src/" }
25 | },
26 | "require": {
27 | "omnipay/common": "^3.0"
28 | },
29 | "require-dev": {
30 | "omnipay/tests": "^3.0",
31 | "doctrine/coding-standard": "^7.0",
32 | "phpstan/phpstan": "^0.12.17"
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/phpunit.xml.dist:
--------------------------------------------------------------------------------
1 |
2 |
12 |
13 |
14 | ./tests/
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 | ./src
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/src/Gateway.php:
--------------------------------------------------------------------------------
1 | '',
38 | 'token' => '',
39 | 'sandbox' => false,
40 | ];
41 | }
42 |
43 | public function getEmail()
44 | {
45 | return $this->getParameter('email');
46 | }
47 |
48 | public function setEmail($value)
49 | {
50 | return $this->setParameter('email', $value);
51 | }
52 |
53 | public function getToken()
54 | {
55 | return $this->getParameter('token');
56 | }
57 |
58 | public function setToken($value)
59 | {
60 | return $this->setParameter('token', $value);
61 | }
62 |
63 | public function getSandbox()
64 | {
65 | return $this->getParameter('sandbox');
66 | }
67 |
68 | public function setSandbox($value)
69 | {
70 | return $this->setParameter('sandbox', $value);
71 | }
72 |
73 | public function purchase(array $parameters = [])
74 | {
75 | return $this->createRequest('Omnipay\\PagSeguro\\Message\\PurchaseRequest', $parameters);
76 | }
77 |
78 | public function refund(array $parameters = [])
79 | {
80 | return $this->createRequest('Omnipay\\PagSeguro\\Message\\RefundRequest', $parameters);
81 | }
82 |
83 | public function completePurchase(array $parameters = [])
84 | {
85 | return $this->createRequest('Omnipay\\PagSeguro\\Message\\CompletePurchaseRequest', $parameters);
86 | }
87 |
88 | public function findTransaction(array $parameters = []) : Message\FindTransactionRequest
89 | {
90 | return $this->createRequest('Omnipay\\PagSeguro\\Message\\FindTransactionRequest', $parameters);
91 | }
92 |
93 | public function transactionSearch(array $parameters = []) : Message\TransactionSearchRequest
94 | {
95 | return $this->createRequest('Omnipay\\PagSeguro\\Message\\TransactionSearchRequest', $parameters);
96 | }
97 |
98 | public function fetchNotification(array $parameters = [])
99 | {
100 | return $this->createRequest('Omnipay\\PagSeguro\\Message\\FetchNotificationRequest', $parameters);
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/src/Item.php:
--------------------------------------------------------------------------------
1 | getParameter('weight');
14 | }
15 |
16 | public function setWeight($value)
17 | {
18 | return $this->setParameter('weight', $value);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/Message/AbstractRequest.php:
--------------------------------------------------------------------------------
1 | getParameter('email');
26 | }
27 |
28 | public function setEmail($value)
29 | {
30 | return $this->setParameter('email', $value);
31 | }
32 |
33 | public function getToken()
34 | {
35 | return $this->getParameter('token');
36 | }
37 |
38 | public function setToken($value)
39 | {
40 | return $this->setParameter('token', $value);
41 | }
42 |
43 | public function getSandbox()
44 | {
45 | return $this->getParameter('sandbox');
46 | }
47 |
48 | public function setSandbox($value)
49 | {
50 | return $this->setParameter('sandbox', $value);
51 | }
52 |
53 | public function getData()
54 | {
55 | $this->validate('email', 'token');
56 |
57 | return [
58 | 'email' => $this->getEmail(),
59 | 'token' => $this->getToken(),
60 | ];
61 | }
62 |
63 | public function getHttpMethod()
64 | {
65 | return 'POST';
66 | }
67 |
68 | public function getResource()
69 | {
70 | return $this->resource;
71 | }
72 |
73 | public function getHeaders()
74 | {
75 | return ['Content-Type' => 'application/x-www-form-urlencoded'];
76 | }
77 |
78 | public function sendData($data)
79 | {
80 | $url = sprintf(
81 | '%s/%s?%s',
82 | $this->getEndpoint(),
83 | trim($this->getResource(), '/'),
84 | http_build_query($data, '', '&')
85 | );
86 |
87 | $httpResponse = $this->httpClient->request($this->getHttpMethod(), $url, $this->getHeaders());
88 | $xml = simplexml_load_string($httpResponse->getBody()->getContents(), 'SimpleXMLElement', LIBXML_NOCDATA);
89 |
90 | return $this->createResponse($this->xml2array($xml));
91 | }
92 |
93 | public function getEndpoint()
94 | {
95 | return $this->getSandbox() ? $this->sandboxEndpoint : $this->endpoint;
96 | }
97 |
98 | protected function xml2array($xml)
99 | {
100 | return json_decode(json_encode($xml), true);
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/src/Message/CompletePurchaseRequest.php:
--------------------------------------------------------------------------------
1 | getParameter('notificationCode');
19 | }
20 |
21 | public function setNotificationCode($value)
22 | {
23 | return $this->setParameter('notificationCode', $value);
24 | }
25 |
26 | protected function createResponse($data)
27 | {
28 | return $this->response = new CompletePurchaseResponse($this, $data);
29 | }
30 |
31 | public function getHttpMethod()
32 | {
33 | return 'GET';
34 | }
35 |
36 | public function sendData($data)
37 | {
38 | $url = sprintf(
39 | '%s/%s/%s?%s',
40 | $this->getEndpoint(),
41 | $this->getResource(),
42 | $this->getNotificationCode(),
43 | http_build_query($data, '', '&')
44 | );
45 |
46 | $httpResponse = $this->httpClient->request($this->getHttpMethod(), $url);
47 | $xml = simplexml_load_string($httpResponse->getBody()->getContents(), 'SimpleXMLElement', LIBXML_NOCDATA);
48 |
49 | return $this->createResponse($this->xml2array($xml));
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/Message/CompletePurchaseResponse.php:
--------------------------------------------------------------------------------
1 | data['error']) ? false : true;
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/Message/FetchNotificationRequest.php:
--------------------------------------------------------------------------------
1 | getParameter('notificationCode');
27 | }
28 |
29 | public function getHttpMethod()
30 | {
31 | return 'GET';
32 | }
33 |
34 | public function setNotificationCode($value)
35 | {
36 | return $this->setParameter('notificationCode', $value);
37 | }
38 |
39 | public function sendData($data)
40 | {
41 | $this->validate('notificationCode');
42 |
43 | $url = sprintf(
44 | '%s/%s/%s?%s',
45 | $this->getEndpoint(),
46 | $this->getResource(),
47 | $this->getNotificationCode(),
48 | http_build_query($data, '', '&')
49 | );
50 |
51 | $httpResponse = $this->httpClient->request($this->getHttpMethod(), $url, $this->getHeaders());
52 | $xml = simplexml_load_string($httpResponse->getBody()->getContents(), 'SimpleXMLElement', LIBXML_NOCDATA);
53 |
54 | return $this->createResponse($this->xml2array($xml));
55 | }
56 |
57 | public function createResponse($data)
58 | {
59 | return $this->response = new FetchNotificationResponse($this, $data);
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/src/Message/FetchNotificationResponse.php:
--------------------------------------------------------------------------------
1 | data['code']);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/Message/FindTransactionRequest.php:
--------------------------------------------------------------------------------
1 | getParameter('transactionId');
19 | }
20 |
21 | public function setTransactionID($value)
22 | {
23 | return $this->setParameter('transactionId', $value);
24 | }
25 |
26 | protected function createResponse($data)
27 | {
28 | return $this->response = new FindTransactionResponse($this, $data);
29 | }
30 |
31 | public function getHttpMethod()
32 | {
33 | return 'GET';
34 | }
35 |
36 | public function sendData($data)
37 | {
38 | $url = sprintf(
39 | '%s/%s/%s?%s',
40 | $this->getEndpoint(),
41 | $this->getResource(),
42 | $this->getTransactionID(),
43 | http_build_query($data, '', '&')
44 | );
45 |
46 | $httpResponse = $this->httpClient->request($this->getHttpMethod(), $url);
47 | $xml = simplexml_load_string($httpResponse->getBody()->getContents(), 'SimpleXMLElement', LIBXML_NOCDATA);
48 |
49 | return $this->createResponse($this->xml2array($xml));
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/Message/FindTransactionResponse.php:
--------------------------------------------------------------------------------
1 | data['error']) ? false : true;
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/Message/PurchaseRequest.php:
--------------------------------------------------------------------------------
1 | getParameter('shippingType');
28 | }
29 |
30 | public function setShippingType($value)
31 | {
32 | return $this->setParameter('shippingType', $value);
33 | }
34 |
35 | public function getShippingCost()
36 | {
37 | return $this->getParameter('shippingCost');
38 | }
39 |
40 | public function setShippingCost($value)
41 | {
42 | return $this->setParameter('shippingCost', $value);
43 | }
44 |
45 | public function getCustomer()
46 | {
47 | return $this->getParameter('customer');
48 | }
49 |
50 | public function setCustomer(Customer $value)
51 | {
52 | return $this->setParameter('customer', $value);
53 | }
54 |
55 | public function getExtraAmount()
56 | {
57 | $extraAmount = $this->getParameter('extraAmount');
58 |
59 | if ($extraAmount !== null && $extraAmount !== 0) {
60 | if ($this->getCurrencyDecimalPlaces() > 0) {
61 | if (is_int($extraAmount) || (is_string($extraAmount) && strpos((string) $extraAmount, '.') === false)) {
62 | throw new InvalidRequestException(
63 | 'Please specify extra amount as a string or float, with decimal places.'
64 | );
65 | }
66 | }
67 |
68 | // Check for rounding that may occur if too many significant decimal digits are supplied.
69 | $decimal_count = strlen(substr(strrchr(sprintf('%.8g', $extraAmount), '.'), 1));
70 | if ($decimal_count > $this->getCurrencyDecimalPlaces()) {
71 | throw new InvalidRequestException('Amount precision is too high for currency.');
72 | }
73 |
74 | return $this->formatCurrency($extraAmount);
75 | }
76 | }
77 |
78 | public function setExtraAmount($value)
79 | {
80 | return $this->setParameter('extraAmount', $value);
81 | }
82 |
83 | public function setItems($items)
84 | {
85 | $serializedItems = [];
86 |
87 | foreach ($items as $item) {
88 | if ($item instanceof Item) {
89 | $serializedItems[] = $item;
90 | } else {
91 | if ($item instanceof \Omnipay\Common\Item) {
92 | $serializedItems[] = new Item($item->getParameters());
93 | } else {
94 | $serializedItems[] = new Item($item);
95 | }
96 | }
97 | }
98 |
99 | return $this->setParameter('items', $serializedItems);
100 | }
101 |
102 | protected function getItemData()
103 | {
104 | $data = [];
105 | $items = $this->getItems();
106 |
107 | if ($items) {
108 | foreach ($items as $n => $item) {
109 | $i = $n + 1;
110 | $data["itemId$i"] = $item->getName();
111 | $data["itemDescription$i"] = $item->getDescription();
112 | $data["itemAmount$i"] = $this->formatCurrency($item->getPrice());
113 | $data["itemQuantity$i"] = $item->getQuantity();
114 | $data["itemWeight$i"] = $item->getWeight();
115 | }
116 | }
117 |
118 | return $data;
119 | }
120 |
121 | protected function getCustomerData()
122 | {
123 | $data = [];
124 | $customer = $this->getCustomer();
125 |
126 | if ($customer) {
127 | $data['senderEmail'] = $customer->getEmail();
128 | $data['senderName'] = $customer->getName();
129 | $data['senderCPF'] = $customer->getCPF();
130 | $data['senderBornDate'] = $customer->getBornDate();
131 |
132 | $phone = $customer->getPhone();
133 | if ($phone->getParameters()) {
134 | $data['senderAreaCode'] = $phone->getAreaCode();
135 | $data['senderPhone'] = $phone->getPhone();
136 | }
137 | }
138 |
139 | return $data;
140 | }
141 |
142 | protected function getShippingData()
143 | {
144 | $data = [];
145 |
146 | $data['shippingType'] = ! empty($this->getShippingType()) ? $this->getShippingType() : $this->shippingType;
147 | $data['shippingCost'] = ! empty($this->getShippingCost()) ? $this->formatCurrency($this->getShippingCost()) : '0.00';
148 |
149 | $customer = $this->getCustomer();
150 | if ($customer) {
151 | $address = $customer->getAddress();
152 |
153 | if ($address->getParameters()) {
154 | $data['shippingAddressCountry'] = $address->getCountry();
155 | $data['shippingAddressState'] = $address->getState();
156 | $data['shippingAddressCity'] = $address->getCity();
157 | $data['shippingAddressPostalCode'] = $address->getPostalCode();
158 | $data['shippingAddressDistrict'] = $address->getDistrict();
159 | $data['shippingAddressStreet'] = $address->getStreet();
160 | $data['shippingAddressNumber'] = $address->getNumber();
161 | $data['shippingAddressComplement'] = $address->getComplement();
162 | }
163 | }
164 |
165 | return $data;
166 | }
167 |
168 | public function getData()
169 | {
170 | $this->validate('currency', 'transactionReference');
171 |
172 | $data = [
173 | 'currency' => $this->getCurrency(),
174 | 'extraAmount' => $this->getExtraAmount(),
175 | 'reference' => $this->getTransactionReference(),
176 | 'redirectURL' => $this->getReturnUrl(),
177 | 'notificationURL' => $this->getNotifyUrl(),
178 | ];
179 |
180 | return array_merge(parent::getData(), $data, $this->getItemData(), $this->getCustomerData(), $this->getShippingData());
181 | }
182 |
183 | protected function createResponse($data)
184 | {
185 | return $this->response = new PurchaseResponse($this, $data);
186 | }
187 | }
188 |
--------------------------------------------------------------------------------
/src/Message/PurchaseResponse.php:
--------------------------------------------------------------------------------
1 | data['error']) ? false : true;
20 | }
21 |
22 | public function isRedirect()
23 | {
24 | return true;
25 | }
26 |
27 | public function getRedirectUrl()
28 | {
29 | $request = $this->getRequest();
30 |
31 | return sprintf(
32 | '%s/%s/payment.html?code=%s',
33 | str_replace('ws.', '', $request->getEndpoint()),
34 | trim($request->getResource(), '/'),
35 | $this->data['code']
36 | );
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/Message/RefundRequest.php:
--------------------------------------------------------------------------------
1 |
18 | * // Do a refund transaction on the gateway
19 | * $transaction = $gateway->refund(array(
20 | * 'amount' => '10.00',
21 | * 'transactionReference' => $transactionCode,
22 | * ));
23 | *
24 | * $response = $transaction->send();
25 | * if ($response->isSuccessful()) {
26 | * }
27 | *
28 | */
29 |
30 | class RefundRequest extends AbstractRequest
31 | {
32 | protected $resource = 'transactions/refunds';
33 |
34 | public function getData()
35 | {
36 | $this->validate('transactionReference');
37 |
38 | $data = [
39 | 'transactionCode' => $this->getTransactionReference(),
40 | ];
41 |
42 | if ($this->getAmount()) {
43 | $data['refundValue'] = $this->getAmount();
44 | }
45 |
46 | return array_merge(parent::getData(), $data);
47 | }
48 |
49 | public function sendData($data)
50 | {
51 | $url = sprintf(
52 | '%s/%s?%s',
53 | $this->getEndpoint(),
54 | $this->getResource(),
55 | http_build_query($data, '', '&')
56 | );
57 |
58 | $httpResponse = $this->httpClient->request($this->getHttpMethod(), $url);
59 | $xml = simplexml_load_string($httpResponse->getBody()->getContents(), 'SimpleXMLElement', LIBXML_NOCDATA);
60 |
61 | return $this->createResponse($this->xml2array($xml));
62 | }
63 |
64 | protected function createResponse($data)
65 | {
66 | return $this->response = new RefundResponse($this, $data);
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/src/Message/RefundResponse.php:
--------------------------------------------------------------------------------
1 | data['result']) && $this->data['result'] === 'OK';
21 | }
22 |
23 | public function getMessage()
24 | {
25 | if (! $this->isSuccessful() && isset($this->data['errors'])) {
26 | return $this->data;
27 | }
28 |
29 | return null;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/Message/TransactionSearchRequest.php:
--------------------------------------------------------------------------------
1 | getParameter('abandoned');
23 | }
24 |
25 | public function setAbandoned($value) : \Omnipay\Common\Message\AbstractRequest
26 | {
27 | return $this->setParameter('abandoned', $value);
28 | }
29 |
30 | public function getStartDate() : ?DateTime
31 | {
32 | return $this->getParameter('startDate');
33 | }
34 |
35 | /**
36 | * @param DateTime|string $date
37 | */
38 | public function setStartDate($date) : \Omnipay\Common\Message\AbstractRequest
39 | {
40 | if (! $date instanceof DateTime) {
41 | $date = new DateTime($date);
42 | }
43 |
44 | return $this->setParameter('startDate', $date);
45 | }
46 |
47 | public function getEndDate() : ?DateTime
48 | {
49 | return $this->getParameter('endDate');
50 | }
51 |
52 | /**
53 | * @param DateTime|string $date
54 | */
55 | public function setEndDate($date) : \Omnipay\Common\Message\AbstractRequest
56 | {
57 | if (! $date instanceof DateTime) {
58 | $date = new DateTime($date);
59 | }
60 |
61 | return $this->setParameter('endDate', $date);
62 | }
63 |
64 | public function getPage()
65 | {
66 | return $this->getParameter('page');
67 | }
68 |
69 | public function setPage(string $page) : \Omnipay\Common\Message\AbstractRequest
70 | {
71 | return $this->setParameter('page', $page);
72 | }
73 |
74 | public function getMaxPageResults()
75 | {
76 | return $this->getParameter('maxPageResults');
77 | }
78 |
79 | public function setMaxPageResults(int $maxPageResults) : \Omnipay\Common\Message\AbstractRequest
80 | {
81 | return $this->setParameter('maxPageResults', $maxPageResults);
82 | }
83 |
84 | protected function createResponse($data)
85 | {
86 | return $this->response = new TransactionSearchResponse($this, $data);
87 | }
88 |
89 | public function getData()
90 | {
91 | $this->validate('startDate', 'endDate');
92 |
93 | $now = new DateTime('now');
94 | $startDate = $this->getStartDate();
95 | $finalDate = $this->getEndDate();
96 |
97 | //Start Date and End Date less than today
98 | if ($startDate >= $now) {
99 | throw new InvalidRequestException('The initial date must be less than today');
100 | }
101 |
102 | if ($finalDate >= $now) {
103 | throw new InvalidRequestException('The final date must be less than today');
104 | }
105 |
106 | //The interval between the initial date and final date must be less than 30 days
107 | if ($startDate->diff($finalDate)->days > 30) {
108 | throw new InvalidRequestException('The interval between the initial date and final date must be less than 30 days');
109 | }
110 |
111 | $data = [
112 | 'initialDate' => $startDate->format('Y-m-d\TH:i'),
113 | 'finalDate' => $finalDate->format('Y-m-d\TH:i'),
114 | 'page' => $this->getPage(),
115 | 'maxPageResults' => $this->getMaxPageResults(),
116 | ];
117 |
118 | return array_merge(parent::getData(), $data);
119 | }
120 |
121 | public function getHttpMethod()
122 | {
123 | return 'GET';
124 | }
125 |
126 | public function sendData($data)
127 | {
128 | $url = sprintf(
129 | '%s/%s?%s',
130 | $this->getEndpoint(),
131 | $this->getResource(),
132 | http_build_query($data, '', '&')
133 | );
134 |
135 | $httpResponse = $this->httpClient->request($this->getHttpMethod(), $url);
136 | $xml = simplexml_load_string($httpResponse->getBody()->getContents(), 'SimpleXMLElement', LIBXML_NOCDATA);
137 |
138 | return $this->createResponse($this->xml2array($xml));
139 | }
140 |
141 | public function getResource()
142 | {
143 | if ($this->getAbandoned()) {
144 | return $this->abandonedResource;
145 | }
146 |
147 | return $this->resource;
148 | }
149 | }
150 |
--------------------------------------------------------------------------------
/src/Message/TransactionSearchResponse.php:
--------------------------------------------------------------------------------
1 | isSuccessful()) {
21 | if ($this->getData()['resultsInThisPage'] === 1) {
22 | $transactions[] = json_decode(json_encode($this->getData()['transactions']['transaction']), true);
23 | } elseif ($this->getData()['resultsInThisPage'] > 0) {
24 | foreach ($this->getData()['transactions']['transaction'] as $transaction) {
25 | $transactions[] = json_decode(json_encode($transaction), true);
26 | }
27 | }
28 | }
29 |
30 | $this->data['transactions'] = $transactions;
31 | }
32 |
33 | public function getTransactions()
34 | {
35 | return $this->data['transactions'];
36 | }
37 |
38 | public function isSuccessful()
39 | {
40 | return isset($this->data['error']) ? false : true;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/Support/Customer/Address.php:
--------------------------------------------------------------------------------
1 | initialize($parameters);
27 | }
28 |
29 | /**
30 | * Initialize the object with parameters.
31 | *
32 | * If any unknown parameters passed, they will be ignored.
33 | *
34 | * @param array $parameters An associative array of parameters
35 | *
36 | * @return $this
37 | */
38 | public function initialize(?array $parameters = null)
39 | {
40 | $this->parameters = new ParameterBag();
41 |
42 | Helper::initialize($this, $parameters);
43 |
44 | return $this;
45 | }
46 |
47 | /**
48 | * Get all parameters.
49 | *
50 | * @return array An associative array of parameters.
51 | */
52 | public function getParameters() : array
53 | {
54 | return $this->parameters->all();
55 | }
56 |
57 | /**
58 | * Get one parameter.
59 | *
60 | * @return mixed A single parameter value.
61 | */
62 | protected function getParameter($key)
63 | {
64 | return $this->parameters->get($key);
65 | }
66 |
67 | /**
68 | * Set one parameter.
69 | *
70 | * @param string $key Parameter key
71 | * @param mixed $value Parameter value
72 | *
73 | * @return $this
74 | */
75 | protected function setParameter(string $key, $value)
76 | {
77 | $this->parameters->set($key, $value);
78 |
79 | return $this;
80 | }
81 |
82 | /**
83 | * Sets country.
84 | *
85 | * @return $this.
86 | */
87 | public function setCountry(string $value)
88 | {
89 | return $this->setParameter('country', $value);
90 | }
91 |
92 | /**
93 | * Get country.
94 | */
95 | public function getCountry() : string
96 | {
97 | return $this->getParameter('country');
98 | }
99 |
100 | /**
101 | * Sets state.
102 | *
103 | * @return $this.
104 | */
105 | public function setState(string $value)
106 | {
107 | return $this->setParameter('state', $value);
108 | }
109 |
110 | /**
111 | * Get state.
112 | */
113 | public function getState() : string
114 | {
115 | return $this->getParameter('state');
116 | }
117 |
118 | /**
119 | * Sets city.
120 | *
121 | * @return $this.
122 | */
123 | public function setCity(string $value)
124 | {
125 | return $this->setParameter('city', $value);
126 | }
127 |
128 | /**
129 | * Get city.
130 | */
131 | public function getCity() : string
132 | {
133 | return $this->getParameter('city');
134 | }
135 |
136 | /**
137 | * Sets postal code.
138 | *
139 | * @return $this.
140 | */
141 | public function setPostalCode(string $value)
142 | {
143 | return $this->setParameter('postalCode', $value);
144 | }
145 |
146 | /**
147 | * Get postal code.
148 | */
149 | public function getPostalCode() : string
150 | {
151 | return $this->getParameter('postalCode');
152 | }
153 |
154 | /**
155 | * Sets postal district.
156 | *
157 | * @return $this.
158 | */
159 | public function setDistrict(string $value)
160 | {
161 | return $this->setParameter('district', $value);
162 | }
163 |
164 | /**
165 | * Get postal district.
166 | */
167 | public function getDistrict() : string
168 | {
169 | return $this->getParameter('district');
170 | }
171 |
172 | /**
173 | * Sets street.
174 | *
175 | * @return $this.
176 | */
177 | public function setStreet(string $value)
178 | {
179 | return $this->setParameter('street', $value);
180 | }
181 |
182 | /**
183 | * Get street.
184 | */
185 | public function getStreet() : string
186 | {
187 | return $this->getParameter('street');
188 | }
189 |
190 | /**
191 | * Sets number.
192 | *
193 | * @return $this.
194 | */
195 | public function setNumber(string $value)
196 | {
197 | return $this->setParameter('number', $value);
198 | }
199 |
200 | /**
201 | * Get number.
202 | */
203 | public function getNumber() : string
204 | {
205 | return $this->getParameter('number');
206 | }
207 |
208 | /**
209 | * Sets complement.
210 | *
211 | * @return $this.
212 | */
213 | public function setComplement(string $value)
214 | {
215 | return $this->setParameter('complement', $value);
216 | }
217 |
218 | /**
219 | * Get complement.
220 | */
221 | public function getComplement() : string
222 | {
223 | return $this->getParameter('complement');
224 | }
225 | }
226 |
--------------------------------------------------------------------------------
/src/Support/Customer/Customer.php:
--------------------------------------------------------------------------------
1 | initialize($parameters);
27 |
28 | $this->setPhone($phone);
29 | $this->setAddress($address);
30 | }
31 |
32 | /**
33 | * Initialize the object with parameters.
34 | *
35 | * If any unknown parameters passed, they will be ignored.
36 | *
37 | * @param array $parameters An associative array of parameters
38 | *
39 | * @return $this
40 | */
41 | public function initialize(?array $parameters = null)
42 | {
43 | $this->parameters = new ParameterBag();
44 |
45 | Helper::initialize($this, $parameters);
46 |
47 | return $this;
48 | }
49 |
50 | /**
51 | * Get all parameters.
52 | *
53 | * @return array An associative array of parameters.
54 | */
55 | public function getParameters() : array
56 | {
57 | return $this->parameters->all();
58 | }
59 |
60 | /**
61 | * Get one parameter.
62 | *
63 | * @return mixed A single parameter value.
64 | */
65 | protected function getParameter($key)
66 | {
67 | return $this->parameters->get($key);
68 | }
69 |
70 | /**
71 | * Set one parameter.
72 | *
73 | * @param string $key Parameter key
74 | * @param mixed $value Parameter value
75 | *
76 | * @return $this
77 | */
78 | protected function setParameter(string $key, $value)
79 | {
80 | $this->parameters->set($key, $value);
81 |
82 | return $this;
83 | }
84 |
85 | /**
86 | * Sets email.
87 | *
88 | * @return $this.
89 | */
90 | public function setEmail(string $value)
91 | {
92 | return $this->setParameter('email', $value);
93 | }
94 |
95 | /**
96 | * Get email.
97 | */
98 | public function getEmail() : string
99 | {
100 | return $this->getParameter('email');
101 | }
102 |
103 | /**
104 | * Sets name.
105 | *
106 | * @return $this.
107 | */
108 | public function setName(string $value)
109 | {
110 | return $this->setParameter('name', $value);
111 | }
112 |
113 | /**
114 | * Get name.
115 | */
116 | public function getName() : string
117 | {
118 | return $this->getParameter('name');
119 | }
120 |
121 | /**
122 | * Sets cpf.
123 | *
124 | * @return $this.
125 | */
126 | public function setCPF(string $value)
127 | {
128 | return $this->setParameter('cpf', $value);
129 | }
130 |
131 | /**
132 | * Get cpf.
133 | */
134 | public function getCPF() : string
135 | {
136 | return $this->getParameter('cpf');
137 | }
138 |
139 | /**
140 | * Sets born date.
141 | *
142 | * @return $this.
143 | */
144 | public function setBornDate(string $value)
145 | {
146 | return $this->setParameter('bornDate', $value);
147 | }
148 |
149 | /**
150 | * Get born date.
151 | */
152 | public function getBornDate() : string
153 | {
154 | return $this->getParameter('bornDate');
155 | }
156 |
157 | /**
158 | * Sets Phone.
159 | *
160 | * @return $this.
161 | */
162 | public function setPhone(Phone $value)
163 | {
164 | return $this->setParameter('phone', $value);
165 | }
166 |
167 | /**
168 | * Get Phone.
169 | */
170 | public function getPhone() : Phone
171 | {
172 | return $this->getParameter('phone');
173 | }
174 |
175 | /**
176 | * Sets Address.
177 | *
178 | * @return $this.
179 | */
180 | public function setAddress(Address $value)
181 | {
182 | return $this->setParameter('address', $value);
183 | }
184 |
185 | /**
186 | * Get Address.
187 | */
188 | public function getAddress() : Address
189 | {
190 | return $this->getParameter('address');
191 | }
192 | }
193 |
--------------------------------------------------------------------------------
/src/Support/Customer/Phone.php:
--------------------------------------------------------------------------------
1 | initialize($parameters);
27 | }
28 |
29 | /**
30 | * Initialize the object with parameters.
31 | *
32 | * If any unknown parameters passed, they will be ignored.
33 | *
34 | * @param array $parameters An associative array of parameters
35 | *
36 | * @return $this
37 | */
38 | public function initialize(?array $parameters = null)
39 | {
40 | $this->parameters = new ParameterBag();
41 |
42 | Helper::initialize($this, $parameters);
43 |
44 | return $this;
45 | }
46 |
47 | /**
48 | * Get all parameters.
49 | *
50 | * @return array An associative array of parameters.
51 | */
52 | public function getParameters() : array
53 | {
54 | return $this->parameters->all();
55 | }
56 |
57 | /**
58 | * Get one parameter.
59 | *
60 | * @return mixed A single parameter value.
61 | */
62 | protected function getParameter($key)
63 | {
64 | return $this->parameters->get($key);
65 | }
66 |
67 | /**
68 | * Set one parameter.
69 | *
70 | * @param string $key Parameter key
71 | * @param mixed $value Parameter value
72 | *
73 | * @return $this
74 | */
75 | protected function setParameter(string $key, $value)
76 | {
77 | $this->parameters->set($key, $value);
78 |
79 | return $this;
80 | }
81 |
82 | /**
83 | * Sets area code.
84 | *
85 | * @return $this.
86 | */
87 | public function setAreaCode(string $value)
88 | {
89 | return $this->setParameter('areaCode', $value);
90 | }
91 |
92 | /**
93 | * Get area code.
94 | */
95 | public function getAreaCode() : string
96 | {
97 | return $this->getParameter('areaCode');
98 | }
99 |
100 | /**
101 | * Sets phone.
102 | *
103 | * @return $this.
104 | */
105 | public function setPhone(string $value)
106 | {
107 | return $this->setParameter('phone', $value);
108 | }
109 |
110 | /**
111 | * Get phone.
112 | */
113 | public function getPhone() : string
114 | {
115 | return $this->getParameter('phone');
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/tests/GatewayTest.php:
--------------------------------------------------------------------------------
1 | gateway = new Gateway($this->getHttpClient(), $this->getHttpRequest());
29 |
30 | $this->options = array(
31 | 'email' => PAGSEGURO_API_EMAIL,
32 | 'token' => PAGSEGURO_API_KEY,
33 | 'currency' => 'BRL',
34 | 'transactionReference' => 'ref',
35 | 'sandbox' => true,
36 | 'returnUrl' => 'https://www.example.com/return',
37 | 'cancelUrl' => 'https://www.example.com/cancel',
38 | );
39 | }
40 |
41 | public function testPurchaseSuccess()
42 | {
43 | $this->options['amount'] = '10.00';
44 |
45 | $this->setMockHttpResponse('PurchaseSuccess.txt');
46 |
47 | $response = $this->gateway->purchase($this->options)->send();
48 |
49 | $this->assertInstanceOf('\Omnipay\PagSeguro\Message\PurchaseResponse', $response);
50 | $this->assertTrue($response->isSuccessful());
51 | $this->assertTrue($response->isRedirect());
52 | $this->assertEquals('https://sandbox.pagseguro.uol.com.br/v2/checkout/payment.html?code=8CF4BE7DCECEF0F004A6DFA0A8243412', $response->getRedirectUrl());
53 | }
54 |
55 | public function testCompletePurchaseHttpOptions()
56 | {
57 | $this->setMockHttpResponse('CompletePurchaseSuccess.txt');
58 |
59 | $this->options['notificationCode'] = '9E884542-81B3-4419-9A75-BCC6FB495EF1';
60 |
61 | $response = $this->gateway->completePurchase($this->options)->send();
62 |
63 | $this->assertInstanceOf('\Omnipay\PagSeguro\Message\CompletePurchaseResponse', $response);
64 | $this->assertTrue($response->isSuccessful());
65 | $this->assertSame('9E884542-81B3-4419-9A75-BCC6FB495EF1', $response->getData()['code']);
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/tests/Mock/CompletePurchaseSuccess.txt:
--------------------------------------------------------------------------------
1 | HTTP/1.1 200 OK
2 | Date: Fri, 15 Feb 2013 19:19:21 GMT
3 | Server: Apache
4 | Content-Length: 136
5 | Connection: close
6 | Content-Type: application/xml; charset=utf-8
7 |
8 |
9 |
10 | 2011-02-10T16:13:41.000-03:00
11 | 9E884542-81B3-4419-9A75-BCC6FB495EF1
12 | REF1234
13 | 1
14 | 3
15 | 2011-02-15T17:39:14.000-03:00
16 |
17 | 1
18 | 101
19 |
20 | 49900.00
21 | 0.00
22 | 0.00
23 | 49900.00
24 | 0.00
25 | 1
26 | 2
27 |
28 | -
29 | 0001
30 | Notebook Prata
31 | 1
32 | 24300.00
33 |
34 |
35 |
36 | José Comprador
37 | comprador@uol.com.br
38 |
39 | 11
40 | 56273440
41 |
42 |
43 |
44 |
45 | Av. Brig. Faria Lima
46 | 1384
47 | 5o andar
48 | Jardim Paulistano
49 | 01452002
50 | Sao Paulo
51 | SP
52 | BRA
53 |
54 | 1
55 | 21.69
56 |
57 |
58 |
--------------------------------------------------------------------------------
/tests/Mock/PurchaseSuccess.txt:
--------------------------------------------------------------------------------
1 | HTTP/1.1 200 OK
2 | Date: Fri, 15 Feb 2013 19:19:21 GMT
3 | Server: Apache
4 | Content-Length: 136
5 | Connection: close
6 | Content-Type: application/xml; charset=utf-8
7 |
8 |
9 |
10 | 8CF4BE7DCECEF0F004A6DFA0A8243412
11 | 2010-12-02T10:11:28.000-02:00
12 |
13 |
--------------------------------------------------------------------------------