Skip to content

Commit

Permalink
Feature/change refund function (#48)
Browse files Browse the repository at this point in the history
* Save successful psp reference to PaymentInfo

* Fixes #17 Use succesfull notification for refunds
  • Loading branch information
Rune Laenen authored Sep 3, 2020
1 parent 3b7729f commit 7d100b4
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 deletions.
20 changes: 19 additions & 1 deletion Components/Adyen/RefundService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Adyen\AdyenException;
use Adyen\Service\Modification;
use AdyenPayment\Components\NotificationManager;
use AdyenPayment\Models\PaymentInfo;
use AdyenPayment\Models\Refund;
use Shopware\Components\Model\ModelManager;
use Shopware\Models\Order\Order;
Expand All @@ -27,6 +28,10 @@ class RefundService
* @var NotificationManager
*/
private $notificationManager;
/**
* @var \Doctrine\Common\Persistence\ObjectRepository|\Doctrine\ORM\EntityRepository
*/
private $paymentInfoRepository;

/**
* PaymentMethodService constructor.
Expand All @@ -42,6 +47,7 @@ public function __construct(
$this->apiFactory = $apiFactory;
$this->modelManager = $modelManager;
$this->notificationManager = $notificationManager;
$this->paymentInfoRepository = $modelManager->getRepository(PaymentInfo::class);
}

/**
Expand All @@ -53,11 +59,23 @@ public function __construct(
*/
public function doRefund(int $orderId): Refund
{
/** @var Order $order */
$order = $this->modelManager->find(Order::class, $orderId);
$apiClient = $this->apiFactory->create($order->getShop());
$modification = new Modification($apiClient);

$notification = $this->notificationManager->getLastNotificationForOrderId($orderId);
/** @var PaymentInfo $paymentInfo */
$paymentInfo = $this->paymentInfoRepository->findOneBy([
'orderId' => $orderId
]);

if ($paymentInfo && !empty($paymentInfo->getPspReference())) {
$notification = $this->notificationManager->getLastNotificationForPspReference(
$paymentInfo->getPspReference()
);
} else {
$notification = $this->notificationManager->getLastNotificationForOrderId($orderId);
}

$request = [
'originalReference' => $notification->getPspReference(),
Expand Down
21 changes: 21 additions & 0 deletions Components/NotificationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,25 @@ public function getLastNotificationForOrderId(int $orderId)
return null;
}
}

/**
* @param string $pspReference
* @return mixed|null
* @throws NonUniqueResultException
*/
public function getLastNotificationForPspReference(string $pspReference)
{
try {
$lastNotification = $this->notificationRepository->createQueryBuilder('n')
->where('n.pspReference = :pspReference')
->setMaxResults(1)
->orderBy('n.createdAt', 'ASC')
->setParameter('pspReference', $pspReference)
->getQuery()
->getSingleResult();
return $lastNotification;
} catch (NoResultException $ex) {
return null;
}
}
}
26 changes: 25 additions & 1 deletion Components/NotificationProcessor/Authorisation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use AdyenPayment\Components\PaymentStatusUpdate;
use AdyenPayment\Models\Event;
use AdyenPayment\Models\Notification;
use AdyenPayment\Models\PaymentInfo;
use Psr\Log\LoggerInterface;
use Shopware\Components\ContainerAwareEventManager;
use Shopware\Components\Model\ModelManager;
use Shopware\Models\Order\Status;

/**
Expand All @@ -29,6 +31,14 @@ class Authorisation implements NotificationProcessorInterface
* @var PaymentStatusUpdate
*/
private $paymentStatusUpdate;
/**
* @var ModelManager
*/
private $modelManager;
/**
* @var \Doctrine\Common\Persistence\ObjectRepository|\Doctrine\ORM\EntityRepository
*/
private $paymentInfoRepository;

/**
* Authorisation constructor.
Expand All @@ -39,11 +49,14 @@ class Authorisation implements NotificationProcessorInterface
public function __construct(
LoggerInterface $logger,
ContainerAwareEventManager $eventManager,
PaymentStatusUpdate $paymentStatusUpdate
PaymentStatusUpdate $paymentStatusUpdate,
ModelManager $modelManager
) {
$this->logger = $logger;
$this->eventManager = $eventManager;
$this->paymentStatusUpdate = $paymentStatusUpdate->setLogger($this->logger);
$this->modelManager = $modelManager;
$this->paymentInfoRepository = $modelManager->getRepository(PaymentInfo::class);
}

/**
Expand Down Expand Up @@ -82,5 +95,16 @@ public function process(Notification $notification)
Status::PAYMENT_STATE_THE_PROCESS_HAS_BEEN_CANCELLED;

$this->paymentStatusUpdate->updatePaymentStatus($order, $status);

if ($notification->isSuccess()) {
/** @var PaymentInfo $paymentInfo */
$paymentInfo = $this->paymentInfoRepository->findOneBy([
'orderId' => $order->getId()
]);

$paymentInfo->setPspReference($notification->getPspReference());
$this->modelManager->persist($paymentInfo);
$this->modelManager->flush($paymentInfo);
}
}
}
24 changes: 23 additions & 1 deletion Components/NotificationProcessor/Capture.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AdyenPayment\Components\NotificationProcessor;

use AdyenPayment\Models\PaymentInfo;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
use Doctrine\ORM\TransactionRequiredException;
Expand All @@ -11,6 +12,7 @@
use AdyenPayment\Models\Notification;
use Psr\Log\LoggerInterface;
use Shopware\Components\ContainerAwareEventManager;
use Shopware\Components\Model\ModelManager;
use Shopware\Models\Order\Status;

/**
Expand All @@ -33,6 +35,14 @@ class Capture implements NotificationProcessorInterface
* @var PaymentStatusUpdate
*/
private $paymentStatusUpdate;
/**
* @var ModelManager
*/
private $modelManager;
/**
* @var \Doctrine\Common\Persistence\ObjectRepository|\Doctrine\ORM\EntityRepository
*/
private $paymentInfoRepository;


/**
Expand All @@ -44,11 +54,14 @@ class Capture implements NotificationProcessorInterface
public function __construct(
LoggerInterface $logger,
ContainerAwareEventManager $eventManager,
PaymentStatusUpdate $paymentStatusUpdate
PaymentStatusUpdate $paymentStatusUpdate,
ModelManager $modelManager
) {
$this->logger = $logger;
$this->eventManager = $eventManager;
$this->paymentStatusUpdate = $paymentStatusUpdate->setLogger($this->logger);
$this->modelManager = $modelManager;
$this->paymentInfoRepository = $modelManager->getRepository(PaymentInfo::class);
}

/**
Expand Down Expand Up @@ -87,6 +100,15 @@ public function process(Notification $notification)
$order,
Status::PAYMENT_STATE_COMPLETELY_PAID
);

/** @var PaymentInfo $paymentInfo */
$paymentInfo = $this->paymentInfoRepository->findOneBy([
'orderId' => $order->getId()
]);

$paymentInfo->setPspReference($notification->getPspReference());
$this->modelManager->persist($paymentInfo);
$this->modelManager->flush($paymentInfo);
}
}
}

0 comments on commit 7d100b4

Please sign in to comment.