├── phpstan.neon ├── .gitignore ├── src └── Rede │ ├── RedeSerializable.php │ ├── Service │ ├── CreateTransactionService.php │ ├── CancelTransactionService.php │ ├── CaptureTransactionService.php │ ├── GetTransactionService.php │ ├── AbstractTransactionsService.php │ └── AbstractService.php │ ├── Exception │ └── RedeException.php │ ├── RedeUnserializable.php │ ├── SerializeTrait.php │ ├── CreateTrait.php │ ├── Additional.php │ ├── Url.php │ ├── SubMerchant.php │ ├── Capture.php │ ├── Brand.php │ ├── Phone.php │ ├── Passenger.php │ ├── Store.php │ ├── Iata.php │ ├── Refund.php │ ├── Flight.php │ ├── Environment.php │ ├── Consumer.php │ ├── Device.php │ ├── Cart.php │ ├── Item.php │ ├── Address.php │ ├── eRede.php │ ├── ThreeDSecure.php │ ├── Authorization.php │ └── Transaction.php ├── tests ├── Dockerfile ├── composer.json ├── LICENSE ├── README.md └── test └── Rede └── eRedeTest.php /phpstan.neon: -------------------------------------------------------------------------------- 1 | parameters: 2 | level: 8 3 | paths: 4 | - src 5 | - test 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | vendor/* 3 | composer.lock 4 | 5 | .phpunit.result.cache 6 | .php-cs-fixer.cache 7 | -------------------------------------------------------------------------------- /src/Rede/RedeSerializable.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | public function jsonSerialize(): array 11 | { 12 | return array_filter(get_object_vars($this), function ($value) { 13 | return $value !== null; 14 | }); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [[ ! -d vendor ]]; then 4 | echo "Vendor dir not found; running composer install" 5 | composer install 6 | fi 7 | 8 | if [[ ! -v REDE_PV ]] || [[ ! -v REDE_TOKEN ]]; then 9 | echo "You need to define the environment variables REDE_PV AND REDE_TOKEN to continue" 10 | exit 1 11 | fi 12 | 13 | ./vendor/bin/phpcs --ignore=vendor --standard=PSR12 src test 14 | ./vendor/bin/phpstan 15 | ./vendor/bin/phpcpd src tests\n 16 | ./vendor/bin/phpunit --testdox --colors='always' test 17 | -------------------------------------------------------------------------------- /src/Rede/Service/CancelTransactionService.php: -------------------------------------------------------------------------------- 1 | transaction === null) { 15 | throw new RuntimeException('Transaction was not defined yet'); 16 | } 17 | 18 | return sprintf('%s/%s/refunds', parent::getService(), $this->transaction->getTid()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM php:8.1-cli 2 | LABEL maintainer="neto.joaobatista@gmail.com" 3 | 4 | RUN apt -y update && apt -y install libzip-dev 5 | RUN docker-php-ext-install zip 6 | 7 | RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" 8 | RUN php composer-setup.php 9 | RUN php -r "unlink('composer-setup.php');" 10 | RUN mv composer.phar /bin/composer 11 | 12 | ADD . /src/erede-php 13 | WORKDIR /src/erede-php 14 | 15 | RUN composer install 16 | 17 | RUN echo "./vendor/bin/phpcs --ignore=vendor --standard=PSR12 src test\n">tests 18 | RUN echo "./vendor/bin/phpstan\n" >>tests 19 | RUN echo "./vendor/bin/phpcpd src tests\n" >>tests 20 | RUN echo "./vendor/bin/phpunit --testdox --colors='always' test" >>tests 21 | 22 | CMD sh tests -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "developersrede/erede-php", 3 | "version": "5.2.1", 4 | "description": "e.Rede integration SDK", 5 | "minimum-stability": "stable", 6 | "license": "MIT", 7 | "type": "library", 8 | "require": { 9 | "php": "^8.1", 10 | "ext-curl": "*", 11 | "ext-json": "*", 12 | "psr/log": "*", 13 | "monolog/monolog": "*" 14 | }, 15 | "require-dev": { 16 | "phpunit/phpunit": "^9.5.0", 17 | "friendsofphp/php-cs-fixer": "^v3.11.0", 18 | "phpstan/phpstan": "^1.8.6", 19 | "squizlabs/php_codesniffer": "^3.7", 20 | "sebastian/phpcpd": "^6.0.0" 21 | }, 22 | "autoload": { 23 | "psr-4": { 24 | "Rede\\": "src/Rede" 25 | } 26 | }, 27 | "authors": [ 28 | { 29 | "name": "João Batista Neto", 30 | "email": "neto.joaobatista@gmail.com" 31 | } 32 | ] 33 | } 34 | -------------------------------------------------------------------------------- /src/Rede/CreateTrait.php: -------------------------------------------------------------------------------- 1 | $value) { 23 | if (array_key_exists($property, $objectKeys)) { 24 | if ($property == 'requestDateTime' || $property == 'dateTime' || $property == 'refundDateTime') { 25 | $value = new DateTime($value); 26 | } 27 | 28 | $object->{$property} = $value; 29 | } 30 | } 31 | 32 | return $object; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Rede 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. -------------------------------------------------------------------------------- /src/Rede/Additional.php: -------------------------------------------------------------------------------- 1 | gateway; 26 | } 27 | 28 | /** 29 | * @param int $gateway 30 | * 31 | * @return $this 32 | */ 33 | public function setGateway(int $gateway): static 34 | { 35 | $this->gateway = $gateway; 36 | return $this; 37 | } 38 | 39 | /** 40 | * @return int|null 41 | */ 42 | public function getModule(): ?int 43 | { 44 | return $this->module; 45 | } 46 | 47 | /** 48 | * @param int $module 49 | * 50 | * @return $this 51 | */ 52 | public function setModule(int $module): static 53 | { 54 | $this->module = $module; 55 | return $this; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/Rede/Service/CaptureTransactionService.php: -------------------------------------------------------------------------------- 1 | transaction); 21 | 22 | if (!is_string($json)) { 23 | throw new RuntimeException('Problem converting the Transaction object to json'); 24 | } 25 | 26 | return $this->sendRequest($json, AbstractService::PUT); 27 | } 28 | 29 | /** 30 | * @return string 31 | */ 32 | protected function getService(): string 33 | { 34 | if ($this->transaction === null) { 35 | throw new RuntimeException('Transaction was not defined yet'); 36 | } 37 | 38 | return sprintf('%s/%s', parent::getService(), $this->transaction->getTid()); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Rede/Url.php: -------------------------------------------------------------------------------- 1 | url; 23 | } 24 | 25 | /** 26 | * @param string $url 27 | * @return $this 28 | */ 29 | public function setUrl(string $url): static 30 | { 31 | $this->url = $url; 32 | return $this; 33 | } 34 | 35 | /** 36 | * @return string 37 | */ 38 | public function getKind(): string 39 | { 40 | return $this->kind; 41 | } 42 | 43 | /** 44 | * @param string $kind 45 | * @return $this 46 | */ 47 | public function setKind(string $kind): static 48 | { 49 | $this->kind = $kind; 50 | return $this; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/Rede/SubMerchant.php: -------------------------------------------------------------------------------- 1 | mcc; 24 | } 25 | 26 | /** 27 | * @param string $mcc 28 | * @return $this 29 | */ 30 | public function setMcc(string $mcc): static 31 | { 32 | $this->mcc = $mcc; 33 | return $this; 34 | } 35 | 36 | /** 37 | * @return string 38 | */ 39 | public function getCity(): string 40 | { 41 | return $this->city; 42 | } 43 | 44 | /** 45 | * @param string $city 46 | * @return $this 47 | */ 48 | public function setCity(string $city): static 49 | { 50 | $this->city = $city; 51 | return $this; 52 | } 53 | 54 | /** 55 | * @return string 56 | */ 57 | public function getCountry(): string 58 | { 59 | return $this->country; 60 | } 61 | 62 | /** 63 | * @param string $country 64 | * @return $this 65 | */ 66 | public function setCountry(string $country): static 67 | { 68 | $this->country = $country; 69 | return $this; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/Rede/Capture.php: -------------------------------------------------------------------------------- 1 | amount; 32 | } 33 | 34 | /** 35 | * @param int|null $amount 36 | * @return Capture 37 | */ 38 | public function setAmount(?int $amount): Capture 39 | { 40 | $this->amount = $amount; 41 | return $this; 42 | } 43 | 44 | /** 45 | * @return DateTime|null 46 | */ 47 | public function getDateTime(): ?DateTime 48 | { 49 | return $this->dateTime; 50 | } 51 | 52 | /** 53 | * @param DateTime|null $dateTime 54 | * @return Capture 55 | */ 56 | public function setDateTime(?DateTime $dateTime): Capture 57 | { 58 | $this->dateTime = $dateTime; 59 | return $this; 60 | } 61 | 62 | /** 63 | * @return string|null 64 | */ 65 | public function getNsu(): ?string 66 | { 67 | return $this->nsu; 68 | } 69 | 70 | /** 71 | * @param string|null $nsu 72 | * @return Capture 73 | */ 74 | public function setNsu(?string $nsu): Capture 75 | { 76 | $this->nsu = $nsu; 77 | return $this; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/Rede/Brand.php: -------------------------------------------------------------------------------- 1 | name; 29 | } 30 | 31 | /** 32 | * @param string|null $name 33 | * @return Brand 34 | */ 35 | public function setName(?string $name): Brand 36 | { 37 | $this->name = $name; 38 | return $this; 39 | } 40 | 41 | /** 42 | * @return string|null 43 | */ 44 | public function getReturnCode(): ?string 45 | { 46 | return $this->returnCode; 47 | } 48 | 49 | /** 50 | * @param string|null $returnCode 51 | * @return Brand 52 | */ 53 | public function setReturnCode(?string $returnCode): Brand 54 | { 55 | $this->returnCode = $returnCode; 56 | return $this; 57 | } 58 | 59 | /** 60 | * @return string|null 61 | */ 62 | public function getReturnMessage(): ?string 63 | { 64 | return $this->returnMessage; 65 | } 66 | 67 | /** 68 | * @param string|null $returnMessage 69 | * @return Brand 70 | */ 71 | public function setReturnMessage(?string $returnMessage): Brand 72 | { 73 | $this->returnMessage = $returnMessage; 74 | return $this; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/Rede/Phone.php: -------------------------------------------------------------------------------- 1 | ddd; 31 | } 32 | 33 | /** 34 | * @param string $ddd 35 | * @return $this 36 | */ 37 | public function setDdd(string $ddd): static 38 | { 39 | $this->ddd = $ddd; 40 | return $this; 41 | } 42 | 43 | /** 44 | * @return string 45 | */ 46 | public function getNumber(): string 47 | { 48 | return $this->number; 49 | } 50 | 51 | /** 52 | * @param string $number 53 | * @return $this 54 | */ 55 | public function setNumber(string $number): static 56 | { 57 | $this->number = $number; 58 | return $this; 59 | } 60 | 61 | /** 62 | * @return int 63 | */ 64 | public function getType(): int 65 | { 66 | return $this->type; 67 | } 68 | 69 | /** 70 | * @param int $type 71 | * @return $this 72 | */ 73 | public function setType(int $type): static 74 | { 75 | $this->type = $type; 76 | return $this; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/Rede/Service/GetTransactionService.php: -------------------------------------------------------------------------------- 1 | sendRequest(); 31 | } 32 | 33 | /** 34 | * @param string $reference 35 | * 36 | * @return $this 37 | */ 38 | public function setReference(string $reference): static 39 | { 40 | $this->reference = $reference; 41 | return $this; 42 | } 43 | 44 | /** 45 | * @param bool $refund 46 | * 47 | * @return $this 48 | */ 49 | public function setRefund(bool $refund = true): static 50 | { 51 | $this->refund = $refund; 52 | 53 | return $this; 54 | } 55 | 56 | /** 57 | * @return string 58 | */ 59 | protected function getService(): string 60 | { 61 | if ($this->reference !== null) { 62 | return sprintf('%s?reference=%s', parent::getService(), $this->reference); 63 | } 64 | 65 | if ($this->refund) { 66 | return sprintf('%s/%s/refunds', parent::getService(), $this->getTid()); 67 | } 68 | 69 | return sprintf('%s/%s', parent::getService(), $this->getTid()); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/Rede/Passenger.php: -------------------------------------------------------------------------------- 1 | phone; 24 | } 25 | 26 | /** 27 | * @param Phone $phone 28 | * @return $this 29 | */ 30 | public function setPhone(Phone $phone): static 31 | { 32 | $this->phone = $phone; 33 | return $this; 34 | } 35 | 36 | /** 37 | * @return string 38 | */ 39 | public function getName(): string 40 | { 41 | return $this->name; 42 | } 43 | 44 | /** 45 | * @param string $name 46 | * @return $this 47 | */ 48 | public function setName(string $name): static 49 | { 50 | $this->name = $name; 51 | return $this; 52 | } 53 | 54 | /** 55 | * @return string 56 | */ 57 | public function getEmail(): string 58 | { 59 | return $this->email; 60 | } 61 | 62 | /** 63 | * @param string $email 64 | * @return $this 65 | */ 66 | public function setEmail(string $email): static 67 | { 68 | $this->email = $email; 69 | return $this; 70 | } 71 | 72 | /** 73 | * @return string 74 | */ 75 | public function getTicket(): string 76 | { 77 | return $this->ticket; 78 | } 79 | 80 | /** 81 | * @param string $ticket 82 | * @return $this 83 | */ 84 | public function setTicket(string $ticket): static 85 | { 86 | $this->ticket = $ticket; 87 | return $this; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/Rede/Store.php: -------------------------------------------------------------------------------- 1 | environment = $environment ?? Environment::production(); 23 | } 24 | 25 | /** 26 | * @return Environment 27 | */ 28 | public function getEnvironment(): Environment 29 | { 30 | return $this->environment; 31 | } 32 | 33 | /** 34 | * @param Environment $environment 35 | * 36 | * @return $this 37 | */ 38 | public function setEnvironment(Environment $environment): static 39 | { 40 | $this->environment = $environment; 41 | return $this; 42 | } 43 | 44 | /** 45 | * @return string 46 | */ 47 | public function getFiliation(): string 48 | { 49 | return $this->filiation; 50 | } 51 | 52 | /** 53 | * @param string $filiation 54 | * 55 | * @return $this 56 | */ 57 | public function setFiliation(string $filiation): static 58 | { 59 | $this->filiation = $filiation; 60 | return $this; 61 | } 62 | 63 | /** 64 | * @return string 65 | */ 66 | public function getToken(): string 67 | { 68 | return $this->token; 69 | } 70 | 71 | /** 72 | * @param string $token 73 | * 74 | * @return $this 75 | */ 76 | public function setToken(string $token): static 77 | { 78 | $this->token = $token; 79 | return $this; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/Rede/Iata.php: -------------------------------------------------------------------------------- 1 | 23 | */ 24 | private array $flight = []; 25 | 26 | /** 27 | * @return string|null 28 | */ 29 | public function getCode(): ?string 30 | { 31 | return $this->code; 32 | } 33 | 34 | /** 35 | * @param string $code 36 | * 37 | * @return $this 38 | */ 39 | public function setCode(string $code): static 40 | { 41 | $this->code = $code; 42 | return $this; 43 | } 44 | 45 | /** 46 | * @return string|null 47 | */ 48 | public function getDepartureTax(): ?string 49 | { 50 | return $this->departureTax; 51 | } 52 | 53 | /** 54 | * @param string $departureTax 55 | * 56 | * @return $this 57 | */ 58 | public function setDepartureTax(string $departureTax): static 59 | { 60 | $this->departureTax = $departureTax; 61 | return $this; 62 | } 63 | 64 | /** 65 | * @return ArrayIterator 66 | */ 67 | public function getFlightIterator(): ArrayIterator 68 | { 69 | return new ArrayIterator($this->flight); 70 | } 71 | 72 | /** 73 | * @param Flight $flight 74 | * 75 | * @return $this 76 | */ 77 | public function setFlight(Flight $flight): static 78 | { 79 | $this->flight = []; 80 | $this->addFlight($flight); 81 | 82 | return $this; 83 | } 84 | 85 | /** 86 | * @param Flight $flight 87 | * 88 | * @return $this 89 | */ 90 | public function addFlight(Flight $flight): static 91 | { 92 | $this->flight[] = $flight; 93 | 94 | return $this; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/Rede/Refund.php: -------------------------------------------------------------------------------- 1 | amount; 38 | } 39 | 40 | /** 41 | * @param int $amount 42 | * 43 | * @return $this 44 | */ 45 | public function setAmount(int $amount): static 46 | { 47 | $this->amount = $amount; 48 | return $this; 49 | } 50 | 51 | /** 52 | * @return DateTime|null 53 | */ 54 | public function getRefundDateTime(): ?DateTime 55 | { 56 | return $this->refundDateTime; 57 | } 58 | 59 | /** 60 | * @param string $refundDateTime 61 | * 62 | * @return $this 63 | * @throws Exception 64 | */ 65 | public function setRefundDateTime(string $refundDateTime): static 66 | { 67 | $this->refundDateTime = new DateTime($refundDateTime); 68 | return $this; 69 | } 70 | 71 | /** 72 | * @return string|null 73 | */ 74 | public function getRefundId(): ?string 75 | { 76 | return $this->refundId; 77 | } 78 | 79 | /** 80 | * @param string $refundId 81 | * 82 | * @return $this 83 | */ 84 | public function setRefundId(string $refundId): static 85 | { 86 | $this->refundId = $refundId; 87 | return $this; 88 | } 89 | 90 | /** 91 | * @return string|null 92 | */ 93 | public function getStatus(): ?string 94 | { 95 | return $this->status; 96 | } 97 | 98 | /** 99 | * @param string $status 100 | * 101 | * @return $this 102 | */ 103 | public function setStatus(string $status): static 104 | { 105 | $this->status = $status; 106 | return $this; 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/Rede/Flight.php: -------------------------------------------------------------------------------- 1 | 11 | */ 12 | private array $passenger = []; 13 | 14 | public function __construct(private string $number, private string $from, private string $to, private string $date) 15 | { 16 | } 17 | 18 | /** 19 | * @return string 20 | */ 21 | public function getDate(): string 22 | { 23 | return $this->date; 24 | } 25 | 26 | /** 27 | * @param string $date 28 | * 29 | * @return $this 30 | */ 31 | public function setDate(string $date): static 32 | { 33 | $this->date = $date; 34 | return $this; 35 | } 36 | 37 | /** 38 | * @return string 39 | */ 40 | public function getFrom(): string 41 | { 42 | return $this->from; 43 | } 44 | 45 | /** 46 | * @param string $from 47 | * 48 | * @return $this 49 | */ 50 | public function setFrom(string $from): static 51 | { 52 | $this->from = $from; 53 | return $this; 54 | } 55 | 56 | /** 57 | * @return string 58 | */ 59 | public function getNumber(): string 60 | { 61 | return $this->number; 62 | } 63 | 64 | /** 65 | * @param string $number 66 | * 67 | * @return $this 68 | */ 69 | public function setNumber(string $number): static 70 | { 71 | $this->number = $number; 72 | return $this; 73 | } 74 | 75 | /** 76 | * @return array 77 | */ 78 | public function getPassenger(): array 79 | { 80 | return $this->passenger; 81 | } 82 | 83 | /** 84 | * @param Passenger $passenger 85 | * 86 | * @return $this 87 | */ 88 | public function setPassenger(Passenger $passenger): static 89 | { 90 | $this->passenger = []; 91 | $this->addPassenger($passenger); 92 | return $this; 93 | } 94 | 95 | /** 96 | * @param Passenger $passenger 97 | * 98 | * @return $this 99 | */ 100 | public function addPassenger(Passenger $passenger): static 101 | { 102 | $this->passenger[] = $passenger; 103 | 104 | return $this; 105 | } 106 | 107 | /** 108 | * @return string 109 | */ 110 | public function getTo(): string 111 | { 112 | return $this->to; 113 | } 114 | 115 | /** 116 | * @param string $to 117 | * 118 | * @return $this 119 | */ 120 | public function setTo(string $to): static 121 | { 122 | $this->to = $to; 123 | return $this; 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /src/Rede/Environment.php: -------------------------------------------------------------------------------- 1 | endpoint = sprintf('%s/%s/', $baseUrl, Environment::VERSION); 36 | } 37 | 38 | /** 39 | * @return Environment A preconfigured production environment 40 | */ 41 | public static function production(): Environment 42 | { 43 | return new Environment(Environment::PRODUCTION); 44 | } 45 | 46 | /** 47 | * @return Environment A preconfigured sandbox environment 48 | */ 49 | public static function sandbox(): Environment 50 | { 51 | return new Environment(Environment::SANDBOX); 52 | } 53 | 54 | /** 55 | * @param string $service 56 | * 57 | * @return string Gets the environment endpoint 58 | */ 59 | public function getEndpoint(string $service): string 60 | { 61 | return $this->endpoint . $service; 62 | } 63 | 64 | /** 65 | * @return string|null 66 | */ 67 | public function getIp(): ?string 68 | { 69 | return $this->ip; 70 | } 71 | 72 | /** 73 | * @param string $ip 74 | * 75 | * @return $this 76 | */ 77 | public function setIp(string $ip): static 78 | { 79 | $this->ip = $ip; 80 | return $this; 81 | } 82 | 83 | /** 84 | * @return string|null 85 | */ 86 | public function getSessionId(): ?string 87 | { 88 | return $this->sessionId; 89 | } 90 | 91 | /** 92 | * @param string $sessionId 93 | * 94 | * @return $this 95 | */ 96 | public function setSessionId(string $sessionId): static 97 | { 98 | $this->sessionId = $sessionId; 99 | return $this; 100 | } 101 | 102 | /** 103 | * @return mixed 104 | * @noinspection PhpMixedReturnTypeCanBeReducedInspection 105 | */ 106 | public function jsonSerialize(): mixed 107 | { 108 | $consumer = new stdClass(); 109 | $consumer->ip = $this->ip; 110 | $consumer->sessionId = $this->sessionId; 111 | 112 | return ['consumer' => $consumer]; 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /src/Rede/Service/AbstractTransactionsService.php: -------------------------------------------------------------------------------- 1 | transaction = $transaction; 37 | } 38 | 39 | /** 40 | * @return Transaction 41 | * @throws InvalidArgumentException 42 | * @throws RuntimeException 43 | * @throws RedeException 44 | */ 45 | public function execute(): Transaction 46 | { 47 | $json = json_encode($this->transaction); 48 | 49 | if (!is_string($json)) { 50 | throw new RuntimeException('Problem converting the Transaction object to json'); 51 | } 52 | 53 | return $this->sendRequest($json, AbstractService::POST); 54 | } 55 | 56 | /** 57 | * @return string 58 | */ 59 | public function getTid(): string 60 | { 61 | return $this->tid; 62 | } 63 | 64 | /** 65 | * @param string $tid 66 | * @return $this 67 | */ 68 | public function setTid(string $tid): static 69 | { 70 | $this->tid = $tid; 71 | return $this; 72 | } 73 | 74 | /** 75 | * @return string 76 | * @see AbstractService::getService() 77 | */ 78 | protected function getService(): string 79 | { 80 | return 'transactions'; 81 | } 82 | 83 | /** 84 | * @param string $response 85 | * @param int $statusCode 86 | * 87 | * @return Transaction 88 | * @throws RedeException 89 | * @throws InvalidArgumentException 90 | * @throws Exception 91 | * @see AbstractService::parseResponse() 92 | */ 93 | protected function parseResponse(string $response, int $statusCode): Transaction 94 | { 95 | $previous = null; 96 | 97 | if ($this->transaction === null) { 98 | $this->transaction = new Transaction(); 99 | } 100 | 101 | try { 102 | $this->transaction->jsonUnserialize($response); 103 | } catch (InvalidArgumentException $e) { 104 | $previous = $e; 105 | } 106 | 107 | if ($statusCode >= 400) { 108 | throw new RedeException( 109 | $this->transaction->getReturnMessage() ?? 'Error on getting the content from the API', 110 | (int)$this->transaction->getReturnCode(), 111 | $previous 112 | ); 113 | } 114 | 115 | return $this->transaction; 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /src/Rede/Consumer.php: -------------------------------------------------------------------------------- 1 | 17 | */ 18 | private array $documents = []; 19 | 20 | /** 21 | * @var string|null 22 | */ 23 | private ?string $gender = null; 24 | 25 | /** 26 | * @var Phone|null 27 | */ 28 | private ?Phone $phone = null; 29 | 30 | /** 31 | * Consumer constructor. 32 | * 33 | * @param string $name 34 | * @param string $email 35 | * @param string $cpf 36 | */ 37 | public function __construct(private string $name, private string $email, private string $cpf) 38 | { 39 | } 40 | 41 | /** 42 | * @param string $type 43 | * @param string $number 44 | * 45 | * @return $this 46 | */ 47 | public function addDocument(string $type, string $number): static 48 | { 49 | $document = new stdClass(); 50 | $document->type = $type; 51 | $document->number = $number; 52 | 53 | $this->documents[] = $document; 54 | 55 | return $this; 56 | } 57 | 58 | /** 59 | * @return ArrayIterator 60 | */ 61 | public function getDocumentsIterator(): ArrayIterator 62 | { 63 | return new ArrayIterator($this->documents); 64 | } 65 | 66 | /** 67 | * @return string|null 68 | */ 69 | public function getGender(): ?string 70 | { 71 | return $this->gender; 72 | } 73 | 74 | /** 75 | * @param string $gender 76 | * @return Consumer 77 | */ 78 | public function setGender(string $gender): Consumer 79 | { 80 | $this->gender = $gender; 81 | return $this; 82 | } 83 | 84 | /** 85 | * @return Phone|null 86 | */ 87 | public function getPhone(): ?Phone 88 | { 89 | return $this->phone; 90 | } 91 | 92 | /** 93 | * @param string $ddd 94 | * @param string $number 95 | * @param int $type 96 | * @return $this 97 | */ 98 | public function setPhone(string $ddd, string $number, int $type = Phone::CELLPHONE): static 99 | { 100 | $this->phone = new Phone($ddd, $number, $type); 101 | return $this; 102 | } 103 | 104 | /** 105 | * @return string 106 | */ 107 | public function getName(): string 108 | { 109 | return $this->name; 110 | } 111 | 112 | /** 113 | * @param string $name 114 | * @return Consumer 115 | */ 116 | public function setName(string $name): Consumer 117 | { 118 | $this->name = $name; 119 | return $this; 120 | } 121 | 122 | /** 123 | * @return string 124 | */ 125 | public function getEmail(): string 126 | { 127 | return $this->email; 128 | } 129 | 130 | /** 131 | * @param string $email 132 | * @return Consumer 133 | */ 134 | public function setEmail(string $email): Consumer 135 | { 136 | $this->email = $email; 137 | return $this; 138 | } 139 | 140 | /** 141 | * @return string 142 | */ 143 | public function getCpf(): string 144 | { 145 | return $this->cpf; 146 | } 147 | 148 | /** 149 | * @param string $cpf 150 | * @return Consumer 151 | */ 152 | public function setCpf(string $cpf): Consumer 153 | { 154 | $this->cpf = $cpf; 155 | return $this; 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /src/Rede/Device.php: -------------------------------------------------------------------------------- 1 | ColorDepth; 36 | } 37 | 38 | /** 39 | * @param string $ColorDepth 40 | * @return $this 41 | */ 42 | public function setColorDepth(string $ColorDepth): static 43 | { 44 | $this->ColorDepth = $ColorDepth; 45 | return $this; 46 | } 47 | 48 | /** 49 | * @return string|null 50 | */ 51 | public function getDeviceType3ds(): ?string 52 | { 53 | return $this->DeviceType3ds; 54 | } 55 | 56 | /** 57 | * @param string $DeviceType3ds 58 | * @return $this 59 | */ 60 | public function setDeviceType3ds(string $DeviceType3ds): static 61 | { 62 | $this->DeviceType3ds = $DeviceType3ds; 63 | return $this; 64 | } 65 | 66 | /** 67 | * @return bool|null 68 | */ 69 | public function getJavaEnabled(): ?bool 70 | { 71 | return $this->JavaEnabled; 72 | } 73 | 74 | /** 75 | * @param bool $JavaEnabled 76 | * @return $this 77 | */ 78 | public function setJavaEnabled(bool $JavaEnabled = true): static 79 | { 80 | $this->JavaEnabled = $JavaEnabled; 81 | return $this; 82 | } 83 | 84 | /** 85 | * @return string 86 | */ 87 | public function getLanguage(): string 88 | { 89 | return $this->Language; 90 | } 91 | 92 | /** 93 | * @param string $Language 94 | * @return $this 95 | */ 96 | public function setLanguage(string $Language): static 97 | { 98 | $this->Language = $Language; 99 | return $this; 100 | } 101 | 102 | /** 103 | * @return int|null 104 | */ 105 | public function getScreenHeight(): ?int 106 | { 107 | return $this->ScreenHeight; 108 | } 109 | 110 | /** 111 | * @param int $ScreenHeight 112 | * @return $this 113 | */ 114 | public function setScreenHeight(int $ScreenHeight): static 115 | { 116 | $this->ScreenHeight = $ScreenHeight; 117 | return $this; 118 | } 119 | 120 | /** 121 | * @return int|null 122 | */ 123 | public function getScreenWidth(): ?int 124 | { 125 | return $this->ScreenWidth; 126 | } 127 | 128 | /** 129 | * @param int $ScreenWidth 130 | * @return $this 131 | */ 132 | public function setScreenWidth(int $ScreenWidth): static 133 | { 134 | $this->ScreenWidth = $ScreenWidth; 135 | return $this; 136 | } 137 | 138 | /** 139 | * @return int|null 140 | */ 141 | public function getTimeZoneOffset(): ?int 142 | { 143 | return $this->TimeZoneOffset; 144 | } 145 | 146 | /** 147 | * @param int $TimeZoneOffset 148 | * @return $this 149 | */ 150 | public function setTimeZoneOffset(int $TimeZoneOffset): static 151 | { 152 | $this->TimeZoneOffset = $TimeZoneOffset; 153 | return $this; 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /src/Rede/Cart.php: -------------------------------------------------------------------------------- 1 | 26 | */ 27 | private array $items = []; 28 | 29 | /** 30 | * @var array
31 | */ 32 | private array $shipping = []; 33 | 34 | /** 35 | * @param int $type 36 | * 37 | * @return Address 38 | */ 39 | public function address(int $type = Address::BOTH): Address 40 | { 41 | $address = new Address(); 42 | 43 | if (($type & Address::BILLING) == Address::BILLING) { 44 | $this->setBillingAddress($address); 45 | } 46 | 47 | if (($type & Address::SHIPPING) == Address::SHIPPING) { 48 | $this->setShippingAddress($address); 49 | } 50 | 51 | return $address; 52 | } 53 | 54 | /** 55 | * @param Item $item 56 | * 57 | * @return $this 58 | */ 59 | public function addItem(Item $item): static 60 | { 61 | $this->items[] = $item; 62 | 63 | return $this; 64 | } 65 | 66 | /** 67 | * @param Address $shippingAddress 68 | * 69 | * @return $this 70 | */ 71 | public function addShippingAddress(Address $shippingAddress): static 72 | { 73 | $this->shipping[] = $shippingAddress; 74 | 75 | return $this; 76 | } 77 | 78 | /** 79 | * @param Address $shippingAddress 80 | * 81 | * @return $this 82 | */ 83 | public function setShippingAddress(Address $shippingAddress): static 84 | { 85 | $this->shipping = [$shippingAddress]; 86 | 87 | return $this; 88 | } 89 | 90 | /** 91 | * @param Address $billingAddress 92 | * 93 | * @return $this 94 | */ 95 | public function setBillingAddress(Address $billingAddress): static 96 | { 97 | $this->billing = $billingAddress; 98 | 99 | return $this; 100 | } 101 | 102 | /** 103 | * @param string $name 104 | * @param string $email 105 | * @param string $cpf 106 | * 107 | * @return Consumer 108 | */ 109 | public function consumer(string $name, string $email, string $cpf): Consumer 110 | { 111 | $consumer = new Consumer($name, $email, $cpf); 112 | 113 | $this->setConsumer($consumer); 114 | 115 | return $consumer; 116 | } 117 | 118 | /** 119 | * @param Flight $flight 120 | * 121 | * @return $this 122 | */ 123 | public function setFlight(Flight $flight): static 124 | { 125 | $this->iata = new Iata(); 126 | $this->iata->setFlight($flight); 127 | return $this; 128 | } 129 | 130 | /** 131 | * @param Iata $iata 132 | * 133 | * @return $this 134 | */ 135 | public function setIata(Iata $iata): static 136 | { 137 | $this->iata = $iata; 138 | 139 | return $this; 140 | } 141 | 142 | /** 143 | * @return Address[] 144 | */ 145 | public function getShippingAddresses(): array 146 | { 147 | return $this->shipping; 148 | } 149 | 150 | /** 151 | * @return Address|null 152 | */ 153 | public function getBilling(): ?Address 154 | { 155 | return $this->billing; 156 | } 157 | 158 | /** 159 | * @return Consumer|null 160 | */ 161 | public function getConsumer(): ?Consumer 162 | { 163 | return $this->consumer; 164 | } 165 | 166 | /** 167 | * @param Consumer $consumer 168 | * @return Cart 169 | */ 170 | public function setConsumer(Consumer $consumer): Cart 171 | { 172 | $this->consumer = $consumer; 173 | return $this; 174 | } 175 | 176 | /** 177 | * @return Item[] 178 | */ 179 | public function getItems(): array 180 | { 181 | return $this->items; 182 | } 183 | } 184 | -------------------------------------------------------------------------------- /src/Rede/Item.php: -------------------------------------------------------------------------------- 1 | amount; 56 | } 57 | 58 | /** 59 | * @param int $amount 60 | * 61 | * @return $this 62 | */ 63 | public function setAmount(int $amount): static 64 | { 65 | $this->amount = $amount; 66 | return $this; 67 | } 68 | 69 | /** 70 | * @return string|null 71 | */ 72 | public function getDescription(): ?string 73 | { 74 | return $this->description; 75 | } 76 | 77 | /** 78 | * @param string $description 79 | * 80 | * @return $this 81 | */ 82 | public function setDescription(string $description): static 83 | { 84 | $this->description = $description; 85 | return $this; 86 | } 87 | 88 | /** 89 | * @return int|null 90 | */ 91 | public function getDiscount(): ?int 92 | { 93 | return $this->discount; 94 | } 95 | 96 | /** 97 | * @param int $discount 98 | * 99 | * @return $this 100 | */ 101 | public function setDiscount(int $discount): static 102 | { 103 | $this->discount = $discount; 104 | return $this; 105 | } 106 | 107 | /** 108 | * @return int|null 109 | */ 110 | public function getFreight(): ?int 111 | { 112 | return $this->freight; 113 | } 114 | 115 | /** 116 | * @param int $freight 117 | * 118 | * @return $this 119 | */ 120 | public function setFreight(int $freight): static 121 | { 122 | $this->freight = $freight; 123 | return $this; 124 | } 125 | 126 | /** 127 | * @return string|null 128 | */ 129 | public function getId(): ?string 130 | { 131 | return $this->id; 132 | } 133 | 134 | /** 135 | * @param string $id 136 | * 137 | * @return $this 138 | */ 139 | public function setId(string $id): static 140 | { 141 | $this->id = $id; 142 | return $this; 143 | } 144 | 145 | /** 146 | * @return int|null 147 | */ 148 | public function getQuantity(): ?int 149 | { 150 | return $this->quantity; 151 | } 152 | 153 | /** 154 | * @param int $quantity 155 | * 156 | * @return $this 157 | */ 158 | public function setQuantity(int $quantity): static 159 | { 160 | $this->quantity = $quantity; 161 | return $this; 162 | } 163 | 164 | /** 165 | * @return string|null 166 | */ 167 | public function getShippingType(): ?string 168 | { 169 | return $this->shippingType; 170 | } 171 | 172 | /** 173 | * @param string $shippingType 174 | * 175 | * @return $this 176 | */ 177 | public function setShippingType(string $shippingType): static 178 | { 179 | $this->shippingType = $shippingType; 180 | return $this; 181 | } 182 | 183 | /** 184 | * @return int|null 185 | */ 186 | public function getType(): ?int 187 | { 188 | return $this->type; 189 | } 190 | 191 | /** 192 | * @param int $type 193 | * 194 | * @return $this 195 | */ 196 | public function setType(int $type): static 197 | { 198 | $this->type = $type; 199 | return $this; 200 | } 201 | } 202 | -------------------------------------------------------------------------------- /src/Rede/Address.php: -------------------------------------------------------------------------------- 1 | address; 69 | } 70 | 71 | /** 72 | * @param string|null $address 73 | * @return $this 74 | */ 75 | public function setAddress(?string $address): static 76 | { 77 | $this->address = $address; 78 | return $this; 79 | } 80 | 81 | /** 82 | * @return string|null 83 | */ 84 | public function getAddresseeName(): ?string 85 | { 86 | return $this->addresseeName; 87 | } 88 | 89 | /** 90 | * @param string|null $addresseeName 91 | * @return $this 92 | */ 93 | public function setAddresseeName(?string $addresseeName): static 94 | { 95 | $this->addresseeName = $addresseeName; 96 | return $this; 97 | } 98 | 99 | /** 100 | * @return string|null 101 | */ 102 | public function getCity(): ?string 103 | { 104 | return $this->city; 105 | } 106 | 107 | /** 108 | * @param string|null $city 109 | * @return $this 110 | */ 111 | public function setCity(?string $city): static 112 | { 113 | $this->city = $city; 114 | return $this; 115 | } 116 | 117 | /** 118 | * @return string|null 119 | */ 120 | public function getComplement(): ?string 121 | { 122 | return $this->complement; 123 | } 124 | 125 | /** 126 | * @param string|null $complement 127 | * @return $this 128 | */ 129 | public function setComplement(?string $complement): static 130 | { 131 | $this->complement = $complement; 132 | return $this; 133 | } 134 | 135 | /** 136 | * @return string|null 137 | */ 138 | public function getNeighbourhood(): ?string 139 | { 140 | return $this->neighbourhood; 141 | } 142 | 143 | /** 144 | * @param string|null $neighbourhood 145 | * @return $this 146 | */ 147 | public function setNeighbourhood(?string $neighbourhood): static 148 | { 149 | $this->neighbourhood = $neighbourhood; 150 | return $this; 151 | } 152 | 153 | /** 154 | * @return string|null 155 | */ 156 | public function getNumber(): ?string 157 | { 158 | return $this->number; 159 | } 160 | 161 | /** 162 | * @param string|null $number 163 | * @return $this 164 | */ 165 | public function setNumber(?string $number): static 166 | { 167 | $this->number = $number; 168 | return $this; 169 | } 170 | 171 | /** 172 | * @return string|null 173 | */ 174 | public function getState(): ?string 175 | { 176 | return $this->state; 177 | } 178 | 179 | /** 180 | * @param string|null $state 181 | * @return $this 182 | */ 183 | public function setState(?string $state): static 184 | { 185 | $this->state = $state; 186 | return $this; 187 | } 188 | 189 | /** 190 | * @return int|null 191 | */ 192 | public function getType(): ?int 193 | { 194 | return $this->type; 195 | } 196 | 197 | /** 198 | * @param int|null $type 199 | * @return $this 200 | */ 201 | public function setType(?int $type): static 202 | { 203 | $this->type = $type; 204 | return $this; 205 | } 206 | 207 | /** 208 | * @return string|null 209 | */ 210 | public function getZipCode(): ?string 211 | { 212 | return $this->zipCode; 213 | } 214 | 215 | /** 216 | * @param string|null $zipCode 217 | * @return $this 218 | */ 219 | public function setZipCode(?string $zipCode): static 220 | { 221 | $this->zipCode = $zipCode; 222 | return $this; 223 | } 224 | } 225 | -------------------------------------------------------------------------------- /src/Rede/eRede.php: -------------------------------------------------------------------------------- 1 | create($transaction); 48 | } 49 | 50 | /** 51 | * @param Transaction $transaction 52 | * 53 | * @return Transaction 54 | */ 55 | public function create(Transaction $transaction): Transaction 56 | { 57 | $service = new CreateTransactionService($this->store, $transaction, $this->logger); 58 | $service->platform($this->platform, $this->platformVersion); 59 | 60 | return $service->execute(); 61 | } 62 | 63 | /** 64 | * @param string $platform 65 | * @param string $platformVersion 66 | * 67 | * @return $this 68 | */ 69 | public function platform(string $platform, string $platformVersion): static 70 | { 71 | $this->platform = $platform; 72 | $this->platformVersion = $platformVersion; 73 | 74 | return $this; 75 | } 76 | 77 | /** 78 | * @param Transaction $transaction 79 | * 80 | * @return Transaction 81 | */ 82 | public function cancel(Transaction $transaction): Transaction 83 | { 84 | $service = new CancelTransactionService($this->store, $transaction, $this->logger); 85 | $service->platform($this->platform, $this->platformVersion); 86 | 87 | return $service->execute(); 88 | } 89 | 90 | /** 91 | * @param string $tid 92 | * 93 | * @return Transaction 94 | * @see eRede::get() 95 | */ 96 | public function getById(string $tid): Transaction 97 | { 98 | return $this->get($tid); 99 | } 100 | 101 | /** 102 | * @param string $tid 103 | * 104 | * @return Transaction 105 | */ 106 | public function get(string $tid): Transaction 107 | { 108 | $service = new GetTransactionService(store: $this->store, logger: $this->logger); 109 | $service->platform($this->platform, $this->platformVersion); 110 | $service->setTid($tid); 111 | 112 | return $service->execute(); 113 | } 114 | 115 | /** 116 | * @param string $reference 117 | * 118 | * @return Transaction 119 | */ 120 | public function getByReference(string $reference): Transaction 121 | { 122 | $service = new GetTransactionService(store: $this->store, logger: $this->logger); 123 | $service->platform($this->platform, $this->platformVersion); 124 | $service->setReference($reference); 125 | 126 | return $service->execute(); 127 | } 128 | 129 | /** 130 | * @param string $tid 131 | * 132 | * @return Transaction 133 | */ 134 | public function getRefunds(string $tid): Transaction 135 | { 136 | $service = new GetTransactionService( 137 | store: $this->store, 138 | logger: $this->logger 139 | ); 140 | $service->platform($this->platform, $this->platformVersion); 141 | $service->setTid($tid); 142 | $service->setRefund(); 143 | 144 | return $service->execute(); 145 | } 146 | 147 | /** 148 | * @param Transaction $transaction 149 | * 150 | * @return Transaction 151 | */ 152 | public function zero(Transaction $transaction): Transaction 153 | { 154 | $amount = (int) $transaction->getAmount(); 155 | $capture = (bool)$transaction->getCapture(); 156 | 157 | $transaction->setAmount(0); 158 | $transaction->capture(); 159 | 160 | $transaction = $this->create($transaction); 161 | 162 | $transaction->setAmount($amount); 163 | $transaction->capture($capture); 164 | 165 | return $transaction; 166 | } 167 | 168 | /** 169 | * @param Transaction $transaction 170 | * 171 | * @return Transaction 172 | */ 173 | public function capture(Transaction $transaction): Transaction 174 | { 175 | $service = new CaptureTransactionService($this->store, $transaction, $this->logger); 176 | $service->platform($this->platform, $this->platformVersion); 177 | 178 | return $service->execute(); 179 | } 180 | } 181 | -------------------------------------------------------------------------------- /src/Rede/Service/AbstractService.php: -------------------------------------------------------------------------------- 1 | platform = $platform; 49 | $this->platformVersion = $platformVersion; 50 | 51 | return $this; 52 | } 53 | 54 | /** 55 | * @return Transaction 56 | * @throws InvalidArgumentException 57 | * @throws RuntimeException 58 | * @throws RedeException 59 | */ 60 | abstract public function execute(): Transaction; 61 | 62 | /** 63 | * @param string $body 64 | * @param string $method 65 | * 66 | * @return Transaction 67 | * @throws RuntimeException 68 | */ 69 | protected function sendRequest(string $body = '', string $method = 'GET'): Transaction 70 | { 71 | $userAgent = $this->getUserAgent(); 72 | $headers = [ 73 | str_replace( 74 | ' ', 75 | ' ', 76 | $userAgent 77 | ), 78 | 'Accept: application/json', 79 | 'Transaction-Response: brand-return-opened' 80 | ]; 81 | 82 | $curl = curl_init($this->store->getEnvironment()->getEndpoint($this->getService())); 83 | 84 | if (!$curl instanceof CurlHandle) { 85 | throw new RuntimeException('Was not possible to create a curl instance.'); 86 | } 87 | 88 | curl_setopt( 89 | $curl, 90 | CURLOPT_USERPWD, 91 | sprintf('%s:%s', $this->store->getFiliation(), $this->store->getToken()) 92 | ); 93 | 94 | curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); 95 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); 96 | 97 | switch ($method) { 98 | case 'GET': 99 | break; 100 | case 'POST': 101 | curl_setopt($curl, CURLOPT_POST, true); 102 | break; 103 | default: 104 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); 105 | } 106 | 107 | if ($body !== '') { 108 | curl_setopt($curl, CURLOPT_POSTFIELDS, $body); 109 | 110 | $headers[] = 'Content-Type: application/json; charset=utf8'; 111 | } else { 112 | $headers[] = 'Content-Length: 0'; 113 | } 114 | 115 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 116 | curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 117 | 118 | $this->logger?->debug( 119 | trim( 120 | sprintf( 121 | "Request Rede\n%s %s\n%s\n\n%s", 122 | $method, 123 | $this->store->getEnvironment()->getEndpoint($this->getService()), 124 | implode("\n", $headers), 125 | preg_replace('/"(cardHolderName|cardnumber|securitycode)":"[^"]+"/i', '"\1":"***"', $body) 126 | ) 127 | ) 128 | ); 129 | 130 | $response = curl_exec($curl); 131 | $httpInfo = curl_getinfo($curl); 132 | 133 | $this->logger?->debug( 134 | sprintf( 135 | "Response Rede\nStatus Code: %s\n\n%s", 136 | $httpInfo['http_code'], 137 | $response 138 | ) 139 | ); 140 | 141 | $this->dumpHttpInfo($httpInfo); 142 | 143 | if (curl_errno($curl)) { 144 | throw new RuntimeException(sprintf('Curl error[%s]: %s', curl_errno($curl), curl_error($curl))); 145 | } 146 | 147 | if (!is_string($response)) { 148 | throw new RuntimeException('Error obtaining a response from the API'); 149 | } 150 | 151 | curl_close($curl); 152 | 153 | return $this->parseResponse($response, $httpInfo['http_code']); 154 | } 155 | 156 | /** 157 | * Gets the User-Agent string. 158 | * 159 | * @return string 160 | */ 161 | private function getUserAgent(): string 162 | { 163 | $userAgent = sprintf( 164 | 'User-Agent: %s', 165 | sprintf( 166 | eRede::USER_AGENT, 167 | phpversion(), 168 | $this->store->getFiliation(), 169 | php_uname('s'), 170 | php_uname('r'), 171 | php_uname('m') 172 | ) 173 | ); 174 | 175 | if (!empty($this->platform) && !empty($this->platformVersion)) { 176 | $userAgent .= sprintf(' %s/%s', $this->platform, $this->platformVersion); 177 | } 178 | 179 | $curlVersion = curl_version(); 180 | 181 | if (is_array($curlVersion)) { 182 | $userAgent .= sprintf( 183 | ' curl/%s %s', 184 | $curlVersion['version'] ?? '', 185 | $curlVersion['ssl_version'] ?? '' 186 | ); 187 | } 188 | 189 | return $userAgent; 190 | } 191 | 192 | /** 193 | * @return string Gets the service that will be used on the request 194 | */ 195 | abstract protected function getService(): string; 196 | 197 | /** 198 | * Dumps the httpInfo log 199 | * 200 | * @param array $httpInfo The http info. 201 | * @return void 202 | * @noinspection PhpPluralMixedCanBeReplacedWithArrayInspection 203 | */ 204 | private function dumpHttpInfo(array $httpInfo): void 205 | { 206 | foreach ($httpInfo as $key => $info) { 207 | if (is_array($info)) { 208 | foreach ($info as $infoKey => $infoValue) { 209 | $this->logger?->debug(sprintf('Curl[%s][%s]: %s', $key, $infoKey, implode(',', $infoValue))); 210 | } 211 | 212 | continue; 213 | } 214 | 215 | $this->logger?->debug(sprintf('Curl[%s]: %s', $key, $info)); 216 | } 217 | } 218 | 219 | /** 220 | * @param string $response Parses the HTTP response from Rede 221 | * @param int $statusCode The HTTP status code 222 | * 223 | * @return Transaction 224 | */ 225 | abstract protected function parseResponse(string $response, int $statusCode): Transaction; 226 | } 227 | -------------------------------------------------------------------------------- /src/Rede/ThreeDSecure.php: -------------------------------------------------------------------------------- 1 | embedded = $mpi === ThreeDSecure::MPI_REDE; 96 | $this->userAgent = $userAgent; 97 | } 98 | 99 | /** 100 | * @return string|null 101 | */ 102 | public function getReturnCode(): ?string 103 | { 104 | return $this->returnCode; 105 | } 106 | 107 | /** 108 | * @return string|null 109 | */ 110 | public function getReturnMessage(): ?string 111 | { 112 | return $this->returnMessage; 113 | } 114 | 115 | /** 116 | * @return Device 117 | */ 118 | public function getDevice(): Device 119 | { 120 | return $this->Device; 121 | } 122 | 123 | /** 124 | * @return int 125 | */ 126 | public function getThreeDIndicator(): int 127 | { 128 | return $this->threeDIndicator; 129 | } 130 | 131 | /** 132 | * @param int $threeDIndicator 133 | * 134 | * @return $this 135 | */ 136 | public function setThreeDIndicator(int $threeDIndicator): static 137 | { 138 | /** 139 | * Support for 3DS 1 will be discontinued. 140 | */ 141 | if ($threeDIndicator < 2) { 142 | trigger_error( 143 | 'Effective 15 October 2022, support for 3DS 1 and all related technology is discontinued.', 144 | time() > strtotime('2022-10-15') ? E_USER_ERROR : E_USER_DEPRECATED 145 | ); 146 | } 147 | 148 | $this->threeDIndicator = $threeDIndicator; 149 | 150 | return $this; 151 | } 152 | 153 | /** 154 | * @return string|null 155 | */ 156 | public function getDirectoryServerTransactionId(): ?string 157 | { 158 | return $this->DirectoryServerTransactionId; 159 | } 160 | 161 | /** 162 | * @param string $DirectoryServerTransactionId 163 | * 164 | * @return $this 165 | */ 166 | public function setDirectoryServerTransactionId(string $DirectoryServerTransactionId): static 167 | { 168 | $this->DirectoryServerTransactionId = $DirectoryServerTransactionId; 169 | return $this; 170 | } 171 | 172 | /** 173 | * @return string|null 174 | */ 175 | public function getCavv(): ?string 176 | { 177 | return $this->cavv; 178 | } 179 | 180 | /** 181 | * @param string $cavv 182 | * 183 | * @return $this 184 | */ 185 | public function setCavv(string $cavv): static 186 | { 187 | $this->cavv = $cavv; 188 | return $this; 189 | } 190 | 191 | /** 192 | * @return string|null 193 | */ 194 | public function getEci(): ?string 195 | { 196 | return $this->eci; 197 | } 198 | 199 | /** 200 | * @param string $eci 201 | * 202 | * @return $this 203 | */ 204 | public function setEci(string $eci): static 205 | { 206 | $this->eci = $eci; 207 | return $this; 208 | } 209 | 210 | /** 211 | * @return string 212 | */ 213 | public function getOnFailure(): string 214 | { 215 | return $this->onFailure; 216 | } 217 | 218 | /** 219 | * @param string $onFailure 220 | * 221 | * @return $this 222 | */ 223 | public function setOnFailure(string $onFailure): static 224 | { 225 | $this->onFailure = $onFailure; 226 | return $this; 227 | } 228 | 229 | /** 230 | * @return string|null 231 | */ 232 | public function getUrl(): ?string 233 | { 234 | return $this->url; 235 | } 236 | 237 | /** 238 | * @param string $url 239 | * 240 | * @return $this 241 | */ 242 | public function setUrl(string $url): static 243 | { 244 | $this->url = $url; 245 | return $this; 246 | } 247 | 248 | /** 249 | * @return string|null 250 | */ 251 | public function getUserAgent(): ?string 252 | { 253 | return $this->userAgent; 254 | } 255 | 256 | /** 257 | * @param string $userAgent 258 | * 259 | * @return $this 260 | */ 261 | public function setUserAgent(string $userAgent): static 262 | { 263 | $this->userAgent = $userAgent; 264 | return $this; 265 | } 266 | 267 | /** 268 | * @return string|null 269 | */ 270 | public function getXid(): ?string 271 | { 272 | return $this->xid; 273 | } 274 | 275 | /** 276 | * @param string $xid 277 | * 278 | * @return $this 279 | */ 280 | public function setXid(string $xid): static 281 | { 282 | $this->xid = $xid; 283 | return $this; 284 | } 285 | 286 | /** 287 | * @return bool 288 | */ 289 | public function isEmbedded(): bool 290 | { 291 | return $this->embedded; 292 | } 293 | 294 | /** 295 | * @param bool $embedded 296 | * 297 | * @return $this 298 | */ 299 | public function setEmbedded(bool $embedded): static 300 | { 301 | $this->embedded = $embedded; 302 | return $this; 303 | } 304 | 305 | /** 306 | * @return string|null 307 | */ 308 | public function getChallengePreference(): ?string 309 | { 310 | return $this->challengePreference; 311 | } 312 | 313 | /** 314 | * @param string|null $challengePreference 315 | * @return ThreeDSecure 316 | */ 317 | public function setChallengePreference(?string $challengePreference): ThreeDSecure 318 | { 319 | $this->challengePreference = $challengePreference; 320 | return $this; 321 | } 322 | } 323 | -------------------------------------------------------------------------------- /src/Rede/Authorization.php: -------------------------------------------------------------------------------- 1 | affiliation; 102 | } 103 | 104 | /** 105 | * @param string|null $affiliation 106 | * @return $this 107 | */ 108 | public function setAffiliation(?string $affiliation): static 109 | { 110 | $this->affiliation = $affiliation; 111 | return $this; 112 | } 113 | 114 | /** 115 | * @return int|null 116 | */ 117 | public function getAmount(): ?int 118 | { 119 | return $this->amount; 120 | } 121 | 122 | /** 123 | * @param int|null $amount 124 | * @return $this 125 | */ 126 | public function setAmount(?int $amount): static 127 | { 128 | $this->amount = $amount; 129 | return $this; 130 | } 131 | 132 | /** 133 | * @return string|null 134 | */ 135 | public function getAuthorizationCode(): ?string 136 | { 137 | return $this->authorizationCode; 138 | } 139 | 140 | /** 141 | * @param string|null $authorizationCode 142 | * @return $this 143 | */ 144 | public function setAuthorizationCode(?string $authorizationCode): static 145 | { 146 | $this->authorizationCode = $authorizationCode; 147 | return $this; 148 | } 149 | 150 | /** 151 | * @return string|null 152 | */ 153 | public function getCardBin(): ?string 154 | { 155 | return $this->cardBin; 156 | } 157 | 158 | /** 159 | * @param string|null $cardBin 160 | * @return $this 161 | */ 162 | public function setCardBin(?string $cardBin): static 163 | { 164 | $this->cardBin = $cardBin; 165 | return $this; 166 | } 167 | 168 | /** 169 | * @return string|null 170 | */ 171 | public function getCardHolderName(): ?string 172 | { 173 | return $this->cardHolderName; 174 | } 175 | 176 | /** 177 | * @param string|null $cardHolderName 178 | * @return $this 179 | */ 180 | public function setCardHolderName(?string $cardHolderName): static 181 | { 182 | $this->cardHolderName = $cardHolderName; 183 | return $this; 184 | } 185 | 186 | /** 187 | * @return DateTime|null 188 | */ 189 | public function getDateTime(): ?DateTime 190 | { 191 | return $this->dateTime; 192 | } 193 | 194 | /** 195 | * @param DateTime|null $dateTime 196 | * @return $this 197 | */ 198 | public function setDateTime(?DateTime $dateTime): static 199 | { 200 | $this->dateTime = $dateTime; 201 | return $this; 202 | } 203 | 204 | /** 205 | * @return int|null 206 | */ 207 | public function getInstallments(): ?int 208 | { 209 | return $this->installments; 210 | } 211 | 212 | /** 213 | * @param int|null $installments 214 | * @return $this 215 | */ 216 | public function setInstallments(?int $installments): static 217 | { 218 | $this->installments = $installments; 219 | return $this; 220 | } 221 | 222 | /** 223 | * @return string|null 224 | */ 225 | public function getKind(): ?string 226 | { 227 | return $this->kind; 228 | } 229 | 230 | /** 231 | * @param string|null $kind 232 | * @return $this 233 | */ 234 | public function setKind(?string $kind): static 235 | { 236 | $this->kind = $kind; 237 | return $this; 238 | } 239 | 240 | /** 241 | * @return string|null 242 | */ 243 | public function getLast4(): ?string 244 | { 245 | return $this->last4; 246 | } 247 | 248 | /** 249 | * @param string|null $last4 250 | * @return $this 251 | */ 252 | public function setLast4(?string $last4): static 253 | { 254 | $this->last4 = $last4; 255 | return $this; 256 | } 257 | 258 | /** 259 | * @return string|null 260 | */ 261 | public function getNsu(): ?string 262 | { 263 | return $this->nsu; 264 | } 265 | 266 | /** 267 | * @param string|null $nsu 268 | * @return $this 269 | */ 270 | public function setNsu(?string $nsu): static 271 | { 272 | $this->nsu = $nsu; 273 | return $this; 274 | } 275 | 276 | /** 277 | * @return string|null 278 | */ 279 | public function getOrigin(): ?string 280 | { 281 | return $this->origin; 282 | } 283 | 284 | /** 285 | * @param string|null $origin 286 | * @return $this 287 | */ 288 | public function setOrigin(?string $origin): static 289 | { 290 | $this->origin = $origin; 291 | return $this; 292 | } 293 | 294 | /** 295 | * @return string|null 296 | */ 297 | public function getReference(): ?string 298 | { 299 | return $this->reference; 300 | } 301 | 302 | /** 303 | * @param string|null $reference 304 | * @return $this 305 | */ 306 | public function setReference(?string $reference): static 307 | { 308 | $this->reference = $reference; 309 | return $this; 310 | } 311 | 312 | /** 313 | * @return string|null 314 | */ 315 | public function getReturnCode(): ?string 316 | { 317 | return $this->returnCode; 318 | } 319 | 320 | /** 321 | * @param string|null $returnCode 322 | * @return $this 323 | */ 324 | public function setReturnCode(?string $returnCode): static 325 | { 326 | $this->returnCode = $returnCode; 327 | return $this; 328 | } 329 | 330 | /** 331 | * @return string|null 332 | */ 333 | public function getReturnMessage(): ?string 334 | { 335 | return $this->returnMessage; 336 | } 337 | 338 | /** 339 | * @param string|null $returnMessage 340 | * @return $this 341 | */ 342 | public function setReturnMessage(?string $returnMessage): static 343 | { 344 | $this->returnMessage = $returnMessage; 345 | return $this; 346 | } 347 | 348 | /** 349 | * @return string|null 350 | */ 351 | public function getStatus(): ?string 352 | { 353 | return $this->status; 354 | } 355 | 356 | /** 357 | * @param string|null $status 358 | * @return $this 359 | */ 360 | public function setStatus(?string $status): static 361 | { 362 | $this->status = $status; 363 | return $this; 364 | } 365 | 366 | /** 367 | * @return string|null 368 | */ 369 | public function getSubscription(): ?string 370 | { 371 | return $this->subscription; 372 | } 373 | 374 | /** 375 | * @param string|null $subscription 376 | * @return $this 377 | */ 378 | public function setSubscription(?string $subscription): static 379 | { 380 | $this->subscription = $subscription; 381 | return $this; 382 | } 383 | 384 | /** 385 | * @return string|null 386 | */ 387 | public function getTid(): ?string 388 | { 389 | return $this->tid; 390 | } 391 | 392 | /** 393 | * @param string|null $tid 394 | * @return $this 395 | */ 396 | public function setTid(?string $tid): static 397 | { 398 | $this->tid = $tid; 399 | return $this; 400 | } 401 | } 402 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SDK PHP 2 | 3 | SDK de integração eRede 4 | 5 | # Funcionalidades 6 | 7 | Este SDK possui as seguintes funcionalidades: 8 | * Autorização 9 | * Captura 10 | * Consultas 11 | * Cancelamento 12 | * 3DS2 13 | * Zero dollar 14 | * iata 15 | * MCC dinâmico. 16 | 17 | # Instalação 18 | 19 | ## Dependências 20 | 21 | * PHP >= 8.1 22 | 23 | ## Instalando o SDK 24 | 25 | Se já possui um arquivo `composer.json`, basta adicionar a seguinte dependência ao seu projeto: 26 | 27 | ```json 28 | { 29 | "require": { 30 | "developersrede/erede-php": "*" 31 | } 32 | } 33 | 34 | ``` 35 | 36 | Com a dependência adicionada ao `composer.json`, basta executar: 37 | 38 | ``` 39 | composer install 40 | ``` 41 | 42 | Alternativamente, você pode executar diretamente em seu terminal: 43 | 44 | ``` 45 | composer require "developersrede/erede-php" 46 | ``` 47 | 48 | # Testes 49 | 50 | O SDK utiliza PHPUnit com TestDox para os testes. Para executá-los em ambiente local, você precisa exportar 51 | as variáveis de ambiente `REDE_PV` e `REDE_TOKEN` com suas credenciais da API. Feito isso, basta rodar: 52 | 53 | ``` 54 | export REDE_PV=1234 55 | export REDE_TOKEN=5678 56 | 57 | ./tests 58 | ``` 59 | 60 | Os testes também podem ser executados através de um container com a configuração ideal para o projeto. Para isso, basta 61 | fazer: 62 | 63 | ``` 64 | docker build . -t erede-docker 65 | docker run -e REDE_PV='1234' -e REDE_TOKEN='5678' erede-docker 66 | ``` 67 | ```` 68 | Caso necessário, o SDK possui a possibilidade de logs de depuração que podem ser utilizados ao executar os testes. Para isso, 69 | basta exportar a variável de ambiente `REDE_DEBUG` com o valor 1: 70 | 71 | ``` 72 | export REDE_DEBUG=1 73 | ``` 74 | 75 | # Utilizando 76 | 77 | ## Autorizando uma transação 78 | 79 | ```php 80 | creditCard( 89 | '5448280000000007', 90 | '235', 91 | '12', 92 | '2020', 93 | 'John Snow' 94 | ); 95 | 96 | // Autoriza a transação 97 | $transaction = (new eRede($store))->create($transaction); 98 | 99 | if ($transaction->getReturnCode() == '00') { 100 | printf("Transação autorizada com sucesso; tid=%s\n", $transaction->getTid()); 101 | } 102 | ``` 103 | 104 | Por padrão, a transação é capturada automaticamente; caso seja necessário apenas autorizar a transação, o método `Transaction::capture()` deverá ser chamado com o parâmetro `false`: 105 | 106 | ```php 107 | creditCard( 116 | '5448280000000007', 117 | '235', 118 | '12', 119 | '2020', 120 | 'John Snow' 121 | )->capture(false); 122 | 123 | // Autoriza a transação 124 | $transaction = (new eRede($store))->create($transaction); 125 | 126 | if ($transaction->getReturnCode() == '00') { 127 | printf("Transação autorizada com sucesso; tid=%s\n", $transaction->getTid()); 128 | } 129 | //... 130 | ``` 131 | 132 | ## Adiciona configuração de parcelamento 133 | ```php 134 | creditCard( 143 | '5448280000000007', 144 | '235', 145 | '12', 146 | '2020', 147 | 'John Snow' 148 | ); 149 | 150 | // Configuração de parcelamento 151 | $transaction->setInstallments(3); 152 | 153 | // Autoriza a transação 154 | $transaction = (new eRede($store))->create($transaction); 155 | 156 | if ($transaction->getReturnCode() == '00') { 157 | printf("Transação autorizada com sucesso; tid=%s\n", $transaction->getTid()); 158 | } 159 | ``` 160 | 161 | ## Adiciona informação adicional de gateway e módulo 162 | 163 | ```php 164 | creditCard( 173 | '5448280000000007', 174 | '235', 175 | '12', 176 | '2020', 177 | 'John Snow' 178 | )->additional(1234, 56); 179 | 180 | // Autoriza a transação 181 | $transaction = (new eRede($store))->create($transaction); 182 | 183 | if ($transaction->getReturnCode() == '00') { 184 | printf("Transação autorizada com sucesso; tid=%s\n", $transaction->getTid()); 185 | } 186 | ``` 187 | 188 | ## Autorizando uma transação com MCC dinâmico 189 | 190 | ```php 191 | creditCard( 200 | '5448280000000007', 201 | '235', 202 | '12', 203 | '2020', 204 | 'John Snow' 205 | )->mcc( 206 | 'LOJADOZE', 207 | '22349202212', 208 | new SubMerchant( 209 | '1234', 210 | 'São Paulo', 211 | 'Brasil' 212 | ) 213 | ); 214 | 215 | // Autoriza a transação 216 | $transaction = (new eRede($store))->create($transaction); 217 | 218 | if ($transaction->getReturnCode() == '00') { 219 | printf("Transação autorizada com sucesso; tid=%s\n", $transaction->getTid()); 220 | } 221 | //... 222 | ``` 223 | 224 | ## Autorizando uma transação IATA 225 | 226 | ```php 227 | creditCard( 236 | '5448280000000007', 237 | '235', 238 | '12', 239 | '2020', 240 | 'John Snow' 241 | )->iata('code123', '250'); 242 | 243 | // Autoriza a transação 244 | $transaction = (new eRede($store))->create($transaction); 245 | 246 | if ($transaction->getReturnCode() == '00') { 247 | printf("Transação autorizada com sucesso; tid=%s\n", $transaction->getTid()); 248 | } 249 | ``` 250 | 251 | ## Capturando uma transação 252 | 253 | ```php 254 | capture((new Transaction(20.99))->setTid('TID123')); 263 | 264 | if ($transaction->getReturnCode() == '00') { 265 | printf("Transação capturada com sucesso; tid=%s\n", $transaction->getTid()); 266 | } 267 | ``` 268 | 269 | ## Cancelando uma transação 270 | 271 | ```php 272 | cancel((new Transaction(20.99))->setTid('TID123')); 281 | 282 | if ($transaction->getReturnCode() == '359') { 283 | printf("Transação cancelada com sucesso; tid=%s\n", $transaction->getTid()); 284 | } 285 | ``` 286 | 287 | ## Consultando uma transação pelo ID 288 | 289 | ```php 290 | get('TID123'); 298 | 299 | printf("O status atual da autorização é %s\n", $transaction->getAuthorization()->getStatus()); 300 | ``` 301 | 302 | ## Consultando uma transação pela referência 303 | 304 | ```php 305 | getByReference('pedido123'); 313 | 314 | printf("O status atual da autorização é %s\n", $transaction->getAuthorization()->getStatus()); 315 | ``` 316 | 317 | ## Consultando cancelamentos de uma transação 318 | 319 | ```php 320 | getRefunds('TID123'); 328 | 329 | printf("O status atual da autorização é %s\n", $transaction->getAuthorization()->getStatus()); 330 | ``` 331 | 332 | ## Transação com autenticação 333 | 334 | ```php 335 | debitCard( 344 | '5277696455399733', 345 | '123', 346 | '01', 347 | '2020', 348 | 'John Snow' 349 | ); 350 | 351 | // Configura o 3dSecure para autenticação 352 | $transaction->threeDSecure( 353 | new Device( 354 | ColorDepth: 1, 355 | DeviceType3ds: 'BROWSER', 356 | JavaEnabled: false, 357 | Language: 'BR', 358 | ScreenHeight: 500, 359 | ScreenWidth: 500, 360 | TimeZoneOffset: 3 361 | ) 362 | ); 363 | $transaction->addUrl('https://redirecturl.com/3ds/success', Url::THREE_D_SECURE_SUCCESS); 364 | $transaction->addUrl('https://redirecturl.com/3ds/failure', Url::THREE_D_SECURE_FAILURE); 365 | 366 | $transaction = (new eRede($store))->create($transaction); 367 | 368 | if ($transaction->getReturnCode() == '220') { 369 | printf("Redirecione o cliente para \"%s\" para autenticação\n", $transaction->getThreeDSecure()->getUrl()); 370 | } 371 | ``` 372 | -------------------------------------------------------------------------------- /test/Rede/eRedeTest.php: -------------------------------------------------------------------------------- 1 | logger = new Logger('eRede SDK Test'); 47 | $this->logger->pushHandler(new StreamHandler('php://stdout', $debug ? Level::Debug : Level::Error)); 48 | 49 | $this->store = new Store($filiation, $token, Environment::sandbox()); 50 | } 51 | 52 | private function generateReferenceNumber(): string 53 | { 54 | return 'pedido' . (time() + eRedeTest::$sequence++); 55 | } 56 | 57 | public function testShouldAuthorizeACreditcardTransaction(): void 58 | { 59 | $transaction = (new Transaction(20.99, $this->generateReferenceNumber()))->creditCard( 60 | '5448280000000007', 61 | '235', 62 | '12', 63 | (int)date('Y') + 1, 64 | 'John Snow' 65 | )->capture(false); 66 | 67 | $transaction = $this->createERede()->create($transaction); 68 | 69 | $this->assertEquals('00', $transaction->getReturnCode()); 70 | } 71 | 72 | public function testShouldAuthorizeAndCaptureACreditcardTransaction(): void 73 | { 74 | $transaction = (new Transaction(20.99, $this->generateReferenceNumber()))->creditCard( 75 | '5448280000000007', 76 | '235', 77 | '12', 78 | (int)date('Y') + 1, 79 | 'John Snow' 80 | )->capture(); 81 | 82 | $transaction = $this->createERede()->create($transaction); 83 | 84 | $this->assertEquals('00', $transaction->getReturnCode()); 85 | } 86 | 87 | public function testShouldAuthorizeACreditcardTransactionWithInstallments(): void 88 | { 89 | $transaction = (new Transaction(20.99, $this->generateReferenceNumber()))->creditCard( 90 | '5448280000000007', 91 | '235', 92 | '12', 93 | (int)date('Y') + 1, 94 | 'John Snow' 95 | )->setInstallments(3); 96 | 97 | $transaction = $this->createERede()->create($transaction); 98 | 99 | $this->assertEquals('00', $transaction->getReturnCode()); 100 | } 101 | 102 | public function testShouldAuthorizeACreditcardTransactionWithSoftdescriptor(): void 103 | { 104 | $transaction = (new Transaction(20.99, $this->generateReferenceNumber()))->creditCard( 105 | '5448280000000007', 106 | '235', 107 | '12', 108 | (int)date('Y') + 1, 109 | 'John Snow' 110 | )->setSoftDescriptor('Loja X'); 111 | 112 | $transaction = $this->createERede()->create($transaction); 113 | 114 | $this->assertEquals('00', $transaction->getReturnCode()); 115 | } 116 | 117 | public function testShouldAuthorizeACreditcardTransactionWithAdditionalGatewayAndModuleInformation(): void 118 | { 119 | $transaction = (new Transaction(20.99, $this->generateReferenceNumber()))->creditCard( 120 | '5448280000000007', 121 | '235', 122 | '12', 123 | (int)date('Y') + 1, 124 | 'John Snow' 125 | )->additional(1234, 56); 126 | 127 | $transaction = $this->createERede()->create($transaction); 128 | 129 | $this->assertEquals('00', $transaction->getReturnCode()); 130 | } 131 | 132 | /** 133 | * @testdox Should authorize a credit card transaction with dynamic MCC 134 | */ 135 | public function testShouldAuthorizeACreditcardTransactionWithDynamicMCC(): void 136 | { 137 | $transaction = (new Transaction(20.99, $this->generateReferenceNumber()))->creditCard( 138 | '5448280000000007', 139 | '235', 140 | '12', 141 | (int)date('Y') + 1, 142 | 'John Snow' 143 | )->mcc( 144 | 'LOJADOZE', 145 | '22349202212', 146 | new SubMerchant( 147 | '1234', 148 | 'São Paulo', 149 | 'Brasil' 150 | ) 151 | ); 152 | 153 | $transaction = $this->createERede()->create($transaction); 154 | 155 | $this->assertEquals('00', $transaction->getReturnCode()); 156 | } 157 | 158 | /** 159 | * @testdox Should authorize a credit card transaction with IATA 160 | */ 161 | public function testShouldAuthorizeACreditcardTransactionWithIATA(): void 162 | { 163 | $transaction = (new Transaction(20.99, $this->generateReferenceNumber()))->creditCard( 164 | '5448280000000007', 165 | '235', 166 | '12', 167 | (int)date('Y') + 1, 168 | 'John Snow' 169 | )->iata('101010', '250'); 170 | 171 | $transaction = $this->createERede()->create($transaction); 172 | 173 | $this->assertEquals('00', $transaction->getReturnCode()); 174 | } 175 | 176 | public function testShouldAuthorizeAZeroDolarCreditcardTransaction(): void 177 | { 178 | $transaction = (new Transaction(0, $this->generateReferenceNumber()))->creditCard( 179 | '5448280000000007', 180 | '235', 181 | '12', 182 | (int)date('Y') + 1, 183 | 'John Snow' 184 | )->setSoftDescriptor('Loja X'); 185 | 186 | $transaction = $this->createERede()->zero($transaction); 187 | 188 | $this->assertEquals('174', $transaction->getReturnCode()); 189 | } 190 | 191 | public function testShouldCreateADebitcardTransactionWithAuthentication(): void 192 | { 193 | $transaction = (new Transaction(25, $this->generateReferenceNumber()))->debitCard( 194 | '5277696455399733', 195 | '123', 196 | '12', 197 | (int)date('Y') + 1, 198 | 'John Snow' 199 | ); 200 | 201 | $transaction->threeDSecure( 202 | new Device( 203 | ColorDepth: 1, 204 | DeviceType3ds: 'BROWSER', 205 | JavaEnabled: false, 206 | Language: 'BR', 207 | ScreenHeight: 500, 208 | ScreenWidth: 500, 209 | TimeZoneOffset: 3 210 | ), 211 | ThreeDSecure::DECLINE_ON_FAILURE 212 | ); 213 | 214 | $transaction->addUrl('https://redirecturl.com/3ds/success', Url::THREE_D_SECURE_SUCCESS); 215 | $transaction->addUrl('https://redirecturl.com/3ds/failure', Url::THREE_D_SECURE_FAILURE); 216 | 217 | $transaction = $this->createERede()->create($transaction); 218 | $returnCode = $transaction->getReturnCode(); 219 | 220 | $this->assertContains($returnCode, ['220', '201']); 221 | 222 | if ($returnCode === '220') { 223 | $this->assertNotEmpty($transaction->getThreeDSecure()->getUrl()); 224 | 225 | printf("\tURL de autenticação: %s\n", $transaction->getThreeDSecure()->getUrl()); 226 | } 227 | } 228 | 229 | public function testShouldCaptureATransaction(): void 230 | { 231 | // First we create a new transaction 232 | $authorizedTransaction = $this->createERede()->create( 233 | (new Transaction(20.99, $this->generateReferenceNumber()))->creditCard( 234 | '5448280000000007', 235 | '235', 236 | '12', 237 | (int)date('Y') + 1, 238 | 'John Snow' 239 | )->capture(false) 240 | ); 241 | 242 | // Then we capture the authorized transaction 243 | $capturedTransaction = $this->createERede() 244 | ->capture($authorizedTransaction); 245 | 246 | $this->assertEquals('00', $authorizedTransaction->getReturnCode()); 247 | $this->assertEquals('00', $capturedTransaction->getReturnCode()); 248 | } 249 | 250 | public function testShouldCancelATransaction(): void 251 | { 252 | // First we create a new transaction 253 | $authorizedTransaction = $this->createAnAuthorizedTransaction(); 254 | 255 | $this->assertEquals('00', $authorizedTransaction->getReturnCode()); 256 | 257 | // Then we capture the authorized transaction 258 | $canceledTransaction = $this->createERede() 259 | ->cancel((new Transaction(20.99)) 260 | ->setTid((string)$authorizedTransaction->getTid())); 261 | 262 | $this->assertEquals('359', $canceledTransaction->getReturnCode()); 263 | } 264 | 265 | /** 266 | * @testdox Should consult a transaction by its TID 267 | */ 268 | public function testShouldConsultATransactionByItsTID(): void 269 | { 270 | // First we create a new transaction 271 | $authorizedTransaction = $this->createAnAuthorizedTransaction(); 272 | $contultedTransaction = $this->createERede()->get((string)$authorizedTransaction->getTid()); 273 | $authorization = $contultedTransaction->getAuthorization(); 274 | 275 | if ($authorization === null) { 276 | throw new RuntimeException('Something happened with the authorized transaction'); 277 | } 278 | 279 | $this->assertEquals($authorizedTransaction->getTid(), $authorization->getTid()); 280 | } 281 | 282 | public function testShouldConsultATransactionByReference(): void 283 | { 284 | // First we create a new transaction 285 | $authorizedTransaction = $this->createAnAuthorizedTransaction(); 286 | $contultedTransaction = $this->createERede()->getByReference((string)$authorizedTransaction->getReference()); 287 | $authorization = $contultedTransaction->getAuthorization(); 288 | 289 | if ($authorization === null) { 290 | throw new RuntimeException('Something happened with the authorized transaction'); 291 | } 292 | 293 | $this->assertEquals($authorizedTransaction->getReference(), $authorization->getReference()); 294 | } 295 | 296 | public function testShouldConsultTheTransactionRefunds(): void 297 | { 298 | // First we create a new transaction 299 | $authorizedTransaction = $this->createAnAuthorizedTransaction(); 300 | 301 | $this->assertEquals('00', $authorizedTransaction->getReturnCode()); 302 | 303 | // Them we cancel the authorized transaction 304 | $canceledTransaction = $this->createERede() 305 | ->cancel((new Transaction(20.99)) 306 | ->setTid((string)$authorizedTransaction->getTid())); 307 | 308 | $this->assertEquals('359', $canceledTransaction->getReturnCode()); 309 | 310 | // Now we can consult the refunds 311 | $refundedTransactions = $this->createERede()->getRefunds((string)$authorizedTransaction->getTid()); 312 | 313 | $this->assertCount(1, $refundedTransactions->getRefunds()); 314 | 315 | foreach ($refundedTransactions->getRefunds() as $refund) { 316 | $this->assertEquals($canceledTransaction->getRefundId(), $refund->getRefundId()); 317 | } 318 | } 319 | 320 | /** 321 | * @return Transaction 322 | */ 323 | private function createAnAuthorizedTransaction(): Transaction 324 | { 325 | return $this->createERede()->create( 326 | (new Transaction(20.99, $this->generateReferenceNumber()))->creditCard( 327 | '5448280000000007', 328 | '235', 329 | 12, 330 | (int)date('Y') + 1, 331 | 'John Snow' 332 | )->capture() 333 | ); 334 | } 335 | 336 | /** 337 | * @return eRede 338 | */ 339 | private function createERede(): eRede 340 | { 341 | if ($this->store === null || $this->logger === null) { 342 | throw new RuntimeException('Store cant be null'); 343 | } 344 | 345 | return new eRede($this->store, $this->logger); 346 | } 347 | } 348 | -------------------------------------------------------------------------------- /src/Rede/Transaction.php: -------------------------------------------------------------------------------- 1 | 136 | */ 137 | private array $refunds = []; 138 | 139 | /** 140 | * @var DateTime|null 141 | */ 142 | private ?DateTime $requestDateTime = null; 143 | 144 | /** 145 | * @var string|null 146 | */ 147 | private ?string $returnCode = null; 148 | 149 | /** 150 | * @var string|null 151 | */ 152 | private ?string $returnMessage = null; 153 | 154 | /** 155 | * @var string|null 156 | */ 157 | private ?string $securityCode = null; 158 | 159 | /** 160 | * @var string|null 161 | */ 162 | private ?string $softDescriptor = null; 163 | 164 | /** 165 | * @var int|null 166 | */ 167 | private ?int $storageCard = null; 168 | 169 | /** 170 | * @var bool 171 | */ 172 | private ?bool $subscription = null; 173 | 174 | /** 175 | * @var ThreeDSecure|null 176 | */ 177 | private ?ThreeDSecure $threeDSecure = null; 178 | 179 | /** 180 | * @var string|null 181 | */ 182 | private ?string $tid = null; 183 | 184 | /** 185 | * @var array 186 | */ 187 | private array $urls = []; 188 | 189 | /** 190 | * @var SubMerchant|null 191 | */ 192 | private ?SubMerchant $subMerchant = null; 193 | 194 | /** 195 | * @var string|null 196 | */ 197 | private ?string $paymentFacilitatorID = null; 198 | 199 | /** 200 | * @var int|null 201 | */ 202 | private ?int $amount = null; 203 | 204 | /** 205 | * Transaction constructor. 206 | * 207 | * @param int|float|null $amount 208 | * @param string|null $reference 209 | */ 210 | public function __construct(int|float|null $amount = null, private ?string $reference = null) 211 | { 212 | if ($amount !== null) { 213 | $this->setAmount($amount); 214 | } 215 | } 216 | 217 | /** 218 | * @param string $url 219 | * @param string $kind 220 | * 221 | * @return $this 222 | */ 223 | public function addUrl(string $url, string $kind = Url::CALLBACK): static 224 | { 225 | $this->urls[] = new Url($url, $kind); 226 | 227 | return $this; 228 | } 229 | 230 | /** 231 | * @param int|null $gateway 232 | * @param int|null $module 233 | * 234 | * @return $this 235 | */ 236 | public function additional(?int $gateway = null, ?int $module = null): static 237 | { 238 | $this->additional = new Additional(); 239 | 240 | if ($gateway !== null) { 241 | $this->additional->setGateway($gateway); 242 | } 243 | 244 | if ($module !== null) { 245 | $this->additional->setModule($module); 246 | } 247 | 248 | return $this; 249 | } 250 | 251 | /** 252 | * @param string $cardNumber 253 | * @param string $cardCvv 254 | * @param int|string $expirationMonth 255 | * @param int|string $expirationYear 256 | * @param string $holderName 257 | * 258 | * @return $this this transaction 259 | */ 260 | public function creditCard( 261 | string $cardNumber, 262 | string $cardCvv, 263 | int|string $expirationMonth, 264 | int|string $expirationYear, 265 | string $holderName 266 | ): static { 267 | return $this->setCard( 268 | $cardNumber, 269 | $cardCvv, 270 | $expirationMonth, 271 | $expirationYear, 272 | $holderName, 273 | Transaction::CREDIT 274 | ); 275 | } 276 | 277 | /** 278 | * @param string $cardNumber 279 | * @param string $securityCode 280 | * @param int|string $expirationMonth 281 | * @param int|string $expirationYear 282 | * @param string $cardHolderName 283 | * @param string $kind 284 | * 285 | * @return $this this transaction 286 | */ 287 | public function setCard( 288 | string $cardNumber, 289 | string $securityCode, 290 | int|string $expirationMonth, 291 | int|string $expirationYear, 292 | string $cardHolderName, 293 | string $kind 294 | ): static { 295 | $this->setCardNumber($cardNumber); 296 | $this->setSecurityCode($securityCode); 297 | $this->setExpirationMonth($expirationMonth); 298 | $this->setExpirationYear($expirationYear); 299 | $this->setCardHolderName($cardHolderName); 300 | $this->setKind($kind); 301 | 302 | return $this; 303 | } 304 | 305 | /** 306 | * @param string $cardNumber 307 | * @param string $cardCvv 308 | * @param int|string $expirationMonth 309 | * @param int|string $expirationYear 310 | * @param string $holderName 311 | * 312 | * @return $this this transaction 313 | */ 314 | public function debitCard( 315 | string $cardNumber, 316 | string $cardCvv, 317 | int|string $expirationMonth, 318 | int|string $expirationYear, 319 | string $holderName 320 | ): static { 321 | $this->capture(); 322 | 323 | return $this->setCard( 324 | $cardNumber, 325 | $cardCvv, 326 | $expirationMonth, 327 | $expirationYear, 328 | $holderName, 329 | Transaction::DEBIT 330 | ); 331 | } 332 | 333 | /** 334 | * @param bool $capture 335 | * 336 | * @return $this 337 | */ 338 | public function capture(bool $capture = true): static 339 | { 340 | if (!$capture && $this->kind === Transaction::DEBIT) { 341 | throw new InvalidArgumentException('Debit transactions will always be captured'); 342 | } 343 | 344 | $this->capture = $capture; 345 | return $this; 346 | } 347 | 348 | /** 349 | * @return mixed 350 | * @see \JsonSerializable::jsonSerialize() 351 | * @noinspection PhpMixedReturnTypeCanBeReducedInspection 352 | */ 353 | public function jsonSerialize(): mixed 354 | { 355 | $capture = null; 356 | 357 | if (is_bool($this->capture)) { 358 | $capture = $this->capture ? 'true' : 'false'; 359 | } 360 | 361 | return array_filter( 362 | [ 363 | 'capture' => $capture, 364 | 'cart' => $this->cart, 365 | 'kind' => $this->kind, 366 | 'threeDSecure' => $this->threeDSecure, 367 | 'reference' => $this->reference, 368 | 'amount' => $this->amount, 369 | 'installments' => $this->installments, 370 | 'cardHolderName' => $this->cardHolderName, 371 | 'cardNumber' => $this->cardNumber, 372 | 'expirationMonth' => $this->expirationMonth, 373 | 'expirationYear' => $this->expirationYear, 374 | 'securityCode' => $this->securityCode, 375 | 'softDescriptor' => $this->softDescriptor, 376 | 'subscription' => $this->subscription, 377 | 'origin' => $this->origin, 378 | 'distributorAffiliation' => $this->distributorAffiliation, 379 | 'storageCard' => $this->storageCard, 380 | 'urls' => $this->urls, 381 | 'iata' => $this->iata, 382 | 'additional' => $this->additional 383 | ], 384 | function ($value) { 385 | return !is_null($value); 386 | } 387 | ); 388 | } 389 | 390 | /** 391 | * @return int|null 392 | */ 393 | public function getAmount(): ?int 394 | { 395 | return $this->amount; 396 | } 397 | 398 | /** 399 | * @param int|float $amount 400 | * 401 | * @return $this 402 | */ 403 | public function setAmount(int|float $amount): static 404 | { 405 | $this->amount = (int)round($amount * 100); 406 | return $this; 407 | } 408 | 409 | /** 410 | * @return Authorization|null 411 | */ 412 | public function getAuthorization(): ?Authorization 413 | { 414 | return $this->authorization; 415 | } 416 | 417 | /** 418 | * @return string|null 419 | */ 420 | public function getAuthorizationCode(): ?string 421 | { 422 | return $this->authorizationCode; 423 | } 424 | 425 | /** 426 | * @return string|null 427 | */ 428 | public function getCancelId(): ?string 429 | { 430 | return $this->cancelId; 431 | } 432 | 433 | /** 434 | * @return bool|Capture|null 435 | */ 436 | public function getCapture(): bool|Capture|null 437 | { 438 | return $this->capture; 439 | } 440 | 441 | /** 442 | * @return string|null 443 | */ 444 | public function getCardBin(): ?string 445 | { 446 | return $this->cardBin; 447 | } 448 | 449 | /** 450 | * @return string|null 451 | */ 452 | public function getCardHolderName(): ?string 453 | { 454 | return $this->cardHolderName; 455 | } 456 | 457 | /** 458 | * @param string $cardHolderName 459 | * 460 | * @return $this 461 | */ 462 | public function setCardHolderName(string $cardHolderName): static 463 | { 464 | $this->cardHolderName = $cardHolderName; 465 | return $this; 466 | } 467 | 468 | /** 469 | * @return string|null 470 | */ 471 | public function getCardNumber(): ?string 472 | { 473 | return $this->cardNumber; 474 | } 475 | 476 | /** 477 | * @param string $cardNumber 478 | * 479 | * @return $this 480 | */ 481 | public function setCardNumber(string $cardNumber): static 482 | { 483 | $this->cardNumber = $cardNumber; 484 | return $this; 485 | } 486 | 487 | /** 488 | * @return Cart|null 489 | */ 490 | public function getCart(): ?Cart 491 | { 492 | return $this->cart; 493 | } 494 | 495 | /** 496 | * @param Cart $cart 497 | * 498 | * @return $this 499 | */ 500 | public function setCart(Cart $cart): static 501 | { 502 | $this->cart = $cart; 503 | return $this; 504 | } 505 | 506 | /** 507 | * @return DateTime|null 508 | */ 509 | public function getDateTime(): ?DateTime 510 | { 511 | return $this->dateTime; 512 | } 513 | 514 | /** 515 | * @return int|null 516 | */ 517 | public function getDistributorAffiliation(): ?int 518 | { 519 | return $this->distributorAffiliation; 520 | } 521 | 522 | /** 523 | * @param int $distributorAffiliation 524 | * 525 | * @return $this 526 | */ 527 | public function setDistributorAffiliation(int $distributorAffiliation): static 528 | { 529 | $this->distributorAffiliation = $distributorAffiliation; 530 | return $this; 531 | } 532 | 533 | /** 534 | * @return int|string|null 535 | */ 536 | public function getExpirationMonth(): int|string|null 537 | { 538 | return $this->expirationMonth; 539 | } 540 | 541 | /** 542 | * @param int|string $expirationMonth 543 | * 544 | * @return $this 545 | */ 546 | public function setExpirationMonth(int|string $expirationMonth): static 547 | { 548 | $this->expirationMonth = $expirationMonth; 549 | return $this; 550 | } 551 | 552 | /** 553 | * @return int|string|null 554 | */ 555 | public function getExpirationYear(): int|string|null 556 | { 557 | return $this->expirationYear; 558 | } 559 | 560 | /** 561 | * @param int|string $expirationYear 562 | * 563 | * @return $this 564 | */ 565 | public function setExpirationYear(int|string $expirationYear): static 566 | { 567 | $this->expirationYear = $expirationYear; 568 | return $this; 569 | } 570 | 571 | /** 572 | * @return Iata|null 573 | */ 574 | public function getIata(): ?Iata 575 | { 576 | return $this->iata; 577 | } 578 | 579 | /** 580 | * @param string $code 581 | * @param string $departureTax 582 | * 583 | * @return $this 584 | */ 585 | public function setIata(string $code, string $departureTax): static 586 | { 587 | $this->iata = new Iata(); 588 | $this->iata->setCode($code); 589 | $this->iata->setDepartureTax($departureTax); 590 | 591 | return $this; 592 | } 593 | 594 | /** 595 | * @return int|null 596 | */ 597 | public function getInstallments(): ?int 598 | { 599 | return $this->installments; 600 | } 601 | 602 | /** 603 | * @param int $installments 604 | * 605 | * @return $this 606 | */ 607 | public function setInstallments(int $installments): static 608 | { 609 | $this->installments = $installments; 610 | return $this; 611 | } 612 | 613 | /** 614 | * @return string|null 615 | */ 616 | public function getKind(): ?string 617 | { 618 | return $this->kind; 619 | } 620 | 621 | /** 622 | * @param string $kind 623 | * 624 | * @return $this 625 | */ 626 | public function setKind(string $kind): static 627 | { 628 | $this->kind = $kind; 629 | return $this; 630 | } 631 | 632 | /** 633 | * @return string|null 634 | */ 635 | public function getLast4(): ?string 636 | { 637 | return $this->last4; 638 | } 639 | 640 | /** 641 | * @return string|null 642 | */ 643 | public function getNsu(): ?string 644 | { 645 | return $this->nsu; 646 | } 647 | 648 | /** 649 | * @return int|null 650 | */ 651 | public function getOrigin(): ?int 652 | { 653 | return $this->origin; 654 | } 655 | 656 | /** 657 | * @param int $origin 658 | * 659 | * @return $this 660 | */ 661 | public function setOrigin(int $origin): static 662 | { 663 | $this->origin = $origin; 664 | return $this; 665 | } 666 | 667 | /** 668 | * @return string|null 669 | */ 670 | public function getReference(): ?string 671 | { 672 | return $this->reference; 673 | } 674 | 675 | /** 676 | * @param string $reference 677 | * 678 | * @return $this 679 | */ 680 | public function setReference(string $reference): static 681 | { 682 | $this->reference = $reference; 683 | return $this; 684 | } 685 | 686 | /** 687 | * @return DateTime|null 688 | */ 689 | public function getRefundDateTime(): ?DateTime 690 | { 691 | return $this->refundDateTime; 692 | } 693 | 694 | /** 695 | * @return string|null 696 | */ 697 | public function getRefundId(): ?string 698 | { 699 | return $this->refundId; 700 | } 701 | 702 | /** 703 | * @return Refund[] 704 | */ 705 | public function getRefunds(): array 706 | { 707 | return $this->refunds; 708 | } 709 | 710 | /** 711 | * @return DateTime|null 712 | */ 713 | public function getRequestDateTime(): ?DateTime 714 | { 715 | return $this->requestDateTime; 716 | } 717 | 718 | /** 719 | * @return string|null 720 | */ 721 | public function getReturnCode(): ?string 722 | { 723 | return $this->returnCode; 724 | } 725 | 726 | /** 727 | * @return string|null 728 | */ 729 | public function getReturnMessage(): ?string 730 | { 731 | return $this->returnMessage; 732 | } 733 | 734 | /** 735 | * @return string|null 736 | */ 737 | public function getSecurityCode(): ?string 738 | { 739 | return $this->securityCode; 740 | } 741 | 742 | /** 743 | * @param string $securityCode 744 | * 745 | * @return $this 746 | */ 747 | public function setSecurityCode(string $securityCode): static 748 | { 749 | $this->securityCode = $securityCode; 750 | return $this; 751 | } 752 | 753 | /** 754 | * @return string|null 755 | */ 756 | public function getSoftDescriptor(): ?string 757 | { 758 | return $this->softDescriptor; 759 | } 760 | 761 | /** 762 | * @param string $softDescriptor 763 | * 764 | * @return $this 765 | */ 766 | public function setSoftDescriptor(string $softDescriptor): static 767 | { 768 | $this->softDescriptor = $softDescriptor; 769 | return $this; 770 | } 771 | 772 | /** 773 | * @return int|null 774 | */ 775 | public function getStorageCard(): ?int 776 | { 777 | return $this->storageCard; 778 | } 779 | 780 | /** 781 | * @param int $storageCard 782 | * 783 | * @return $this 784 | */ 785 | public function setStorageCard(int $storageCard): static 786 | { 787 | $this->storageCard = $storageCard; 788 | return $this; 789 | } 790 | 791 | /** 792 | * @param string $code 793 | * @param string $departureTax 794 | * 795 | * @return $this 796 | */ 797 | public function iata(string $code, string $departureTax): static 798 | { 799 | return $this->setIata($code, $departureTax); 800 | } 801 | 802 | /** 803 | * @return bool 804 | */ 805 | public function isSubscription(): bool 806 | { 807 | return $this->subscription ?? false; 808 | } 809 | 810 | /** 811 | * @param bool $subscription 812 | * 813 | * @return $this 814 | */ 815 | public function setSubscription(bool $subscription): static 816 | { 817 | $this->subscription = $subscription; 818 | return $this; 819 | } 820 | 821 | /** 822 | * @return ThreeDSecure 823 | */ 824 | public function getThreeDSecure(): ThreeDSecure 825 | { 826 | return $this->threeDSecure ?? new ThreeDSecure(); 827 | } 828 | 829 | /** 830 | * @return string|null 831 | */ 832 | public function getTid(): ?string 833 | { 834 | return $this->tid; 835 | } 836 | 837 | /** 838 | * @param string $tid 839 | * 840 | * @return $this 841 | */ 842 | public function setTid(string $tid): static 843 | { 844 | $this->tid = $tid; 845 | return $this; 846 | } 847 | 848 | /** 849 | * @return ArrayIterator 850 | */ 851 | public function getUrlsIterator(): ArrayIterator 852 | { 853 | return new ArrayIterator($this->urls); 854 | } 855 | 856 | /** 857 | * @param string $softDescriptor 858 | * @param string $paymentFacilitatorID 859 | * @param SubMerchant $subMerchant 860 | * 861 | * @return $this 862 | */ 863 | public function mcc(string $softDescriptor, string $paymentFacilitatorID, SubMerchant $subMerchant): static 864 | { 865 | $this->setSoftDescriptor($softDescriptor); 866 | $this->setPaymentFacilitatorID($paymentFacilitatorID); 867 | $this->setSubMerchant($subMerchant); 868 | 869 | return $this; 870 | } 871 | 872 | /** 873 | * @return SubMerchant|null 874 | */ 875 | public function getSubMerchant(): ?SubMerchant 876 | { 877 | return $this->subMerchant; 878 | } 879 | 880 | /** 881 | * @param SubMerchant $subMerchant 882 | * 883 | * @return $this 884 | */ 885 | public function setSubMerchant(SubMerchant $subMerchant): static 886 | { 887 | $this->subMerchant = $subMerchant; 888 | return $this; 889 | } 890 | 891 | /** 892 | * @return string|null 893 | */ 894 | public function getPaymentFacilitatorID(): ?string 895 | { 896 | return $this->paymentFacilitatorID; 897 | } 898 | 899 | /** 900 | * @param string $paymentFacilitatorID 901 | * 902 | * @return $this 903 | */ 904 | public function setPaymentFacilitatorID(string $paymentFacilitatorID): static 905 | { 906 | $this->paymentFacilitatorID = $paymentFacilitatorID; 907 | return $this; 908 | } 909 | 910 | /** 911 | * @param Device $device 912 | * @param string $onFailure 913 | * @param string $mpi 914 | * @param string $directoryServerTransactionId 915 | * @param string|null $userAgent 916 | * @param int $threeDIndicator 917 | * 918 | * @return $this 919 | */ 920 | public function threeDSecure( 921 | Device $device, 922 | string $onFailure = ThreeDSecure::DECLINE_ON_FAILURE, 923 | string $mpi = ThreeDSecure::MPI_REDE, 924 | string $directoryServerTransactionId = '', 925 | ?string $userAgent = null, 926 | int $threeDIndicator = 2 927 | ): static { 928 | $threeDSecure = new ThreeDSecure($device, $onFailure, $mpi, $userAgent); 929 | $threeDSecure->setThreeDIndicator($threeDIndicator); 930 | $threeDSecure->setDirectoryServerTransactionId($directoryServerTransactionId); 931 | 932 | $this->threeDSecure = $threeDSecure; 933 | 934 | return $this; 935 | } 936 | 937 | /** 938 | * @return int|null 939 | */ 940 | public function getBrandTid(): ?int 941 | { 942 | return $this->brandTid; 943 | } 944 | 945 | /** 946 | * @param int $brandTid 947 | * 948 | * @return $this 949 | */ 950 | public function setBrandTid(int $brandTid): static 951 | { 952 | $this->brandTid = $brandTid; 953 | return $this; 954 | } 955 | 956 | /** 957 | * @return Brand|null 958 | */ 959 | public function getBrand(): ?Brand 960 | { 961 | return $this->brand; 962 | } 963 | 964 | /** 965 | * @param Brand $brand 966 | * 967 | * @return $this 968 | */ 969 | public function setBrand(Brand $brand): static 970 | { 971 | $this->brand = $brand; 972 | return $this; 973 | } 974 | 975 | /** 976 | * @param string $serialized 977 | * 978 | * @return $this 979 | * @throws Exception 980 | */ 981 | public function jsonUnserialize(string $serialized): static 982 | { 983 | $properties = json_decode($serialized); 984 | 985 | if (json_last_error() !== JSON_ERROR_NONE) { 986 | throw new InvalidArgumentException(sprintf('JSON: %s', json_last_error_msg())); 987 | } 988 | 989 | foreach (get_object_vars($properties) as $property => $value) { 990 | if ($property == 'links') { 991 | continue; 992 | } 993 | 994 | match ($property) { 995 | 'refunds' => $this->unserializeRefunds($property, $value), 996 | 'urls' => $this->unserializeUrls($property, $value), 997 | 'capture' => $this->unserializeCapture($property, $value), 998 | 'authorization' => $this->unserializeAuthorization($property, $value), 999 | 'additional' => $this->unserializeAdditional($property, $value), 1000 | 'threeDSecure' => $this->unserializeThreeDSecure($property, $value), 1001 | 'requestDateTime', 'dateTime', 'refundDateTime' => $this->unserializeRequestDateTime($property, $value), 1002 | 'brand' => $this->unserializeBrand($property, $value), 1003 | default => $this->{$property} = $value, 1004 | }; 1005 | } 1006 | 1007 | return $this; 1008 | } 1009 | 1010 | /** 1011 | * @param string $property 1012 | * @param mixed $value 1013 | * @return void 1014 | * @throws Exception 1015 | */ 1016 | private function unserializeRefunds(string $property, mixed $value): void 1017 | { 1018 | if ($property === 'refunds' && is_array($value)) { 1019 | $this->refunds = []; 1020 | 1021 | foreach ($value as $refundValue) { 1022 | /** 1023 | * @var Refund $refund 1024 | */ 1025 | $refund = Refund::create($refundValue); 1026 | 1027 | $this->refunds[] = $refund; 1028 | } 1029 | } 1030 | } 1031 | 1032 | /** 1033 | * @param string $property 1034 | * @param mixed $value 1035 | * @return void 1036 | */ 1037 | private function unserializeUrls(string $property, mixed $value): void 1038 | { 1039 | if ($property === 'urls' && is_array($value)) { 1040 | $this->urls = []; 1041 | 1042 | foreach ($value as $urlValue) { 1043 | $this->urls[] = new Url($urlValue->url, $urlValue->kind); 1044 | } 1045 | } 1046 | } 1047 | 1048 | /** 1049 | * @param string $property 1050 | * @param mixed $value 1051 | * @return void 1052 | * @throws Exception 1053 | */ 1054 | private function unserializeCapture(string $property, mixed $value): void 1055 | { 1056 | if ($property === 'capture' && is_object($value)) { 1057 | /** 1058 | * @var Capture $capture 1059 | */ 1060 | $capture = Capture::create($value); 1061 | 1062 | $this->capture = $capture; 1063 | } 1064 | } 1065 | 1066 | /** 1067 | * @param string $property 1068 | * @param mixed $value 1069 | * @return void 1070 | * @throws Exception 1071 | */ 1072 | private function unserializeAuthorization(string $property, mixed $value): void 1073 | { 1074 | if ($property == 'authorization' && is_object($value)) { 1075 | /** 1076 | * @var Authorization $authorization 1077 | */ 1078 | $authorization = Authorization::create($value); 1079 | 1080 | $this->authorization = $authorization; 1081 | } 1082 | } 1083 | 1084 | /** 1085 | * @param string $property 1086 | * @param mixed $value 1087 | * @return void 1088 | * @throws Exception 1089 | */ 1090 | private function unserializeAdditional(string $property, mixed $value): void 1091 | { 1092 | if ($property == 'additional' && is_object($value)) { 1093 | /** 1094 | * @var Additional $additional 1095 | */ 1096 | $additional = Additional::create($value); 1097 | 1098 | $this->additional = $additional; 1099 | } 1100 | } 1101 | 1102 | /** 1103 | * @param string $property 1104 | * @param mixed $value 1105 | * @return void 1106 | * @throws Exception 1107 | */ 1108 | private function unserializeThreeDSecure(string $property, mixed $value): void 1109 | { 1110 | if ($property == 'threeDSecure' && is_object($value)) { 1111 | /** 1112 | * @var ThreeDSecure $threeDSecure 1113 | */ 1114 | $threeDSecure = ThreeDSecure::create($value); 1115 | 1116 | $this->threeDSecure = $threeDSecure; 1117 | } 1118 | } 1119 | 1120 | /** 1121 | * @param string $property 1122 | * @param mixed $value 1123 | * @return void 1124 | * @throws Exception 1125 | */ 1126 | private function unserializeRequestDateTime(string $property, mixed $value): void 1127 | { 1128 | if ($property == 'requestDateTime' || $property == 'dateTime' || $property == 'refundDateTime') { 1129 | $value = new DateTime($value); 1130 | } 1131 | 1132 | $this->{$property} = $value; 1133 | } 1134 | 1135 | /** 1136 | * @param string $property 1137 | * @param mixed $value 1138 | * @return void 1139 | * @throws Exception 1140 | */ 1141 | private function unserializeBrand(string $property, mixed $value): void 1142 | { 1143 | if ($property == 'brand') { 1144 | /** 1145 | * @var Brand $brand 1146 | */ 1147 | $brand = Brand::create($value); 1148 | 1149 | $this->brand = $brand; 1150 | } 1151 | } 1152 | } 1153 | --------------------------------------------------------------------------------