Skip to content

Commit

Permalink
Merge pull request vindi#11 from bizcommerce/master
Browse files Browse the repository at this point in the history
feat: Do not allow purchases with installments greater than the plan installments
  • Loading branch information
cedran authored Apr 17, 2024
2 parents 09e9ee3 + 972bf74 commit 44c2417
Show file tree
Hide file tree
Showing 24 changed files with 367 additions and 185 deletions.
10 changes: 0 additions & 10 deletions Api/PixConfigurationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,10 @@

interface PixConfigurationInterface
{

const PATH_ENABLED_DOCUMENT = 'payment/vindi_pix/enabled_document';
const PATH_INFO_MESSAGE = 'checkout/vindi_pix/info_message';
const PATH_INFO_MESSAGE_ONEPAGE_SUCCESS = 'checkout/vindi_pix/info_message_onepage_success';
const PATH_QR_CODE_WARNING_MESSAGE = 'checkout/vindi_pix/qr_code_warning_message';

/**
* @param string $scopeType
* @param string|null $scopeCode
*
* @return bool
*/
public function isEnabledDocument(string $scopeType = ScopeInterface::SCOPE_STORE, string $scopeCode = null);

/**
* @param string $scopeType
* @param string|null $scopeCode
Expand Down
14 changes: 10 additions & 4 deletions Block/Info/BankSlipPix.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Magento\Backend\Block\Template\Context;
use Magento\Framework\Pricing\Helper\Data;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Payment\Block\Info;
use Vindi\Payment\Api\PixConfigurationInterface;
use Vindi\Payment\Model\Payment\PaymentMethod;
Expand Down Expand Up @@ -32,6 +33,9 @@ class BankSlipPix extends Info
*/
protected $pixConfiguration;

/** @var TimezoneInterface */
protected $timezone;

/**
* @var Json
*/
Expand All @@ -53,12 +57,14 @@ public function __construct(
Context $context,
PixConfigurationInterface $pixConfiguration,
Json $json,
TimezoneInterface $timezone,
array $data = []
) {
parent::__construct($context, $data);
$this->paymentMethod = $paymentMethod;
$this->currency = $currency;
$this->pixConfiguration = $pixConfiguration;
$this->timezone = $timezone;
$this->json = $json;
}

Expand Down Expand Up @@ -164,16 +170,16 @@ protected function isValidToPayment($timestampMaxDays)
return false;
}

return $timestampMaxDays >= strtotime("now");
return $timestampMaxDays >= $this->timezone->scopeTimeStamp();
}

/**
* @return mixed
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function getMaxDaysToPayment()
protected function getMaxDaysToPayment(): string
{
return $this->getOrder()->getPayment()->getAdditionalInformation('max_days_to_keep_waiting_payment');
return (string) $this->getOrder()->getPayment()->getAdditionalInformation('max_days_to_keep_waiting_payment');
}

public function getPrintUrl()
Expand Down
22 changes: 10 additions & 12 deletions Block/Info/Pix.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Magento\Backend\Block\Template\Context;
use Magento\Framework\Pricing\Helper\Data;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Payment\Block\Info;
use Vindi\Payment\Api\PixConfigurationInterface;
use Vindi\Payment\Model\Payment\PaymentMethod;
Expand Down Expand Up @@ -33,33 +34,30 @@ class Pix extends Info
*/
protected $pixConfiguration;

/** @var TimezoneInterface */
protected $timezone;

/**
* @var Json
*/
protected $json;

protected $paymentMethod;

/**
* @param PaymentMethod $paymentMethod
* @param Data $currency
* @param Context $context
* @param PixConfigurationInterface $pixConfiguration
* @param Json $json
* @param array $data
*/
public function __construct(
PaymentMethod $paymentMethod,
Data $currency,
Context $context,
PixConfigurationInterface $pixConfiguration,
TimezoneInterface $timezone,
Json $json,
array $data = []
) {
parent::__construct($context, $data);
$this->paymentMethod = $paymentMethod;
$this->currency = $currency;
$this->pixConfiguration = $pixConfiguration;
$this->timezone = $timezone;
$this->json = $json;
}

Expand Down Expand Up @@ -167,15 +165,15 @@ protected function isValidToPayment($timestampMaxDays)
return false;
}

return $timestampMaxDays >= strtotime("now");
return $timestampMaxDays >= $this->timezone->scopeTimeStamp();
}

/**
* @return mixed
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function getMaxDaysToPayment()
protected function getMaxDaysToPayment(): string
{
return $this->getOrder()->getPayment()->getAdditionalInformation('max_days_to_keep_waiting_payment');
return (string) $this->getOrder()->getPayment()->getAdditionalInformation('max_days_to_keep_waiting_payment');
}
}
4 changes: 2 additions & 2 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ public static function sanitizeItemSku($code)
"[^a-zA-Z0-9-]",
"-",
strtr(
utf8_decode(trim(preg_replace('/[ -]+/', '-', $code))),
utf8_decode("áàãâéêíóôõúüñçÁÀÃÂÉÊÍÓÔÕÚÜÑÇ"),
mb_convert_encoding(trim(preg_replace('/[ -]+/', '-', $code)), 'ISO-8859-1', 'UTF-8'),
mb_convert_encoding("áàãâéêíóôõúüñçÁÀÃÂÉÊÍÓÔÕÚÜÑÇ", 'ISO-8859-1', 'UTF-8'),
"aaaaeeiooouuncAAAAEEIOOOUUNC-"
)
));
Expand Down
9 changes: 0 additions & 9 deletions Helper/PixConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@

class PixConfiguration extends AbstractHelper implements PixConfigurationInterface
{

/**
* @inheritDoc
*/
public function isEnabledDocument(string $scopeType = ScopeInterface::SCOPE_STORE, string $scopeCode = null)
{
return $this->scopeConfig->isSetFlag(static::PATH_ENABLED_DOCUMENT, $scopeType, $scopeCode);
}

/**
* @inheritDoc
*/
Expand Down
49 changes: 49 additions & 0 deletions Model/Bankslip/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Vindi\Payment\Model\Bankslip;

use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Customer\Model\Session as CustomerSession;


/**
* Class ConfigProvider
* @package Vindi\Payment\Model
*/
class ConfigProvider implements ConfigProviderInterface
{
/**
* @var CustomerSession $customerSession
*/
protected $customerSession;

/**
* @param CustomerSession $customerSession
*/
public function __construct(
CustomerSession $customerSession
) {
$this->customerSession = $customerSession;
}

/**
* {@inheritdoc}
*/
public function getConfig()
{
$customerTaxvat = '';
$customer = $this->customerSession->getCustomer();
if ($customer && $customer->getTaxvat()) {
$customerTaxvat = $customer->getTaxvat();
}

return [
'payment' => [
'vindi_bankslip' => [
'enabledDocument' => true,
'customer_taxvat' => $customerTaxvat
]
]
];
}
}
8 changes: 3 additions & 5 deletions Model/BankslipPix/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<?php

namespace Vindi\Payment\Model\Pix;

namespace Vindi\Payment\Model\BankslipPix;

use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Customer\Model\Session as CustomerSession;
use Vindi\Payment\Api\PixConfigurationInterface;


/**
* Class ConfigProvider
* @package Vindi\Payment\Model
Expand Down Expand Up @@ -50,8 +48,8 @@ public function getConfig()

return [
'payment' => [
'vindi_pix' => [
'enabledDocument' => $this->pixConfiguration->isEnabledDocument(),
'vindi_bankslippix' => [
'enabledDocument' => true,
'info_message' => $this->pixConfiguration->getInfoMessage(),
'customer_taxvat' => $customerTaxvat
]
Expand Down
13 changes: 11 additions & 2 deletions Model/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,22 @@ private function getAttributeValue(ProductInterface $product, $attribute = '')

public function getPaymentProfiles(): array
{
$paymentProfiles = [];
if ($this->customerSession->isLoggedIn()) {
$customerId = $this->customerSession->getCustomerId();
$this->paymentProfileCollection->addFieldToFilter('customer_id', $customerId);
return $this->paymentProfileCollection->getItems();
$this->paymentProfileCollection->addFieldToFilter('cc_type', ['neq' => '']);
$this->paymentProfileCollection->addFieldToFilter('cc_type', ['neq' => null]);
foreach ($this->paymentProfileCollection as $paymentProfile) {
$paymentProfiles[] = [
'id' => $paymentProfile->getId(),
'card_number' => (string) $paymentProfile->getCcLast4(),
'card_type' => (string) $paymentProfile->getCcType()
];
}
}

return [];
return $paymentProfiles;
}

/**
Expand Down
31 changes: 19 additions & 12 deletions Model/Payment/AbstractMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,32 +81,32 @@ abstract class AbstractMethod extends OriginAbstractMethod
/**
* @var ProductManagementInterface
*/
private $productManagement;
protected $productManagement;

/**
* @var \Vindi\Payment\Helper\Data
*/
private $helperData;
protected $helperData;

/**
* @var PlanManagementInterface
*/
private $planManagement;
protected $planManagement;

/**
* @var SubscriptionInterface
*/
private $subscriptionRepository;
protected $subscriptionRepository;

/**
* @var PaymentProfileFactory
*/
private $paymentProfileFactory;
protected $paymentProfileFactory;

/**
* @var PaymentProfileRepository
*/
private $paymentProfileRepository;
protected $paymentProfileRepository;

/**
* @var ResourceConnection
Expand Down Expand Up @@ -329,7 +329,9 @@ protected function processPayment(InfoInterface $payment, $amount)
];

if ($body['payment_method_code'] === PaymentMethod::CREDIT_CARD) {
$paymentProfile = $this->createPaymentProfile($order, $payment, $customerId);
$paymentProfile = ($payment->getAdditionalInformation('payment_profile'))
? $this->getPaymentProfile((int) $payment->getAdditionalInformation('payment_profile'))
: $this->createPaymentProfile($order, $payment, $customerId);
$body['payment_profile'] = ['id' => $paymentProfile->getData('payment_profile_id')];
}

Expand All @@ -350,13 +352,18 @@ protected function processPayment(InfoInterface $payment, $amount)
return $this->handleError($order);
}

protected function getPaymentProfile(int $paymentProfileId): PaymentProfile
{
return $this->paymentProfileRepository->getById($paymentProfileId);
}

/**
* @param InfoInterface $payment
* @param OrderItemInterface $orderItem
* @return mixed
* @throws LocalizedException
*/
private function handleSubscriptionOrder(InfoInterface $payment, OrderItemInterface $orderItem)
protected function handleSubscriptionOrder(InfoInterface $payment, OrderItemInterface $orderItem)
{
try {
$order = $payment->getOrder();
Expand Down Expand Up @@ -436,7 +443,7 @@ private function handleSubscriptionOrder(InfoInterface $payment, OrderItemInterf
* @return void
* @throws \Exception
*/
private function saveSubscriptionToDatabase(array $subscription, Order $order)
protected function saveSubscriptionToDatabase(array $subscription, Order $order)
{
$tableName = $this->resourceConnection->getTableName('vindi_subscription');
$startAt = new \DateTime($subscription['start_at']);
Expand Down Expand Up @@ -466,7 +473,7 @@ private function saveSubscriptionToDatabase(array $subscription, Order $order)
* @param Order $order
* @return OrderItemInterface|bool
*/
private function isSubscriptionOrder(Order $order)
protected function isSubscriptionOrder(Order $order)
{
foreach ($order->getItems() as $item) {
try {
Expand All @@ -490,7 +497,7 @@ private function isSubscriptionOrder(Order $order)
* @param Order $order
* @throws LocalizedException
*/
private function handleError(Order $order)
protected function handleError(Order $order)
{
$this->psrLogger->error(__(sprintf('Error on order payment %d.', $order->getId())));
$message = __('There has been a payment confirmation error. Verify data and try again');
Expand Down Expand Up @@ -536,7 +543,7 @@ protected function handleBankSplitAdditionalInformation(InfoInterface $payment,
* @param array $subscription
* @return bool
*/
private function successfullyPaid(array $body, $bill, array $subscription = [])
protected function successfullyPaid(array $body, $bill, array $subscription = [])
{
// nova validação para permitir pedidos com pagamento/fatura pendente
if (!$bill) {
Expand Down
Loading

0 comments on commit 44c2417

Please sign in to comment.