Skip to content

Commit

Permalink
Fixes #17 Use succesfull notification for refunds
Browse files Browse the repository at this point in the history
  • Loading branch information
Rune Laenen committed Aug 28, 2020
1 parent e89487b commit 60088f7
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 38 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;
}
}
}
42 changes: 21 additions & 21 deletions Components/NotificationProcessor/Authorisation.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ class Authorisation implements NotificationProcessorInterface
* @var PaymentStatusUpdate
*/
private $paymentStatusUpdate;
/**
* @var ModelManager
*/
private $modelManager;
/**
* @var \Doctrine\Common\Persistence\ObjectRepository|\Doctrine\ORM\EntityRepository
*/
private $paymentInfoRepository;
/**
* @var ModelManager
*/
private $modelManager;
/**
* @var \Doctrine\Common\Persistence\ObjectRepository|\Doctrine\ORM\EntityRepository
*/
private $paymentInfoRepository;

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

/**
* Returns boolean on whether this processor can process the Notification object
Expand Down Expand Up @@ -97,14 +97,14 @@ public function process(Notification $notification)
$this->paymentStatusUpdate->updatePaymentStatus($order, $status);

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

$paymentInfo->setPspReference($notification->getPspReference());
$this->modelManager->persist($paymentInfo);
$this->modelManager->flush($paymentInfo);
}
$paymentInfo->setPspReference($notification->getPspReference());
$this->modelManager->persist($paymentInfo);
$this->modelManager->flush($paymentInfo);
}
}
}
32 changes: 16 additions & 16 deletions Components/NotificationProcessor/Capture.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ class Capture implements NotificationProcessorInterface
* @var PaymentStatusUpdate
*/
private $paymentStatusUpdate;
/**
* @var ModelManager
*/
private $modelManager;
/**
* @var \Doctrine\Common\Persistence\ObjectRepository|\Doctrine\ORM\EntityRepository
*/
private $paymentInfoRepository;
/**
* @var ModelManager
*/
private $modelManager;
/**
* @var \Doctrine\Common\Persistence\ObjectRepository|\Doctrine\ORM\EntityRepository
*/
private $paymentInfoRepository;


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

/**
* Returns boolean on whether this processor can process the Notification object
Expand Down Expand Up @@ -101,9 +101,9 @@ public function process(Notification $notification)
Status::PAYMENT_STATE_COMPLETELY_PAID
);

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

$paymentInfo->setPspReference($notification->getPspReference());
Expand Down

0 comments on commit 60088f7

Please sign in to comment.