Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 1.5.0 #53

Merged
merged 20 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Validate composer.json and composer.lock
run: composer validate

- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Run PHP Code Sniffer
run: vendor/bin/phpcs --ignore=*vendor/ --ignore=AdyenPayment.php --ignore=Controllers --standard=PSR2 .
run: vendor/bin/phpcs .

- name: Make sure project files are compilable
run: find -L . -path ./vendor -prune -o -path ./tests -prune -o -name '*.php' -print0 | xargs -0 -n 1 -P 4 php -l
5 changes: 4 additions & 1 deletion AdyenPayment.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

//phpcs:disable PSR1.Files.SideEffects

namespace AdyenPayment;

use Doctrine\Common\Cache\Cache;
Expand Down Expand Up @@ -40,7 +42,7 @@ class AdyenPayment extends Plugin
*/
public static function isPackage(): bool
{
return file_exists(self::getPackageVendorAutoload());
return file_exists(static::getPackageVendorAutoload());
}

/**
Expand Down Expand Up @@ -236,3 +238,4 @@ private function rebuildAttributeModels()
if (AdyenPayment::isPackage()) {
require_once AdyenPayment::getPackageVendorAutoload();
}
//phpcs:enable PSR1.Files.SideEffects
9 changes: 8 additions & 1 deletion Components/Adyen/PaymentMethodService.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function getPaymentMethods(
$locale = null,
$cache = true
): array {
$cache = $cache && $this->configuration->isPaymentmethodsCacheEnabled();
$cacheKey = $this->getCacheKey($countryCode ?? '', $currency ?? '', (string)$value ?? '');
if ($cache && isset($this->cache[$cacheKey])) {
return $this->cache[$cacheKey];
Expand All @@ -88,7 +89,13 @@ public function getPaymentMethods(
try {
$paymentMethods = $checkout->paymentMethods($requestParams);
} catch (AdyenException $e) {
$this->logger->critical($e);
$this->logger->critical('Adyen Exception', [
'message' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'errorType' => $e->getErrorType(),
'status' => $e->getStatus()
]);
return [];
}

Expand Down
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
5 changes: 5 additions & 0 deletions Components/BasketService.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ public function restoreFromOrder(Order $order)
foreach ($orderDetails as $orderDetail) {
$this->processOrderDetail($order, $orderDetail);
}

$this->events->notify(Event::BASKET_RESTORE_FROM_ORDER, [
'order' => $order
]);

$this->sBasket->sRefreshBasket();
}

Expand Down
49 changes: 33 additions & 16 deletions Components/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,18 @@ public function __construct(
* @param bool $shop
* @return string
*/
public function getEnvironment($shop = false): string
public function getEnvironment($shop = false, $lowercase = false): string
{
$environment = Environment::TEST;
if ($this->getConfig('environment', $shop) === self::ENV_LIVE) {
return Environment::LIVE;
$environment = Environment::LIVE;
}

return Environment::TEST;
if ($lowercase) {
return strtolower($environment);
}

return $environment;
}

/**
Expand Down Expand Up @@ -97,22 +102,16 @@ public function getConfig($key = null, $shop = false)
return null;
}

/**
* @param bool|Shop $shop
* @return string
*/
public function getJsComponents3DS2ChallengeImageSize($shop = false): string
{
return (string)$this->getConfig('js_components_3DS2_challenge_image_size', $shop);
}

/**
* @param bool|Shop $shop
* @return string
*/
public function getApiKey($shop = false): string
{
return (string)$this->getConfig('api_key', $shop);
return (string)$this->getConfig(
'api_key_' . $this->getEnvironment($shop, true),
$shop
);
}

/**
Expand All @@ -139,7 +138,10 @@ public function getOriginKey($shop = false): string
*/
public function getNotificationHmac($shop = false): string
{
return (string)$this->getConfig('notification_hmac', $shop);
return (string)$this->getConfig(
'notification_hmac_' . $this->getEnvironment($shop, true),
$shop
);
}

/**
Expand All @@ -148,7 +150,10 @@ public function getNotificationHmac($shop = false): string
*/
public function getNotificationAuthUsername($shop = false): string
{
return (string)$this->getConfig('notification_auth_username', $shop);
return (string)$this->getConfig(
'notification_auth_username_' . $this->getEnvironment($shop, true),
$shop
);
}

/**
Expand All @@ -157,7 +162,10 @@ public function getNotificationAuthUsername($shop = false): string
*/
public function getNotificationAuthPassword($shop = false): string
{
return (string)$this->getConfig('notification_auth_password', $shop);
return (string)$this->getConfig(
'notification_auth_password_' . $this->getEnvironment($shop, true),
$shop
);
}

/**
Expand All @@ -169,6 +177,15 @@ public function getGoogleMerchantId($shop = false): string
return (string)$this->getConfig('google_merchant_id', $shop);
}

/**
* @param bool $shop
* @return bool
*/
public function isPaymentmethodsCacheEnabled($shop = false): bool
{
return (bool)$this->getConfig('paymentmethods_cache', $shop);
}

/**
* @return string
*/
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;
}
}
}
6 changes: 5 additions & 1 deletion Components/NotificationProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ private function process(Notification $notification)
} catch (\Exception $exception) {
$status = NotificationStatus::STATUS_FATAL;
$this->logger->notice('General Exception', [
'exception' => $exception,
'exception' => [
'message' => $exception->getMessage(),
'file' => $exception->getFile(),
'line' => $exception->getLine()
],
'notificationId' => $notification->getId()
]);
yield new NotificationProcessorFeedback(
Expand Down
28 changes: 26 additions & 2 deletions 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 @@ -78,9 +91,20 @@ public function process(Notification $notification)
);

$status = $notification->isSuccess() ?
Status::PAYMENT_STATE_THE_CREDIT_HAS_BEEN_ACCEPTED :
Status::PAYMENT_STATE_COMPLETELY_PAID :
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);
}
}
}
26 changes: 24 additions & 2 deletions 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 @@ -85,8 +98,17 @@ public function process(Notification $notification)
if ($notification->isSuccess()) {
$this->paymentStatusUpdate->updatePaymentStatus(
$order,
Status::PAYMENT_STATE_THE_CREDIT_HAS_BEEN_ACCEPTED
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);
}
}
}
Loading