Skip to content

Commit

Permalink
Merge pull request #5 from tpay-com/dx-499
Browse files Browse the repository at this point in the history
generic payment fix
  • Loading branch information
kwojc authored Apr 15, 2024
2 parents 6b63d0b + 4dd9995 commit 89aa96b
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 28 deletions.
33 changes: 18 additions & 15 deletions controllers/admin/TpayConfigurationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,22 +378,11 @@ public function formBasicOptions(): array
'required' => false,
],
[
'type' => 'text',
'type' => '',
'name' => 'TPAY_NOTIFICATION_ADDRESS',
'disabled' => 'disabled',
'label' => $this->module->l('Your address for notifications'),
'desc' => $this->context->link->getModuleLink('tpay', 'notifications'),
],
[
'type' => 'select',
'label' => $this->module->l('Status of a paid transaction with virtual products only'),
'name' => 'TPAY_VIRTUAL_CONFIRMED',
'options' => [
'query' => $this->getOrderStates(),
'id' => 'id_order_state',
'name' => 'name',
],
],
],
'submit' => [
'title' => $this->module->l('Save'),
Expand Down Expand Up @@ -497,7 +486,7 @@ public function formPaymentOptions(): array

[
'type' => 'switch',
'label' => $this->module->l('Alior Installment (from 300 PLN to 9 259,25 PLN)'),
'label' => $this->module->l('Alior Installment (from 300 PLN to 13 888,00 PLN)'),
'name' => 'TPAY_INSTALLMENTS_ACTIVE',
'desc' => $this->module->l('Show the method as a separate payment'),
'is_bool' => true,
Expand Down Expand Up @@ -697,6 +686,16 @@ public function formStatusesOptions(): array
'name' => 'name',
],
],
[
'type' => 'select',
'label' => $this->module->l('Status of a paid transaction with virtual products only'),
'name' => 'TPAY_VIRTUAL_CONFIRMED',
'options' => [
'query' => $this->getOrderStates(),
'id' => 'id_order_state',
'name' => 'name',
],
],
],
'submit' => [
'title' => $this->module->l('Save'),
Expand All @@ -710,7 +709,11 @@ private function formGenericPaymentOptions(): array
$result = [];

if ($this->module->api()) {
$result = $this->module->api()->transactions()->getChannels();
try {
$result = $this->module->api()->transactions()->getChannels();
} catch (Exception $exception) {
PrestaShopLogger::addLog($exception->getMessage(), 3);
}
}

return [
Expand All @@ -719,7 +722,7 @@ private function formGenericPaymentOptions(): array
'input' => [
[
'type' => 'select',
'label' => 'Select payments to generic onsite mechanism to: ',
'label' => $this->module->l('Select payments to generic onsite mechanism to'),
'name' => 'TPAY_GENERIC_PAYMENTS[]',
'multiple' => true,
'size' => $result ? 20 : 1,
Expand Down
2 changes: 1 addition & 1 deletion src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Config
public const TWISTO_MIN = 1.00;
public const TWISTO_MAX = 1500;
public const ALIOR_RATY_MIN = 300;
public const ALIOR_RATY_MAX = 9259.25;
public const ALIOR_RATY_MAX = 13888.00;

public const PEKAO_RATY_MIN = 100;
public const PEKAO_RATY_MAX = 20000;
Expand Down
15 changes: 14 additions & 1 deletion src/Handler/OrderStatusHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function setOrdersAsConfirmed(Order $order, string $tpayPaymentId, bool $
*/
private function changeOrderStatus(Order $order, string $tpayPaymentId, bool $error = false): void
{
$orderStateId = !$error ? Cfg::get('TPAY_CONFIRMED') : Cfg::get('TPAY_ERROR');
$orderStateId = $this->getOrderStatus($order, $error);
$orderStatusesHistory = $this->transactionsRepository->getOrderStatusHistory($order->id);
if (!in_array($orderStateId, $orderStatusesHistory)) {
if (!$error) {
Expand All @@ -83,4 +83,17 @@ private function changeOrderStatus(Order $order, string $tpayPaymentId, bool $er
$this->orderHistory->addWithemail(true);
}
}

private function getOrderStatus(Order $order, bool $error = false): string
{
$isVirtual = true;
foreach ($order->getProducts() as $product) {
if (!$product['is_virtual']) {
$isVirtual = false;
break;
}
}

return !$error ? Cfg::get($isVirtual ? 'TPAY_VIRTUAL_CONFIRMED' : 'TPAY_CONFIRMED') : Cfg::get('TPAY_ERROR');
}
}
38 changes: 32 additions & 6 deletions src/Service/PaymentOptions/PaymentOptionsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class PaymentOptionsService
/** @var ConstraintValidator */
private $constraintValidator;

/** @var array */
private $installmentChannels = [];

/**
* @throws \PrestaShopException
* @throws \Exception
Expand All @@ -66,6 +69,7 @@ public function getGroup(): void
{
try {
$this->getPaymentGroups();
$this->getInstallmentChannels();
} catch (\PrestaShopException $e) {
\PrestaShopLogger::addLog('Error getGroup ' . $e->getMessage(), 4);
throw new \PrestaShopException($e->getMessage());
Expand Down Expand Up @@ -187,32 +191,43 @@ private function getSeparatePayments(): array
return $result;
}


/**
* @throws \Exception
*/
private function aliorBetweenPriceRange(): bool
{
$total = $this->surchargeService->getTotalOrderAndSurchargeCost();
return $total >= Config::ALIOR_RATY_MIN && $total <= Config::ALIOR_RATY_MAX;
$min = $this->installmentChannels[Config::GATEWAY_ALIOR_RATY][0]['value'] ?? Config::ALIOR_RATY_MIN;
$max = $this->installmentChannels[Config::GATEWAY_ALIOR_RATY][1]['value'] ?? Config::ALIOR_RATY_MAX;

return $total >= $min && $total <= $max;
}

private function twistoBetweenPriceRange(): bool
{
$total = $this->surchargeService->getTotalOrderAndSurchargeCost();
return $total >= Config::TWISTO_MIN && $total <= Config::TWISTO_MAX;
$min = $this->installmentChannels[Config::GATEWAY_TWISTO][0]['value'] ?? Config::TWISTO_MIN;
$max = $this->installmentChannels[Config::GATEWAY_TWISTO][1]['value'] ?? Config::TWISTO_MAX;

return $total >= $min && $total <= $max;
}

private function pekaoBetweenPriceRange(): bool
{
$total = $this->surchargeService->getTotalOrderAndSurchargeCost();
return $total >= Config::PEKAO_RATY_MIN && $total <= Config::PEKAO_RATY_MAX;
$min = $this->installmentChannels[Config::GATEWAYS_PEKAO_RATY][0]['value'] ?? Config::PEKAO_RATY_MIN;
$max = $this->installmentChannels[Config::GATEWAYS_PEKAO_RATY][1]['value'] ?? Config::PEKAO_RATY_MAX;

return $total >= $min && $total <= $max;
}

private function paypoBetweenPriceRange(): bool
{
$total = $this->surchargeService->getTotalOrderAndSurchargeCost();
return $total >= Config::PAYPO_MIN && $total <= Config::PAYPO_MAX;
$min = $this->installmentChannels[Config::GATEWAY_PAYPO][0]['value'] ?? Config::PAYPO_MIN;
$max = $this->installmentChannels[Config::GATEWAY_PAYPO][1]['value'] ?? Config::PAYPO_MAX;

return $total >= $min && $total <= $max;
}

private function hasActiveCard(): bool
Expand All @@ -237,6 +252,17 @@ private function getPaymentGroups(): void
}
}

private function getInstallmentChannels(): void
{
$installmentIds = [Config::GATEWAY_ALIOR_RATY, Config::GATEWAY_PAYPO, Config::GATEWAY_TWISTO, Config::GATEWAYS_PEKAO_RATY];

foreach ($this->module->api->Transactions->getChannels()['channels'] as $channel) {
if (in_array($channel['id'], $installmentIds) && !empty($channel['constraints'])) {
$this->installmentChannels[$channel['id']] = $channel;
}
}
}

/**
* Grouping of payments delivered from api
* * @param array $group
Expand Down Expand Up @@ -308,7 +334,7 @@ public function getGroupTransfers(): array
*/
protected function genericPayments(): array
{
$generics = json_decode(Helper::getMultistoreConfigurationValue('TPAY_GENERIC_PAYMENTS'));
$generics = Helper::getMultistoreConfigurationValue('TPAY_GENERIC_PAYMENTS') ? json_decode(Helper::getMultistoreConfigurationValue('TPAY_GENERIC_PAYMENTS')) : [];
$channels = unserialize(Cache::get('channels', 'N;'));

if (null === $channels) {
Expand Down
4 changes: 3 additions & 1 deletion src/Service/PaymentOptions/Transfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace Tpay\Service\PaymentOptions;

use Configuration;
use Tpay\Config\Config;
use Context;
use PrestaShop\PrestaShop\Core\Payment\PaymentOption;
Expand All @@ -35,7 +36,8 @@ public function getPaymentOption(
'transfer_type' => Helper::getMultistoreConfigurationValue('TPAY_TRANSFER_WIDGET') ? 'widget' : 'redirect',
'transfer_gateway' => $data['id'],
'transfer_moduleLink' => $moduleLink,
'gateways' => $data['gateways']
'gateways' => $data['gateways'],
'isDirect' => (bool) Configuration::get('TPAY_REDIRECT_TO_CHANNEL'),
]);

$paymentOption->setCallToActionText($module->l('Pay by online transfer with Tpay'))
Expand Down
4 changes: 4 additions & 0 deletions src/Util/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public static function getFields(): array
'TPAY_SECRET_KEY',
'TPAY_BLIK_ACTIVE',
'TPAY_BLIK_WIDGET',
'TPAY_TRANSFER_WIDGET',
'TPAY_CARD_WIDGET',
'TPAY_INSTALLMENTS_ACTIVE',
'TPAY_PEKAO_INSTALLMENTS_ACTIVE',
'TPAY_TWISTO_ACTIVE',
Expand Down Expand Up @@ -77,6 +79,8 @@ public static function getFieldsDefaultValues(): array
return [
'TPAY_BLIK_ACTIVE' => '1',
'TPAY_BLIK_WIDGET' => '1',
'TPAY_TRANSFER_WIDGET' => '1',
'TPAY_CARD_WIDGET' => '1',
'TPAY_INSTALLMENTS_ACTIVE' => '0',
'TPAY_PEKAO_INSTALLMENTS_ACTIVE' => '0',
'TPAY_TWISTO_ACTIVE' => '0',
Expand Down
5 changes: 3 additions & 2 deletions tpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function __construct()
{
$this->name = 'tpay';
$this->tab = 'payments_gateways';
$this->version = '1.8.6';
$this->version = '1.8.7';
$this->author = 'Krajowy Integrator Płatności S.A.';
$this->need_instance = 0;
$this->ps_versions_compliancy = [
Expand Down Expand Up @@ -222,9 +222,10 @@ private function initAPI()
}

/**
* @return false|object|null
* @throws Exception
*/
public function getService(string $serviceName): ?object
public function getService(string $serviceName)
{
$container = Container::getInstance();
if ($container !== null) {
Expand Down
5 changes: 3 additions & 2 deletions translations/pl.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
$_MODULE['<{tpay}prestashop>tpayconfigurationcontroller_7501403723d5060f09da5075eceb1447'] = 'Widget BLIK';
$_MODULE['<{tpay}prestashop>tpayconfigurationcontroller_e89f6cb3b9d321faa3e5d83d05791b68'] = 'Wyświetl metodę płatności w widgecie';
$_MODULE['<{tpay}prestashop>tpayconfigurationcontroller_81d1c1d1c8e6a330af12300fc80e2d6c'] = 'GooglePay';
$_MODULE['<{tpay}prestashop>tpayconfigurationcontroller_8d994c263ab23603399787901a1b497f'] = 'Alior Raty (od 300 PLN do 9 259,25 PLN) ';
$_MODULE['<{tpay}prestashop>tpayconfigurationcontroller_22d3de78770a61d09fc0609f2863c770'] = 'Alior Raty (od 300 PLN do 13 888,00 PLN) ';
$_MODULE['<{tpay}prestashop>tpayconfigurationcontroller_8ee7505b25a00805b969c9fdc7e66ecd'] = 'Raty Pekao (od 100 PLN do 20 000 PLN) ';
$_MODULE['<{tpay}prestashop>tpayconfigurationcontroller_5f7fdf12e87a9627c437a422b8a0f988'] = 'Twisto - Kup teraz, zapłać później (od 1 PLN do 1 500 PLN) ';
$_MODULE['<{tpay}prestashop>tpayconfigurationcontroller_1d682521d46887b13c231ce84055eec6'] = 'Używaj ustawień globalnych';
Expand Down Expand Up @@ -169,7 +169,7 @@
$_MODULE['<{tpay}prestashop>tpay_7501403723d5060f09da5075eceb1447'] = 'Widget BLIK';
$_MODULE['<{tpay}prestashop>tpay_e89f6cb3b9d321faa3e5d83d05791b68'] = 'Wyświetl metodę płatności w widgecie';
$_MODULE['<{tpay}prestashop>tpay_81d1c1d1c8e6a330af12300fc80e2d6c'] = 'GooglePay';
$_MODULE['<{tpay}prestashop>tpay_8d994c263ab23603399787901a1b497f'] = 'Alior Raty (od 300 PLN do 9 259,25 PLN) ';
$_MODULE['<{tpay}prestashop>tpay_22d3de78770a61d09fc0609f2863c770'] = 'Alior Raty (od 300 PLN do 13 888,00 PLN) ';
$_MODULE['<{tpay}prestashop>tpay_8ee7505b25a00805b969c9fdc7e66ecd'] = 'Raty Pekao (od 100 PLN do 20 000 PLN) ';
$_MODULE['<{tpay}prestashop>tpay_5f7fdf12e87a9627c437a422b8a0f988'] = 'Twisto - Kup teraz, zapłać później (od 1 PLN do 1 500 PLN) ';
$_MODULE['<{tpay}prestashop>tpay_1d682521d46887b13c231ce84055eec6'] = 'Używaj ustawień globalnych';
Expand All @@ -181,6 +181,7 @@
$_MODULE['<{tpay}prestashop>tpay_0bca037451ac432ae9fae53a78387a6e'] = 'Status transakcji opłaconej';
$_MODULE['<{tpay}prestashop>tpay_f69d7a9f87546d78e76e0ee400dabeac'] = 'Status opłaconej transakcji, wyłącznie z produktami wirtualnymi';
$_MODULE['<{tpay}prestashop>tpay_0af709feff4fe0f53053a235f7678645'] = 'Status transakcji zakończonej błędem';
$_MODULE['<{tpay}prestashop>tpay_a05879ff84ed16d3bf42d1add10a8d03 '] = 'Wybierz płatności do mechanizmu generic onsite';
$_MODULE['<{tpay}prestashop>invoicesurcharge_bb8278c756e2855300ee104481ad7d27'] = 'Opłata za płatność online';
$_MODULE['<{tpay}prestashop>renewpayment_26e79570e8d6e15ee7168fc8bfdbf805'] = 'Błąd podczas zlecenia';
$_MODULE['<{tpay}prestashop>renewpayment_782c8b38bce4f2f6975ca7f33ac8189b'] = 'Historia zamówienia';
Expand Down

0 comments on commit 89aa96b

Please sign in to comment.