Skip to content

Commit

Permalink
Release 1.0.24
Browse files Browse the repository at this point in the history
  • Loading branch information
edgaraswallee committed Mar 15, 2024
1 parent 5788e03 commit e9db123
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 65 deletions.
24 changes: 24 additions & 0 deletions PostFinanceCheckoutHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use JTL\Plugin\Data\Localization;
use PostFinanceCheckout\Sdk\ApiClient;
use JTL\Plugin\Helper as PluginHelper;
use JTL\Checkout\Nummern;

/**
* Class PostFinanceCheckoutHelper
Expand Down Expand Up @@ -101,6 +102,29 @@ public static function getLanguageString(): string
}
}

public static function getNextOrderNr(): string
{
$conf = Shop::getSettingSection(\CONF_KAUFABWICKLUNG);
$number = new Nummern(\JTL_GENNUMBER_ORDERNUMBER);
$increment = (int)($conf['bestellabschluss_bestellnummer_anfangsnummer'] ?? 1);
if ($number) {
$orderNo = $number->getNummer() + $increment;

$prefix = \str_replace(
['%Y', '%m', '%d', '%W'],
[\date('Y'), \date('m'), \date('d'), \date('W')],
$conf['bestellabschluss_bestellnummer_praefix']
);
$suffix = \str_replace(
['%Y', '%m', '%d', '%W'],
[\date('Y'), \date('m'), \date('d'), \date('W')],
$conf['bestellabschluss_bestellnummer_suffix']
);

return $prefix . $orderNo . $suffix;
}
}

/**
* @param $isAdmin
* @return string
Expand Down
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.postfinance-checkout.ch/pfpayments/jtl-5/1.0.23/docs/en/documentation.html)
[Documentation](https://plugin-documentation.postfinance-checkout.ch/pfpayments/jtl-5/1.0.24/docs/en/documentation.html)

## License

Expand Down
108 changes: 71 additions & 37 deletions Services/PostFinanceCheckoutTransactionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
TransactionCreate,
TransactionInvoice,
TransactionPending,
TransactionState};
TransactionState
};

class PostFinanceCheckoutTransactionService
{
Expand Down Expand Up @@ -82,6 +83,9 @@ public function createTransaction(Bestellung $order): Transaction
$transactionPayload->setSuccessUrl($successUrl);
$transactionPayload->setFailedUrl($failedUrl);

$orderNr = PostFinanceCheckoutHelper::getNextOrderNr();
$transactionPayload->setMerchantReference($orderNr);

$createdTransaction = $this->apiClient->getTransactionService()->create($this->spaceId, $transactionPayload);

return $createdTransaction;
Expand All @@ -108,8 +112,7 @@ public function confirmTransaction(Transaction $transaction): void

if ($createOrderAfterPayment === 1) {
$orderId = null;
$orderHandler = new OrderHandler(Shop::Container()->getDB(), Frontend::getCustomer(), Frontend::getCart());
$orderNr = $orderHandler->createOrderNo();
$orderNr = PostFinanceCheckoutHelper::getNextOrderNr();

$order = new \stdClass();
$order->transaction_id = $transactionId;
Expand All @@ -136,14 +139,21 @@ public function confirmTransaction(Transaction $transaction): void

$this->createLocalPostFinanceCheckoutTransaction((string)$transactionId, (array)$order);
}
$_SESSION['orderId'] = $orderId;
$_SESSION['nextOrderNr'] = $orderNr;

$pendingTransaction->setMetaData([
'orderId' => $orderId,
'spaceId' => $this->spaceId,
'orderAfterPayment' => $createOrderAfterPayment
]);

$pendingTransaction->setMerchantReference($orderNr);
$successUrl = Shop::getURL() . '/' . PostFinanceCheckoutHelper::PLUGIN_CUSTOM_PAGES['thank-you-page'][$_SESSION['cISOSprache']];
$failedUrl = Shop::getURL() . '/' . PostFinanceCheckoutHelper::PLUGIN_CUSTOM_PAGES['fail-page'][$_SESSION['cISOSprache']];

$pendingTransaction->setSuccessUrl($successUrl . '?tID=' . $transactionId);
$pendingTransaction->setFailedUrl($failedUrl . '?tID=' . $transactionId);


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

Expand Down Expand Up @@ -340,7 +350,7 @@ public function getLineItems(array $products): array
$product->nAnzahl = 1;
$product->fPreis = $_SESSION['Bestellung']->fGuthabenGenutzt;

$lineItems[] = $this->createLineItemProductItem($product, true);
$lineItems[] = $this->createLineItemProductItem($product, true, true);
}
}

Expand Down Expand Up @@ -376,11 +386,23 @@ public function addIncommingPayment(string $transactionId, Bestellung $order, Tr
{
$localTransaction = $this->getLocalPostFinanceCheckoutTransactionById($transactionId);
if ($localTransaction->state !== TransactionState::FULFILL) {
$_SESSION['orderId'] = (int)$order->kBestellung;
$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
$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);
Expand All @@ -396,25 +418,25 @@ public function addIncommingPayment(string $transactionId, Bestellung $order, Tr
}

/**
* @return void
* @param int $transactionId
* @return int
*/
public function createOrderAfterPayment(): void
public function createOrderAfterPayment(int $transactionId): int
{
$_SESSION['finalize'] = true;
$orderHandler = new OrderHandler(Shop::Container()->getDB(), Frontend::getCustomer(), Frontend::getCart());
$order = $orderHandler->finalizeOrder($_SESSION['nextOrderNr']);
$orderData = $order->fuelleBestellung(true);
$_SESSION['orderData'] = $orderData;
$orderNr = $orderHandler->createOrderNo();
$order = $orderHandler->finalizeOrder($orderNr);

$transactionId = $_SESSION['transactionId'] ?? null;

if ($transactionId) {
$this->updateLocalPostFinanceCheckoutTransaction((string)$transactionId, TransactionState::AUTHORIZED, (int)$order->kBestellung);
$transaction = $this->getTransactionFromPortal($transactionId);
if ($transaction->getState() === TransactionState::FULFILL) {
$this->addIncommingPayment((string)$transactionId, $orderData, $transaction);
}
$this->updateLocalPostFinanceCheckoutTransaction((string)$transactionId, TransactionState::AUTHORIZED, (int)$order->kBestellung);
$transaction = $this->getTransactionFromPortal($transactionId);
if ($transaction->getState() === TransactionState::FULFILL) {
// fuelleBestellung - JTL5 native function to append all required data to order
$orderData = $order->fuelleBestellung(true);
$this->addIncommingPayment((string)$transactionId, $orderData, $transaction);
}

return (int)$order->kBestellung;
}

/**
Expand All @@ -428,22 +450,27 @@ public function updateLocalPostFinanceCheckoutTransaction(string $transactionId,
$state = TransactionState::PROCESSING;
}

if ($orderId === null) {
$orderId = $_SESSION['orderId'];
$data = [
'state' => $state,
'space_id' => $this->spaceId,
'updated_at' => date('Y-m-d H:i:s')
];

$paymentMethodName = $_SESSION['possiblePaymentMethodName'];
if ($paymentMethodName) {
$data['payment_method'] = $paymentMethodName;
}

if ($orderId) {
$data['order_id'] = $orderId;
}

Shop::Container()
->getDB()->update(
'postfinancecheckout_transactions',
['transaction_id'],
[$transactionId],
(object)[
'state' => $state,
'payment_method' => $_SESSION['possiblePaymentMethodName'],
'order_id' => $orderId,
'space_id' => $this->spaceId,
'updated_at' => date('Y-m-d H:i:s')
]
(object)$data
);
}

Expand All @@ -468,7 +495,7 @@ private function downloadDocument($document)
* @param CartItem $productData
* @return LineItemCreate
*/
private function createLineItemProductItem(CartItem $productData, $isDiscount = false): LineItemCreate
private function createLineItemProductItem(CartItem $productData, $isDiscount = false, $isCustomerCredit = false): LineItemCreate
{
$lineItem = new LineItemCreate();
$name = \is_array($productData->cName) ? $productData->cName[$_SESSION['cISOSprache']] : $productData->cName;
Expand All @@ -488,13 +515,20 @@ private function createLineItemProductItem(CartItem $productData, $isDiscount =
$lineItem->setQuantity($productData->nAnzahl);

$currencyFactor = Frontend::getCurrency()->getConversionFactor();
// fPreis is price, nAnzahl is quantity
$priceDecimal = Tax::getGross(
$productData->fPreis * $productData->nAnzahl,
CartItem::getTaxRate($productData)
);
$priceDecimal *= $currencyFactor;
$priceDecimal = (float)number_format($priceDecimal, 2, '.', '');

if (!$isCustomerCredit) {
// fPreis is price, nAnzahl is quantity
$priceDecimal = Tax::getGross(
$productData->fPreis * $productData->nAnzahl,
CartItem::getTaxRate($productData)
);
$priceDecimal *= $currencyFactor;
$priceDecimal = (float)number_format($priceDecimal, 2, '.', '');
} else {
// For customer credit - do not apply taxes
$priceDecimal = $productData->fPreis * $productData->nAnzahl;
$priceDecimal = (float)number_format($priceDecimal, 2, '.', '');
}

$type = LineItemType::PRODUCT;
if ($isDiscount === true) {
Expand Down
2 changes: 1 addition & 1 deletion docs/en/documentation.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ <h2>Documentation</h2> </div>
</a>
</li>
<li>
<a href="https://github.com/pfpayments/jtl-5/releases/tag/1.0.23/">
<a href="https://github.com/pfpayments/jtl-5/releases/tag/1.0.24/">
Source
</a>
</li>
Expand Down
8 changes: 5 additions & 3 deletions frontend/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,10 @@ private function handleTransaction(): void
if ($createdTransactionId) {
$this->transactionService->updateTransaction($createdTransactionId);
}

$_SESSION['addressCheck'] = md5(json_encode((array)$_SESSION['Lieferadresse']));
$_SESSION['currencyCheck'] = md5(json_encode((array)$_SESSION['Warenkorb']->PositionenArr));
$_SESSION['lineItemsCheck'] = $_SESSION['cWaehrungName'];
$_SESSION['lineItemsCheck'] = md5(json_encode((array)$_SESSION['Warenkorb']->PositionenArr));
$_SESSION['currencyCheck'] = $_SESSION['cWaehrungName'];
}

if (!$createdTransactionId || !$arrayOfPossibleMethods) {
Expand Down Expand Up @@ -228,7 +229,6 @@ public function getRedirectUrlAfterCreatedTransaction($orderData): string
header("Location: " . $failedUrl);
exit;
}
$this->confirmTransaction($spaceId, $createdTransactionId);

// TODO create setting with options ['payment_page', 'iframe'];
$integration = 'iframe';
Expand All @@ -249,6 +249,8 @@ public function getRedirectUrlAfterCreatedTransaction($orderData): string
$_SESSION['possiblePaymentMethodName'] = $paymentMethod->getName();
$_SESSION['orderData'] = $orderData;

$this->confirmTransaction($spaceId, $createdTransactionId);

if ($integration == 'payment_page') {
$redirectUrl = $this->apiClient->getTransactionPaymentPageService()
->paymentPageUrl($spaceId, $createdTransactionId);
Expand Down
28 changes: 17 additions & 11 deletions frontend/postfinancecheckout_thank_you_page.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,31 @@
/** @global \JTL\Smarty\JTLSmarty $smarty */
/** @global JTL\Plugin\PluginInterface $plugin */

// When order has to be created after payment
if ($_SESSION['Zahlungsart']?->nWaehrendBestellung ?? null === 1) {
if ($_SESSION['Warenkorb'] && $_SESSION['transactionId'] && $_SESSION['arrayOfPossibleMethods']) {
$apiClient = new PostFinanceCheckoutApiClient($plugin->getId());
$transactionService = new PostFinanceCheckoutTransactionService($apiClient->getApiClient(), $plugin);
$transactionService->createOrderAfterPayment();
$transactionId = (int)$_GET['tID'] ?? null;

if ($transactionId) {
$apiClient = new PostFinanceCheckoutApiClient($plugin->getId());
$transactionService = new PostFinanceCheckoutTransactionService($apiClient->getApiClient(), $plugin);
$localTransaction = $transactionService->getLocalPostFinanceCheckoutTransactionById((string)$transactionId);
$orderId = (int) $localTransaction->order_id;
$transaction = $transactionService->getTransactionFromPortal($transactionId);
$createAfterPayment = (int)$transaction->getMetaData()['orderAfterPayment'] ?? 1;
if ($createAfterPayment) {
if (empty($orderId)) {
$orderId = $transactionService->createOrderAfterPayment($transactionId);
}
}
} else {
$order = new Bestellung($_SESSION['orderData']->kBestellung);
$_SESSION['orderData'] = $order->fuelleBestellung(true);
}

$_SESSION['transactionId'] = null;
$_SESSION['Warenkorb'] = null;
$_SESSION['transactionId'] = null;
$_SESSION['arrayOfPossibleMethods'] = null;

$linkHelper = Shop::Container()->getLinkService();
$orderId = $_SESSION['orderData']->kBestellung ?? 0;
$bestellid = $this->db->select('tbestellid', 'kBestellung', $orderId);
if ($orderId > 0) {
$bestellid = $this->db->select('tbestellid', 'kBestellung', $orderId);
}
$controlId = $bestellid->cId ?? '';
$url = $linkHelper->getStaticRoute('bestellabschluss.php') . '?i=' . $controlId;

Expand Down
2 changes: 1 addition & 1 deletion info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<Icon>logo.jpg</Icon>
<PluginID>jtl_postfinancecheckout</PluginID>
<CreateDate>2023-05-29</CreateDate>
<Version>1.0.23</Version>
<Version>1.0.24</Version>
<Install>
<Locales>
<Variable>
Expand Down
2 changes: 1 addition & 1 deletion vendor/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInit3e3af994b9371ec93824fba4bc584f40::getLoader();
return ComposerAutoloaderInit0a07ce749856c27fdaf9ea30903361e0::getLoader();
8 changes: 4 additions & 4 deletions vendor/composer/autoload_real.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// autoload_real.php @generated by Composer

class ComposerAutoloaderInit3e3af994b9371ec93824fba4bc584f40
class ComposerAutoloaderInit0a07ce749856c27fdaf9ea30903361e0
{
private static $loader;

Expand All @@ -24,12 +24,12 @@ public static function getLoader()

require __DIR__ . '/platform_check.php';

spl_autoload_register(array('ComposerAutoloaderInit3e3af994b9371ec93824fba4bc584f40', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit0a07ce749856c27fdaf9ea30903361e0', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit3e3af994b9371ec93824fba4bc584f40', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit0a07ce749856c27fdaf9ea30903361e0', 'loadClassLoader'));

require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit3e3af994b9371ec93824fba4bc584f40::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit0a07ce749856c27fdaf9ea30903361e0::getInitializer($loader));

$loader->register(true);

Expand Down
8 changes: 4 additions & 4 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Composer\Autoload;

class ComposerStaticInit3e3af994b9371ec93824fba4bc584f40
class ComposerStaticInit0a07ce749856c27fdaf9ea30903361e0
{
public static $prefixLengthsPsr4 = array (
'W' =>
Expand Down Expand Up @@ -35,9 +35,9 @@ class ComposerStaticInit3e3af994b9371ec93824fba4bc584f40
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit3e3af994b9371ec93824fba4bc584f40::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit3e3af994b9371ec93824fba4bc584f40::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit3e3af994b9371ec93824fba4bc584f40::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit0a07ce749856c27fdaf9ea30903361e0::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit0a07ce749856c27fdaf9ea30903361e0::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit0a07ce749856c27fdaf9ea30903361e0::$classMap;

}, null, ClassLoader::class);
}
Expand Down
Loading

0 comments on commit e9db123

Please sign in to comment.