Skip to content

Commit

Permalink
V2.3 Different fixes. Update readme and translations (#36)
Browse files Browse the repository at this point in the history
* [NN2-136] Fetch phone num from billing address. Fixed type error handling. Fixing ACL

* [NN2-136] Fixed transaction attempt registration. Removed SID from frontend URL

* [NN2-136] Fixed statistics collection

* [NN2-136] Updated readme. Added template for report email layout

* [NN2-136] Added ability to set cron expressions manually. Fixed email sending error for Magento 2.3.

* [NN2-136] Updated readme.

* codestyle

* [NN2-136] Fixed report sending by cron (undefined index). Added translations. Minor fixes.
  • Loading branch information
Mavlyan authored and luckyraul committed Apr 29, 2019
1 parent 9906baa commit df7de31
Show file tree
Hide file tree
Showing 26 changed files with 350 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Api/Data/ItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function getTax(): string;
* @param string $tax
* @return $this
*/
public function setTax(string $tax);
public function setTax($tax);

/**
* @return float
Expand Down
8 changes: 4 additions & 4 deletions Api/Data/RequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ public function setSno(string $sno): self;
public function getEmail(): string;

/**
* @param string $email
* @param string|null $email
* @return $this
*/
public function setEmail(string $email): self;
public function setEmail($email): self;

/**
* @return string
*/
public function getPhone(): string;

/**
* @param string $phone
* @param string|null $phone
* @return $this
*/
public function setPhone(string $phone): self;
public function setPhone($phone): self;

/**
* @return \Mygento\Kkm\Api\Data\ItemInterface[]
Expand Down
5 changes: 5 additions & 0 deletions Controller/Adminhtml/Cheque/CheckStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

class CheckStatus extends \Magento\Backend\App\Action
{
/**
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Mygento_Kkm::cheque_checkstatus';

/** @var \Mygento\Kkm\Helper\Data */
protected $kkmHelper;

Expand Down
9 changes: 9 additions & 0 deletions Controller/Adminhtml/Cheque/Resend.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

class Resend extends \Magento\Backend\App\Action
{
/**
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Mygento_Kkm::cheque_resend';

/** @var \Mygento\Kkm\Helper\Data */
protected $kkmHelper;

Expand Down Expand Up @@ -100,6 +105,10 @@ public function execute()
$this->getMessageManager()->addErrorMessage($exc->getMessage());
$this->kkmHelper->error('Resend failed. Reason: ' . $exc->getMessage());
$this->errorHelper->processKkmChequeRegistrationError($entity, $exc);
} catch (\Throwable $thr) {
$this->getMessageManager()->addErrorMessage(__('Something went wrong. See log.'));
$this->kkmHelper->error('Resend failed. Reason: ' . $thr->getMessage());
$this->errorHelper->processKkmChequeRegistrationError($entity, $thr);
} finally {
return $this->resultRedirectFactory->create()->setUrl(
$this->_redirect->getRefererUrl()
Expand Down
2 changes: 1 addition & 1 deletion Controller/Adminhtml/Logs/Clear.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function execute()
$this->getMessageManager()->addSuccessMessage(
__('Logs have been cleared')
);
} catch (\Exception $exc) {
} catch (\Throwable $exc) {
$this->getMessageManager()->addErrorMessage($exc->getMessage());
}

Expand Down
2 changes: 1 addition & 1 deletion Controller/Frontend/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function execute()
->sendResponse();

$result->setContents($entity->getIncrementId());
} catch (\Exception $exc) {
} catch (\Throwable $exc) {
$this->kkmHelper->error($exc->getMessage());
$this->kkmHelper->debug("Callback RAW: {$json}");

Expand Down
5 changes: 3 additions & 2 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ class Data extends \Mygento\Base\Helper\Data

/**
* @param string $param
* @param string|null $scopeCode
* @return string
*/
public function getConfig($param, $scopeCode = NULL)
public function getConfig($param, $scopeCode = null)
{
return parent::getConfig($this->getCode() . '/' . $param);
return parent::getConfig($this->getCode() . '/' . $param, $scopeCode);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Helper/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function setSender($email, $name)
*/
public function setRecipient($email)
{
$this->recipient['email'] = $email;
$this->recipient = explode(',', $email);

return $this;
}
Expand Down
7 changes: 4 additions & 3 deletions Helper/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public function __construct(
/**
* Makes different notifications if cheque was not successfully sent to KKM
* @param \Magento\Sales\Api\Data\EntityInterface $entity
* @param \Exception|null $exception
* @param \Throwable|null $exception
*/
public function processKkmChequeRegistrationError($entity, \Exception $exception = null)
public function processKkmChequeRegistrationError($entity, \Throwable $exception = null)
{
try {
$entityType = ucfirst($entity->getEntityType());
Expand All @@ -74,6 +74,7 @@ public function processKkmChequeRegistrationError($entity, \Exception $exception
$fullMessage .= $uuid ? ". Transaction Id (uuid): {$uuid}" : '';
}
$this->baseHelper->error($fullMessage);
$this->baseHelper->debug($exception->getTraceAsString());

//Show Admin Messages
if ($this->baseHelper->getConfig('general/admin_notifications')) {
Expand All @@ -92,7 +93,7 @@ public function processKkmChequeRegistrationError($entity, \Exception $exception
$fullMessage
);
$this->orderRepository->save($order);
} catch (\Exception $e) {
} catch (\Throwable $e) {
$this->baseHelper->error($e->getMessage());
}
}
Expand Down
15 changes: 15 additions & 0 deletions Helper/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ public function __construct(
$this->invoiceFactory = $invoiceFactory;
}

/**
* @param CreditmemoInterface|InvoiceInterface $entity
* @param ResponseInterface $response
* @throws \Magento\Framework\Exception\LocalizedException
* @return \Magento\Sales\Api\Data\TransactionInterface
*/
public function registerTransaction($entity, ResponseInterface $response)
{
if ($entity instanceof InvoiceInterface) {
return $this->saveSellTransaction($entity, $response);
}

return $this->saveRefundTransaction($entity, $response);
}

/**
* @param \Magento\Sales\Api\Data\InvoiceInterface $invoice
* @param ResponseInterface $response
Expand Down
4 changes: 3 additions & 1 deletion Helper/TransactionAttempt.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ public function getTrials(RequestInterface $request)
public function registerAttempt(RequestInterface $request, $entityIncrementId, $orderId)
{
$attempt = $this->attemptRepository
->getByEntityId($request->getOperationType(), $request->getSalesEntityId());
->getByEntityId($request->getOperationType(), $entityIncrementId);

$this->kkmHelper->debug('Attempt found: ' . $attempt->getId(), $attempt->getData());

$trials = $attempt->getNumberOfTrials();

Expand Down
2 changes: 1 addition & 1 deletion Model/Atol/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function sendRefund($request): ResponseInterface
$this->kkmHelper->debug('Response:', [$response]);
} catch (VendorBadServerAnswerException $exc) {
throw $exc;
} catch (\Exception $exc) {
} catch (\Throwable $exc) {
throw new CreateDocumentFailedException(
$exc->getMessage(),
$response ?? null,
Expand Down
2 changes: 1 addition & 1 deletion Model/Atol/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function getTax(): string
* @inheritdoc
* @throws \Exception
*/
public function setTax(string $tax)
public function setTax($tax)
{
if (!in_array($tax, Tax::getAllTaxes(), true)) {
throw new \Exception("Incorrect tax {$tax} for Item {$this->getName()}");
Expand Down
4 changes: 2 additions & 2 deletions Model/Atol/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function getEmail(): string
/**
* @inheritdoc
*/
public function setEmail(string $email): RequestInterface
public function setEmail($email): RequestInterface
{
$this->email = $email;

Expand All @@ -127,7 +127,7 @@ public function getPhone(): string
/**
* @inheritdoc
*/
public function setPhone(string $phone): RequestInterface
public function setPhone($phone): RequestInterface
{
$this->phone = $phone;

Expand Down
14 changes: 9 additions & 5 deletions Model/Atol/Vendor.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ public function buildRequest($salesEntity): RequestInterface
->setPaymentObject($paymentObject);
}

$telephone = $order->getShippingAddress()
? $order->getShippingAddress()->getTelephone()
$telephone = $order->getBillingAddress()
? (string) $order->getBillingAddress()->getTelephone()
: '';

$request
Expand Down Expand Up @@ -342,7 +342,10 @@ public function generateExternalId(EntityInterface $entity, $postfix = '')
public function getCallbackUrl()
{
return $this->kkmHelper->getConfig('atol/callback_url')
?? $this->urlHelper->getUrl('kkm/frontend/callback', ['_secure' => true]);
?? $this->urlHelper->getUrl('kkm/frontend/callback', [
'_secure' => true,
'_nosid' => true,
]);
}

/**
Expand Down Expand Up @@ -385,6 +388,7 @@ public function addCommentToOrder($entity, ResponseInterface $response, $txnId =
* @throws CreateDocumentFailedException
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Throwable
* @return ResponseInterface|null
*/
private function sendRequest($request, $callback, $entity = null)
Expand Down Expand Up @@ -413,12 +417,12 @@ private function sendRequest($request, $callback, $entity = null)
$response = $this->apiClient->{$callback}($request);

//Save transaction data
$txn = $this->transactionHelper->saveSellTransaction($entity, $response);
$txn = $this->transactionHelper->registerTransaction($entity, $response);
$this->addCommentToOrder($entity, $response, $txn->getId() ?? null);

//Mark attempt as Sent
$this->attemptHelper->finishAttempt($attempt);
} catch (\Exception $e) {
} catch (\Throwable $e) {
//Mark attempt as Error
$this->attemptHelper->failAttempt($attempt, $e->getMessage());

Expand Down
4 changes: 2 additions & 2 deletions Model/Queue/Consumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function sendSellRequest($request)

$request->setIgnoreTrialsNum(false);
$this->publisher->publish(Processor::TOPIC_NAME_SELL, $request);
} catch (\Exception $e) {
} catch (\Throwable $e) {
$entity = $this->requestHelper->getEntityByRequest($request);
$this->errorHelper->processKkmChequeRegistrationError($entity, $e);
}
Expand All @@ -120,7 +120,7 @@ public function sendRefundRequest($request)

$request->setIgnoreTrialsNum(false);
$this->publisher->publish(Processor::TOPIC_NAME_REFUND, $request);
} catch (\Exception $e) {
} catch (\Throwable $e) {
$entity = $this->requestHelper->getEntityByRequest($request);
$this->errorHelper->processKkmChequeRegistrationError($entity, $e);
}
Expand Down
18 changes: 12 additions & 6 deletions Model/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public function getWeekStatistics()
/**
* @param string $from
* @param string|null $to
* @throws \Magento\Framework\Exception\LocalizedException
* @return \Mygento\Kkm\Model\Statistics
*/
public function getStatisticsByPeriod($from, $to = null)
Expand All @@ -110,23 +111,28 @@ public function getStatisticsByPeriod($from, $to = null)
}

$searchCriteria = $this->searchCriteriaBuilder
->addFilter('created_at', $from, 'gteq')
->create();
->addFilter('created_at', $from, 'gteq');

return $this->collectStatistics($searchCriteria)
->setFromDate($from)
->setToDate($to);
}

/**
* @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
* @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
* @throws \Magento\Framework\Exception\LocalizedException
* @return \Mygento\Kkm\Model\Statistics
*/
private function collectStatistics($searchCriteria)
private function collectStatistics($searchCriteriaBuilder)
{
$transactions = $this->transactionRepo->getList($searchCriteria);
$transactionAttempts = $this->attemptRepository->getList($searchCriteria);
$transactions = $this->transactionRepo->getList(
$searchCriteriaBuilder
->addFilter('kkm_status', null, 'neq')
->create()
);
$transactionAttempts = $this->attemptRepository->getList(
$searchCriteriaBuilder->create()
);

/** @var $statistics \Mygento\Kkm\Model\Statistics */
$statistics = $this->statisticsFactory->create();
Expand Down
9 changes: 9 additions & 0 deletions Observer/Send.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
use Magento\Sales\Api\Data\InvoiceInterface;
use Mygento\Kkm\Model\VendorInterface;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Send implements ObserverInterface
{
/** @var \Mygento\Kkm\Helper\Data */
Expand Down Expand Up @@ -142,6 +145,12 @@ private function proceed($entity)
);

$this->errorHelper->processKkmChequeRegistrationError($entity, $exc);
} catch (\Throwable $thr) {
$this->messageManager->addErrorMessage(
__('Cheque has not been successfully registered on KKM vendor side. See log.')
);
$this->kkmHelper->error('Resend failed. Reason: ' . $thr->getMessage());
$this->errorHelper->processKkmChequeRegistrationError($entity, $thr);
}
}

Expand Down
Loading

0 comments on commit df7de31

Please sign in to comment.