Skip to content

Commit

Permalink
[CC-1377] Add Paypage V2:
Browse files Browse the repository at this point in the history
- update methodConfig
- Add method to easier add method config.
  • Loading branch information
Ryouzanpaku committed Aug 20, 2024
1 parent d362bff commit c99844b
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 9 deletions.
25 changes: 25 additions & 0 deletions src/Resources/EmbeddedResources/Paypage/PaymentMethodConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class PaymentMethodConfig extends AbstractUnzerResource
protected ?string $name = null;
protected ?string $checkoutType = null; // paypal only.

protected ?bool $credentialOnFile = null; // card only.
protected ?string $exemption = null; // card only.

/**
* @param bool|null $enabled
* @param int|null $order
Expand Down Expand Up @@ -67,4 +70,26 @@ public function setCheckoutType(?string $checkoutType): PaymentMethodConfig
$this->checkoutType = $checkoutType;
return $this;
}

public function getCredentialOnFile(): ?bool
{
return $this->credentialOnFile;
}

public function setCredentialOnFile(?bool $credentialOnFile): PaymentMethodConfig
{
$this->credentialOnFile = $credentialOnFile;
return $this;
}

public function getExemption(): ?string
{
return $this->exemption;
}

public function setExemption(?string $exemption): PaymentMethodConfig
{
$this->exemption = $exemption;
return $this;
}
}
45 changes: 45 additions & 0 deletions src/Resources/EmbeddedResources/Paypage/PaymentMethodsConfigs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,41 @@
namespace UnzerSDK\Resources\EmbeddedResources\Paypage;

use UnzerSDK\Resources\AbstractUnzerResource;
use UnzerSDK\Resources\PaymentTypes\BasePaymentType;
use UnzerSDK\Services\ResourceNameService;

class PaymentMethodsConfigs extends AbstractUnzerResource
{
const PAYPAGE_TYPE_NAME_MAPPING = [
'card' => 'cards',
'eps' => 'eps',
'payu' => 'payu',
'postfinanceefinance' => 'pfefinance',
'postfinancecard' => 'pfcard'
];

protected ?PaymentMethodConfig $default = null;
protected ?string $preselectedMethod = null;

/** @var PaymentMethodConfig[] */
protected ?array $methodConfigs = null;

/**
* Takes Class name of a payment type and maps it to a method name used in Paypage service if needed.
*
* @param BasePaymentType $paymentType Full or short class name of a payment type.
* @param PaymentMethodConfig $methodConfig Configuration object for the payment method.
* @return $this
*/
public function addMethodConfig(string $paymentType, PaymentMethodConfig $methodConfig): PaymentMethodsConfigs
{
$classShortName = ResourceNameService::getClassShortName($paymentType);
$mappedName = $this->mapClassToMethodName($classShortName);

$this->methodConfigs[lcfirst($mappedName)] = $methodConfig;
return $this;
}

public function expose()
{
$exposeArray = parent::expose();
Expand Down Expand Up @@ -62,4 +88,23 @@ public function setMethodConfigs(?array $methodConfigs): PaymentMethodsConfigs
$this->methodConfigs = $methodConfigs;
return $this;
}

/**
* @param array $replace
* @param array $with
* @param string $classShortName
* @return array|string|string[]|null
*/
public function mapClassToMethodName(string $classShortName)
{
$typeMapping = self::PAYPAGE_TYPE_NAME_MAPPING;

// keys are in lower case.
$searchKey = strtolower($classShortName);
if (isset($typeMapping[$searchKey])) {
return $typeMapping[$searchKey];
}

return $classShortName;
}
}
2 changes: 1 addition & 1 deletion test/integration/Resources/AuthTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function verifyTokenCanBeCreated()
// Validate expiry time with default buffer of 60 seconds.
$this->assertTrue(JwtService::validateExpiryTime($jwtToken));
// Validate expiry time with buffer set to have 1 second remaining.
$this->assertTrue(JwtService::validateExpiryTime($jwtToken, 60 * 7 - 1));
$this->assertTrue(JwtService::validateExpiryTime($jwtToken, 60 * 7 - 3));

// Validate expiry time with buffer set to have 0 second remaining.
$this->assertFalse(JwtService::validateExpiryTime($jwtToken, 60 * 7));
Expand Down
66 changes: 58 additions & 8 deletions test/integration/Resources/PaypageV2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,41 @@

namespace UnzerSDK\test\integration\Resources;

use UnzerSDK\Constants\ExemptionType;
use UnzerSDK\Resources\EmbeddedResources\Paypage\PaymentMethodConfig;
use UnzerSDK\Resources\EmbeddedResources\Paypage\PaymentMethodsConfigs;
use UnzerSDK\Resources\EmbeddedResources\Paypage\Resources;
use UnzerSDK\Resources\EmbeddedResources\Paypage\Style;
use UnzerSDK\Resources\EmbeddedResources\Paypage\Urls;
use UnzerSDK\Resources\EmbeddedResources\RiskData;
use UnzerSDK\Resources\Metadata;
use UnzerSDK\Resources\PaymentTypes\Alipay;
use UnzerSDK\Resources\PaymentTypes\Applepay;
use UnzerSDK\Resources\PaymentTypes\Bancontact;
use UnzerSDK\Resources\PaymentTypes\Card;
use UnzerSDK\Resources\PaymentTypes\EPS;
use UnzerSDK\Resources\PaymentTypes\Googlepay;
use UnzerSDK\Resources\PaymentTypes\Ideal;
use UnzerSDK\Resources\PaymentTypes\Klarna;
use UnzerSDK\Resources\PaymentTypes\PaylaterDirectDebit;
use UnzerSDK\Resources\PaymentTypes\PaylaterInstallment;
use UnzerSDK\Resources\PaymentTypes\PaylaterInvoice;
use UnzerSDK\Resources\PaymentTypes\Paypal;
use UnzerSDK\Resources\PaymentTypes\PayU;
use UnzerSDK\Resources\PaymentTypes\PostFinanceCard;
use UnzerSDK\Resources\PaymentTypes\PostFinanceEfinance;
use UnzerSDK\Resources\PaymentTypes\Prepayment;
use UnzerSDK\Resources\PaymentTypes\Przelewy24;
use UnzerSDK\Resources\PaymentTypes\SepaDirectDebit;
use UnzerSDK\Resources\PaymentTypes\Twint;
use UnzerSDK\Resources\PaymentTypes\Wechatpay;
use UnzerSDK\Resources\V2\Paypage;
use UnzerSDK\test\BaseIntegrationTest;

/**
* @group CC-1309
* @group CC-1377
* @backupStaticAttributes enabled
*/
class PaypageV2Test extends BaseIntegrationTest
{
Expand Down Expand Up @@ -109,7 +131,6 @@ public function createPaypageWithOptionalStringProperties()
$this->getUnzerObject()->createPaypage($paypage);

$this->assertCreatedPaypage($paypage);
//TODO: fetch paypage and compare properties.
}

/**
Expand Down Expand Up @@ -225,30 +246,59 @@ public function assertCreatedPaypage(Paypage $paypage): void

public function paymentMethodsConfigsDataProvider()
{
$paypalEnabledConfig = new PaymentMethodConfig(true, 1);
$enabledConfig = new PaymentMethodConfig(true, 1);
$cardConfig = (new PaymentMethodConfig(true, 1))
->setCredentialOnFile(true)
->setExemption(ExemptionType::LOW_VALUE_PAYMENT);

$withDefaultEnabled = (new PaymentMethodsConfigs())
->setDefault(
(new PaymentMethodConfig())->setEnabled(true)
);
->setDefault((new PaymentMethodConfig())->setEnabled(true));

$withDefaultDisabled = (new PaymentMethodsConfigs())
->setDefault(
(new PaymentMethodConfig())->setEnabled(false)
)->setMethodConfigs(['paypal' => $paypalEnabledConfig]);
)->setMethodConfigs(['paypal' => $enabledConfig]);

$withMethodConfigs = new PaymentMethodsConfigs();
$withMethodConfigs->setMethodConfigs([
'paypal' => $paypalEnabledConfig
'paypal' => $enabledConfig
]);
$paymentMethodsConfigs = (new PaymentMethodsConfigs())->setPreselectedMethod('cards');

$withCardSpecificConfig = (new PaymentMethodsConfigs())->setPreselectedMethod('cards')
->addMethodConfig(Card::class, $cardConfig);

$withClassNames = (new PaymentMethodsConfigs())->setPreselectedMethod('cards')
->setDefault((new PaymentMethodConfig())->setEnabled(false))
->addMethodConfig(Card::class, $cardConfig)
->addMethodConfig(Paypal::class, $enabledConfig)
->addMethodConfig(PaylaterInstallment::class, $enabledConfig)
->addMethodConfig(Googlepay::class, $enabledConfig)
->addMethodConfig(Applepay::class, $enabledConfig)
->addMethodConfig(Klarna::class, $enabledConfig)
->addMethodConfig(SepaDirectDebit::class, $enabledConfig)
->addMethodConfig(EPS::class, $enabledConfig)
->addMethodConfig(PaylaterInvoice::class, $enabledConfig)
->addMethodConfig(PaylaterDirectDebit::class, $enabledConfig)
->addMethodConfig(Prepayment::class, $enabledConfig)
->addMethodConfig(PayU::class, $enabledConfig)
->addMethodConfig(Ideal::class, $enabledConfig)
->addMethodConfig(Przelewy24::class, $enabledConfig)
->addMethodConfig(Alipay::class, $enabledConfig)
->addMethodConfig(Wechatpay::class, $enabledConfig)
->addMethodConfig(Bancontact::class, $enabledConfig)
->addMethodConfig(PostFinanceEfinance::class, $enabledConfig)
->addMethodConfig(PostFinanceCard::class, $enabledConfig)
->addMethodConfig(Twint::class, $enabledConfig);

return [
'empty' => [new PaymentMethodsConfigs()],
'withDefaultEnabled' => [$withDefaultEnabled],
'withDefaultDisabled' => [$withDefaultDisabled],
'withPreselectedMethod' => [$paymentMethodsConfigs],
'withMethodConfigs' => [$withMethodConfigs]
'withMethodConfigs' => [$withMethodConfigs],
'withCardSpecificConfig' => [$withCardSpecificConfig],
'withClassNames' => [$withClassNames]
];
}
}

0 comments on commit c99844b

Please sign in to comment.