Skip to content

Commit

Permalink
Merge pull request vindi#60 from cedran/master
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
cedran authored Jun 17, 2024
2 parents b58ed73 + 8967277 commit a2a02a9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
15 changes: 15 additions & 0 deletions Block/Subscription/SubscriptionList.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Vindi\Payment\Block\Subscription;

use DateTime;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
Expand Down Expand Up @@ -105,6 +106,20 @@ public function getSubscriptions()
return $this->_subscriptionCollection;
}

/**
* @return string
*/
public function getStartAt($startAt)
{
try {
$startAt = new DateTime($startAt);
return $startAt->format('d/m/Y');
} catch (Exception $e) {
}

return '-';
}

/**
* Get status label
*
Expand Down
18 changes: 17 additions & 1 deletion Controller/Subscription/CancelSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
use Vindi\Payment\Model\Vindi\Subscription;
use Vindi\Payment\Model\SubscriptionRepository;

class CancelSubscription extends Action
{
Expand All @@ -19,19 +20,28 @@ class CancelSubscription extends Action
*/
protected $subscription;

/**
* @var SubscriptionRepository
*/
protected $subscriptionRepository;

/**
* CancelSubscription constructor.
*
* @param Context $context
* @param JsonFactory $resultJsonFactory
* @param Subscription $subscription
* @param SubscriptionRepository $subscriptionRepository
*/
public function __construct(
Context $context,
JsonFactory $resultJsonFactory,
Subscription $subscription
Subscription $subscription,
SubscriptionRepository $subscriptionRepository
) {
$this->resultJsonFactory = $resultJsonFactory;
$this->subscription = $subscription;
$this->subscriptionRepository = $subscriptionRepository;
parent::__construct($context);
}

Expand All @@ -48,6 +58,12 @@ public function execute()
if ($subscriptionId) {
try {
$this->subscription->deleteAndCancelBills($subscriptionId);

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$subscription = $objectManager->create(\Vindi\Payment\Model\Subscription::class)->load($subscriptionId);
$subscription->setStatus('canceled');
$subscription->save();

$this->messageManager->addSuccessMessage(__('Subscription canceled successfully.'));
} catch (\Exception $e) {
$this->messageManager->addWarningMessage(__('Something went wrong while cancel the Subscription.'));
Expand Down
22 changes: 21 additions & 1 deletion Helper/WebHookHandlers/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\OrderFactory;
use Psr\Log\LoggerInterface;
use Vindi\Payment\Model\SubscriptionRepository;

/**
* Class Subscription
Expand All @@ -32,23 +33,31 @@ class Subscription
*/
private $orderFactory;

/**
* @var SubscriptionRepository
*/
protected $subscriptionRepository;

/**
* Subscription constructor.
* @param OrderRepositoryInterface $orderRepository
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param OrderFactory $orderFactory
* @param LoggerInterface $logger
* @param SubscriptionRepository $subscriptionRepository
*/
public function __construct(
OrderRepositoryInterface $orderRepository,
SearchCriteriaBuilder $searchCriteriaBuilder,
OrderFactory $orderFactory,
LoggerInterface $logger
LoggerInterface $logger,
SubscriptionRepository $subscriptionRepository
) {
$this->logger = $logger;
$this->orderRepository = $orderRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->orderFactory = $orderFactory;
$this->subscriptionRepository = $subscriptionRepository;
}

/**
Expand Down Expand Up @@ -78,11 +87,22 @@ public function canceled($data)
return false;
}

if (isset($data['subscription']['id'])) {
return false;
}

$subscriptionId = $data['subscription']['id'];

$order->addCommentToStatusHistory(__('The subscription was canceled')->getText());
$this->orderRepository->save($order);

$this->cancel($order->getIncrementId());

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$subscription = $objectManager->create(\Vindi\Payment\Model\Subscription::class)->load($subscriptionId);
$subscription->setStatus('canceled');
$subscription->save();

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ $pageTitle = __("Subscriptions");
<table>
<thead>
<tr>
<th><?= __('ID') ?></th>
<th><?= __('Client') ?></th>
<th><?= __('Plan') ?></th>
<th><?= __('Start') ?></th>
Expand All @@ -18,9 +19,10 @@ $pageTitle = __("Subscriptions");
<tbody>
<?php foreach ($subscriptions as $subscription): ?>
<tr onclick="window.location='<?= $block->getUrl('vindi_vr/subscription/details', ['id' => $subscription->getId()]) ?>';" style="cursor: pointer;">
<td><?= $subscription->getId() ?></td>
<td><?= $subscription->getClient() ?></td>
<td><?= $subscription->getPlan() ?></td>
<td><?= $block->formatDate($subscription->getStartAt(), \IntlDateFormatter::SHORT, false) ?></td>
<td><?= $block->getStartAt($subscription->getStartAt()) ?></td>
<td><?= $block->getPaymentMethodLabel($subscription->getPaymentMethod()) ?></td>
<td><?= $block->getStatusLabel($subscription->getStatus()) ?></td>
</tr>
Expand Down

0 comments on commit a2a02a9

Please sign in to comment.