4
4
5
5
use JTL \Alert \Alert ;
6
6
use JTL \Cart \CartItem ;
7
- use JTL \Checkout \Nummern ;
8
- use JTL \Catalog \Product \Preise ;
9
7
use JTL \Checkout \Bestellung ;
10
8
use JTL \Checkout \OrderHandler ;
11
9
use JTL \Checkout \Zahlungsart ;
12
- use JTL \Helpers \PaymentMethod ;
13
10
use JTL \Helpers \Tax ;
14
- use JTL \Mail \Mailer ;
15
11
use JTL \Mail \Mail \Mail ;
12
+ use JTL \Mail \Mailer ;
16
13
use JTL \Plugin \Payment \Method ;
17
- use JTL \Plugin \Plugin ;
18
14
use JTL \Session \Frontend ;
19
15
use JTL \Shop ;
20
16
use Plugin \jtl_wallee \WalleeHelper ;
21
- use Plugin \ jtl_wallee \ Services \ WalleeMailService ;
17
+ use stdClass ;
22
18
use Wallee \Sdk \ApiClient ;
23
- use Wallee \Sdk \Model \{
24
- AddressCreate ,
25
- Gender ,
26
- LineItemCreate ,
27
- LineItemType ,
28
- TaxCreate ,
29
- Transaction ,
30
- TransactionCreate ,
31
- TransactionInvoice ,
32
- TransactionPending ,
33
- TransactionState ,
34
- CreationEntityState ,
35
- CriteriaOperator ,
36
- EntityQuery ,
37
- EntityQueryFilter ,
38
- EntityQueryFilterType ,
39
- RefundState ,
40
- TransactionInvoiceState ,
41
- WebhookListener ,
42
- WebhookListenerCreate ,
43
- WebhookUrl ,
44
- WebhookUrlCreate ,
45
- };
19
+ use Wallee \Sdk \Model \{AddressCreate ,
20
+ CreationEntityState ,
21
+ CriteriaOperator ,
22
+ EntityQuery ,
23
+ EntityQueryFilter ,
24
+ EntityQueryFilterType ,
25
+ Gender ,
26
+ LineItemCreate ,
27
+ LineItemType ,
28
+ RefundState ,
29
+ TaxCreate ,
30
+ Transaction ,
31
+ TransactionCreate ,
32
+ TransactionInvoice ,
33
+ TransactionInvoiceState ,
34
+ TransactionPending ,
35
+ TransactionState ,
36
+ WebhookListener ,
37
+ WebhookListenerCreate ,
38
+ WebhookUrl ,
39
+ WebhookUrlCreate , };
46
40
47
41
class WalleeTransactionService
48
42
{
@@ -206,7 +200,8 @@ public function confirmTransaction(Transaction $transaction): void
206
200
$ pendingTransaction ->setSuccessUrl ($ successUrl . '?tID= ' . $ transactionId );
207
201
$ pendingTransaction ->setFailedUrl ($ failedUrl . '?tID= ' . $ transactionId );
208
202
209
-
203
+
204
+
210
205
$ this ->apiClient ->getTransactionService ()
211
206
->confirm ($ this ->spaceId , $ pendingTransaction );
212
207
@@ -503,40 +498,38 @@ public function createLocalWalleeTransaction(string $transactionId, array $order
503
498
*/
504
499
public function addIncommingPayment (string $ transactionId , Bestellung $ order , Transaction $ transaction ): void
505
500
{
506
- $ localTransaction = $ this ->getLocalWalleeTransactionById ($ transactionId );
507
- if ($ localTransaction ->state !== TransactionState::FULFILL ) {
508
- $ orderId = (int )$ order ->kBestellung ;
509
-
510
- if ($ orderId === 0 ) {
501
+ $ orderId = (int )$ order ->kBestellung ;
502
+ if ($ orderId === 0 ) {
503
+ return ;
504
+ }
505
+ $ this ->updateTransactionStatus ($ transactionId , TransactionState::FULFILL );
506
+
507
+ $ portalTransaction = $ this ->getTransactionFromPortal ($ transactionId );
508
+ if ($ portalTransaction ->getState () === TransactionState::FULFILL ) {
509
+ // tzahlungseingang - table name of incomming payments
510
+ // kBestellung - table field which represents order ID
511
+ $ incomingPaymentCheck = Shop::Container ()->getDB ()->selectSingleRow ('tzahlungseingang ' , 'kBestellung ' , $ orderId );
512
+ // We check if there's record for incomming payment for current order
513
+ if (!empty ($ incomingPaymentCheck ->kZahlungseingang )) {
511
514
return ;
512
515
}
513
- $ this ->updateTransactionStatus ($ transactionId , TransactionState::FULFILL );
514
-
515
- $ portalTransaction = $ this ->getTransactionFromPortal ($ transactionId );
516
- if ($ portalTransaction ->getState () === TransactionState::FULFILL ) {
517
- // tzahlungseingang - table name of incomming payments
518
- // kBestellung - table field which represents order ID
519
- $ incommingPayment = Shop::Container ()->getDB ()->selectSingleRow ('tzahlungseingang ' , 'kBestellung ' , $ orderId );
520
- // We check if there's record for incomming payment for current order
521
- if (!empty ($ incommingPayment ->kZahlungseingang )) {
522
- return ;
523
- }
524
516
525
- $ paymentMethodEntity = new Zahlungsart ((int )$ order ->kZahlungsart );
526
- $ moduleId = $ paymentMethodEntity ->cModulId ?? '' ;
527
- $ paymentMethod = new Method ($ moduleId );
528
- $ paymentMethod ->setOrderStatusToPaid ($ order );
529
- $ incomingPayment = new \stdClass ();
530
- $ incomingPayment ->fBetrag = $ transaction ->getAuthorizationAmount ();
531
- $ incomingPayment ->cISO = $ transaction ->getCurrency ();
532
- $ incomingPayment ->cZahlungsanbieter = $ order ->cZahlungsartName ;
533
- $ incomingPayment ->cHinweis = $ transactionId ;
534
- $ paymentMethod ->addIncomingPayment ($ order , $ incomingPayment );
535
-
536
- // At this stage, the transaction goes directly to fulfill, so it's also authorized.
537
- // Even when the sendEmail is invoked here, the email will be or not sent according to several conditions.
538
- $ this ->sendEmail ($ orderId , 'fulfill ' );
539
- }
517
+ $ paymentMethodEntity = new Zahlungsart ((int )$ order ->kZahlungsart );
518
+ $ moduleId = $ paymentMethodEntity ->cModulId ?? '' ;
519
+ $ paymentMethod = new Method ($ moduleId );
520
+ $ paymentMethod ->setOrderStatusToPaid ($ order );
521
+ $ incomingPayment = new \stdClass ();
522
+ $ incomingPayment ->fBetrag = $ transaction ->getAuthorizationAmount ();
523
+ $ incomingPayment ->cISO = $ transaction ->getCurrency ();
524
+ $ incomingPayment ->cZahlungsanbieter = $ order ->cZahlungsartName ;
525
+ $ incomingPayment ->cHinweis = $ transactionId ;
526
+ $ paymentMethod ->addIncomingPayment ($ order , $ incomingPayment );
527
+
528
+ // At this stage, the transaction goes directly to fulfill, so it's also authorized.
529
+ // Even when the sendEmail is invoked here, the email will be or not sent according to several conditions.
530
+ $ this ->sendEmail ($ orderId , 'fulfill ' );
531
+ } else {
532
+ Shop::Container ()->getLogService ()->error ('addIncommingPayment payment was not created, because transaction was not in FULFILL status. TransactionId: ' . $ transactionId );
540
533
}
541
534
}
542
535
@@ -549,34 +542,26 @@ public function createOrderAfterPayment(int $transactionId): int
549
542
$ _SESSION ['finalize ' ] = true ;
550
543
551
544
$ transaction = $ this ->getTransactionFromPortal ($ transactionId );
552
-
553
545
$ orderNr = $ transaction ->getMetaData ()['order_nr ' ];
554
546
$ orderHandler = new OrderHandler (Shop::Container ()->getDB (), Frontend::getCustomer (), Frontend::getCart ());
555
547
556
548
if ($ this ->isPreventFromDuplicatedOrders ()) {
557
549
// We check if order exist with such order nr. If yes, we select it's data, if not - we create it.
558
550
// This check prevents only in these cases when webhook is triggered more than once or user refresh the page, or
559
551
// got lost internet connection. It's more like catching edge case
560
- $ data = Shop::Container ()->getDB ()->select (
561
- 'tbestellung ' ,
562
- 'cBestellNr ' ,
563
- $ orderNr ,
564
- null ,
565
- null ,
566
- null ,
567
- null ,
568
- false ,
569
- 'kBestellung '
570
- );
571
- if ($ data !== null && isset ($ data ->kBestellung )) {
572
- $ order = new Bestellung ((int )$ data ->kBestellung );
573
- } else {
552
+ $ data = $ this ->getOrderIfExists ($ orderNr );
553
+ if ($ data === null ) {
554
+ // Order wasn't created before, so we insert new record
574
555
$ order = $ orderHandler ->finalizeOrder ($ orderNr , false );
556
+ } else {
557
+ // We select order from database and creating backup with all session data
558
+ $ order = new Bestellung ((int )$ data ->kBestellung );
575
559
}
576
560
} else {
577
561
// Updates order number for next order. Increase by 1 if is needed
578
562
$ lastOrderNo = $ transaction ->getMetaData ()['order_no ' ];
579
563
WalleeHelper::createOrderNo (true , $ lastOrderNo );
564
+ // Always inserting new order
580
565
$ order = $ orderHandler ->finalizeOrder ($ orderNr , false );
581
566
}
582
567
$ this ->updateLocalWalleeTransaction ((string )$ transactionId , TransactionState::AUTHORIZED , (int )$ order ->kBestellung );
@@ -589,6 +574,24 @@ public function createOrderAfterPayment(int $transactionId): int
589
574
590
575
return (int )$ order ->kBestellung ;
591
576
}
577
+
578
+ /**
579
+ * @param string $orderNr
580
+ * @return void
581
+ */
582
+ public function getOrderIfExists (string $ orderNr ): ?stdClass
583
+ {
584
+ $ db = Shop::Container ()->getDB ();
585
+
586
+ // Prepare and execute the query directly
587
+ $ query = "SELECT kBestellung FROM tbestellung WHERE cBestellNr = :orderNr ORDER BY dErstellt DESC LIMIT 1 " ;
588
+ $ params = ['orderNr ' => $ orderNr ];
589
+
590
+ $ data = $ db ->executeQueryPrepared ($ query , $ params , 1 ); // The '1' here signifies to fetch one row only
591
+
592
+ // Check if data is retrieved, otherwise return null
593
+ return $ data ?: null ;
594
+ }
592
595
593
596
/**
594
597
* @param string $transactionId
@@ -848,7 +851,7 @@ private function isPreventFromDuplicatedOrders(): bool
848
851
$ config = WalleeHelper::getConfigByID ($ this ->plugin ->getId ());
849
852
$ preventFromDuplicatedOrders = $ config [WalleeHelper::PREVENT_FROM_DUPLICATED_ORDERS ] ?? null ;
850
853
851
- return $ preventFromDuplicatedOrders === 'YES ' ;
854
+ return strtolower ( $ preventFromDuplicatedOrders) === 'yes ' ;
852
855
}
853
856
854
857
/**
0 commit comments