Skip to content

Commit

Permalink
fix recovery order (#52)
Browse files Browse the repository at this point in the history
* error

* CRD-437 Fix recovery orders when quote has not a valid shipping address

* CRD-437 add missing data

* Refactor phone handling in Data.php and MissingOrders.php

* error intencional

* error intencional

* error intencional

* error intencional

* error intencional

* update CHANGELOG.md
  • Loading branch information
fcarrero authored Jul 25, 2024
1 parent e44e920 commit b189226
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 5.1.4 - 2024/07/18
* Fix:
- Fix recovery orders when quote has not a valid shipping address
- Fix, error to install another payment method
### 5.1.3 - 2024/03/07
* Fix:
- [CRD-191] Fix bundle product. @fcarrero [#51]
Expand Down
10 changes: 9 additions & 1 deletion Controller/Webhook/Index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Conekta\Payments\Controller\Webhook;

use Conekta\Payments\Exception\EntityNotFoundException;
use Conekta\Payments\Logger\Logger as ConektaLogger;
use Conekta\Payments\Model\WebhookRepository;
use Conekta\Payments\Service\MissingOrders;
Expand Down Expand Up @@ -154,7 +155,14 @@ public function execute()
break;
}

} catch (Exception $e) {
}catch (EntityNotFoundException $e) {
$errorResponse = [
'error' => 'Entity Not Found',
'message' => $e->getMessage(),
];
return $this->sendJsonResponse($errorResponse, Response::STATUS_CODE_404);
}
catch (Exception $e) {
$this->_conektaLogger->error('Controller Index :: '. $e->getMessage());
$errorResponse = [
'error' => 'Internal Server Error',
Expand Down
4 changes: 3 additions & 1 deletion Exception/ConektaException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Conekta\Payments\Exception;

class ConektaException extends \Exception
use Exception;

class ConektaException extends Exception
{
public const INVALID_PHONE_MESSAGE = 'Télefono no válido.
El télefono debe tener al menos 10 carácteres.
Expand Down
10 changes: 10 additions & 0 deletions Exception/EntityNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Conekta\Payments\Exception;

use Exception;

class EntityNotFoundException extends Exception
{

}
2 changes: 1 addition & 1 deletion Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Conekta\Payments\Logger\Logger as ConektaLogger;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\ProductMetadataInterface;
use Magento\Framework\Encryption\EncryptorInterface;
use Magento\Framework\Exception\LocalizedException;
Expand Down Expand Up @@ -676,6 +675,7 @@ public function getBillingAddress(int $quoteId): array
$phone = $this->removePhoneSpecialCharacter($address->getTelephone());

$billingContact = [
'phone'=> $phone,
'name' => $this->getCustomerName($address),
'address' => [
'city' => $address->getCity(),
Expand Down
3 changes: 2 additions & 1 deletion Model/WebhookRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Conekta\Payments\Model;

use Conekta\Payments\Exception\EntityNotFoundException;
use Conekta\Payments\Logger\Logger as ConektaLogger;
use Conekta\Payments\Api\Data\ConektaSalesOrderInterface;
use Exception;
Expand Down Expand Up @@ -150,7 +151,7 @@ public function payOrder($body)
$this->_conektaLogger->error(
'WebhookRepository :: execute - ' . $message
);
throw new LocalizedException(__($message));
throw new EntityNotFoundException(__($message));
}

$order->setState(Order::STATE_PROCESSING);
Expand Down
14 changes: 12 additions & 2 deletions Observer/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Conekta\Payments\Model\Config;
use Magento\Framework\Event\Observer;
use Magento\Framework\Validator\Exception;
use Conekta\Payments\Helper\Data as ConektaHelper;

/**
* Class CreateWebhook
Expand All @@ -18,6 +19,8 @@ class Webhook implements ObserverInterface
* @var Config
*/
protected Config $config;

protected ConektaHelper $_conektaHelper;
/**
* @var ManagerInterface
*/
Expand All @@ -28,10 +31,12 @@ class Webhook implements ObserverInterface
*/
public function __construct(
Config $config,
ManagerInterface $messageManager
ManagerInterface $messageManager,
ConektaHelper $_conektaHelper
) {
$this->config = $config;
$this->messageManager = $messageManager;
$this->_conektaHelper = $_conektaHelper;
}

/**
Expand All @@ -43,6 +48,11 @@ public function __construct(
*/
public function execute(Observer $observer)
{
$this->config->createWebhook();
if ($this->_conektaHelper->isCashEnabled()
|| $this->_conektaHelper->isBankTransferEnabled()
|| $this->_conektaHelper->isCreditCardEnabled()){

$this->config->createWebhook();
}
}
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![alt tag](https://conekta.com/static/assets/Home/conekta-logo-blue-full.svg)

Magento 2 Plugin v.5.1.3 (Stable)
Magento 2 Plugin v.5.1.4 (Stable)
========================

Installation for Magento 2.3
Expand All @@ -13,7 +13,7 @@ composer config repositories.conekta git https://github.com/conekta/customer-mag

2. Add composer dependency
```bash
composer require conekta/conekta_payments 5.1.3
composer require conekta/conekta_payments 5.1.4
```

3. Update Magento
Expand Down
54 changes: 47 additions & 7 deletions Service/MissingOrders.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
use Conekta\Payments\Model\WebhookRepository;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\QuoteManagement;
use Exception;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Sales\Api\Data\OrderInterface;

use Conekta\Payments\Helper\Util;
use Conekta\Payments\Helper\Data as ConektaData;
class MissingOrders
{
/**
Expand All @@ -30,6 +33,8 @@ class MissingOrders

private ObjectManager $objectManager;

private Util $utilHelper;


public function __construct(
WebhookRepository $webhookRepository,
Expand All @@ -45,6 +50,7 @@ public function __construct(

$this->objectManager = ObjectManager::getInstance();
$this->_cartRepository = $cartRepository;
$this->utilHelper = $this->objectManager->create(ConektaData::class);
}

/**
Expand Down Expand Up @@ -82,7 +88,7 @@ public function recover_order($event){
];
$additionalInformation= array_merge($additionalInformation, $this->getAdditionalInformation($conektaOrder));
$quoteCreated->getPayment()->setAdditionalInformation($additionalInformation);

$this->saveMissingFieldsQuote($quoteCreated, $conektaOrder);
$order = $this->quoteManagement->submit($quoteCreated);


Expand All @@ -92,17 +98,51 @@ public function recover_order($event){
->setIsCustomerNotified(true)
->save();
$this->updateConektaReference($conektaOrder["charges"]["data"][0]["id"], $order->getRealOrderId());


$this->_conektaLogger->info('end submit new flow');
return ;

} catch (Exception | LocalizedException $e) {
$this->_conektaLogger->error('creating order '.$e->getMessage());
}catch (NoSuchEntityException $e){
$this->_conektaLogger->error($e->getMessage());
return;
}
catch (Exception | LocalizedException $e) {
$this->_conektaLogger->error('recovery order '.$e->getMessage());
throw $e;
}
}

private function saveMissingFieldsQuote(Quote $quoteCreated, array $conektaOrder){
$shippingNameReceiver = $this->utilHelper->splitName($conektaOrder["shipping_contact"]["receiver"]);
$shipping_address = [
'firstname' => $shippingNameReceiver["firstname"],
'lastname' => $shippingNameReceiver["lastname"],
'street' => [ $conektaOrder["shipping_contact"]["address"]["street1"], $conektaOrder["shipping_contact"]["address"]["street2"] ?? ""],
'city' => $conektaOrder["shipping_contact"]["address"]["city"],
'country_id' => strtoupper($conektaOrder["fiscal_entity"]["address"]["country"]),
'region' => $conektaOrder["shipping_contact"]["address"]["state"],
'postcode' => $conektaOrder["shipping_contact"]["address"]["postal_code"],
'telephone' => $conektaOrder["shipping_contact"]["phone"] ?? "5200000000",
'region_id' => $conektaOrder["shipping_contact"]["metadata"]["region_id"],
'company' => $conektaOrder["shipping_contact"]["metadata"]["company"],
];
$billingAddressName = $this->utilHelper->splitName($conektaOrder["fiscal_entity"]["name"]);
$billing_address = [
'firstname' => $billingAddressName["firstname"],
'lastname' => $billingAddressName["lastname"],
'street' => [ $conektaOrder["fiscal_entity"]["address"]["street1"] , $conektaOrder["fiscal_entity"]["address"]["street2"] ?? "" ],
'city' => $conektaOrder["fiscal_entity"]["address"]["city"],
'country_id' => strtoupper($conektaOrder["fiscal_entity"]["address"]["country"]),
'region' => $conektaOrder["fiscal_entity"]["address"]["state"],
'postcode' => $conektaOrder["fiscal_entity"]["address"]["postal_code"],
'telephone' => $conektaOrder["fiscal_entity"]["phone"] ?? $conektaOrder["shipping_contact"]["phone"] ?? "5200000000",
'region_id' =>$conektaOrder["fiscal_entity"]["metadata"]["region_id"],
'company' => $conektaOrder["fiscal_entity"]["metadata"]["company"]
];

//Set Address to quote
$quoteCreated->getBillingAddress()->addData($billing_address);

$quoteCreated->getShippingAddress()->addData($shipping_address);
}
private function getAdditionalInformation(array $conektaOrder) :array{
switch ($conektaOrder["charges"]["data"][0]["payment_method"]["object"]){
case "card_payment":
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"conekta/conekta-php": "v6.0.8"
},
"type": "magento2-module",
"version": "5.1.3",
"version": "5.1.4",
"license": [
"OSL-3.0",
"AFL-3.0"
Expand Down
2 changes: 1 addition & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<system>
<section id="payment">
<group id="conekta" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0">
<comment><![CDATA[<div class="conekta-payment-logo"></div><div class="conekta-payment-text">Conekta Configuration. (v5.1.3) </div>]]></comment>
<comment><![CDATA[<div class="conekta-payment-logo"></div><div class="conekta-payment-text">Conekta Configuration. (v5.1.4) </div>]]></comment>
<fieldset_css>complex conekta-section</fieldset_css>
<frontend_model>Magento\Paypal\Block\Adminhtml\System\Config\Fieldset\Payment</frontend_model>
<!--global-->
Expand Down
2 changes: 1 addition & 1 deletion etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
<global>
<api_version><![CDATA[2.0.0]]></api_version>
<plugin_type><![CDATA[Magento 2]]></plugin_type>
<plugin_version><![CDATA[5.1.3]]></plugin_version>
<plugin_version><![CDATA[5.1.4]]></plugin_version>
</global>
</conekta>
</default>
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Conekta_Payments" setup_version="5.1.3">
<module name="Conekta_Payments" setup_version="5.1.4">
<sequence>
<module name="Magento_Sales"/>
<module name="Magento_Payment"/>
Expand Down

0 comments on commit b189226

Please sign in to comment.