Skip to content

Commit

Permalink
PAOPN-576: add shipping and taxs in subs
Browse files Browse the repository at this point in the history
(#242 )
  • Loading branch information
fabiano-mallmann authored Sep 5, 2023
1 parent 0d97e9e commit 05a83dd
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 32 deletions.
86 changes: 63 additions & 23 deletions Concrete/Magento2PlatformOrderDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use Pagarme\Core\Payment\ValueObjects\CustomerType;
use Pagarme\Core\Payment\ValueObjects\Phone;
use Pagarme\Core\Recurrence\Aggregates\Plan;
use Pagarme\Core\Recurrence\Aggregates\Repetition;
use Pagarme\Core\Recurrence\Services\RecurrenceService;
use Pagarme\Pagarme\Helper\BuildChargeAddtionalInformationHelper;
use Pagarme\Pagarme\Helper\RecurrenceProductHelper;
Expand All @@ -55,6 +56,7 @@
use Pagarme\Core\Kernel\ValueObjects\TransactionType;
use Magento\Quote\Model\Quote;
use Pagarme\Pagarme\Helper\Marketplace\WebkulHelper;
use Pagarme\Pagarme\Model\PagarmeConfigProvider;

class Magento2PlatformOrderDecorator extends AbstractPlatformOrderDecorator
{
Expand All @@ -72,12 +74,21 @@ class Magento2PlatformOrderDecorator extends AbstractPlatformOrderDecorator
* @var OrderService
*/
private $orderService;
/**
* @var PagarmeConfigProvider
*/
private $config;
/**
* @var MoneyService
*/
private $moneyService;

public function __construct()
{
$this->i18n = new LocalizationService();
$objectManager = ObjectManager::getInstance();

$this->moneyService = new MoneyService();
$this->config = $objectManager->get('Pagarme\Pagarme\Model\PagarmeConfigProvider');
$this->orderFactory = $objectManager->get('Magento\Sales\Model\Order');
$this->orderService = new OrderService();
parent::__construct();
Expand Down Expand Up @@ -624,9 +635,9 @@ private function getGuestCustomer($quote)
/** @return Item[] */
public function getItemCollection()
{
$moneyService = new MoneyService();
$quote = $this->getQuote();
$itemCollection = $quote->getItemsCollection();
$hasSubscriptionItem = false;
$items = [];
foreach ($itemCollection as $quoteItem) {
//adjusting price.
Expand All @@ -649,7 +660,7 @@ public function getItemCollection()

$item = new Item;
$item->setAmount(
$moneyService->floatToCents($price)
$this->moneyService->floatToCents($price)
);

if ($quoteItem->getProductId()) {
Expand All @@ -663,18 +674,53 @@ public function getItemCollection()
);

$item->setName($quoteItem->getName());

$helper = new RecurrenceProductHelper();
$selectedRepetition = $helper->getSelectedRepetition($quoteItem);
$item->setSelectedOption($selectedRepetition);

$this->setRecurrenceInfo($item, $quoteItem);

if ($this->getRecurrenceService()->getRecurrenceProductByProductId($quoteItem->getProductId())) {
$hasSubscriptionItem = true;
$helper = new RecurrenceProductHelper();
$selectedRepetition = $helper->getSelectedRepetition($quoteItem);
$item->setSelectedOption($selectedRepetition);
$this->setRecurrenceInfo($item, $quoteItem);
}
$items[] = $item;
}

if($hasSubscriptionItem) {
if($this->getPlatformOrder()->getShippingAmount() && $this->config->canAddShippingInItemsOnRecurrence()) {
$items[] = $this->addCustomItem(
$this->getPlatformOrder()->getShippingAmount(),
$this->getPlatformOrder()->getShippingDescription(),
$selectedRepetition
);
}
if($this->getPlatformOrder()->getBaseTaxAmount() && $this->config->canAddTaxInItemsOnRecurrence()) {
$items[] = $this->addCustomItem(
$this->getPlatformOrder()->getBaseTaxAmount(),
__("Taxs"),
$selectedRepetition
);
}
}
return $items;
}

private function addCustomItem($value, $name, $selectedRepetition)
{
$product = new Item();
$product->setName($name);
$product->setDescription($name);
$product->setQuantity(1);
$product->setCode(0);
$product->setSelectedOption($this->mountRepetition($value, $selectedRepetition));
$product->setType("subscription");
$product->setAmount($this->moneyService->floatToCents($value));
return $product;
}
private function mountRepetition($value, $selectedRepetition)
{
$selectedRepetition->setRecurrencePrice($this->moneyService->floatToCents($value));
return $selectedRepetition;
}

public function setRecurrenceInfo($item, $quoteItem)
{
$recurrenceService = $this->getRecurrenceService();
Expand Down Expand Up @@ -809,7 +855,6 @@ private function extractPaymentDataFromPagarmeDebit(

private function extractBasePaymentData($additionalInformation)
{
$moneyService = new MoneyService();
$identifier = null;
$customerId = null;
$brand = null;
Expand Down Expand Up @@ -874,7 +919,6 @@ private function extractBasePaymentData($additionalInformation)

private function extractPaymentDataFromPagarmeTwoCreditCard($additionalInformation, &$paymentData, $payment)
{
$moneyService = new MoneyService();
$indexes = ['first', 'second'];
foreach ($indexes as $index) {
$identifier = null;
Expand Down Expand Up @@ -918,11 +962,11 @@ private function extractPaymentDataFromPagarmeTwoCreditCard($additionalInformati
$index
);

$amount = $moneyService->removeSeparators(
$amount = $this->moneyService->removeSeparators(
$additionalInformation["cc_{$index}_card_amount"]
);

$newPaymentData->amount = $moneyService->floatToCents($amount / 100);
$newPaymentData->amount = $this->moneyService->floatToCents($amount / 100);
$newPaymentData->saveOnSuccess =
isset($additionalInformation["cc_savecard_{$index}"]) &&
$additionalInformation["cc_savecard_{$index}"] === '1';
Expand Down Expand Up @@ -988,7 +1032,6 @@ private function extractPaymentDataFromPagarmeBilletCreditcard(
&$paymentData,
$payment
) {
$moneyService = new MoneyService();
$identifier = null;
$customerId = null;

Expand Down Expand Up @@ -1034,7 +1077,7 @@ private function extractPaymentDataFromPagarmeBilletCreditcard(
"",
$additionalInformation["cc_cc_amount"] ?? ''
);
$newPaymentData->amount = $moneyService->floatToCents($amount / 100);
$newPaymentData->amount = $this->moneyService->floatToCents($amount / 100);

$creditCardDataIndex = AbstractCreditCardPayment::getBaseCode();
if (!isset($paymentData[$creditCardDataIndex])) {
Expand All @@ -1059,7 +1102,7 @@ private function extractPaymentDataFromPagarmeBilletCreditcard(
);

$newPaymentData->amount =
$moneyService->floatToCents($amount / 100);
$this->moneyService->floatToCents($amount / 100);

$boletoDataIndex = BoletoPayment::getBaseCode();
if (!isset($paymentData[$boletoDataIndex])) {
Expand All @@ -1079,10 +1122,9 @@ private function extractPaymentDataFromPagarmeBillet(
&$paymentData,
$payment
) {
$moneyService = new MoneyService();
$newPaymentData = new \stdClass();
$newPaymentData->amount =
$moneyService->floatToCents($this->platformOrder->getGrandTotal());
$this->moneyService->floatToCents($this->platformOrder->getGrandTotal());

$boletoDataIndex = BoletoPayment::getBaseCode();
if (!isset($paymentData[$boletoDataIndex])) {
Expand All @@ -1104,10 +1146,9 @@ private function extractPaymentDataFromPagarmePix(
&$paymentData,
$payment
) {
$moneyService = new MoneyService();
$newPaymentData = new \stdClass();
$newPaymentData->amount =
$moneyService->floatToCents($this->platformOrder->getGrandTotal());
$this->moneyService->floatToCents($this->platformOrder->getGrandTotal());

$pixDataIndex = PixPayment::getBaseCode();
if (!isset($paymentData[$pixDataIndex])) {
Expand All @@ -1126,7 +1167,6 @@ private function extractPaymentDataFromPagarmePix(

public function getShipping()
{
$moneyService = new MoneyService();
/** @var Shipping $shipping */
$shipping = null;
$quote = $this->getQuote();
Expand All @@ -1141,7 +1181,7 @@ public function getShipping()
$shipping = new Shipping();

$shipping->setAmount(
$moneyService->floatToCents($platformShipping->getShippingAmount())
$this->moneyService->floatToCents($platformShipping->getShippingAmount())
);
$shipping->setDescription($platformShipping->getShippingDescription());
$shipping->setRecipientName($platformShipping->getName());
Expand Down
19 changes: 18 additions & 1 deletion Model/PagarmeConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class PagarmeConfigProvider implements ConfigProviderInterface
/**
* Contains if the module is active or not
*/
const XML_PATH_IS_GATEWAY_INTEGRATION_TYPE = 'pagarme_pagarme/global/is_gateway_integration_type';
const XML_PATH_IS_GATEWAY_INTEGRATION_TYPE = 'pagarme_pagarme/global/is_gateway_integration_type';
const XML_PATH_RECURRENCE_ADD_SHIPPING_IN_ITEMS = 'pagarme_pagarme/recurrence/add_shipping_in_items';
const XML_PATH_RECURRENCE_ADD_TAX_IN_ITEMS = 'pagarme_pagarme/recurrence/add_tax_in_items';
const XML_PATH_IS_ENABLE_SAVED_CARDS = 'payment/pagarme_creditcard/enabled_saved_cards';
const XML_PATH_SOFT_DESCRIPTION = 'payment/pagarme_creditcard/soft_description';
const XML_PATH_MAX_INSTALLMENT = 'payment/pagarme_creditcard/installments_number';
Expand Down Expand Up @@ -93,6 +95,21 @@ public function isGatewayIntegrationType()
);
}

public function canAddShippingInItemsOnRecurrence()
{
return $this->scopeConfig->getValue(
self::XML_PATH_RECURRENCE_ADD_SHIPPING_IN_ITEMS,
ScopeInterface::SCOPE_STORE
) ?? false;
}
public function canAddTaxInItemsOnRecurrence()
{
return $this->scopeConfig->getValue(
self::XML_PATH_RECURRENCE_ADD_TAX_IN_ITEMS,
ScopeInterface::SCOPE_STORE
) ?? false;
}

public function validateSoftDescription()
{
$isGatewayIntegrationType = $this->isGatewayIntegrationType();
Expand Down
3 changes: 0 additions & 3 deletions etc/adminhtml/system/marketplace.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,5 @@
<field id="active">1</field>
</depends>
</field>
<depends>
<field id="*/*/pagarme_pagarme_global/is_gateway_integration_type">1</field>
</depends>
</group>
</include>
41 changes: 37 additions & 4 deletions etc/adminhtml/system/recurrence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<include xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_include.xsd">
<group id="pagarme_recurrence" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="40" translate="label">
<label>Recurrence Settings (Beta)</label>
<label>Recurrence Settings</label>
<field id="active" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="20" translate="label" type="select">
<label>Enable this Solution</label>
<comment />
Expand All @@ -14,40 +14,73 @@
<comment />
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>pagarme_pagarme/recurrence/show_recurrence_currency_widget</config_path>
<depends>
<field id="active">1</field>
</depends>
</field>

<field id="purchase_recurrence_product_with_normal_product" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="22" translate="label" type="select">
<label>Allow purchase with other product</label>
<comment>Allow to purchase recurrence products with simple products on the same shopping cart</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>pagarme_pagarme/recurrence/purchase_recurrence_product_with_normal_product</config_path>
<depends>
<field id="active">1</field>
</depends>
</field>
<field id="conflict_recurrence_product_with_normal_product" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="23" translate="label" type="textarea">
<label>Checkout conflict message</label>
<comment />
<config_path>pagarme_pagarme/recurrence/conflict_recurrence_product_with_normal_product</config_path>
<depends>
<field id="active">1</field>
</depends>
</field>

<field id="purchase_recurrence_product_with_recurrence_product" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="24" translate="label" type="select">
<label>Allow purchase with other recurrence product</label>
<comment>Allow to purchase more than one recurrence product on the same shopping cart</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>pagarme_pagarme/recurrence/purchase_recurrence_product_with_recurrence_product</config_path>
<depends>
<field id="active">1</field>
</depends>
</field>
<field id="conflict_recurrence_product_with_recurrence_product" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="25" translate="label" type="textarea">
<label>Conflict checkout message</label>
<comment />
<config_path>pagarme_pagarme/recurrence/conflict_recurrence_product_with_recurrence_product</config_path>
<depends>
<field id="active">1</field>
</depends>
</field>

<field id="decrease_stock" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="26" translate="label" type="select">
<label>Update product stock at each cycle</label>
<comment>With this option enabled, the module will decrease product stock with each recurrence cycle</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>pagarme_pagarme/recurrence/decrease_stock</config_path>
<depends>
<field id="active">1</field>
</depends>
</field>
<field id="add_shipping_in_items" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="20" translate="label" type="select">
<label>Add shipping values in items</label>
<comment />
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>pagarme_pagarme/recurrence/add_shipping_in_items</config_path>
<depends>
<field id="active">1</field>
</depends>
</field>
<field id="add_tax_in_items" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="20" translate="label" type="select">
<label>Add tax values in items</label>
<comment />
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>pagarme_pagarme/recurrence/add_tax_in_items</config_path>
<depends>
<field id="active">1</field>
</depends>
</field>
<depends>
<field id="*/*/pagarme_pagarme_global/is_gateway_integration_type">1</field>
</depends>
</group>
</include>
5 changes: 4 additions & 1 deletion i18n/pt_BR.csv
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"Multi-means Double Credit Card","Multimeios Dois Cartões de Crédito"
"Multi-means Credit Card and Boleto","Multimeios Cartão de Crédito e Boleto"
"Debit Card","Cartão de Débito"
"Recurrence Settings (Beta)","Configurações de Recorrência (Beta)"
"Recurrence Settings","Configurações de Recorrência"
"Show Recurrence Currency Widget","Mostrar Widget de Moeda de Recorrência"
"Conflict checkout message","Mensagem de Conflito do Checkout"
"Print Billet","Imprimir Boleto"
Expand Down Expand Up @@ -496,3 +496,6 @@
"Please, enter valid Name on Card","Por favor, digite um Nome no Cartão válido"
"Please, enter valid Expiration Date","Por favor, digite um Data de Validade válida"
"The cvv field must be a minimum length of 3 and a maximum length of 4.","O campo de cvv deve ter um tamanho mínimo de 3 e um tamanho máximo de 4."
"Add shipping values in items","Realizar a cobrança do frete"
"Add tax values in items","Realizar a cobrança das taxas"
"Taxs","Taxas"

0 comments on commit 05a83dd

Please sign in to comment.