Skip to content

Commit

Permalink
Release 1.0.32
Browse files Browse the repository at this point in the history
  • Loading branch information
edgaraswallee committed Sep 26, 2024
1 parent 9215a09 commit 497fa75
Show file tree
Hide file tree
Showing 593 changed files with 4,100 additions and 4,025 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ account dashboard.

## Documentation

[Documentation](https://plugin-documentation.wallee.com/wallee-payment/jtl-5/1.0.31/docs/en/documentation.html)
[Documentation](https://plugin-documentation.wallee.com/wallee-payment/jtl-5/1.0.32/docs/en/documentation.html)

## License

Expand Down
157 changes: 80 additions & 77 deletions Services/WalleeTransactionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,39 @@

use JTL\Alert\Alert;
use JTL\Cart\CartItem;
use JTL\Checkout\Nummern;
use JTL\Catalog\Product\Preise;
use JTL\Checkout\Bestellung;
use JTL\Checkout\OrderHandler;
use JTL\Checkout\Zahlungsart;
use JTL\Helpers\PaymentMethod;
use JTL\Helpers\Tax;
use JTL\Mail\Mailer;
use JTL\Mail\Mail\Mail;
use JTL\Mail\Mailer;
use JTL\Plugin\Payment\Method;
use JTL\Plugin\Plugin;
use JTL\Session\Frontend;
use JTL\Shop;
use Plugin\jtl_wallee\WalleeHelper;
use Plugin\jtl_wallee\Services\WalleeMailService;
use stdClass;
use Wallee\Sdk\ApiClient;
use Wallee\Sdk\Model\{
AddressCreate,
Gender,
LineItemCreate,
LineItemType,
TaxCreate,
Transaction,
TransactionCreate,
TransactionInvoice,
TransactionPending,
TransactionState,
CreationEntityState,
CriteriaOperator,
EntityQuery,
EntityQueryFilter,
EntityQueryFilterType,
RefundState,
TransactionInvoiceState,
WebhookListener,
WebhookListenerCreate,
WebhookUrl,
WebhookUrlCreate,
};
use Wallee\Sdk\Model\{AddressCreate,
CreationEntityState,
CriteriaOperator,
EntityQuery,
EntityQueryFilter,
EntityQueryFilterType,
Gender,
LineItemCreate,
LineItemType,
RefundState,
TaxCreate,
Transaction,
TransactionCreate,
TransactionInvoice,
TransactionInvoiceState,
TransactionPending,
TransactionState,
WebhookListener,
WebhookListenerCreate,
WebhookUrl,
WebhookUrlCreate,};

class WalleeTransactionService
{
Expand Down Expand Up @@ -206,7 +200,8 @@ public function confirmTransaction(Transaction $transaction): void
$pendingTransaction->setSuccessUrl($successUrl . '?tID=' . $transactionId);
$pendingTransaction->setFailedUrl($failedUrl . '?tID=' . $transactionId);




$this->apiClient->getTransactionService()
->confirm($this->spaceId, $pendingTransaction);

Expand Down Expand Up @@ -503,40 +498,38 @@ public function createLocalWalleeTransaction(string $transactionId, array $order
*/
public function addIncommingPayment(string $transactionId, Bestellung $order, Transaction $transaction): void
{
$localTransaction = $this->getLocalWalleeTransactionById($transactionId);
if ($localTransaction->state !== TransactionState::FULFILL) {
$orderId = (int)$order->kBestellung;

if ($orderId === 0) {
$orderId = (int)$order->kBestellung;
if ($orderId === 0) {
return;
}
$this->updateTransactionStatus($transactionId, TransactionState::FULFILL);

$portalTransaction = $this->getTransactionFromPortal($transactionId);
if ($portalTransaction->getState() === TransactionState::FULFILL) {
// tzahlungseingang - table name of incomming payments
// kBestellung - table field which represents order ID
$incomingPaymentCheck = Shop::Container()->getDB()->selectSingleRow('tzahlungseingang', 'kBestellung', $orderId);
// We check if there's record for incomming payment for current order
if (!empty($incomingPaymentCheck->kZahlungseingang)) {
return;
}
$this->updateTransactionStatus($transactionId, TransactionState::FULFILL);

$portalTransaction = $this->getTransactionFromPortal($transactionId);
if ($portalTransaction->getState() === TransactionState::FULFILL) {
// tzahlungseingang - table name of incomming payments
// kBestellung - table field which represents order ID
$incommingPayment = Shop::Container()->getDB()->selectSingleRow('tzahlungseingang', 'kBestellung', $orderId);
// We check if there's record for incomming payment for current order
if (!empty($incommingPayment->kZahlungseingang)) {
return;
}

$paymentMethodEntity = new Zahlungsart((int)$order->kZahlungsart);
$moduleId = $paymentMethodEntity->cModulId ?? '';
$paymentMethod = new Method($moduleId);
$paymentMethod->setOrderStatusToPaid($order);
$incomingPayment = new \stdClass();
$incomingPayment->fBetrag = $transaction->getAuthorizationAmount();
$incomingPayment->cISO = $transaction->getCurrency();
$incomingPayment->cZahlungsanbieter = $order->cZahlungsartName;
$incomingPayment->cHinweis = $transactionId;
$paymentMethod->addIncomingPayment($order, $incomingPayment);

// At this stage, the transaction goes directly to fulfill, so it's also authorized.
// Even when the sendEmail is invoked here, the email will be or not sent according to several conditions.
$this->sendEmail($orderId, 'fulfill');
}
$paymentMethodEntity = new Zahlungsart((int)$order->kZahlungsart);
$moduleId = $paymentMethodEntity->cModulId ?? '';
$paymentMethod = new Method($moduleId);
$paymentMethod->setOrderStatusToPaid($order);
$incomingPayment = new \stdClass();
$incomingPayment->fBetrag = $transaction->getAuthorizationAmount();
$incomingPayment->cISO = $transaction->getCurrency();
$incomingPayment->cZahlungsanbieter = $order->cZahlungsartName;
$incomingPayment->cHinweis = $transactionId;
$paymentMethod->addIncomingPayment($order, $incomingPayment);

// At this stage, the transaction goes directly to fulfill, so it's also authorized.
// Even when the sendEmail is invoked here, the email will be or not sent according to several conditions.
$this->sendEmail($orderId, 'fulfill');
} else {
Shop::Container()->getLogService()->error('addIncommingPayment payment was not created, because transaction was not in FULFILL status. TransactionId: ' . $transactionId);
}
}

Expand All @@ -549,34 +542,26 @@ public function createOrderAfterPayment(int $transactionId): int
$_SESSION['finalize'] = true;

$transaction = $this->getTransactionFromPortal($transactionId);

$orderNr = $transaction->getMetaData()['order_nr'];
$orderHandler = new OrderHandler(Shop::Container()->getDB(), Frontend::getCustomer(), Frontend::getCart());

if ($this->isPreventFromDuplicatedOrders()) {
// We check if order exist with such order nr. If yes, we select it's data, if not - we create it.
// This check prevents only in these cases when webhook is triggered more than once or user refresh the page, or
// got lost internet connection. It's more like catching edge case
$data = Shop::Container()->getDB()->select(
'tbestellung',
'cBestellNr',
$orderNr,
null,
null,
null,
null,
false,
'kBestellung'
);
if ($data !== null && isset($data->kBestellung)) {
$order = new Bestellung((int)$data->kBestellung);
} else {
$data = $this->getOrderIfExists($orderNr);
if ($data === null) {
// Order wasn't created before, so we insert new record
$order = $orderHandler->finalizeOrder($orderNr, false);
} else {
// We select order from database and creating backup with all session data
$order = new Bestellung((int)$data->kBestellung);
}
} else {
// Updates order number for next order. Increase by 1 if is needed
$lastOrderNo = $transaction->getMetaData()['order_no'];
WalleeHelper::createOrderNo(true, $lastOrderNo);
// Always inserting new order
$order = $orderHandler->finalizeOrder($orderNr, false);
}
$this->updateLocalWalleeTransaction((string)$transactionId, TransactionState::AUTHORIZED, (int)$order->kBestellung);
Expand All @@ -589,6 +574,24 @@ public function createOrderAfterPayment(int $transactionId): int

return (int)$order->kBestellung;
}

/**
* @param string $orderNr
* @return void
*/
public function getOrderIfExists(string $orderNr): ?stdClass
{
$db = Shop::Container()->getDB();

// Prepare and execute the query directly
$query = "SELECT kBestellung FROM tbestellung WHERE cBestellNr = :orderNr ORDER BY dErstellt DESC LIMIT 1";
$params = ['orderNr' => $orderNr];

$data = $db->executeQueryPrepared($query, $params, 1); // The '1' here signifies to fetch one row only

// Check if data is retrieved, otherwise return null
return $data ?: null;
}

/**
* @param string $transactionId
Expand Down Expand Up @@ -848,7 +851,7 @@ private function isPreventFromDuplicatedOrders(): bool
$config = WalleeHelper::getConfigByID($this->plugin->getId());
$preventFromDuplicatedOrders = $config[WalleeHelper::PREVENT_FROM_DUPLICATED_ORDERS] ?? null;

return $preventFromDuplicatedOrders === 'YES';
return strtolower($preventFromDuplicatedOrders) === 'yes';
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Plugin\jtl_wallee\Webhooks\Strategies;

use JTL\Shop;
use Plugin\jtl_wallee\Services\WalleeOrderService;
use Plugin\jtl_wallee\Services\WalleeTransactionService;
use Plugin\jtl_wallee\Webhooks\Strategies\Interfaces\WalleeOrderUpdateStrategyInterface;
Expand Down Expand Up @@ -39,8 +40,9 @@ public function updateOrderStatus(string $entityId): void
->getTransaction();

$transactionId = $transaction->getId();
$localTransaction = $this->transactionService->getLocalWalleeTransactionById((string)$transactionId);
$orderId = (int)$localTransaction->order_id;
$orderNr = $transaction->getMetaData()['order_nr'];
$orderData = $this->transactionService->getOrderIfExists($orderNr);
$orderId = (int)$orderData->kBestellung;

switch ($transactionInvoice->getState()) {
case TransactionInvoiceState::DERECOGNIZED:
Expand Down
16 changes: 5 additions & 11 deletions Webhooks/Strategies/WalleeNameOrderUpdateTransactionStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,12 @@

use JTL\Checkout\Bestellung;
use JTL\Checkout\Zahlungsart;
use JTL\Customer\Customer;
use JTL\Mail\Mail\Mail;
use JTL\Mail\Mailer;
use JTL\Plugin\Data\PaymentMethod;
use JTL\Plugin\Payment\Method;
use JTL\Plugin\Plugin;
use JTL\Session\Frontend;
use JTL\Shop;
use Plugin\jtl_wallee\Services\WalleeOrderService;
use Plugin\jtl_wallee\Services\WalleeTransactionService;
use Plugin\jtl_wallee\Webhooks\Strategies\Interfaces\WalleeOrderUpdateStrategyInterface;
use Plugin\jtl_wallee\WalleeHelper;
use stdClass;
use Wallee\Sdk\Model\Transaction;
use Wallee\Sdk\Model\TransactionState;

Expand Down Expand Up @@ -52,11 +45,12 @@ public function updateOrderStatus(string $entityId): void
{
$transaction = $this->transactionService->getTransactionFromPortal($entityId);
$transactionId = $transaction->getId();

$localTransaction = $this->transactionService->getLocalWalleeTransactionById((string)$transactionId);
$orderId = (int)$localTransaction->order_id;

$orderNr = $transaction->getMetaData()['order_nr'];
$orderData = $this->transactionService->getOrderIfExists($orderNr);
$orderId = (int)$orderData->kBestellung;
$transactionState = $transaction->getState();

switch ($transactionState) {
case TransactionState::FULFILL:
$order = new Bestellung($orderId);
Expand Down
Loading

0 comments on commit 497fa75

Please sign in to comment.