Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions Model/Webhook/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

namespace Iways\PayPalPlus\Model\Webhook;

use Magento\Sales\Model\Order\Email\Sender\InvoiceSender;

/**
* Iways PayPalPlus Event Handler
*
Expand Down Expand Up @@ -67,6 +69,13 @@ class Event
*/
protected $salesOrderFactory;

/**
* Protected $invoiceSender
*
* @var \Magento\Sales\Model\Order\Email\Sender\InvoiceSender
*/
protected $invoiceSender;

/**
* Protected $logger
*
Expand All @@ -77,10 +86,12 @@ class Event
public function __construct(
\Magento\Sales\Model\Order\Payment\TransactionFactory $salesOrderPaymentTransactionFactory,
\Magento\Sales\Model\OrderFactory $salesOrderFactory,
\Magento\Sales\Model\Order\Email\Sender\InvoiceSender $invoiceSender,
\Psr\Log\LoggerInterface $logger
) {
$this->salesOrderPaymentTransactionFactory = $salesOrderPaymentTransactionFactory;
$this->salesOrderFactory = $salesOrderFactory;
$this->invoiceSender = $invoiceSender;
$this->logger = $logger;
}

Expand Down Expand Up @@ -163,13 +174,20 @@ protected function paymentSaleCompleted(\PayPal\Api\WebhookEvent $webhookEvent)
// notify customer
$invoice = $payment->getCreatedInvoice();
if ($invoice && !$this->_order->getEmailSent()) {
$this->_order->queueNewOrderEmail()
->addStatusHistoryComment(
__(
'Notified customer about invoice #%1.',
$invoice->getIncrementId()
)
)->setIsCustomerNotified(true)->save();
try {
$this->invoiceSender->send($invoice);
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
}

$this->_order->addStatusToHistory(
false,
__(
'Notified customer about invoice #%1.',
$invoice->getIncrementId()
),
true
)->save();
}
}

Expand Down