Skip to content

Commit

Permalink
Using hook instead of AdminIncludes, code style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Franck Allimant committed Sep 13, 2019
1 parent 3f2b273 commit 2577e60
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 161 deletions.
75 changes: 0 additions & 75 deletions AdminIncludes/module_configuration.html

This file was deleted.

135 changes: 81 additions & 54 deletions CmCIC.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@

use CmCIC\Model\Config;
use Propel\Runtime\Connection\ConnectionInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Routing\Router;
use Thelia\Core\HttpFoundation\Response;
use Thelia\Core\Template\TemplateDefinition;
use Thelia\Log\Tlog;
use Thelia\Model\ModuleImageQuery;
use Thelia\Model\Order;
use Thelia\Model\OrderAddress;
Expand All @@ -38,19 +39,15 @@
class CmCIC extends AbstractPaymentModule
{
const DOMAIN_NAME = "cmcic";

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

const CMCIC_CTLHMAC = "V1.04.sha1.php--[CtlHmac%s%s]-%s";
const CMCIC_CTLHMACSTR = "CtlHmac%s%s";

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

protected $sKey;
protected $sUsableKey;


protected $config;

/**
*
* This method is call on Payment loop.
Expand All @@ -63,41 +60,41 @@ class CmCIC extends AbstractPaymentModule
public function isValidPayment()
{
$debug = $this->getConfigValue('debug', false);

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

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

$allowed_client_ips = array();

foreach ($raw_ips as $ip) {
$allowed_client_ips[] = trim($ip);
}

$client_ip = $this->getRequest()->getClientIp();

$valid = in_array($client_ip, $allowed_client_ips);
} else {
$valid = true;
}

if ($this->getCurrentOrderTotalAmount() <= 0) {
$valid = false;
}

return $valid;
}

public function postActivation(ConnectionInterface $con = null)
{
/* insert the images from image folder if first module activation */
$module = $this->getModuleModel();
if (ModuleImageQuery::create()->filterByModule($module)->count() == 0) {
$this->deployImageFolder($module, sprintf('%s/images', __DIR__), $con);
}

/* set module title */
$this->setTitle(
$module,
Expand All @@ -106,16 +103,34 @@ public function postActivation(ConnectionInterface $con = null)
"fr_FR" => "Paiement par Carte Bancaire",
)
);

}


public function update($currentVersion , $newVersion , ConnectionInterface $con = null)
{
// Delete obsolete admin includes
$fs = new Filesystem();

try {
$fs -> remove(__DIR__ . '/AdminIncludes');
$fs -> remove(__DIR__ . 'I18n/AdminIncludes');
} catch (\Exception $ex) {
Tlog::getInstance()->addWarning("Failed to delete CmCIC module AdminIncludes directory (".__DIR__ . '/AdminIncludes): ' . $ex->getMessage());
}
}

/**
* @param Order $order
* @return Response|null
* @throws \Exception
*/
public function pay(Order $order)
{
$c = Config::read(CmCIC::JSON_CONFIG_PATH);
$currency = $order->getCurrency()->getCode();
$cmCicRouter = $this->container->get('router.cmcic');
$mainRouter = $this->container->get('router.front');

$vars = array(
"version" => $c["CMCIC_VERSION"],
"TPE" => $c["CMCIC_TPE"],
Expand All @@ -132,22 +147,22 @@ public function pay(Order $order)
"3dsdebrayable" => "0",
"ThreeDSecureChallenge" => "challenge_preferred",
);
$hashable = self::getHashable($vars);

$hashable = self::getHashable($vars);

$mac = self::computeHmac(
$hashable,
self::getUsableKey($c["CMCIC_KEY"])
);
$vars["MAC"] = $mac;

return $this->generateGatewayFormResponse(
$order,
$c["CMCIC_SERVER"] . $c["CMCIC_PAGE"],
$vars
);
}

protected function harmonise($value, $type, $len)
{
switch ($type) {
Expand All @@ -170,17 +185,17 @@ protected function harmonise($value, $type, $len)
}
break;
}

return $value;
}

public static function getUsableKey($key)
{
$hexStrKey = substr($key, 0, 38);
$hexFinal = "" . substr($key, 38, 2) . "00";

$cca0 = ord($hexFinal);

if ($cca0 > 70 && $cca0 < 97) {
$hexStrKey .= chr($cca0 - 23) . substr($hexFinal, 1, 1);
} else {
Expand All @@ -190,36 +205,46 @@ public static function getUsableKey($key)
$hexStrKey .= substr($hexFinal, 0, 2);
}
}

return pack("H*", $hexStrKey);
}

public static function computeHmac($sData, $key)
{
return strtolower(hash_hmac("sha1", $sData, $key));
}


/**
* @param Order $order
* @return string
* @throws \Propel\Runtime\Exception\PropelException
*/
public static function getCommandContext(Order $order) {

$orderAddressId = $order->getInvoiceOrderAddressId();
$orderAddress = OrderAddressQuery::create()->findPk($orderAddressId);
$billing = self::orderAddressForCbPayment($orderAddress);


$deliveryAddressId = $order->getDeliveryOrderAddressId();
$deliveryAddress = OrderAddressQuery::create()->findPk($deliveryAddressId);
$shipping = self::orderAddressForCbPayment($deliveryAddress);

$commandContext = array("billing" => $billing,
"shipping" => $shipping);
$json = json_encode($commandContext);

$json = json_encode($commandContext);
$utf8 = utf8_encode( $json );
return base64_encode( $utf8 );
}


/**
* @param OrderAddress $orderAddress
* @return array
* @throws \Propel\Runtime\Exception\PropelException
*/
public static function orderAddressForCbPayment(OrderAddress $orderAddress) {

$address = array("name" => substr($orderAddress->getFirstname()." ".$orderAddress->getLastname()." ".$orderAddress->getCompany(), 0, 45),
"firstName" => substr($orderAddress->getFirstname(), 0, 45),
"lastName" => substr($orderAddress->getLastname(), 0, 45),
Expand All @@ -230,30 +255,32 @@ public static function orderAddressForCbPayment(OrderAddress $orderAddress) {
"city" => substr($orderAddress->getCity(), 0, 50),
"postalCode" => $orderAddress->getZipcode(),
);

if($orderAddress->getState() != null)
{
$address["stateOrProvince"] = $orderAddress->getState()->getIsocode();
}

$address["country"] = $orderAddress->getCountry()->getIsoalpha2();
$address["phone"] = (substr($orderAddress->getPhone(),0,1) == "+")? $orderAddress->getPhone():"";
$address["mobilePhone"] = (substr($orderAddress->getCellphone(),0,1) == "+")? $orderAddress->getPhone():"";

return $address;
}

// Get the new format for seal content, for DSP-2 (cf https://www.monetico-paiement.fr/fr/info/documentations/Monetico_Paiement_documentation_migration_3DSv2_1.0.pdf#%5B%7B%22num%22%3A83%2C%22gen%22%3A0%7D%2C%7B%22name%22%3A%22XYZ%22%7D%2C68%2C716%2C0%5D )
public static function getHashable($vars) {

/**
* Get the new format for seal content, for DSP-2 (cf https://www.monetico-paiement.fr/fr/info/documentations/Monetico_Paiement_documentation_migration_3DSv2_1.0.pdf#%5B%7B%22num%22%3A83%2C%22gen%22%3A0%7D%2C%7B%22name%22%3A%22XYZ%22%7D%2C68%2C716%2C0%5D )
* @param $vars
* @return string
*/
public static function getHashable($vars) {
// Sort by keys according to ASCII order
ksort($vars);

// Formats the values in the following way : Nom_champ=Valeur_champ
array_walk($vars, function (&$value, $key) {$value = "$key=$value";});

// Make it as a single string with * as separation character
$hashable = join("*", $vars);

return $hashable;
}
// Make it as a single string with * as separation character
return implode("*", $vars);
}
}
2 changes: 1 addition & 1 deletion Config/config.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"CMCIC_TPE":"0000001","CMCIC_KEY":"12345678901234567890123456789012345678P0","CMCIC_CODESOCIETE":"4b18fba7070c8ae6bea8","CMCIC_VERSION":"3.0","CMCIC_SERVER":"https:\/\/p.monetico-services.com\/test\/","CMCIC_PAGE":"paiement.cgi"}
{"CMCIC_TPE":"0000001","CMCIC_KEY":"12345678901234567890123456789012345678P0","CMCIC_CODESOCIETE":"4b18fba7070c8ae6bea8","CMCIC_VERSION":"3.0","CMCIC_SERVER":"https:\/\/p.monetico-services.com\/","CMCIC_PAGE":"paiement.cgi"}
6 changes: 6 additions & 0 deletions Config/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@
</service>
</services>

<hooks>
<hook id="cmcic.back.hook" class="CmCIC\Hook\HookManager">
<tag name="hook.event_listener" event="module.configuration" type="back" method="onModuleConfigure" />
</hook>
</hooks>

</config>
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<language>fr_FR</language>
<language>en_US</language>
</languages>
<version>1.3.2</version>
<version>1.4.0</version>
<authors>
<author>
<name>Thelia</name>
Expand Down
7 changes: 0 additions & 7 deletions Config/schema.xml

This file was deleted.

Loading

0 comments on commit 2577e60

Please sign in to comment.