Completed
Push — master ( 178071...b132cf )
by Florian
04:12
created

Paid::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
c 1
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
3
/**
4
 * PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * PAYONE Magento 2 Connector is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>.
16
 *
17
 * PHP version 5
18
 *
19
 * @category  Payone
20
 * @package   Payone_Magento2_Plugin
21
 * @author    FATCHIP GmbH <[email protected]>
22
 * @copyright 2003 - 2016 Payone GmbH
23
 * @license   <http://www.gnu.org/licenses/> GNU Lesser General Public License
24
 * @link      http://www.payone.de
25
 */
26
27
namespace Payone\Core\Observer\Transactionstatus;
28
29
use Magento\Sales\Model\Order;
30
use Magento\Sales\Model\Order\Invoice;
31
use Magento\Sales\Model\Service\InvoiceService;
32
use Magento\Sales\Model\Order\Email\Sender\InvoiceSender;
33
use Magento\Framework\Event\ObserverInterface;
34
use Magento\Framework\Event\Observer;
35
use Payone\Core\Helper\Base;
36
use Payone\Core\Model\PayoneConfig;
37
38
/**
39
 * Event observer for Transactionstatus paid
40
 */
41
class Paid implements ObserverInterface
42
{
43
    /**
44
     * InvoiceService object
45
     *
46
     * @var InvoiceService
47
     */
48
    protected $invoiceService;
49
50
    /**
51
     * InvoiceSender object
52
     *
53
     * @var InvoiceSender
54
     */
55
    protected $invoiceSender;
56
57
    /**
58
     * Payone base helper
59
     *
60
     * @var Base
61
     */
62
    protected $baseHelper;
63
64
    /**
65
     * Constructor.
66
     *
67
     * @param InvoiceService $invoiceService
68
     * @param InvoiceSender  $invoiceSender
69
     * @param Base           $baseHelper
70
     */
71
    public function __construct(InvoiceService $invoiceService, InvoiceSender $invoiceSender, Base $baseHelper)
72
    {
73
        $this->invoiceService = $invoiceService;
74
        $this->invoiceSender = $invoiceSender;
75
        $this->baseHelper = $baseHelper;
76
    }
77
78
    /**
79
     * Generate an invoice for the order to mark the order as paid
80
     *
81
     * @param  Observer $observer
82
     * @return void
83
     */
84
    public function execute(Observer $observer)
85
    {
86
        /* @var $oOrder Order */
87
        $oOrder = $observer->getOrder();
88
89
        // order is not guaranteed to exist if using transaction status forwarding
90
        // advance payment should not create an invoice
91
        if (null === $oOrder || $oOrder->getPayment()->getMethodInstance()->getCode() == PayoneConfig::METHOD_ADVANCE_PAYMENT){
92
            return;
93
        }
94
95
        $oInvoice = $this->invoiceService->prepareInvoice($oOrder);
96
        $oInvoice->setRequestedCaptureCase(Invoice::CAPTURE_OFFLINE);
97
        $oInvoice->setTransactionId($oOrder->getPayment()->getLastTransId());
98
        $oInvoice->register();
99
        $oInvoice->save();
100
101
        $oOrder->save();
102
103
        if ($this->baseHelper->getConfigParam('send_invoice_email', 'emails')) {
104
            $this->invoiceSender->send($oInvoice);
105
        }
106
    }
107
}
108