Skip to content

Commit

Permalink
Merge pull request #558 from alma/release/v4.3.0
Browse files Browse the repository at this point in the history
Release v4.3.0
  • Loading branch information
Benjamin-Freoua-Alma authored Aug 12, 2024
2 parents 6a5d729 + 19a172c commit fe05921
Show file tree
Hide file tree
Showing 11 changed files with 496 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ repos:
stages: [commit]

- repo: https://github.com/returntocorp/semgrep
rev: v1.81.0
rev: v1.83.0
hooks:
- id: semgrep
args:
Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## v4.3.0 - 2024-08-12

### Changes

### 🚀 New Features

- feat: Shipment info analysis (#545)

#### Contributors

@Benjamin-Freoua-Alma, @Francois-Gomis, @alma-renovate-bot and @github-actions

## v4.2.0 - 2024-08-06

### Changes
Expand Down
13 changes: 11 additions & 2 deletions alma/alma.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

class Alma extends PaymentModule
{
const VERSION = '4.2.0';
const VERSION = '4.3.0';

public $_path;
public $local_path;
Expand Down Expand Up @@ -80,7 +80,7 @@ public function __construct()
{
$this->name = 'alma';
$this->tab = 'payments_gateways';
$this->version = '4.2.0';
$this->version = '4.3.0';
$this->author = 'Alma';
$this->need_instance = false;
$this->bootstrap = true;
Expand Down Expand Up @@ -754,4 +754,13 @@ public function hookActionGetProductPropertiesBefore($params)
{
return $this->runHookController('actionGetProductPropertiesBefore', $params);
}

public function hookActionObjectUpdateAfter($params)
{
$orderFactory = new Alma\PrestaShop\Factories\OrderFactory();
$clientHelper = new Alma\PrestaShop\Helpers\ClientHelper();
$carrierFactory = new Alma\PrestaShop\Factories\CarrierFactory();
$actionObjectUpdateAfter = new Alma\PrestaShop\Controllers\Hook\ActionObjectUpdateAfter($orderFactory, $clientHelper, $carrierFactory);
$actionObjectUpdateAfter->run($params);
}
}
2 changes: 1 addition & 1 deletion alma/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"require": {
"php": "^5.6 || ~7.0 || ~7.1 || ~7.2 || ~7.3 || ~7.4 || ~8.0 || ~8.1",
"alma/alma-php-client": "^2.0.7",
"alma/alma-php-client": "^2.1.0",
"ext-json": "*",
"ext-openssl": "*",
"prestashop/prestashop-accounts-installer": "^v1.0.4",
Expand Down
192 changes: 192 additions & 0 deletions alma/controllers/hook/ActionObjectUpdateAfter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
<?php

namespace Alma\PrestaShop\Controllers\Hook;

use Alma\API\Client;
use Alma\API\Exceptions\AlmaException;
use Alma\PrestaShop\Exceptions\AlmaActionObjectUpdateException;
use Alma\PrestaShop\Exceptions\ClientException;
use Alma\PrestaShop\Factories\CarrierFactory;
use Alma\PrestaShop\Factories\OrderFactory;
use Alma\PrestaShop\Helpers\ClientHelper;
use Alma\PrestaShop\Helpers\ConstantsHelper;
use Alma\PrestaShop\Logger;

if (!defined('_PS_VERSION_')) {
exit;
}

class ActionObjectUpdateAfter
{
/**
* @var OrderFactory
*/
private $orderFactory;

/**
* @var ClientHelper
*/
private $clientHelper;

/**
* @var CarrierFactory
*/
private $carrierFactory;

public function __construct($orderFactory, $clientHelper, $carrierFactory)
{
$this->orderFactory = $orderFactory;
$this->clientHelper = $clientHelper;
$this->carrierFactory = $carrierFactory;
}

/**
* Send tracking information to Alma only for Alma Orders
*
* @param $params
*
* @return void
*/
public function run($params)
{
try {
$orderCarrier = $this->checkParamsContainValidOrderCarrierObject($params);
$carrier = $this->carrierFactory->create($orderCarrier->id_carrier);
$order = $this->getAlmaOrderFromOrderCarrierOrderId($orderCarrier->id_order);
$almaPaymentExternalId = $this->getAlmaPaymentExternalId($order);
$almaClient = $this->getAlmaClient();
$orderExternalId = $this->getOrCreateAlmaOrderExternalId($almaClient, $order, $almaPaymentExternalId);
$almaClient->orders->addTracking($orderExternalId, $carrier->name, $orderCarrier->tracking_number, $carrier->url);
} catch (AlmaActionObjectUpdateException $e) {
return;
} catch (AlmaException $e) {
Logger::instance()->error('[Alma] - Add tracking error: ' . $e->getMessage());
}
}

/**
* Check if params Object is set and is an OrderCarrier
*
* @param $params
*
* @return \OrderCarrierCore $orderCarrier
*
* @throws AlmaActionObjectUpdateException
*/
private function checkParamsContainValidOrderCarrierObject($params)
{
if (
!isset($params['object']) ||
!($params['object'] instanceof \OrderCarrierCore) ||
$params['object']->tracking_number === ''
) {
throw new AlmaActionObjectUpdateException('Object is not an OrderCarrier');
}

return $params['object'];
}

/**
* Get Alma Order from OrderCarrier OrderId
* Throw Exception for no Alma Order
*
* @param $orderId
*
* @return \Order
*
* @throws AlmaActionObjectUpdateException
*/
private function getAlmaOrderFromOrderCarrierOrderId($orderId)
{
try {
$order = $this->orderFactory->create($orderId);
} catch (\PrestaShopException $e) {
Logger::instance()->error('[Alma] - PrestaShopException - Impossible to get Order with id :' . $orderId);
throw new AlmaActionObjectUpdateException('Impossible to get Order');
}
if ($order->module != ConstantsHelper::ALMA_MODULE_NAME) {
throw new AlmaActionObjectUpdateException('Order is not an Alma Order');
}

return $order;
}

/**
* Get Alma Payment External Id from Order
* Throw Exception if no Payment or no Alma Payment External Id in Order
*
* @param \Order $order
*
* @return string
*
* @throws AlmaActionObjectUpdateException
*/
private function getAlmaPaymentExternalId($order)
{
if (empty($order->getOrderPayments())) {
throw new AlmaActionObjectUpdateException('Order is not an Alma Order');
}
foreach ($order->getOrderPayments() as $orderPayment) {
/** @var \OrderPayment $orderPayment */
if (isset($orderPayment->transaction_id)) {
return $orderPayment->transaction_id;
}
}
Logger::instance()->error('[Alma] - No Alma Payment External Id in order ' . $order->reference);
throw new AlmaActionObjectUpdateException('No Alma Payment External Id');
}

/**
* Get Alma Client or throw Exception
*
* @return Client
*
* @throws AlmaActionObjectUpdateException
*/
private function getAlmaClient()
{
try {
return $this->clientHelper->getAlmaClient();
} catch (ClientException $e) {
Logger::instance()->error('[Alma] - ClientException - ' . $e->getMessage());
throw new AlmaActionObjectUpdateException('Impossible to get Alma Client');
}
}

/**
* Get or Create Alma Order External Id
*
* @param $almaClient
* @param $order
* @param $almaPaymentExternalId
*
* @return mixed|null
*
* @throws AlmaActionObjectUpdateException
*/
private function getOrCreateAlmaOrderExternalId($almaClient, $order, $almaPaymentExternalId)
{
try {
$almaPayment = $almaClient->payments->fetch($almaPaymentExternalId);

$orderExternalId = null;
foreach ($almaPayment->orders as $almaOrder) {
if ($order->reference === $almaOrder->getMerchantReference()) {
$orderExternalId = $almaOrder->getExternalId();
}
}
if (!isset($orderExternalId)) {
$almaOrder = $almaClient->payments->addOrder($almaPaymentExternalId, [
'merchant_reference' => $order->reference,
]
);
$orderExternalId = $almaOrder->getExternalId();
}

return $orderExternalId;
} catch (AlmaException $e) {
Logger::instance()->error('[Alma] - AlmaException ' . $e->getMessage());
throw new AlmaActionObjectUpdateException('Impossible to get or create Alma Order');
}
}
}
11 changes: 11 additions & 0 deletions alma/exceptions/AlmaActionObjectUpdateException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Alma\PrestaShop\Exceptions;

if (!defined('_PS_VERSION_')) {
exit;
}

class AlmaActionObjectUpdateException extends AlmaException
{
}
19 changes: 19 additions & 0 deletions alma/lib/Factories/OrderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Alma\PrestaShop\Factories;

class OrderFactory
{
/**
* @param $id
*
* @return \Order
*
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
*/
public function create($id = null, $id_lang = null)
{
return new \Order($id, $id_lang);
}
}
1 change: 1 addition & 0 deletions alma/lib/Helpers/HookHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function __construct()
public static $almaHooks = [
'moduleRoutes' => 'all',
'actionAdminControllerInitBefore' => 'all',
'actionObjectUpdateAfter' => 'all',
'header' => 'all',
'displayHeader' => 'all',
'displayBackOfficeHeader' => 'all',
Expand Down
Loading

0 comments on commit fe05921

Please sign in to comment.