Skip to content

Commit

Permalink
PagBank 😍 Magento
Browse files Browse the repository at this point in the history
  • Loading branch information
elisei committed Jan 19, 2024
1 parent 8525162 commit fef6ee6
Show file tree
Hide file tree
Showing 23 changed files with 1,105 additions and 56 deletions.
55 changes: 55 additions & 0 deletions Api/Data/ThreeDsSessionDataInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* PagBank Payment Magento Module.
*
* Copyright © 2023 PagBank. All rights reserved.
*
* @author Bruno Elisei <brunoelisei@o2ti.com>
* @license See LICENSE for license details.
*/

declare(strict_types=1);

namespace PagBank\PaymentMagento\Api\Data;

/**
* Interface 3ds Session.
*
* @api
*
* @since 100.0.1
*/
interface ThreeDsSessionDataInterface
{
/**
* Return Three Ds Session.
*
* @return string|null
*/
public function getSessionId();

/**
* Set Three Ds Session Id.
*
* @param string $sessionId
*
* @return $this
*/
public function setSessionId($sessionId);

/**
* Return Three Ds Expires At.
*
* @return string|null
*/
public function getExpiresAt();

/**
* Set Three Ds Session Expires At.
*
* @param string $expiresAt
*
* @return $this
*/
public function setExpiresAt($expiresAt);
}
30 changes: 30 additions & 0 deletions Api/ThreeDsSessionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* PagBank Payment Magento Module.
*
* Copyright © 2023 PagBank. All rights reserved.
*
* @author Bruno Elisei <brunoelisei@o2ti.com>
* @license See LICENSE for license details.
*/

declare(strict_types=1);

namespace PagBank\PaymentMagento\Api;

use PagBank\PaymentMagento\Api\Data\ThreeDsSessionDataInterface;

/**
* Interface for obtaining session for 3ds.
*
* @api
*/
interface ThreeDsSessionInterface
{
/**
* Get 3ds Session.
*
* @return \PagBank\PaymentMagento\Api\Data\ThreeDsSessionDataInterface
*/
public function getSession(): ThreeDsSessionDataInterface;
}
91 changes: 91 additions & 0 deletions Gateway/Config/ConfigCc.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ class ConfigCc extends PaymentConfig
*/
public const PAYMENT_ACTION = 'payment_action';

/**
* @const string
*/
public const USE_THREE_DS_AUTH = 'use_three_ds';

/**
* @const string
*/
public const REJECT_NOT_AUTH = 'reject_not_auth';

/**
* @const string
*/
public const INSTRUCTION_THREE_DS = 'instruction_three_ds';

/**
* @var ScopeConfigInterface
*/
Expand Down Expand Up @@ -183,6 +198,82 @@ public function hasPhoneCapture($storeId = null): bool
);
}

/**
* Has 3ds Auth.
*
* @param int|null $storeId
*
* @return bool
*/
public function hasThreeDsAuth($storeId = null): bool
{
$pathPattern = 'payment/%s/%s';

return (bool) $this->scopeConfig->getValue(
sprintf($pathPattern, self::METHOD, self::USE_THREE_DS_AUTH),
ScopeInterface::SCOPE_STORE,
$storeId
);
}

/**
* Get Instruction for 3ds.
*
* @param string|null $storeId
*
* @return string
*/
public function getInstructionForThreeDs($storeId = null): string
{
$pathPattern = 'payment/%s/%s';

return $this->scopeConfig->getValue(
sprintf($pathPattern, self::METHOD, self::INSTRUCTION_THREE_DS),
ScopeInterface::SCOPE_STORE,
$storeId
);
}

/**
* Has 3ds Env.
*
* @param int|null $storeId
*
* @return string
*/
public function getThreeDsEnv($storeId = null): string
{
$env = $this->scopeConfig->getValue(
'payment/pagbank_paymentmagento/environment',
ScopeInterface::SCOPE_STORE,
$storeId
);

if ($env === 'sandbox') {
return 'SANDBOX';
}

return 'PROD';
}

/**
* Has Reject Not Auth.
*
* @param int|null $storeId
*
* @return bool
*/
public function hasRejectNotAuth($storeId = null): bool
{
$pathPattern = 'payment/%s/%s';

return (bool) $this->scopeConfig->getValue(
sprintf($pathPattern, self::METHOD, self::REJECT_NOT_AUTH),
ScopeInterface::SCOPE_STORE,
$storeId
);
}

/**
* Has Capture.
*
Expand Down
92 changes: 92 additions & 0 deletions Gateway/Request/CreditCard/ThreeDsAuthDataRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* PagBank Payment Magento Module.
*
* Copyright © 2023 PagBank. All rights reserved.
*
* @author Bruno Elisei <brunoelisei@o2ti.com>
* @license See LICENSE for license details.
*/

namespace PagBank\PaymentMagento\Gateway\Request\CreditCard;

use Magento\Payment\Gateway\Data\PaymentDataObject;
use Magento\Payment\Gateway\Helper\SubjectReader;
use Magento\Payment\Gateway\Request\BuilderInterface;
use Magento\Payment\Model\InfoInterface;
use PagBank\PaymentMagento\Gateway\Request\ChargesDataRequest;
use PagBank\PaymentMagento\Gateway\Request\CreditCard\PaymentCcDataRequest;

/**
* Class 3ds Auth Data Request - Payment structure for credit card with 3ds authentication.
*/
class ThreeDsAuthDataRequest implements BuilderInterface
{
/**
* Authentication Method - block name.
*/
public const AUTHENTICATION_METHOD = 'authentication_method';

/**
* Type - Block name.
*/
public const TYPE = 'type';

/**
* Id - Block name.
*/
public const ID = 'id';

/**
* Type Value - Value.
*/
public const TYPE_VALUE = 'THREEDS';

/**
* Build.
*
* @param array $buildSubject
*
* @SuppressWarnings(PHPMD.StaticAccess)
*/
public function build(array $buildSubject)
{
$result = [];

/** @var PaymentDataObject $paymentDO * */
$paymentDO = SubjectReader::readPayment($buildSubject);

/** @var InfoInterface $payment * */
$payment = $paymentDO->getPayment();

$result[ChargesDataRequest::CHARGES][] = [
PaymentCcDataRequest::PAYMENT_METHOD => $this->getDataThreeDs($payment),
];

return $result;
}

/**
* Get data for 3ds.
*
* @param InfoInterface $payment
*
* @return array
*/
public function getDataThreeDs($payment)
{
$dataThreeDs = [];
$sessionID = $payment->getAdditionalInformation('three_ds_session');

if ($sessionID) {
$dataThreeDs = [
self::AUTHENTICATION_METHOD => [
self::TYPE => self::TYPE_VALUE,
self::ID => $sessionID,
],
];
}

return $dataThreeDs;
}
}
2 changes: 1 addition & 1 deletion Model/Api/ConsultPSInstallments.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function getPagBankInstallments($storeId, $creditCardBin, $amount)
$response = $list;
}

if (!$client->request()->isSuccessful()) {
if (!isset($response[0])) {
$response[0] = [
'installments' => 1,
'installment_value' => $amount,
Expand Down
63 changes: 63 additions & 0 deletions Model/Api/Data/Session.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* PagBank Payment Magento Module.
*
* Copyright © 2023 PagBank. All rights reserved.
*
* @author Bruno Elisei <brunoelisei@o2ti.com>
* @license See LICENSE for license details.
*/

declare(strict_types=1);

namespace PagBank\PaymentMagento\Model\Api\Data;

use PagBank\PaymentMagento\Api\Data\ThreeDsSessionDataInterface;

/**
* Class 3ds Session - Model data.
*/
class Session implements ThreeDsSessionDataInterface
{
/**
* @var string $sessionId
*/
protected $sessionId;

/**
* @var string $expiresAt
*/
protected $expiresAt;

/**
* @inheritdoc
*/
public function getSessionId()
{
return $this->sessionId;
}

/**
* @inheritdoc
*/
public function setSessionId($sessionId)
{
return $this->sessionId = $sessionId;
}

/**
* @inheritdoc
*/
public function getExpiresAt()
{
return $this->expiresAt;
}

/**
* @inheritdoc
*/
public function setExpiresAt($expiresAt)
{
return $this->expiresAt = $expiresAt;
}
}
Loading

0 comments on commit fef6ee6

Please sign in to comment.