Completed
Push — master ( 1137b9...97c77e )
by
unknown
20s queued 11s
created

Index::checkDbLogTable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 15
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace DigitalOrigin\Pmt\Controller\Payment;
4
5
use Magento\Framework\App\Action\Action;
6
use Magento\Framework\App\Action\Context;
7
use Magento\Quote\Model\QuoteRepository;
8
use Magento\Sales\Model\ResourceModel\Order\Collection as OrderCollection;
9
use Magento\Checkout\Model\Session;
10
use DigitalOrigin\Pmt\Helper\Config;
11
use Magento\Framework\App\ResourceConnection;
12
use Magento\Framework\App\ProductMetadataInterface;
13
use Magento\Framework\Module\ModuleList;
14
use PagaMasTarde\OrdersApiClient\Model\Order\User\Address;
15
use Magento\Framework\DB\Ddl\Table;
16
17
/**
18
 * Class Index
19
 * @package DigitalOrigin\Pmt\Controller\Payment
20
 */
21
class Index extends Action
22
{
23
    /** Orders tablename */
24
    const ORDERS_TABLE = 'cart_process';
25
26
    /** Concurrency tablename */
27
    const LOGS_TABLE = 'pmt_logs';
28
29
    /** @var Context $context */
30
    protected $context;
31
32
    /** @var QuoteRepository  $quoteRepository */
33
    protected $quoteRepository;
34
35
    /** @var OrderCollection $orderCollection */
36
    protected $orderCollection;
37
38
    /** @var Session $session */
39
    protected $session;
40
41
    /** @var mixed $config */
42
    protected $config;
43
44
    /** @var ResourceConnection $dbObject */
45
    protected $dbObject;
46
47
    /** @var ProductMetadataInterface $productMetadataInterface */
48
    protected $productMetadataInterface;
49
50
    /** @var ModuleList $moduleList */
51
    protected $moduleList;
52
53
    /**
54
     * Index constructor.
55
     *
56
     * @param Context                  $context
57
     * @param Session                  $session
58
     * @param Config                   $config
59
     * @param QuoteRepository          $quoteRepository
60
     * @param OrderCollection          $orderCollection
61
     * @param ResourceConnection       $dbObject
62
     * @param ModuleList               $moduleList
63
     * @param ProductMetadataInterface $productMetadataInterface
64
     *
65
     */
66
    public function __construct(
67
        Context $context,
68
        QuoteRepository $quoteRepository,
69
        OrderCollection $orderCollection,
70
        Session $session,
71
        Config $config,
72
        ResourceConnection $dbObject,
73
        ProductMetadataInterface $productMetadataInterface,
74
        ModuleList $moduleList
75
    ) {
76
        parent::__construct($context);
77
        $this->session = $session;
78
        $this->context = $context;
79
        $this->config = $config->getConfig();
80
        $this->quoteRepository = $quoteRepository;
81
        $this->orderCollection = $orderCollection;
82
        $this->dbObject = $dbObject;
83
        $this->moduleList = $moduleList;
84
        $this->productMetadataInterface = $productMetadataInterface;
85
    }
86
87
    /**
88
     * Main function
89
     *
90
     * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|void
91
     * @throws \Magento\Framework\Exception\NoSuchEntityException
92
     */
93
    public function execute()
94
    {
95
        try {
96
            $cancelUrl = $this->_url->getUrl('checkout', ['_fragment' => 'payment']);
97
            $quote = $this->session->getQuote();
98
            /** @var Order $order */
99
            $lastOrder = $this->session->getLastRealOrder();
0 ignored issues
show
Unused Code introduced by
The assignment to $lastOrder is dead and can be removed.
Loading history...
100
            $params = $this->getRequest()->getParams();
101
            $customer = $quote->getCustomer();
102
            $shippingAddress = $quote->getShippingAddress();
103
104
            if (isset($params['email']) && $params['email']!='') {
105
                $this->session->setEmail($params['email']); //Get guest email after refresh page
106
                $customer->setEmail($params['email']);
0 ignored issues
show
Bug introduced by
The method setEmail() does not exist on Magento\Framework\Api\ExtensibleDataInterface. It seems like you code against a sub-type of Magento\Framework\Api\ExtensibleDataInterface such as Magento\Sales\Api\Data\OrderAddressInterface or Magento\Sales\Model\Order\Shipment\Track or Magento\Store\Model\Website or Magento\CatalogInventory\Model\Stock\Status or Magento\Sales\Model\Order\Creditmemo\Item or Magento\Tax\Model\Sales\Quote\QuoteDetails or Magento\Catalog\Model\ProductLink\Link or Magento\Framework\Model\AbstractExtensibleModel or Magento\Quote\Api\Data\AddressInterface or Magento\Customer\Api\Data\CustomerInterface or Magento\CatalogRule\Model\Rule or Magento\Catalog\Model\Product or Magento\Quote\Model\AddressAdditionalData or Magento\Quote\Model\Cart\TotalsAdditionalData or Magento\Framework\View\E...t\DataProvider\Document or Magento\Checkout\Model\TotalsInformation or Magento\CatalogRule\Model\Data\Condition or Magento\Customer\Model\Data\Customer or Magento\Quote\Model\EstimateAddress or Magento\Catalog\Model\Category or Magento\Eav\Model\Entity...ibute\AbstractAttribute or Magento\Catalog\Model\ResourceModel\Eav\Attribute or Magento\Catalog\Model\Category\Attribute or Magento\Checkout\Model\ShippingInformation or Magento\Tax\Model\Calculation\Rule or Magento\Sales\Model\Order\Status\History or Magento\Store\Model\Group or Magento\Catalog\Model\Product\Option or Magento\Catalog\Model\Product\Gallery\Entry or Magento\Quote\Model\Quote\ProductOption or Magento\Tax\Model\Sales\Quote\ItemDetails or Magento\Tax\Model\TaxDetails\AppliedTax or Magento\Sales\Model\Order\Tax\Item or Magento\Bundle\Model\Option or Magento\Eav\Model\Entity\Attribute\Set or Magento\Quote\Model\Cart\Currency or Magento\Sales\Model\Order\Shipment\Comment or Magento\Catalog\Model\Product\Price\Cost or Magento\Quote\Model\Cart\Totals or Magento\Sales\Model\Order\Invoice\Item or Magento\Downloadable\Model\File\Content or Magento\Catalog\Model\ProductRender\PriceInfo or Magento\CatalogInventory\Model\Stock or Magento\Quote\Model\ShippingAssignment or Magento\Quote\Model\Quote\Payment or Magento\Catalog\Model\ProductRender\Image or Magento\Tax\Model\TaxClass\Key or Magento\Sales\Model\Order\Creditmemo\Comment or Magento\Sales\Model\Order\Payment or Magento\Sales\Model\Order\Payment\Transaction or Magento\GiftMessage\Model\Message or Magento\Sales\Model\Order\Shipment\Item or Magento\Catalog\Model\Product\Price\TierPrice or Magento\Catalog\Model\Product\Attribute\Type or Magento\Tax\Model\TaxDetails\ItemDetails or Magento\Store\Model\Store or Magento\Catalog\Model\CustomOptions\CustomOption or Magento\Catalog\Model\Pr...nder\FormattedPriceInfo or Magento\Catalog\Model\ProductRender or Magento\Sales\Model\Order or Magento\Catalog\Model\Product\Option\Type or Magento\Quote\Model\Quote or Magento\Tax\Model\Calculation\Rate or Magento\Catalog\Model\Product\TierPrice or Magento\Tax\Model\Sales\Order\Details or Magento\Framework\Api\Te...erator\ExtensibleSample or Magento\Sales\Model\Order\Invoice\Comment or Magento\Sales\Model\Order\ShippingAssignment or Magento\Msrp\Model\ProductRender\MsrpPriceInfo or Magento\Sales\Model\Order\Item or Magento\Sales\Model\Order\Creditmemo or Magento\Sales\Model\Order\Invoice or Magento\Quote\Model\Shipping or Magento\Catalog\Model\Product\Price\BasePrice or Magento\Downloadable\Model\Sample or Magento\Eav\Model\Entity\Attribute\Group or Magento\Tax\Model\Sales\Order\Tax or Magento\Quote\Model\Cart\TotalSegment or Magento\Downloadable\Model\Link or Magento\CatalogInventory\Model\Stock\Item or Magento\Catalog\Model\Pr...Price\PriceUpdateResult or Magento\Tax\Model\Calculation\Rate\Title or Magento\Sales\Model\Order\Shipment or Magento\Catalog\Model\Product\Price\SpecialPrice or Magento\Tax\Model\ClassModel or Magento\Bundle\Model\Link or Magento\Bundle\Model\BundleOption or Magento\Tax\Model\TaxDetails\AppliedTaxRate or Magento\Bundle\Model\Source\Option\Type or Magento\Sales\Model\Order\Shipping or Magento\Quote\Model\Quote\Item or Magento\SalesRule\Model\Coupon or Magento\Tax\Model\TaxDetails\TaxDetails or Magento\Sales\Model\Order\ShippingTotal or Magento\Catalog\Model\ProductOption or Magento\Catalog\Model\ProductRender\Button. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
                $customer->/** @scrutinizer ignore-call */ 
107
                           setEmail($params['email']);
Loading history...
107
                $quote->setCheckoutMethod('guest');
108
                $quote->getBillingAddress()->setEmail($params['email']);
109
            } elseif ($customer->getEmail()=='') {
0 ignored issues
show
Bug introduced by
The method getEmail() does not exist on Magento\Framework\Api\ExtensibleDataInterface. It seems like you code against a sub-type of Magento\Framework\Api\ExtensibleDataInterface such as Magento\Sales\Api\Data\OrderAddressInterface or Magento\Sales\Model\Order\Shipment\Track or Magento\Store\Model\Website or Magento\CatalogInventory\Model\Stock\Status or Magento\Sales\Model\Order\Creditmemo\Item or Magento\Tax\Model\Sales\Quote\QuoteDetails or Magento\Catalog\Model\ProductLink\Link or Magento\Framework\Model\AbstractExtensibleModel or Magento\Quote\Api\Data\AddressInterface or Magento\Customer\Api\Data\CustomerInterface or Magento\CatalogRule\Model\Rule or Magento\Catalog\Model\Product or Magento\Quote\Model\AddressAdditionalData or Magento\Quote\Model\Cart\TotalsAdditionalData or Magento\Framework\View\E...t\DataProvider\Document or Magento\Checkout\Model\TotalsInformation or Magento\CatalogRule\Model\Data\Condition or Magento\Customer\Model\Data\Customer or Magento\Quote\Model\EstimateAddress or Magento\Catalog\Model\Category or Magento\Eav\Model\Entity...ibute\AbstractAttribute or Magento\Catalog\Model\ResourceModel\Eav\Attribute or Magento\Catalog\Model\Category\Attribute or Magento\Checkout\Model\ShippingInformation or Magento\Tax\Model\Calculation\Rule or Magento\Sales\Model\Order\Status\History or Magento\Store\Model\Group or Magento\Catalog\Model\Product\Option or Magento\Catalog\Model\Product\Gallery\Entry or Magento\Quote\Model\Quote\ProductOption or Magento\Tax\Model\Sales\Quote\ItemDetails or Magento\Tax\Model\TaxDetails\AppliedTax or Magento\Sales\Model\Order\Tax\Item or Magento\Bundle\Model\Option or Magento\Eav\Model\Entity\Attribute\Set or Magento\Quote\Model\Cart\Currency or Magento\Sales\Model\Order\Shipment\Comment or Magento\Catalog\Model\Product\Price\Cost or Magento\Quote\Model\Cart\Totals or Magento\Sales\Model\Order\Invoice\Item or Magento\Downloadable\Model\File\Content or Magento\Catalog\Model\ProductRender\PriceInfo or Magento\CatalogInventory\Model\Stock or Magento\Quote\Model\ShippingAssignment or Magento\Quote\Model\Quote\Payment or Magento\Catalog\Model\ProductRender\Image or Magento\Tax\Model\TaxClass\Key or Magento\Sales\Model\Order\Creditmemo\Comment or Magento\Sales\Model\Order\Payment or Magento\Sales\Model\Order\Payment\Transaction or Magento\GiftMessage\Model\Message or Magento\Sales\Model\Order\Shipment\Item or Magento\Catalog\Model\Product\Price\TierPrice or Magento\Catalog\Model\Product\Attribute\Type or Magento\Tax\Model\TaxDetails\ItemDetails or Magento\Store\Model\Store or Magento\Catalog\Model\CustomOptions\CustomOption or Magento\Catalog\Model\Pr...nder\FormattedPriceInfo or Magento\Catalog\Model\ProductRender or Magento\Sales\Model\Order or Magento\Catalog\Model\Product\Option\Type or Magento\Quote\Model\Quote or Magento\Tax\Model\Calculation\Rate or Magento\Catalog\Model\Product\TierPrice or Magento\Tax\Model\Sales\Order\Details or Magento\Framework\Api\Te...erator\ExtensibleSample or Magento\Sales\Model\Order\Invoice\Comment or Magento\Sales\Model\Order\ShippingAssignment or Magento\Msrp\Model\ProductRender\MsrpPriceInfo or Magento\Sales\Model\Order\Item or Magento\Sales\Model\Order\Creditmemo or Magento\Sales\Model\Order\Invoice or Magento\Quote\Model\Shipping or Magento\Catalog\Model\Product\Price\BasePrice or Magento\Downloadable\Model\Sample or Magento\Eav\Model\Entity\Attribute\Group or Magento\Tax\Model\Sales\Order\Tax or Magento\Quote\Model\Cart\TotalSegment or Magento\Downloadable\Model\Link or Magento\CatalogInventory\Model\Stock\Item or Magento\Catalog\Model\Pr...Price\PriceUpdateResult or Magento\Tax\Model\Calculation\Rate\Title or Magento\Sales\Model\Order\Shipment or Magento\Catalog\Model\Product\Price\SpecialPrice or Magento\Tax\Model\ClassModel or Magento\Bundle\Model\Link or Magento\Bundle\Model\BundleOption or Magento\Tax\Model\TaxDetails\AppliedTaxRate or Magento\Bundle\Model\Source\Option\Type or Magento\Sales\Model\Order\Shipping or Magento\Quote\Model\Quote\Item or Magento\SalesRule\Model\Coupon or Magento\Tax\Model\TaxDetails\TaxDetails or Magento\Sales\Model\Order\ShippingTotal or Magento\Catalog\Model\ProductOption or Magento\Catalog\Model\ProductRender\Button. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
            } elseif ($customer->/** @scrutinizer ignore-call */ getEmail()=='') {
Loading history...
110
                $customer->setEmail($this->session->getEmail());
0 ignored issues
show
Bug introduced by
It seems like $this->session->getEmail() can also be of type Magento\Checkout\Model\Session; however, parameter $email of Magento\Customer\Api\Dat...erInterface::setEmail() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

110
                $customer->setEmail(/** @scrutinizer ignore-type */ $this->session->getEmail());
Loading history...
111
                $quote->setCheckoutMethod('guest');
112
                $quote->getBillingAddress()->setEmail($this->session->getEmail());
113
            }
114
115
            /** @var Quote $currentQuote */
116
            $currentQuote = $this->quoteRepository->get($quote->getId());
117
            $currentQuote->setCustomerEmail($customer->getEmail());
118
            $this->quoteRepository->save($currentQuote);
119
120
            $userAddress =  new Address();
121
            $userAddress
122
                ->setZipCode($shippingAddress->getPostcode())
123
                ->setFullName($shippingAddress->getFirstname()." ".$shippingAddress->getLastname())
124
                ->setCountryCode('ES')
125
                ->setCity($shippingAddress->getCity())
126
                ->setAddress($shippingAddress->getStreetFull())
127
            ;
128
129
            $orderShippingAddress = new Address();
130
            $orderShippingAddress
131
                ->setZipCode($shippingAddress->getPostcode())
132
                ->setFullName($shippingAddress->getFirstname()." ".$shippingAddress->getLastname())
133
                ->setCountryCode('ES')
134
                ->setCity($shippingAddress->getCity())
135
                ->setAddress($shippingAddress->getStreetFull())
136
                ->setFixPhone($shippingAddress->getTelephone())
137
                ->setMobilePhone($shippingAddress->getTelephone())
138
            ;
139
140
            $orderBillingAddress =  new Address();
141
            $billingAddress = $quote->getBillingAddress();
142
            $orderBillingAddress
143
                ->setZipCode($billingAddress->getPostcode())
144
                ->setFullName($billingAddress->getFirstname()." ".$shippingAddress->getLastname())
145
                ->setCountryCode('ES')
146
                ->setCity($billingAddress->getCity())
147
                ->setAddress($billingAddress->getStreetFull())
148
                ->setFixPhone($billingAddress->getTelephone())
149
                ->setMobilePhone($billingAddress->getTelephone())
150
            ;
151
152
            $orderUser = new \PagaMasTarde\OrdersApiClient\Model\Order\User();
153
            $billingAddress->setEmail($customer->getEmail());
154
            $orderUser
155
                ->setAddress($userAddress)
156
                ->setFullName($shippingAddress->getFirstname()." ".$shippingAddress->getLastname())
157
                ->setBillingAddress($orderBillingAddress)
158
                ->setEmail($customer->getEmail())
159
                ->setFixPhone($shippingAddress->getTelephone())
160
                ->setMobilePhone($shippingAddress->getTelephone())
161
                ->setShippingAddress($orderShippingAddress)
162
            ;
163
164
            if ($customer->getDob()) {
0 ignored issues
show
Bug introduced by
The method getDob() does not exist on Magento\Framework\Api\ExtensibleDataInterface. It seems like you code against a sub-type of Magento\Framework\Api\ExtensibleDataInterface such as Magento\Sales\Model\Order\Shipment\Track or Magento\Store\Model\Website or Magento\CatalogInventory\Model\Stock\Status or Magento\Sales\Model\Order\Creditmemo\Item or Magento\Tax\Model\Sales\Quote\QuoteDetails or Magento\Catalog\Model\ProductLink\Link or Magento\Framework\Model\AbstractExtensibleModel or Magento\Customer\Api\Data\CustomerInterface or Magento\CatalogRule\Model\Rule or Magento\Catalog\Model\Product or Magento\Quote\Model\AddressAdditionalData or Magento\Quote\Model\Quote\Address or Magento\Quote\Model\Cart\TotalsAdditionalData or Magento\Framework\View\E...t\DataProvider\Document or Magento\Checkout\Model\TotalsInformation or Magento\CatalogRule\Model\Data\Condition or Magento\Customer\Model\Data\Customer or Magento\Quote\Model\EstimateAddress or Magento\Catalog\Model\Category or Magento\Eav\Model\Entity...ibute\AbstractAttribute or Magento\Catalog\Model\ResourceModel\Eav\Attribute or Magento\Catalog\Model\Category\Attribute or Magento\Checkout\Model\ShippingInformation or Magento\Tax\Model\Calculation\Rule or Magento\Sales\Model\Order\Status\History or Magento\Store\Model\Group or Magento\Catalog\Model\Product\Option or Magento\Catalog\Model\Product\Gallery\Entry or Magento\Quote\Model\Quote\ProductOption or Magento\Tax\Model\Sales\Quote\ItemDetails or Magento\Tax\Model\TaxDetails\AppliedTax or Magento\Sales\Model\Order\Tax\Item or Magento\Bundle\Model\Option or Magento\Eav\Model\Entity\Attribute\Set or Magento\Quote\Model\Cart\Currency or Magento\Sales\Model\Order\Shipment\Comment or Magento\Catalog\Model\Product\Price\Cost or Magento\Quote\Model\Cart\Totals or Magento\Sales\Model\Order\Invoice\Item or Magento\Downloadable\Model\File\Content or Magento\Catalog\Model\ProductRender\PriceInfo or Magento\CatalogInventory\Model\Stock or Magento\Quote\Model\ShippingAssignment or Magento\Quote\Model\Quote\Payment or Magento\Catalog\Model\ProductRender\Image or Magento\Sales\Model\Order\Address or Magento\Tax\Model\TaxClass\Key or Magento\Sales\Model\Order\Creditmemo\Comment or Magento\Sales\Model\Order\Payment or Magento\Sales\Model\Order\Payment\Transaction or Magento\GiftMessage\Model\Message or Magento\Sales\Model\Order\Shipment\Item or Magento\Catalog\Model\Product\Price\TierPrice or Magento\Catalog\Model\Product\Attribute\Type or Magento\Tax\Model\TaxDetails\ItemDetails or Magento\Store\Model\Store or Magento\Catalog\Model\CustomOptions\CustomOption or Magento\Catalog\Model\Pr...nder\FormattedPriceInfo or Magento\Catalog\Model\ProductRender or Magento\Sales\Model\Order or Magento\Catalog\Model\Product\Option\Type or Magento\Quote\Model\Quote or Magento\Tax\Model\Calculation\Rate or Magento\Catalog\Model\Product\TierPrice or Magento\Tax\Model\Sales\Order\Details or Magento\Framework\Api\Te...erator\ExtensibleSample or Magento\Sales\Model\Order\Invoice\Comment or Magento\Sales\Model\Order\ShippingAssignment or Magento\Msrp\Model\ProductRender\MsrpPriceInfo or Magento\Sales\Model\Order\Item or Magento\Sales\Model\Order\Creditmemo or Magento\Sales\Model\Order\Invoice or Magento\Quote\Model\Shipping or Magento\Catalog\Model\Product\Price\BasePrice or Magento\Downloadable\Model\Sample or Magento\Eav\Model\Entity\Attribute\Group or Magento\Tax\Model\Sales\Order\Tax or Magento\Quote\Model\Cart\TotalSegment or Magento\Downloadable\Model\Link or Magento\CatalogInventory\Model\Stock\Item or Magento\Catalog\Model\Pr...Price\PriceUpdateResult or Magento\Tax\Model\Calculation\Rate\Title or Magento\Sales\Model\Order\Shipment or Magento\Catalog\Model\Product\Price\SpecialPrice or Magento\Tax\Model\ClassModel or Magento\Bundle\Model\Link or Magento\Bundle\Model\BundleOption or Magento\Tax\Model\TaxDetails\AppliedTaxRate or Magento\Bundle\Model\Source\Option\Type or Magento\Sales\Model\Order\Shipping or Magento\Quote\Model\Quote\Item or Magento\SalesRule\Model\Coupon or Magento\Tax\Model\TaxDetails\TaxDetails or Magento\Sales\Model\Order\ShippingTotal or Magento\Catalog\Model\ProductOption or Magento\Catalog\Model\ProductRender\Button. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

164
            if ($customer->/** @scrutinizer ignore-call */ getDob()) {
Loading history...
165
                $orderUser->setDateOfBirth($customer->getDob());
166
            }
167
            if ($customer->getTaxvat()!='') {
0 ignored issues
show
Bug introduced by
The method getTaxvat() does not exist on Magento\Framework\Api\ExtensibleDataInterface. It seems like you code against a sub-type of Magento\Framework\Api\ExtensibleDataInterface such as Magento\Sales\Model\Order\Shipment\Track or Magento\Store\Model\Website or Magento\CatalogInventory\Model\Stock\Status or Magento\Sales\Model\Order\Creditmemo\Item or Magento\Tax\Model\Sales\Quote\QuoteDetails or Magento\Catalog\Model\ProductLink\Link or Magento\Framework\Model\AbstractExtensibleModel or Magento\Customer\Api\Data\CustomerInterface or Magento\CatalogRule\Model\Rule or Magento\Catalog\Model\Product or Magento\Quote\Model\AddressAdditionalData or Magento\Quote\Model\Quote\Address or Magento\Quote\Model\Cart\TotalsAdditionalData or Magento\Framework\View\E...t\DataProvider\Document or Magento\Checkout\Model\TotalsInformation or Magento\CatalogRule\Model\Data\Condition or Magento\Customer\Model\Data\Customer or Magento\Quote\Model\EstimateAddress or Magento\Catalog\Model\Category or Magento\Eav\Model\Entity...ibute\AbstractAttribute or Magento\Catalog\Model\ResourceModel\Eav\Attribute or Magento\Catalog\Model\Category\Attribute or Magento\Checkout\Model\ShippingInformation or Magento\Tax\Model\Calculation\Rule or Magento\Sales\Model\Order\Status\History or Magento\Store\Model\Group or Magento\Catalog\Model\Product\Option or Magento\Catalog\Model\Product\Gallery\Entry or Magento\Quote\Model\Quote\ProductOption or Magento\Tax\Model\Sales\Quote\ItemDetails or Magento\Tax\Model\TaxDetails\AppliedTax or Magento\Sales\Model\Order\Tax\Item or Magento\Bundle\Model\Option or Magento\Eav\Model\Entity\Attribute\Set or Magento\Quote\Model\Cart\Currency or Magento\Sales\Model\Order\Shipment\Comment or Magento\Catalog\Model\Product\Price\Cost or Magento\Quote\Model\Cart\Totals or Magento\Sales\Model\Order\Invoice\Item or Magento\Downloadable\Model\File\Content or Magento\Catalog\Model\ProductRender\PriceInfo or Magento\CatalogInventory\Model\Stock or Magento\Quote\Model\ShippingAssignment or Magento\Quote\Model\Quote\Payment or Magento\Catalog\Model\ProductRender\Image or Magento\Sales\Model\Order\Address or Magento\Tax\Model\TaxClass\Key or Magento\Sales\Model\Order\Creditmemo\Comment or Magento\Sales\Model\Order\Payment or Magento\Sales\Model\Order\Payment\Transaction or Magento\GiftMessage\Model\Message or Magento\Sales\Model\Order\Shipment\Item or Magento\Catalog\Model\Product\Price\TierPrice or Magento\Catalog\Model\Product\Attribute\Type or Magento\Tax\Model\TaxDetails\ItemDetails or Magento\Store\Model\Store or Magento\Catalog\Model\CustomOptions\CustomOption or Magento\Catalog\Model\Pr...nder\FormattedPriceInfo or Magento\Catalog\Model\ProductRender or Magento\Sales\Model\Order or Magento\Catalog\Model\Product\Option\Type or Magento\Quote\Model\Quote or Magento\Tax\Model\Calculation\Rate or Magento\Catalog\Model\Product\TierPrice or Magento\Tax\Model\Sales\Order\Details or Magento\Framework\Api\Te...erator\ExtensibleSample or Magento\Sales\Model\Order\Invoice\Comment or Magento\Sales\Model\Order\ShippingAssignment or Magento\Msrp\Model\ProductRender\MsrpPriceInfo or Magento\Sales\Model\Order\Item or Magento\Sales\Model\Order\Creditmemo or Magento\Sales\Model\Order\Invoice or Magento\Quote\Model\Shipping or Magento\Catalog\Model\Product\Price\BasePrice or Magento\Downloadable\Model\Sample or Magento\Eav\Model\Entity\Attribute\Group or Magento\Tax\Model\Sales\Order\Tax or Magento\Quote\Model\Cart\TotalSegment or Magento\Downloadable\Model\Link or Magento\CatalogInventory\Model\Stock\Item or Magento\Catalog\Model\Pr...Price\PriceUpdateResult or Magento\Tax\Model\Calculation\Rate\Title or Magento\Sales\Model\Order\Shipment or Magento\Catalog\Model\Product\Price\SpecialPrice or Magento\Tax\Model\ClassModel or Magento\Bundle\Model\Link or Magento\Bundle\Model\BundleOption or Magento\Tax\Model\TaxDetails\AppliedTaxRate or Magento\Bundle\Model\Source\Option\Type or Magento\Sales\Model\Order\Shipping or Magento\Quote\Model\Quote\Item or Magento\SalesRule\Model\Coupon or Magento\Tax\Model\TaxDetails\TaxDetails or Magento\Sales\Model\Order\ShippingTotal or Magento\Catalog\Model\ProductOption or Magento\Catalog\Model\ProductRender\Button. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

167
            if ($customer->/** @scrutinizer ignore-call */ getTaxvat()!='') {
Loading history...
168
                $orderUser->setDni($customer->getTaxvat());
169
                $orderBillingAddress->setDni($customer->getTaxvat());
170
                $orderShippingAddress->setDni($customer->getTaxvat());
171
            }
172
173
            $previousOrders = $this->getOrders($customer->getId());
0 ignored issues
show
Bug introduced by
The method getId() does not exist on Magento\Framework\Api\ExtensibleDataInterface. It seems like you code against a sub-type of Magento\Framework\Api\ExtensibleDataInterface such as Magento\Store\Api\Data\WebsiteInterface or Magento\Tax\Api\Data\TaxRuleInterface or Magento\Store\Api\Data\GroupInterface or Magento\Customer\Api\Data\GroupInterface or Magento\Catalog\Api\Data...iaGalleryEntryInterface or Magento\Directory\Api\Da...tryInformationInterface or Magento\Store\Api\Data\StoreInterface or Magento\Catalog\Api\Data\ProductRenderInterface or Magento\Quote\Api\Data\CartInterface or Magento\Store\Api\Data\StoreConfigInterface or Magento\Tax\Api\Data\TaxRateInterface or Magento\Downloadable\Api\Data\SampleInterface or Magento\Directory\Api\Da...ionInformationInterface or Magento\Downloadable\Api\Data\LinkInterface or Magento\Bundle\Api\Data\LinkInterface or Magento\Sales\Model\Order\Shipment\Track or Magento\CatalogInventory\Model\Stock\Status or Magento\Sales\Model\Order\Creditmemo\Item or Magento\Tax\Model\Sales\Quote\QuoteDetails or Magento\Catalog\Model\ProductLink\Link or Magento\Framework\Model\AbstractExtensibleModel or Magento\Catalog\Api\Data\ProductInterface or Magento\Quote\Api\Data\AddressInterface or Magento\Customer\Api\Data\CustomerInterface or Magento\Framework\Api\Search\DocumentInterface or Magento\Catalog\Api\Data\CategoryInterface or Magento\Customer\Api\Data\AddressInterface or Magento\CatalogRule\Model\Rule or Magento\Quote\Model\AddressAdditionalData or Magento\Quote\Model\Cart\TotalsAdditionalData or Magento\Checkout\Model\TotalsInformation or Magento\CatalogRule\Model\Data\Condition or Magento\Store\Model\Data\StoreConfig or Magento\Customer\Model\Data\Customer or Magento\Directory\Model\Data\RegionInformation or Magento\Customer\Model\Data\Group or Magento\Directory\Model\Data\CountryInformation or Magento\Customer\Model\Data\Address or Magento\Quote\Model\EstimateAddress or Magento\Eav\Model\Entity...ibute\AbstractAttribute or Magento\Catalog\Model\ResourceModel\Eav\Attribute or Magento\Catalog\Model\Category\Attribute or Magento\Checkout\Model\ShippingInformation or Magento\Sales\Model\Order\Status\History or Magento\Catalog\Model\Product\Option or Magento\Quote\Model\Quote\ProductOption or Magento\Tax\Model\Sales\Quote\ItemDetails or Magento\Tax\Model\TaxDetails\AppliedTax or Magento\Sales\Model\Order\Tax\Item or Magento\Bundle\Model\Option or Magento\Eav\Model\Entity\Attribute\Set or Magento\Quote\Model\Cart\Currency or Magento\Sales\Model\Order\Shipment\Comment or Magento\Catalog\Model\Product\Price\Cost or Magento\Quote\Model\Cart\Totals or Magento\Sales\Model\Order\Invoice\Item or Magento\Downloadable\Model\File\Content or Magento\Catalog\Model\ProductRender\PriceInfo or Magento\CatalogInventory\Model\Stock or Magento\Quote\Model\ShippingAssignment or Magento\Quote\Model\Quote\Payment or Magento\Catalog\Model\ProductRender\Image or Magento\Sales\Model\Order\Address or Magento\Tax\Model\TaxClass\Key or Magento\Sales\Model\Order\Creditmemo\Comment or Magento\Sales\Model\Order\Payment or Magento\Sales\Model\Order\Payment\Transaction or Magento\GiftMessage\Model\Message or Magento\Sales\Model\Order\Shipment\Item or Magento\Catalog\Model\Product\Price\TierPrice or Magento\Catalog\Model\Product\Attribute\Type or Magento\Tax\Model\TaxDetails\ItemDetails or Magento\Catalog\Model\CustomOptions\CustomOption or Magento\Catalog\Model\Pr...nder\FormattedPriceInfo or Magento\Sales\Model\Order or Magento\Catalog\Model\Product\Option\Type or Magento\Catalog\Model\Product\TierPrice or Magento\Tax\Model\Sales\Order\Details or Magento\Framework\Api\Te...erator\ExtensibleSample or Magento\Sales\Model\Order\Invoice\Comment or Magento\Sales\Model\Order\ShippingAssignment or Magento\Msrp\Model\ProductRender\MsrpPriceInfo or Magento\Sales\Model\Order\Item or Magento\Sales\Model\Order\Creditmemo or Magento\Sales\Model\Order\Invoice or Magento\Quote\Model\Shipping or Magento\Catalog\Model\Product\Price\BasePrice or Magento\Eav\Model\Entity\Attribute\Group or Magento\Tax\Model\Sales\Order\Tax or Magento\Quote\Model\Cart\TotalSegment or Magento\CatalogInventory\Model\Stock\Item or Magento\Catalog\Model\Pr...Price\PriceUpdateResult or Magento\Tax\Model\Calculation\Rate\Title or Magento\Sales\Model\Order\Shipment or Magento\Catalog\Model\Product\Price\SpecialPrice or Magento\Tax\Model\ClassModel or Magento\Bundle\Model\BundleOption or Magento\Tax\Model\TaxDetails\AppliedTaxRate or Magento\Bundle\Model\Source\Option\Type or Magento\Sales\Model\Order\Shipping or Magento\Quote\Model\Quote\Item or Magento\SalesRule\Model\Coupon or Magento\Tax\Model\TaxDetails\TaxDetails or Magento\Sales\Model\Order\ShippingTotal or Magento\Catalog\Model\ProductOption or Magento\Catalog\Model\ProductRender\Button. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

173
            $previousOrders = $this->getOrders($customer->/** @scrutinizer ignore-call */ getId());
Loading history...
174
            foreach ($previousOrders as $orderElement) {
175
                $orderHistory = new \PagaMasTarde\OrdersApiClient\Model\Order\User\OrderHistory();
176
                $orderHistory
177
                    ->setAmount(intval(100 * $orderElement['grand_total']))
178
                    ->setDate(new \DateTime($orderElement['created_at']))
0 ignored issues
show
Bug introduced by
new DateTime($orderElement['created_at']) of type DateTime is incompatible with the type string expected by parameter $date of PagaMasTarde\OrdersApiCl...OrderHistory::setDate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

178
                    ->setDate(/** @scrutinizer ignore-type */ new \DateTime($orderElement['created_at']))
Loading history...
179
                ;
180
                $orderUser->addOrderHistory($orderHistory);
181
            }
182
183
            $details = new \PagaMasTarde\OrdersApiClient\Model\Order\ShoppingCart\Details();
184
            $shippingCost = $quote->collectTotals()->getTotals()['shipping']->getData('value');
185
            $details->setShippingCost(intval(strval(100 * $shippingCost)));
186
            $items = $quote->getAllVisibleItems();
187
            foreach ($items as $key => $item) {
188
                $product = new \PagaMasTarde\OrdersApiClient\Model\Order\ShoppingCart\Details\Product();
189
                $product
190
                    ->setAmount(intval(100 * $item->getPrice()))
191
                    ->setQuantity($item->getQty())
192
                    ->setDescription($item->getName());
193
                $details->addProduct($product);
194
            }
195
196
            $orderShoppingCart = new \PagaMasTarde\OrdersApiClient\Model\Order\ShoppingCart();
197
            $orderShoppingCart
198
                ->setDetails($details)
199
                ->setOrderReference($quote->getId())
200
                ->setPromotedAmount(0)
201
                ->setTotalAmount(intval(strval(100 * $quote->getGrandTotal())))
202
            ;
203
204
            $orderConfigurationUrls = new \PagaMasTarde\OrdersApiClient\Model\Order\Configuration\Urls();
205
            $quoteId = $quote->getId();
206
            $okUrl = $this->_url->getUrl('paylater/notify', ['_query' => ['quoteId'=>$quoteId]]);
207
            $orderConfigurationUrls
208
                ->setCancel($cancelUrl)
209
                ->setKo($okUrl)
210
                ->setNotificationCallback($okUrl)
211
                ->setOk($okUrl)
212
            ;
213
214
            $orderChannel = new \PagaMasTarde\OrdersApiClient\Model\Order\Configuration\Channel();
215
            $orderChannel
216
                ->setAssistedSale(false)
217
                ->setType(\PagaMasTarde\OrdersApiClient\Model\Order\Configuration\Channel::ONLINE)
218
            ;
219
            $orderConfiguration = new \PagaMasTarde\OrdersApiClient\Model\Order\Configuration();
220
            $orderConfiguration
221
                ->setChannel($orderChannel)
222
                ->setUrls($orderConfigurationUrls)
223
            ;
224
225
            $metadataOrder = new \PagaMasTarde\OrdersApiClient\Model\Order\Metadata();
226
            $metadata = $this->getMetadata();
227
            foreach ($metadata as $key => $metadatum) {
228
                $metadataOrder->addMetadata($key, $metadatum);
229
            }
230
231
            $order = new \PagaMasTarde\OrdersApiClient\Model\Order();
232
            $order
233
                ->setConfiguration($orderConfiguration)
234
                ->setMetadata($metadataOrder)
235
                ->setShoppingCart($orderShoppingCart)
236
                ->setUser($orderUser)
237
            ;
238
239
            if ($this->config['public_key']=='' || $this->config['secret_key']=='') {
240
                throw new \Exception('Public and Secret Key not found');
241
            }
242
243
            $orderClient = new \PagaMasTarde\OrdersApiClient\Client(
244
                $this->config['public_key'],
245
                $this->config['secret_key']
246
            );
247
248
            $order = $orderClient->createOrder($order);
249
            if ($order instanceof \PagaMasTarde\OrdersApiClient\Model\Order) {
0 ignored issues
show
introduced by
$order is always a sub-type of PagaMasTarde\OrdersApiClient\Model\Order. If $order can have other possible types, add them to Controller/Payment/Index.php:98.
Loading history...
250
                $url = $order->getActionUrls()->getForm();
251
                $result = $this->insertRow($quote->getId(), $order->getId());
252
                if (!$result) {
253
                    throw new \Exception('Unable to save pmt-order-id');
254
                }
255
            } else {
256
                throw new \Exception('Order not created');
257
            }
258
        } catch (\Exception $exception) {
259
            $this->insertLog($exception);
260
            echo $cancelUrl;
261
            exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
262
        }
263
264
        $displayMode = $this->config['display_mode'];
265
        if (!$displayMode) {
266
            echo $url;
267
            exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
268
        } else {
269
            $iframeUrl = $this->_url->getUrl(
270
                "paylater/Payment/iframe",
271
                ['_query' => ["orderId"=>$order->getId()]]
272
            );
273
            echo $iframeUrl;
274
            exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
275
        }
276
    }
277
278
    /**
279
     * Get the orders of a customer
280
     * @param $customerId
281
     *
282
     * @return array
283
     */
284
    private function getOrders($customerId)
285
    {
286
        $orderCollection = array();
287
        if ($customerId!='') {
288
            $this->orderCollection->addAttributeToFilter('customer_id', $customerId)
289
                            ->addAttributeToFilter(
290
                                'status',
291
                                ['in' => ['processing','pending','complete']]
292
                            )
293
                            ->load();
294
            $orderCollection = $this->orderCollection->getData();
295
        }
296
        return $orderCollection;
297
    }
298
299
    /**
300
     * @return void|\Zend_Db_Statement_Interface
301
     * @throws \Zend_Db_Exception
302
     */
303
    private function checkDbTable()
304
    {
305
        $dbConnection = $this->dbObject->getConnection();
306
        $tableName = $this->dbObject->getTableName(self::ORDERS_TABLE);
307
        if (!$dbConnection->isTableExists($tableName)) {
308
            $table = $dbConnection
309
                ->newTable($tableName)
310
                ->addColumn('id', Table::TYPE_SMALLINT, null, array('primary'=>true))
311
                ->addColumn('order_id', Table::TYPE_TEXT, 50)
312
                ->addColumn('mg_order_id', Table::TYPE_TEXT, 50);
313
            return $dbConnection->createTable($table);
314
        }
315
316
        return;
317
    }
318
319
    /**
320
     * Create relationship between quote_id & pmt_order_id
321
     * @param $quoteId
322
     * @param $pmtOrderId
323
     *
324
     * @return int
325
     * @throws \Zend_Db_Exception
326
     */
327
    private function insertRow($quoteId, $pmtOrderId)
328
    {
329
        $this->checkDbTable();
330
        $dbConnection = $this->dbObject->getConnection();
331
        $tableName = $this->dbObject->getTableName(self::ORDERS_TABLE);
332
        return $dbConnection->insertOnDuplicate(
333
            $tableName,
334
            array('id'=>$quoteId,'order_id'=>$pmtOrderId),
335
            array('order_id')
336
        );
337
    }
338
339
    /**
340
     * @return array
341
     */
342
    private function getMetadata()
343
    {
344
        $curlInfo = curl_version();
345
        $curlVersion = $curlInfo['version'];
346
        $magentoVersion = $this->productMetadataInterface->getVersion();
347
        $moduleInfo = $this->moduleList->getOne('DigitalOrigin_Pmt');
348
        return array(  'magento' => $magentoVersion,
349
                       'pmt' => $moduleInfo['setup_version'],
350
                       'php' => phpversion(),
351
                       'curl' => $curlVersion);
352
    }
353
354
    /**
355
     * Check if log table exists, otherwise create it
356
     *
357
     * @return void|\Zend_Db_Statement_Interface
358
     * @throws \Zend_Db_Exception
359
     */
360
    private function checkDbLogTable()
361
    {
362
        /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */
363
        $dbConnection = $this->dbObject->getConnection();
364
        $tableName = $this->dbObject->getTableName(self::LOGS_TABLE);
365
        if (!$dbConnection->isTableExists($tableName)) {
366
            $table = $dbConnection
367
                ->newTable($tableName)
368
                ->addColumn('id', Table::TYPE_SMALLINT, null, array('nullable'=>false, 'auto_increment'=>true, 'primary'=>true))
369
                ->addColumn('log', Table::TYPE_TEXT, null, array('nullable'=>false))
370
                ->addColumn('createdAt', Table::TYPE_TIMESTAMP, null, array('nullable'=>false, 'default'=>Table::TIMESTAMP_INIT));
371
            return $dbConnection->createTable($table);
372
        }
373
374
        return;
375
    }
376
377
    /**
378
     * @param $exceptionMessage
379
     *
380
     * @throws \Zend_Db_Exception
381
     */
382
    private function insertLog($exceptionMessage)
383
    {
384
        if ($exceptionMessage instanceof \Exception) {
385
            $this->checkDbLogTable();
386
            $logObject          = new \stdClass();
387
            $logObject->message = $exceptionMessage->getMessage();
388
            $logObject->code    = $exceptionMessage->getCode();
389
            $logObject->line    = $exceptionMessage->getLine();
390
            $logObject->file    = $exceptionMessage->getFile();
391
            $logObject->trace   = $exceptionMessage->getTraceAsString();
392
393
            /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */
394
            $dbConnection = $this->dbObject->getConnection();
395
            $tableName    = $this->dbObject->getTableName(self::LOGS_TABLE);
396
            $dbConnection->insert($tableName, array('log' => json_encode($logObject)));
397
        }
398
    }
399
}
400