Skip to content

Commit

Permalink
Merge pull request #89 from PaymentSuite/feature/extra-data
Browse files Browse the repository at this point in the history
Add extra data in paylands
  • Loading branch information
juanjomb authored Jan 29, 2021
2 parents 2d07d9b + 417854d commit ac29302
Show file tree
Hide file tree
Showing 16 changed files with 672 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ composer.lock
phpunit.xml
**/Tests/app/cache/
**/var/cache/
.idea
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"twig/twig": "^2.11",
"stripe/stripe-php": "3.4.0",
"monolog/monolog": "^1.17",
"wearemarketing/paylands-php": "0.1.*",
"wearemarketing/paylands-php": "0.2.*",
"endelwar/gestpayws": "^1.4",
"symfony/expression-language": "^4.3",
"symfony/templating": "^4.3",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the PaymentSuite package.
*
* Copyright (c) 2013-2016 Marc Morera
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <[email protected]>
*/

namespace PaymentSuite\PaylandsBundle\Exception;

use PaymentSuite\PaymentCoreBundle\Exception\PaymentException;

/**
* Class InvalidValueException
* @author Juanjo Martínez <[email protected]>
*/
class InvalidValueException extends \InvalidArgumentException
{
public static function create(string $class, string $value): self
{
return new self(sprintf('%s is not a valid value for %s', $value, $class));
}
}
60 changes: 60 additions & 0 deletions src/PaymentSuite/PaylandsBundle/Services/ExtraDataBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php


namespace PaymentSuite\PaylandsBundle\Services;

use PaymentSuite\PaylandsBundle\Util\Address;
use PaymentSuite\PaylandsBundle\Util\ExtraData;
use PaymentSuite\PaylandsBundle\Util\Profile;

/**
* @author Juanjo Martínez <[email protected]>
*/
final class ExtraDataBuilder
{
private $profile;

private $address;

private $shippingAddress;

private $billingAddress;

public function withProfile(Profile $profile): self
{
$this->profile = $profile;

return $this;
}

public function withAddress(Address $address): self
{
$this->address = $address;

return $this;
}

public function withShippingAddress(Address $address): self
{
$this->shippingAddress = $address;

return $this;
}

public function withBillingAddress(Address $address): self
{
$this->billingAddress = $address;

return $this;
}

public function build(): ExtraData
{
return ExtraData::create(
$this->profile,
$this->address,
$this->shippingAddress,
$this->billingAddress
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace PaymentSuite\PaylandsBundle\Services\Interfaces;

use PaymentSuite\PaylandsBundle\Util\ExtraData;

interface ExtraDataBuilderAwareInterface
{
public function buildExtraData(): ExtraData;
}
10 changes: 8 additions & 2 deletions src/PaymentSuite/PaylandsBundle/Services/PaylandsApiAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use PaymentSuite\PaylandsBundle\Exception\CardNotFoundException;
use PaymentSuite\PaylandsBundle\PaylandsMethod;
use PaymentSuite\PaylandsBundle\Services\Interfaces\ExtraDataBuilderAwareInterface;
use PaymentSuite\PaymentCoreBundle\Services\Interfaces\PaymentBridgeInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use WAM\Paylands\ClientInterface;
Expand Down Expand Up @@ -73,11 +74,16 @@ public function __construct(
*/
public function createTransaction(PaylandsMethod $paymentMethod)
{
$extraData = $this->paymentBridge instanceof ExtraDataBuilderAwareInterface
? $this->paymentBridge->buildExtraData()->toArray()
: [];

$paymentOrder = $this->client->createPayment(
$paymentMethod->getCustomerExternalId(),
$this->paymentBridge->getAmount(),
(string)$this->paymentBridge->getOrder(),
$this->currencyServiceResolver->getService()
$this->currencyServiceResolver->getService(),
$extraData
);

$transaction = $this->client->directPayment(
Expand Down Expand Up @@ -117,4 +123,4 @@ public function validateCard(PaylandsMethod $paymentMethod)
$paymentMethod->getCustomerExternalId()
));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/*
* This file is part of the PaymentSuite package.
*
* Copyright (c) 2013-2016 Marc Morera
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <[email protected]>
*/

namespace PaymentSuite\PaylandsBundle\Tests\Services;

use PaymentSuite\PaylandsBundle\Services\ExtraDataBuilder;
use PaymentSuite\PaylandsBundle\Util\Address;
use PaymentSuite\PaylandsBundle\Util\CountryCode;
use PaymentSuite\PaylandsBundle\Util\DocumentIdentificationIssuerType;
use PaymentSuite\PaylandsBundle\Util\DocumentIdentificationType;
use PaymentSuite\PaylandsBundle\Util\Profile;
use PHPUnit\Framework\TestCase;

/**
* Class PaylandsCurrencyServiceResolverTest.
*
* @author WAM Team <[email protected]>
*/
class ExtraDataBuilderTest extends TestCase
{
public function testBuildWithProfile()
{
$builder = new ExtraDataBuilder();

$extraData = $builder->withProfile(
Profile::create(
'John',
'Doe',
'John Doe',
'[email protected]',
DocumentIdentificationIssuerType::create('STATE_GOVERNMENT'),
DocumentIdentificationType::create('RESIDENCE_CARD')
)
)->build();

$this->assertCount(1, $extraData->toArray());
$this->assertEquals('John', $extraData->toArray()['profile']['first_name']);
$this->assertEquals('Doe', $extraData->toArray()['profile']['last_name']);
$this->assertEquals('John Doe', $extraData->toArray()['profile']['cardholder_name']);
$this->assertEquals('[email protected]', $extraData->toArray()['profile']['email']);
$this->assertEquals('STATE_GOVERNMENT', (string)$extraData->toArray()['profile']['document_identification_issuer_type']);
$this->assertEquals('RESIDENCE_CARD', (string)$extraData->toArray()['profile']['document_identification_type']);
}

/**
* @dataProvider getAddresses
*/
public function testBuildWithAddress(string $builderMethod, string $extraDataIndex)
{
$builder = new ExtraDataBuilder();

$extraData = $builder->$builderMethod(Address::create('Valencia', CountryCode::create('ESP'), 'Calle Invent', '46010', 'Valencia'))->build();

$this->assertCount(1, $extraData->toArray());
$this->assertEquals('Valencia', $extraData->toArray()[$extraDataIndex]['city']);
$this->assertEquals('ESP', $extraData->toArray()[$extraDataIndex]['country']);
$this->assertEquals('Calle Invent', $extraData->toArray()[$extraDataIndex]['address1']);
$this->assertEquals('46010', $extraData->toArray()[$extraDataIndex]['zip_code']);
$this->assertEquals('Valencia', $extraData->toArray()[$extraDataIndex]['state_code']);
}

public function getAddresses()
{
return [
'Simple address' => ['withAddress', 'address'],
'Shipping address' => ['withShippingAddress', 'shipping_address'],
'Billing address' => ['withBillingAddress', 'billing_address'],
];
}
}
79 changes: 79 additions & 0 deletions src/PaymentSuite/PaylandsBundle/Util/Address.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php


namespace PaymentSuite\PaylandsBundle\Util;

use PaymentSuite\PaylandsBundle\Util\Interfaces\ArrayableInterface;

/**
* @author Juanjo Martínez <[email protected]>
*/
final class Address implements ArrayableInterface
{
private $city;

private $country;

private $address1;

private $address2;

private $address3;

private $zipCode;

private $stateCode;

public function __construct(
string $city,
CountryCode $country,
string $address1,
string $zipCode,
string $stateCode,
string $address2 = null,
string $address3 = null
)
{
$this->city = $city;
$this->country = $country;
$this->address1 = $address1;
$this->zipCode = $zipCode;
$this->stateCode = $stateCode;
$this->address2 = $address2;
$this->address3 = $address3;
}

public static function create(
string $city,
CountryCode $country,
string $address1,
string $zipCode,
string $stateCode,
string $address2 = null,
string $address3 = null
): self
{
return new self(
$city,
$country,
$address1,
$zipCode,
$stateCode,
$address2,
$address3
);
}

public function toArray(): array
{
return array_filter([
'city' => $this->city,
'country' => (string) $this->country,
'address1' => $this->address1,
'zip_code' => $this->zipCode,
'state_code' => $this->stateCode,
'address2' => $this->address2,
'address3' => $this->address3,
]);
}
}
54 changes: 54 additions & 0 deletions src/PaymentSuite/PaylandsBundle/Util/CountryCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace PaymentSuite\PaylandsBundle\Util;

use PaymentSuite\PaylandsBundle\Exception\InvalidValueException;

/**
* @author Juanjo Martínez <[email protected]>
*/
final class CountryCode
{
const COUNTRY_CODES = [
'ABW','AFG','AGO','AIA','ALA','ALB','AND','ARE','ARG','ARM','ASM','ATA','ATF','ATG','AUS','AUT','AZE','BDI',
'BEL','BEN','BES','BFA','BGD','BGR','BHR','BHS','BIH','BLM','BLR','BLZ','BMU','BOL','BRA','BRB','BRN','BTN',
'BVT','BWA','CAF','CAN','CCK','CHE','CHL','CHN','CIV','CMR','COD','COG','COK','COL','COM','CPV','CRI','CUB',
'CUW','CXR','CYM','CYP','CZE','DEU','DJI','DMA','DNK','DOM','DZA','ECU','EGY','ERI','ESH','ESP','EST','ETH',
'FIN','FJI','FLK','FRA','FRO','FSM','GAB','GBR','GEO','GGY','GHA','GIB','GIN','GLP','GMB','GNB','GNQ','GRC',
'GRD','GRL','GTM','GUF','GUM','GUY','HKG','HMD','HND','HRV','HTI','HUN','IDN','IMN','IND','IOT','IRL','IRN',
'IRQ','ISL','ISR','ITA','JAM','JEY','JOR','JPN','KAZ','KEN','KGZ','KHM','KIR','KNA','KOR','KWT','LAO','LBN',
'LBR','LBY','LCA','LIE','LKA','LSO','LTU','LUX','LVA','MAC','MAF','MAR','MCO','MDA','MDG','MDV','MEX','MHL',
'MKD','MLI','MLT','MMR','MNE','MNG','MNP','MOZ','MRT','MSR','MTQ','MUS','MWI','MYS','MYT','NAM','NCL','NER',
'NFK','NGA','NIC','NIU','NLD','NOR','NPL','NRU','NZL','OMN','PAK','PAN','PCN','PER','PHL','PLW','PNG','POL',
'PRI','PRK','PRT','PRY','PSE','PYF','QAT','REU','ROU','RUS','RWA','SAU','SDN','SEN','SGP','SGS','SHN','SJM',
'SLB','SLE','SLV','SMR','SOM','SPM','SRB','SSD','STP','SUR','SVK','SVN','SWE','SWZ','SXM','SYC','SYR','TCA',
'TCD','TGO','THA','TJK','TKL','TKM','TLS','TON','TTO','TUN','TUR','TUV','TWN','TZA','UGA','UKR','UMI','URY',
'USA','UZB','VAT','VCT','VEN','VGB','VIR','VNM','VUT','WLF','WSM','YEM','ZAF','ZMB','ZWE'
];

private $value;

/**
* CountryCode constructor.
* @param $value
*/
public function __construct(string $value)
{
$this->value = $value;
}

public static function create(string $value): self
{
if(!in_array($value, self::COUNTRY_CODES))
{
throw new InvalidValueException(self::class, $value);
}

return new self($value);
}

public function __toString()
{
return $this->value;
}
}
Loading

0 comments on commit ac29302

Please sign in to comment.