Skip to content

Commit

Permalink
Merge pull request #272 from OXID-eSales/b-7.0.x-cancelled-order-UNZE…
Browse files Browse the repository at this point in the history
…R-386

B 7.0.x cancelled order unzer 386
  • Loading branch information
mariolorenz authored May 30, 2024
2 parents 337f9b2 + b83d71d commit e6ecc23
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 20 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [2.2.0] - 2024-??-??

### NEW
- provide content for smarty and twig
Expand Down
66 changes: 48 additions & 18 deletions src/Controller/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
use OxidEsales\Eshop\Application\Model\Country;
use OxidEsales\Eshop\Application\Model\Order;
use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Application\Model\Payment;
use OxidEsales\Eshop\Core\DatabaseProvider;
use OxidEsales\Eshop\Core\Exception\DatabaseErrorException;
use OxidEsales\Eshop\Core\Registry;
use OxidSolutionCatalysts\Unzer\Exception\Redirect;
use OxidSolutionCatalysts\Unzer\Exception\RedirectWithMessage;
use OxidSolutionCatalysts\Unzer\Model\Payment;
use OxidSolutionCatalysts\Unzer\Model\Order as UnzerOrder;
use OxidSolutionCatalysts\Unzer\Model\Payment as UnzerPayment;
use OxidSolutionCatalysts\Unzer\Service\ModuleSettings;
use OxidSolutionCatalysts\Unzer\Service\Payment as PaymentService;
use OxidSolutionCatalysts\Unzer\Service\ResponseHandler;
Expand All @@ -25,6 +27,7 @@
use OxidSolutionCatalysts\Unzer\Traits\ServiceContainer;
use OxidSolutionCatalysts\Unzer\Service\UnzerDefinitions;
use OxidSolutionCatalysts\Unzer\Core\UnzerDefinitions as CoreUnzerDefinitions;
use UnzerSDK\Constants\PaymentState;
use UnzerSDK\Exceptions\UnzerApiException;

/**
Expand Down Expand Up @@ -109,10 +112,12 @@ public function unzerExecuteAfterRedirect(): void
// get basket contents
$oUser = $this->getUser();
$oBasket = Registry::getSession()->getBasket();
if ($oBasket->getProductsCount()) {
if (!$oBasket->getProductsCount()) {
return;
}
$oDB = DatabaseProvider::getDb();

/** @var \OxidSolutionCatalysts\Unzer\Model\Order $oOrder */
/** @var UnzerOrder $oOrder */
$oOrder = $this->getActualOrder();

$oDB->startTransaction();
Expand All @@ -127,28 +132,31 @@ public function unzerExecuteAfterRedirect(): void
$unzerService = $this->getServiceFromContainer(Unzer::class);
Registry::getSession()->setVariable('orderDisableSqlActiveSnippet', false);

if (stripos($nextStep, 'thankyou') !== false) {
if (stripos($nextStep, 'thankyou') !== false) {
$oDB->commitTransaction();
$paymentService = $this->getServiceFromContainer(PaymentService::class);

$paymentService = $this->getServiceFromContainer(PaymentService::class);
if ($unzerService->ifImmediatePostAuthCollect($paymentService)) {
$paymentService->doUnzerCollect(
$oOrder,
$oUser->getId(),
$oBasket->getDiscountedProductsBruttoPrice()
);
}
if ($this->isPaymentCancelled($paymentService)) {
$this->redirectUserToCheckout($unzerService, $oOrder);
}

throw new Redirect($unzerService->prepareRedirectUrl($nextStep));
if ($unzerService->ifImmediatePostAuthCollect($paymentService)) {
$paymentService->doUnzerCollect(
$oOrder,
$oUser->getId(),
$oBasket->getDiscountedProductsBruttoPrice()
);
}

throw new Redirect($unzerService->prepareRedirectUrl($nextStep));
}

$oDB->rollbackTransaction();
$translator = $this->getServiceFromContainer(Translator::class);
throw new RedirectWithMessage(
$unzerService->prepareRedirectUrl($nextStep),
$translator->translate('OSCUNZER_ERROR_DURING_CHECKOUT')
);
}
}

/**
Expand Down Expand Up @@ -195,7 +203,7 @@ public function isSepaConfirmed(): ?bool
*/
public function saveUnzerTransaction(): void
{
/** @var \OxidSolutionCatalysts\Unzer\Model\Order $order */
/** @var UnzerOrder $order */
$order = $this->getActualOrder();
$order->initWriteTransactionToDB();
}
Expand Down Expand Up @@ -244,7 +252,7 @@ public function getUserCountryIso(): string
*/
public function getActualOrder(): Order
{
if (!($this->actualOrder instanceof \OxidSolutionCatalysts\Unzer\Model\Order)) {
if (!($this->actualOrder instanceof Order)) {
$this->actualOrder = oxNew(Order::class);
/** @var string $sess_challenge */
$sess_challenge = Registry::getSession()->getVariable('sess_challenge');
Expand Down Expand Up @@ -302,7 +310,7 @@ public function executeoscunzer(): ?string
}

$paymentService = $this->getServiceFromContainer(PaymentService::class);
/** @var \OxidEsales\Eshop\Application\Model\Payment $payment */
/** @var Payment $payment */
$payment = $this->getPayment();
$paymentOk = $paymentService->executeUnzerPayment($payment);

Expand All @@ -320,7 +328,7 @@ public function executeoscunzer(): ?string
*/
public function getExecuteFnc()
{
/** @var Payment $payment */
/** @var UnzerPayment $payment */
$payment = $this->getPayment();
if (
$payment->isUnzerPayment()
Expand Down Expand Up @@ -380,4 +388,26 @@ protected function getTrancactionIds(): array
}
return $result;
}

private function isPaymentCancelled(PaymentService $paymentService): bool
{
$return = false;
$sessionUnzerPaymentObj = $paymentService->getSessionUnzerPayment();
if ($sessionUnzerPaymentObj !== null) {
$return = $sessionUnzerPaymentObj->getState() === PaymentState::STATE_CANCELED;
}

return $return;
}

private function redirectUserToCheckout(Unzer $unzerService, Order $order): void
{
$translator = $this->getServiceFromContainer(Translator::class);
/** @var UnzerOrder $order */
$unzerOrderNr = $order->getUnzerOrderNr();
throw new RedirectWithMessage(
$unzerService->prepareRedirectUrl('payment?payerror=-6'),
sprintf($translator->translate('OSCUNZER_CANCEL_DURING_CHECKOUT'), $unzerOrderNr)
);
}
}
2 changes: 1 addition & 1 deletion tests/PhpMd/standard.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
<property name="cyclomaticComplexity" value="55"/>
</properties>
</rule>

<exclude-pattern>*OrderController.php</exclude-pattern>
</ruleset>
1 change: 1 addition & 0 deletions translations/de/oscunzer_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

// Error
'OSCUNZER_ERROR_DURING_CHECKOUT' => 'Bei der Abwicklung der Zahlung ist ein Fehler aufgetreten. Der Prozess wurde rückgängig gemacht. Bitte wählen Sie alternativ eine andere Zahlart aus.',
'OSCUNZER_CANCEL_DURING_CHECKOUT' => 'Die Zahlung wurde abgebrochen. Diese Bestellung ist als "nicht abgeschlossen" unter der Nummer %s, in Ihrem Kundenkonto gespeichert, damit Sie den Vorgang nachvollziehen können.',

// Invoice
'OSCUNZER_BANK_DETAILS_AMOUNT' => 'Bitte überweisen sie den Betrag von %s %s auf folgendes Bankkonto:<br /><br />',
Expand Down
1 change: 1 addition & 0 deletions translations/en/oscunzer_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

// Error
'OSCUNZER_ERROR_DURING_CHECKOUT' => 'An error occurred while processing the payment. The process was reversed. Alternatively, please select another payment method.',
'OSCUNZER_CANCEL_DURING_CHECKOUT' => 'The payment was cancelled. This order is saved as "not finished" under the number %s in your customer account so that you can track the process.',

//Invoice
'OSCUNZER_BANK_DETAILS_AMOUNT' => 'Please transfer the amount of %s %s to the following account:<br /><br />',
Expand Down

0 comments on commit e6ecc23

Please sign in to comment.