Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save data in db not in json,fix log download #25

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions CmCIC.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

namespace CmCIC;

use CmCIC\Model\Config;
use Propel\Runtime\Connection\ConnectionInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ServicesConfigurator;
Expand All @@ -32,6 +31,7 @@
use Thelia\Core\HttpFoundation\Response;
use Thelia\Core\Translation\Translator;
use Thelia\Log\Tlog;
use Thelia\Model\ModuleConfigQuery;
use Thelia\Model\ModuleImageQuery;
use Thelia\Model\Order;
use Thelia\Model\OrderAddress;
Expand All @@ -43,12 +43,23 @@ class CmCIC extends AbstractPaymentModule
{
const DOMAIN_NAME = "cmcic";

const JSON_CONFIG_PATH = "/Config/config.json";

const CMCIC_CGI2_RECEIPT = "version=2\ncdr=%s";
const CMCIC_CGI2_MACOK = "0";
const CMCIC_CGI2_MACNOTOK = "1\n";

const CMCIC_DEFAULT_CONF_VALUES =
[
'CMCIC_KEY' => '12345678901234567890123456789012345678P0',
'CMCIC_TPE' => '0000001',
'CMCIC_VERSION' => '3.0',
'CMCIC_CODESOCIETE' => '4b18fba7070c8ae6bea8',
'CMCIC_SERVER' => 'https://p.monetico-services.com/',
'CMCIC_PAGE' => 'paiement.cgi',
'CMCIC_DEBUG' => false,
'CMCIC_ALLOWED_IPS' => null,
'CMCIC_send_confirmation_message_only_if_paid' => false,
];

protected $config;

/**
Expand All @@ -62,11 +73,11 @@ class CmCIC extends AbstractPaymentModule
*/
public function isValidPayment()
{
$debug = $this->getConfigValue('debug', false);
$debug = $this->getConfigValue('CMCIC_DEBUG', false);

if ($debug) {
// Check allowed IPs when in test mode.
$testAllowedIps = $this->getConfigValue('allowed_ips', '');
$testAllowedIps = $this->getConfigValue('CMCIC_ALLOWED_IPS', '');

$raw_ips = explode("\n", $testAllowedIps);

Expand Down Expand Up @@ -98,17 +109,9 @@ public function isValidPayment()
public function postActivation(ConnectionInterface $con = null): void
{
/* insert the images from image folder if first module activation */
$configFile = __DIR__ . self::JSON_CONFIG_PATH;
$configDistFile = __DIR__ . self::JSON_CONFIG_PATH . '.dist';

if (!file_exists($configFile)) {
if (!copy($configDistFile, $configFile)) {
throw new \Exception(
Translator::getInstance()->trans(
"Can't create file %file%. Please change the rights on the file and/or directory."
)
);
}

foreach ($this::CMCIC_DEFAULT_CONF_VALUES as $nameConf => $valueConf){
CmCIC::setConfigValue($nameConf, $valueConf);
}

$module = $this->getModuleModel();
Expand Down Expand Up @@ -148,7 +151,11 @@ public function update($currentVersion, $newVersion, ConnectionInterface $con =
*/
public function pay(Order $order)
{
$c = Config::read(CmCIC::JSON_CONFIG_PATH);

$c = [];
foreach (ModuleConfigQuery::create()->filterByModuleId(CmCIC::getModuleId())->find()->getData() as $moduleConf) {
$c[$moduleConf->getName()] = $moduleConf->getValue();
}
$currency = $order->getCurrency()->getCode();

$vars = array(
Expand Down
1 change: 0 additions & 1 deletion Config/config.json.dist

This file was deleted.

3 changes: 0 additions & 3 deletions Config/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
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">

<loops>
<loop name="cmcic.check.rights" class="CmCIC\Loop\CheckRightsLoop" />
</loops>

<hooks>
<hook id="cmcic.back.hook" class="CmCIC\Hook\HookManager">
Expand Down
6 changes: 2 additions & 4 deletions Controller/CmcicPayResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
namespace CmCIC\Controller;

use CmCIC\CmCIC;
use CmCIC\Model\Config;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Thelia\Controller\Front\BaseFrontController;
use Thelia\Core\Event\Order\OrderEvent;
Expand Down Expand Up @@ -81,8 +80,7 @@ public function receiveResponse(EventDispatcherInterface $eventDispatcher)
/*
* Retrieve HMac for CGI2
*/
$config = Config::read(CmCIC::JSON_CONFIG_PATH);


$vars = $request->request->all();

unset($vars['MAC']);
Expand All @@ -91,7 +89,7 @@ public function receiveResponse(EventDispatcherInterface $eventDispatcher)

$computed_mac = CmCIC::computeHmac(
$hashable,
CmCIC::getUsableKey($config["CMCIC_KEY"])
CmCIC::getUsableKey(CmCIC::getConfigValue("CMCIC_KEY"))
);
$response=CmCIC::CMCIC_CGI2_MACNOTOK.$hashable;

Expand Down
57 changes: 27 additions & 30 deletions Controller/CmcicSaveConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

use CmCIC\CmCIC;
use CmCIC\Form\ConfigureCmCIC;
use CmCIC\Model\Config;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Thelia\Controller\Admin\BaseAdminController;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Core\HttpFoundation\Response;
Expand Down Expand Up @@ -57,14 +58,14 @@ public function downloadLog()
if (empty($data)) {
$data = Translator::getInstance()->trans("The CmCIC server log is currently empty.", [], CmCIC::DOMAIN_NAME);
}
return Response::create(
$data,
200,
array(
'Content-type' => "text/plain",
'Content-Disposition' => sprintf('Attachment;filename=log-cmcic.txt')
)
);

$header = [
'Content-Type' => "text/plain",
'Content-Disposition' => sprintf(
sprintf('Attachment;filename=log-cmcic.txt')
),
];
return new Response($data, 200, $header);
}

public function save(Request $request)
Expand All @@ -74,23 +75,18 @@ public function save(Request $request)
}

$error_message="";
$conf = new Config();
$form = $this->createForm(ConfigureCmCIC::getName());

try {
$vform = $this->validateForm($form);

CmCIC::setConfigValue('debug', $vform->get('debug')->getData());
CmCIC::setConfigValue('allowed_ips', $vform->get('allowed_ips')->getData());
CmCIC::setConfigValue('send_confirmation_message_only_if_paid', $vform->get('send_confirmation_message_only_if_paid')->getData());

// After post checks (PREG_MATCH) & create json file
if (preg_match("#^\d{7}$#", $vform->get('TPE')->getData()) &&
preg_match("#^[a-z\d]{40}$#i", $vform->get('com_key')->getData()) &&
preg_match("#^[a-z\-\d]+$#i", $vform->get('com_soc')->getData()) &&
preg_match("#^cic|cm|obc|mon$#", $vform->get('server')->getData())
// After post checks (PREG_MATCH)
if (preg_match("#^\d{7}$#", $vform->get('CMCIC_TPE')->getData()) &&
preg_match("#^[a-z\d]{40}$#i", $vform->get('CMCIC_KEY')->getData()) &&
preg_match("#^[a-z\-\d]+$#i", $vform->get('CMCIC_CODESOCIETE')->getData()) &&
preg_match("#^cic|cm|obc|mon$#", $vform->get('CMCIC_SERVER')->getData())
) {
$serv = $vform->get('server')->getData();
$serv = $vform->get('CMCIC_SERVER')->getData();

switch($serv) {
case 'mon':
Expand All @@ -113,19 +109,20 @@ public function save(Request $request)
throw new \InvalidArgumentException("Unknown server type '$serv'");
}

if ($vform->get('debug')->getData() === true) {
if ($vform->get('CMCIC_DEBUG')->getData() === true) {
$serv .= 'test/';
}

$conf
->setCMCICKEY($vform->get('com_key')->getData())
->setCMCICVERSION(self::CMCIC_VERSION)
->setCMCICCODESOCIETE($vform->get('com_soc')->getData())
->setCMCICPAGE($vform->get('page')->getData())
->setCMCICTPE($vform->get('TPE')->getData())
->setCMCICSERVER($serv)
->write(CmCIC::JSON_CONFIG_PATH)
;
$data = $vform->getData();

$data['CMCIC_SERVER'] = $serv;

foreach ($data as $name => $value) {
if (!preg_match('/^CMCIC/', $name)) {
continue;
}
CmCIC::setConfigValue($name, $value);
}
} else {
throw new \Exception($this->getTranslator()->trans("Error in form syntax, please check that your values are correct."));
}
Expand Down
4 changes: 2 additions & 2 deletions EventListeners/SendConfirmationEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(EventDispatcherInterface $dispatcher)
*/
public function sendConfirmationEmail(OrderEvent $event)
{
if (CmCIC::getConfigValue('send_confirmation_message_only_if_paid')) {
if (CmCIC::getConfigValue('CMCIC_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() == CmCIC::getModuleId()) {
Expand All @@ -53,7 +53,7 @@ public function updateStatus(OrderEvent $event)
$order = $event->getOrder();
if ($order->isPaid() && $order->getPaymentModuleId() == CmCIC::getModuleId()) {
// Send confirmation email if required.
if (CmCIC::getConfigValue('send_confirmation_message_only_if_paid')) {
if (CmCIC::getConfigValue('CMCIC_send_confirmation_message_only_if_paid')) {
$this->dispatcher->dispatch($event, TheliaEvents::ORDER_SEND_CONFIRMATION_EMAIL);
}

Expand Down
42 changes: 22 additions & 20 deletions Form/ConfigureCmCIC.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,48 +31,50 @@
use Symfony\Component\Validator\Constraints\NotBlank;
use Thelia\Core\Translation\Translator;
use Thelia\Form\BaseForm;
use Thelia\Model\ModuleConfigQuery;

class ConfigureCmCIC extends BaseForm
{
protected function buildForm()
{
$values = null;
$path = __DIR__ . "/../" . CmCIC::JSON_CONFIG_PATH;
if (is_readable($path)) {
$values = json_decode(file_get_contents($path), true);
$values = [];
foreach (ModuleConfigQuery::create()->filterByModuleId(CmCIC::getModuleId())->find()->getData() as $moduleConf) {
$values[$moduleConf->getName()] = $moduleConf->getValue();
}


$this->formBuilder
->add('com_key', TextType::class, [
->add('CMCIC_KEY', TextType::class, [
'label' => Translator::getInstance()->trans('Merchant key', [], CmCIC::DOMAIN_NAME),
'label_attr' => [
'for' => 'com_key'
'for' => 'CMCIC_KEY'
],
'data' => (null === $values ? '' : $values["CMCIC_KEY"]),
'constraints' => [
new NotBlank()
]
])
->add('TPE', TextType::class, [
->add('CMCIC_TPE', TextType::class, [
'label' => Translator::getInstance()->trans('TPE', [], CmCIC::DOMAIN_NAME),
'label_attr' => [
'for' => 'TPE'
'for' => 'CMCIC_TPE'
],
'data' => (null === $values ? '' : $values["CMCIC_TPE"]),
'constraints' => [
new NotBlank()
]
])
->add('com_soc', TextType::class, [
->add('CMCIC_CODESOCIETE', TextType::class, [
'label' => Translator::getInstance()->trans('Society code', [], CmCIC::DOMAIN_NAME),
'label_attr' => [
'for' => 'com_soc'
'for' => 'CMCIC_CODESOCIETE'
],
'data' => (null === $values ? '' : $values["CMCIC_CODESOCIETE"]),
'constraints' => [
new NotBlank()
]
])
->add('server', ChoiceType::class, [
->add('CMCIC_SERVER', ChoiceType::class, [
'label' => Translator::getInstance()->trans('server', [], CmCIC::DOMAIN_NAME),
'choices' => [
"CIC" => "cic",
Expand Down Expand Up @@ -108,7 +110,7 @@ protected function buildForm()
],

])
->add('page', TextType::class, [
->add('CMCIC_PAGE', TextType::class, [
'label' => Translator::getInstance()->trans('page', [], CmCIC::DOMAIN_NAME),
'label_attr' => [
'help' => Translator::getInstance()->trans(
Expand All @@ -122,28 +124,28 @@ protected function buildForm()
new NotBlank()
]
])
->add('debug', CheckboxType::class, [
->add('CMCIC_DEBUG', CheckboxType::class, [
'label' => Translator::getInstance()->trans('Run in test mode', [], CmCIC::DOMAIN_NAME),
'required' => false,
'label_attr' => [
'for' => 'debug',
'for' => 'CMCIC_DEBUG',
'help' => Translator::getInstance()->trans(
"Check this box to test the payment system, using test credit cards to simulate various situations.",
[],
CmCIC::DOMAIN_NAME
)
],
'data' => boolval(CmCIC::getConfigValue('debug', false))
'data' => !empty($values['CMCIC_DEBUG'])
])
->add(
'allowed_ips',
'CMCIC_ALLOWED_IPS',
TextareaType::class,
[
'required' => false,
'label' => Translator::getInstance()->trans('Allowed IPs in test mode', [], CmCIC::DOMAIN_NAME),
'data' => CmCIC::getConfigValue('allowed_ips', ''),
'data' => $values['CMCIC_ALLOWED_IPS'] ?? "",
'label_attr' => [
'for' => 'allowed_ips',
'for' => 'CMCIC_ALLOWED_IPS',
'help' => Translator::getInstance()->trans(
'List of IP addresses allowed to use this payment on the front-office when in test mode (your current IP is %ip). One address per line',
['%ip' => $this->getRequest()->getClientIp()],
Expand All @@ -153,11 +155,11 @@ protected function buildForm()
]
]
)->add(
'send_confirmation_message_only_if_paid',
'CMCIC_send_confirmation_message_only_if_paid',
CheckboxType::class,
[
'value' => 1,
'data' => !empty(CmCIC::getConfigValue('send_confirmation_message_only_if_paid', '')),
'data' => !empty($values['CMCIC_send_confirmation_message_only_if_paid']),
'required' => false,
'label' => $this->translator->trans('Send order confirmation on payment success', [], CmCIC::DOMAIN_NAME),
'label_attr' => [
Expand Down
Loading