Skip to content

Commit

Permalink
PLUG-106: Do not send order emails upfront; cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lighe committed Jun 13, 2024
1 parent 2bb7906 commit 30bfebd
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 12 deletions.
1 change: 0 additions & 1 deletion Controller/Checkout/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\Plugin\AuthenticationException;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order;
use TrueLayer\Connect\Api\Log\LogService;
use TrueLayer\Connect\Service\Order\HPPService;
use TrueLayer\Connect\Service\Order\PaymentCreationService;
Expand Down
1 change: 0 additions & 1 deletion Controller/Checkout/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

use Exception;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Controller\ResultInterface;
Expand Down
10 changes: 9 additions & 1 deletion Gateway/Command/AuthorizePaymentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Magento\Payment\Gateway\Helper\SubjectReader;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order\Payment;
use TrueLayer\Connect\Api\Log\LogService as LogRepository;

class AuthorizePaymentCommand extends AbstractCommand
Expand All @@ -27,9 +28,16 @@ public function __construct(OrderRepositoryInterface $orderRepository, LogReposi
*/
protected function executeCommand(array $subject): void
{
/** @var Payment $payment */
$payment = SubjectReader::readPayment($subject)->getPayment();

// Set the transaction to pending so that the order is created in a pending state
// The pending state set by magento is not the one we want, so we will overwrite that in OrderPlacedHandler
// This status will also help third party code that may be listening to transactions.
SubjectReader::readPayment($subject)->getPayment()->setIsTransactionPending(true);
$payment->setIsTransactionPending(true);

// Do not send emails when the order is placed
// We will instead send emails when the payment is settled
$payment->getOrder()->setCanSendNewEmailFlag(false);
}
}
1 change: 0 additions & 1 deletion Model/Webapi/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use TrueLayer\Connect\Service\Order\PaymentUpdate\PaymentFailedService;
use TrueLayer\Connect\Service\Order\PaymentUpdate\PaymentSettledService;
use TrueLayer\Connect\Service\Order\RefundUpdate\RefundFailedService;
use TrueLayer\Connect\Helper\ValidationService;
use TrueLayer\Exceptions\Exception;
use TrueLayer\Exceptions\InvalidArgumentException;
use TrueLayer\Exceptions\SignerException;
Expand Down
4 changes: 3 additions & 1 deletion Observer/CreditMemoObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace TrueLayer\Connect\Observer;

use Exception;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\LocalizedException;
Expand Down Expand Up @@ -36,6 +37,7 @@ public function __construct(RefundTransactionRepositoryInterface $refundTransact
/**
* @param Observer $observer
* @throws LocalizedException
* @throws Exception
*/
public function execute(Observer $observer)
{
Expand All @@ -56,7 +58,7 @@ public function execute(Observer $observer)
RefundTransactionDataInterface::ORDER_ID => $order->getEntityId(),
RefundTransactionDataInterface::CREDITMEMO_ID => ['null' => true],
], [RefundTransactionDataInterface::ENTITY_ID => 'DESC']);
} catch (\Exception $e) {
} catch (Exception $e) {
$this->logger->error('Failed loading transaction', $e);
throw $e;
}
Expand Down
5 changes: 3 additions & 2 deletions Service/Log/LogService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace TrueLayer\Connect\Service\Log;

use Exception;
use Monolog\Logger;
use TrueLayer\Connect\Api\Config\RepositoryInterface as ConfigProvider;
use TrueLayer\Connect\Api\Log\LogService as LogServiceInterface;
Expand Down Expand Up @@ -105,7 +106,7 @@ private function buildMessage(string $msg, $data = ''): string
*/
private function convertDataToString($data): string
{
if ($data instanceof \Exception) {
if ($data instanceof Exception) {
return $data->getMessage() . " " . $data->getTraceAsString();
}

Expand All @@ -119,6 +120,6 @@ private function convertDataToString($data): string
}
}

return "{$data}";
return "$data";
}
}
2 changes: 1 addition & 1 deletion Service/Order/BaseTransactionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function execute(Callable $fn): void
try {
$this->logger->debug('Execute logic');
$fn($transaction);
} catch (\Exception $e) {
} catch (Exception $e) {
$this->logger->error('Exception in transaction', $e);
throw $e;
} finally {
Expand Down
2 changes: 1 addition & 1 deletion Service/Order/PaymentUpdate/PaymentFailedService.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private function cancelOrder(PaymentTransactionDataInterface $transaction, strin
}

$niceMessage = PaymentFailureReasonHelper::getHumanReadableLabel($failureReason);
$orderComment = "Order cancelled. {$niceMessage} ($failureReason)";
$orderComment = "Order cancelled. $niceMessage ($failureReason)";
$order->addStatusToHistory($order->getStatus(), $orderComment, true);
$this->orderRepository->save($order);
$this->logger->debug('Order comment added');
Expand Down
4 changes: 2 additions & 2 deletions Service/Order/PaymentUpdate/PaymentSettledService.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function __construct(
* @throws LocalizedException
* @throws Exception
*/
public function handle(string $paymentId)
public function handle(string $paymentId): void
{
$prefix = "PaymentSettledService $paymentId";
$this->logger->addPrefix($prefix);
Expand All @@ -78,7 +78,7 @@ public function handle(string $paymentId)
$this->logger->removePrefix($prefix);
}

private function updateOrder(OrderInterface $order, string $paymentId)
private function updateOrder(OrderInterface $order, string $paymentId): void
{
// Update order payment
$payment = $order->getPayment();
Expand Down
1 change: 0 additions & 1 deletion Service/Order/RefundUpdate/RefundTransactionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Magento\Framework\Exception\NoSuchEntityException;
use TrueLayer\Connect\Api\Log\LogService;
use TrueLayer\Connect\Api\Transaction\BaseTransactionDataInterface;
use TrueLayer\Connect\Api\Transaction\Payment\PaymentTransactionRepositoryInterface;
use TrueLayer\Connect\Api\Transaction\Refund\RefundTransactionRepositoryInterface;
use TrueLayer\Connect\Service\Order\BaseTransactionService;

Expand Down

0 comments on commit 30bfebd

Please sign in to comment.