Skip to content

Commit

Permalink
Merge pull request #29 from ThomasDaSilva/main
Browse files Browse the repository at this point in the history
Fix module category + Fix Thelia 2.5 + CS fixer
  • Loading branch information
zawaze authored May 31, 2024
2 parents a41bab1 + 896688e commit 5bd3454
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 169 deletions.
4 changes: 2 additions & 2 deletions Config/thelia.sql → Config/TheliaMain.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ SET FOREIGN_KEY_CHECKS = 0;

DROP TABLE IF EXISTS `payzen_config`;

CREATE TABLE IF NOT EXISTS `payzen_config`
CREATE TABLE `payzen_config`
(
`name` VARCHAR(128) NOT NULL,
`value` TEXT,
PRIMARY KEY (`name`)
) ENGINE=InnoDB;

# This restores the fkey checks, after having unset them earlier
SET FOREIGN_KEY_CHECKS = 1;
SET FOREIGN_KEY_CHECKS = 1;
9 changes: 0 additions & 9 deletions Config/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,4 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://thelia.net/schema/dic/config http://thelia.net/schema/dic/config/thelia-1.0.xsd">

<forms>
<form name="payzen.configuration.form" class="Payzen\Form\ConfigurationForm" />
</forms>

<hooks>
<hook id="payzen.configuration.hook" class="Payzen\Hook\HookManager">
<tag name="hook.event_listener" event="module.configuration" type="back" method="onModuleConfigure" />
</hook>
</hooks>
</config>
4 changes: 2 additions & 2 deletions Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>2.0.2</version>
<version>2.0.3</version>
<authors>
<author>
<name>Franck Allimant</name>
Expand All @@ -22,7 +22,7 @@
<website>www.openstudio.fr</website>
</author>
</authors>
<type>classic</type>
<type>payment</type>
<thelia>2.5.0</thelia>
<stability>prod</stability>
<mandatory>0</mandatory>
Expand Down
8 changes: 0 additions & 8 deletions Config/routing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,4 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="payzen.configure" path="/admin/module/payzen/configure" methods="post">
<default key="_controller">Payzen\Controller\ConfigurationController::configure</default>
</route>

<route id="payzen.payment.confirmation" path="payzen/callback" methods="post">
<default key="_controller">Payzen\Controller\PaymentController::processPayzenRequest</default>
</route>

</routes>
7 changes: 5 additions & 2 deletions Config/schema.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<database defaultIdMethod="native" name="TheliaMain" namespace="Payzen\Model">
<table name="payzen_config">
<database defaultIdMethod="native" name="TheliaMain"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../vendor/propel/propel/resources/xsd/database.xsd" >

<table name="payzen_config" namespace="Payzen\Model">
<column name="name" required="true" size="128" type="VARCHAR" primaryKey="true"/>
<column name="value" type="LONGVARCHAR" />
</table>
Expand Down
2 changes: 2 additions & 0 deletions Config/sqldb.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Sqlfile -> Database map
TheliaMain.sql=TheliaMain
34 changes: 13 additions & 21 deletions Controller/ConfigurationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,23 @@

namespace Payzen\Controller;

use Exception;
use Payzen\Form\ConfigurationForm;
use Payzen\Model\PayzenConfigQuery;
use Payzen\Payzen;
use Symfony\Component\Routing\Attribute\Route;
use Thelia\Controller\Admin\BaseAdminController;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Core\Security\AccessManager;
use Thelia\Core\Security\Resource\AdminResources;
use Thelia\Core\Translation\Translator;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Tools\URL;
use Symfony\Component\Routing\Annotation\Route;

/**
* Payzen payment module
*
* @Route("/admin/module/payzen/configure", name="payzen")
* @author Franck Allimant <franck@cqfdev.fr>
*/
#[Route('/admin/module/Payzen', name: 'payzen_configuration_')]
class ConfigurationController extends BaseAdminController
{

/**
* @param Request $request
* @Route("", name="_configure")
* @return mixed an HTTP response, or
*/
#[Route('/configure', name: 'configure', methods: ['POST'])]
public function configure(Request $request)
{
if (null !== $response = $this->checkAuth(AdminResources::MODULE, 'Payzen', AccessManager::UPDATE)) {
Expand All @@ -58,7 +50,7 @@ public function configure(Request $request)
$configurationForm = $this->createForm(ConfigurationForm::getName());

try {
// Check the form against constraints violations
// Check the form against constraint violations
$form = $this->validateForm($configurationForm, "POST");

// Get the form field values
Expand All @@ -76,11 +68,11 @@ public function configure(Request $request)
$this->adminLogAppend(
"payzen.configuration.message",
AccessManager::UPDATE,
sprintf("Payzen configuration updated")
"Payzen configuration updated"
);

// Redirect to the success URL,
if ($request->get('save_mode') == 'stay') {
if ($request->get('save_mode') === 'stay') {
// If we have to stay on the same page, redisplay the configuration page/
$route = '/admin/module/Payzen';
} else {
Expand All @@ -92,19 +84,19 @@ public function configure(Request $request)

// An exit is performed after redirect.+
} catch (FormValidationException $ex) {
// Form cannot be validated. Create the error message using
// The Form cannot be validated. Create the error message using
// the BaseAdminController helper method.
$error_msg = $this->createStandardFormValidationErrorMessage($ex);
} catch (\Exception $ex) {
} catch (Exception $ex) {
// Any other error
$error_msg = $ex->getMessage();
}

// At this point, the form has errors, and should be redisplayed. We don not redirect,
// At this point, the form has errors, and should be redisplayed. We do not redirect,
// just redisplay the same template.
// Setup the Form error context, to make error information available in the template.
// Set up the Form error context to make error information available in the template.
$this->setupFormErrorContext(
$this->getTranslator()->trans("Payzen configuration", [], Payzen::MODULE_DOMAIN),
Translator::getInstance()->trans("Payzen configuration", [], Payzen::MODULE_DOMAIN),
$error_msg,
$configurationForm,
$ex
Expand Down
51 changes: 22 additions & 29 deletions Controller/PaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,32 @@

namespace Payzen\Controller;

use Exception;
use Payzen\Model\PayzenConfigQuery;
use Payzen\Payzen\PayzenResponse;
use Payzen\Payzen;
use Propel\Runtime\Exception\PropelException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;
use Thelia\Core\HttpFoundation\Response;
use Thelia\Core\Translation\Translator;
use Thelia\Module\BasePaymentModuleController;
use Symfony\Component\Routing\Annotation\Route;

/**
* Payzen payment module
*
* @Route("/payzen/callback", name="payzen_callback")
* @author Franck Allimant <franck@cqfdev.fr>
*/

#[Route('/payzen', name: 'payzen_payment_')]
class PaymentController extends BasePaymentModuleController
{
protected function getModuleCode()
protected function getModuleCode(): string
{
return "Payzen";
}

/**
* Process a Payzen platform request
*
* @Route("", name="_process")
* @return \Symfony\Component\HttpFoundation\Response
* @throws \Exception
* @throws PropelException
* @throws Exception
*/
public function processPayzenRequest(Request $request, EventDispatcherInterface $eventDispatcher)
#[Route('/callback', name: 'process', methods: ['POST'])]
public function processPayzenRequest(Request $request, EventDispatcherInterface $eventDispatcher): Response
{
// The response code to the server
$gateway_response_code = 'ko';
Expand All @@ -65,7 +60,7 @@ public function processPayzenRequest(Request $request, EventDispatcherInterface
PayzenConfigQuery::read('production_certificate')
);

$order_id = intval($request->get('vads_order_id'));
$order_id = (int)$request->get('vads_order_id');

$this->getLog()->addInfo(Translator::getInstance()->trans("Payzen platform request received for order ID %id.", array('%id' => $order_id), Payzen::MODULE_DOMAIN));

Expand All @@ -88,31 +83,29 @@ public function processPayzenRequest(Request $request, EventDispatcherInterface

$gateway_response_code = 'payment_ok';
}
} else if ($payzenResponse->isCancelledPayment()) {
// Payment was canceled.
$this->cancelPayment($eventDispatcher, $order_id);
} else {
if ($payzenResponse->isCancelledPayment()) {
// Payment was canceled.
$this->cancelPayment($eventDispatcher, $order_id);
// Payment was not accepted.
$this->getLog()->addError(Translator::getInstance()->trans("Order ID %id payment failed.", array('%id' => $order_id), Payzen::MODULE_DOMAIN));

if ($order->isPaid()) {
$gateway_response_code = 'payment_ko_already_done';
} else {
// Payment was not accepted.
$this->getLog()->addError(Translator::getInstance()->trans("Order ID %id payment failed.", array('%id' => $order_id), Payzen::MODULE_DOMAIN));

if ($order->isPaid()) {
$gateway_response_code = 'payment_ko_already_done';
} else {
$gateway_response_code = 'payment_ko';
}
$gateway_response_code = 'payment_ko';
}
}
} else {
$this->getLog()->addError(Translator::getInstance()->trans("Response could not be authentified."));
$this->getLog()->addError(Translator::getInstance()->trans("Response could not be authenticated."));

$gateway_response_code = 'auth_fail';
}
} else {
$gateway_response_code = 'order_not_found';
}

$this->getLog()->info(Translator::getInstance()->trans("Payzen platform request for order ID %id processing teminated.", array('%id' => $order_id), Payzen::MODULE_DOMAIN));
$this->getLog()->info(Translator::getInstance()->trans("Payzen platform request for order ID %id processing terminated.", array('%id' => $order_id), Payzen::MODULE_DOMAIN));

return new Response($payzenResponse->getOutputForGateway($gateway_response_code));
}
Expand Down
20 changes: 10 additions & 10 deletions EventListener/SendConfirmationEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@

namespace Payzen\EventListener;

use Exception;
use Payzen\Model\PayzenConfigQuery;
use Payzen\Payzen;
use Propel\Runtime\Exception\PropelException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Action\BaseAction;
Expand All @@ -40,7 +42,7 @@
class SendConfirmationEmail extends BaseAction implements EventSubscriberInterface
{
/** @var MailerFactory */
protected $mailer;
protected MailerFactory $mailer;

public function __construct(MailerFactory $mailer)
{
Expand All @@ -50,31 +52,29 @@ public function __construct(MailerFactory $mailer)
/**
* @param OrderEvent $event
*
* @throws \Exception if the message cannot be loaded.
* @throws Exception if the message cannot be loaded.
*/
public function sendConfirmationEmail(OrderEvent $event)
public function sendConfirmationEmail(OrderEvent $event): void
{
if (PayzenConfigQuery::read('send_confirmation_message_only_if_paid')) {
// We send the order confirmation email only if the order is paid
$order = $event->getOrder();

if (! $order->isPaid() && $order->getPaymentModuleId() == Payzen::getModuleId()) {
if (! $order->isPaid() && $order->getPaymentModuleId() === (int)Payzen::getModuleId()) {
$event->stopPropagation();
}
}
}

/**
* Checks if order payment module is paypal and if order new status is paid, send an email to the customer.
* Checks if order payment module is PayPal and if order new status is paid, email the customer.
*
* @param OrderEvent $event
* @param $eventName
* @param EventDispatcherInterface $dispatcher
* @param MailerFactory $mailerFactory
*
* @throws \Propel\Runtime\Exception\PropelException
* @throws PropelException
*/
public function updateStatus(OrderEvent $event, $eventName, EventDispatcherInterface $dispatcher)
public function updateStatus(OrderEvent $event, $eventName, EventDispatcherInterface $dispatcher): void
{
$order = $event->getOrder();

Expand All @@ -97,7 +97,7 @@ public function updateStatus(OrderEvent $event, $eventName, EventDispatcherInter
}
}

public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
return array(
TheliaEvents::ORDER_UPDATE_STATUS => array("updateStatus", 128),
Expand Down
15 changes: 5 additions & 10 deletions Form/ConfigurationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@
*/
class ConfigurationForm extends BaseForm
{
protected function trans($str, $params = [])
protected function trans($str, $params = []): string
{
return Translator::getInstance()->trans($str, $params, Payzen::MODULE_DOMAIN);
}

protected function buildForm()
protected function buildForm(): void
{
$api = new PayzenApi();

Expand All @@ -77,7 +77,7 @@ protected function buildForm()

// If the Multi plugin is not enabled, all multi_fields are hidden
/** @var Module $multiModule */
$multiEnabled = (null !== $multiModule = ModuleQuery::create()->findOneByCode('PayzenMulti')) && $multiModule->getActivate() != 0;
$multiEnabled = (null !== $multiModule = ModuleQuery::create()->findOneByCode('PayzenMulti')) && $multiModule->getActivate() !== 0;

$this->formBuilder
->add(
Expand Down Expand Up @@ -403,7 +403,7 @@ protected function buildForm()
'value' => 1,
'required' => false,
'label' => $this->trans('Send order confirmation on payment success'),
'data' => boolval(PayzenConfigQuery::read('send_confirmation_message_only_if_paid', true)),
'data' => (bool)PayzenConfigQuery::read('send_confirmation_message_only_if_paid', true),
'label_attr' => [
'help' => $this->trans(
'If checked, the order confirmation message is sent to the customer only when the payment is successful. The order notification is always sent to the shop administrator'
Expand All @@ -418,7 +418,7 @@ protected function buildForm()
'value' => 1,
'required' => false,
'label' => $this->trans('Send a payment confirmation e-mail'),
'data' => boolval(PayzenConfigQuery::read('send_payment_confirmation_message', true)),
'data' => (bool)PayzenConfigQuery::read('send_payment_confirmation_message', true),
'label_attr' => [
'help' => $this->translator->trans(
'If checked, a payment confirmation e-mail is sent to the customer.'
Expand Down Expand Up @@ -525,9 +525,4 @@ protected function buildForm()
;
}
}

public static function getName()
{
return 'payzen_configuration_form';
}
}
Loading

0 comments on commit 5bd3454

Please sign in to comment.