Skip to content

Commit

Permalink
Merge pull request #286 from Adyen/develop
Browse files Browse the repository at this point in the history
Release 3.6.0
  • Loading branch information
peterojo authored Sep 1, 2022
2 parents 3188ae5 + f718348 commit 6a9616e
Show file tree
Hide file tree
Showing 24 changed files with 1,582 additions and 894 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
}
],
"description": "Official Shopware 6 Plugin to connect to Payment Service Provider Adyen",
"version": "3.5.0",
"version": "3.6.0",
"type": "shopware-platform-plugin",
"license": "MIT",
"require": {
Expand Down
1,413 changes: 646 additions & 767 deletions composer.lock

Large diffs are not rendered by default.

129 changes: 128 additions & 1 deletion src/Controller/StoreApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,19 @@

namespace Adyen\Shopware\Controller;

use Adyen\AdyenException;
use Adyen\Service\Validator\CheckoutStateDataValidator;
use Adyen\Shopware\Exception\PaymentFailedException;
use Adyen\Shopware\Handlers\AbstractPaymentMethodHandler;
use Adyen\Shopware\Handlers\PaymentResponseHandler;
use Adyen\Shopware\Service\ConfigurationService;
use Adyen\Shopware\Service\DonationService;
use Adyen\Shopware\Service\PaymentDetailsService;
use Adyen\Shopware\Service\PaymentMethodsService;
use Adyen\Shopware\Service\PaymentResponseService;
use Adyen\Shopware\Service\PaymentStatusService;
use Adyen\Shopware\Service\Repository\OrderRepository;
use Adyen\Shopware\Service\Repository\OrderTransactionRepository;
use OpenApi\Annotations as OA;
use Psr\Log\LoggerInterface;
use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
Expand All @@ -41,6 +46,7 @@
use Shopware\Core\Checkout\Order\SalesChannel\OrderService;
use Shopware\Core\Checkout\Order\SalesChannel\SetPaymentOrderRouteResponse;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
Expand Down Expand Up @@ -90,6 +96,14 @@ class StoreApiController
* @var OrderService
*/
private $orderService;
/**
* @var EntityRepositoryInterface
*/
private $orderTransactionRepository;
/**
* @var OrderTransactionRepository
*/
private $adyenOrderTransactionRepository;
/**
* @var StateMachineRegistry
*/
Expand All @@ -98,12 +112,21 @@ class StoreApiController
* @var LoggerInterface
*/
private $logger;
/**
* @var DonationService
*/
private $donationService;
/**
* @var ConfigurationService
*/
private $configurationService;

/**
* StoreApiController constructor.
*
* @param PaymentMethodsService $paymentMethodsService
* @param PaymentDetailsService $paymentDetailsService
* @param DonationService $donationService
* @param CheckoutStateDataValidator $checkoutStateDataValidator
* @param PaymentStatusService $paymentStatusService
* @param PaymentResponseHandler $paymentResponseHandler
Expand All @@ -112,18 +135,24 @@ class StoreApiController
* @param OrderService $orderService
* @param StateMachineRegistry $stateMachineRegistry
* @param LoggerInterface $logger
* @param EntityRepositoryInterface $orderTransactionRepository
* @param ConfigurationService $configurationService
*/
public function __construct(
PaymentMethodsService $paymentMethodsService,
PaymentDetailsService $paymentDetailsService,
DonationService $donationService,
CheckoutStateDataValidator $checkoutStateDataValidator,
PaymentStatusService $paymentStatusService,
PaymentResponseHandler $paymentResponseHandler,
PaymentResponseService $paymentResponseService,
OrderRepository $orderRepository,
OrderService $orderService,
StateMachineRegistry $stateMachineRegistry,
LoggerInterface $logger
LoggerInterface $logger,
EntityRepositoryInterface $orderTransactionRepository,
ConfigurationService $configurationService,
OrderTransactionRepository $adyenOrderTransactionRepository
) {
$this->paymentMethodsService = $paymentMethodsService;
$this->paymentDetailsService = $paymentDetailsService;
Expand All @@ -135,6 +164,10 @@ public function __construct(
$this->orderService = $orderService;
$this->stateMachineRegistry = $stateMachineRegistry;
$this->logger = $logger;
$this->donationService = $donationService;
$this->orderTransactionRepository = $orderTransactionRepository;
$this->configurationService = $configurationService;
$this->adyenOrderTransactionRepository = $adyenOrderTransactionRepository;
}

/**
Expand Down Expand Up @@ -206,6 +239,33 @@ public function postPaymentDetails(
return new JsonResponse($message, 500);
}

// If donation token is present in the result, store it in the custom fields of order transaction.
$donationToken = $result->getDonationToken();
if (isset($donationToken) &&
$this->configurationService->isAdyenGivingEnabled($context->getSalesChannelId())) {
$storedTransactionCustomFields = $paymentResponse->getOrderTransaction()->getCustomFields() ?: [];
$transactionCustomFields[PaymentResponseHandler::DONATION_TOKEN] = $donationToken;

$customFields = array_merge(
$storedTransactionCustomFields,
$transactionCustomFields
);

$paymentResponse->getOrderTransaction()->setCustomFields($customFields);
$orderTransactionId = $paymentResponse->getOrderTransactionId();
$context->getContext()->scope(
Context::SYSTEM_SCOPE,
function (Context $context) use ($orderTransactionId, $customFields) {
$this->orderTransactionRepository->update([
[
'id' => $orderTransactionId,
'customFields' => $customFields,
]
], $context);
}
);
}

return new JsonResponse($this->paymentResponseHandler->handleAdyenApis($result));
}

Expand Down Expand Up @@ -354,4 +414,71 @@ public function cancelOrderTransaction(

return new JsonResponse($this->paymentStatusService->getWithOrderId($orderId));
}

/**
* @Route(
* "/store-api/adyen/donate",
* name="store-api.action.adyen.donate",
* methods={"POST"}
* )
*
* @param Request $request
* @param SalesChannelContext $salesChannelContext
* @return JsonResponse
*/
public function donate(
Request $request,
SalesChannelContext $salesChannelContext
): JsonResponse {
$payload = $request->get('payload');

$orderId = $payload['orderId'];
$currency = $payload['amount']['currency'];
$value = $payload['amount']['value'];
$returnUrl = $payload['returnUrl'];

$transaction = $this->adyenOrderTransactionRepository
->getFirstAdyenOrderTransactionByStates($orderId, [OrderTransactionStates::STATE_AUTHORIZED]);

/** @var AbstractPaymentMethodHandler $paymentMethodIdentifier */
$paymentMethodIdentifier = $transaction->getPaymentMethod()->getHandlerIdentifier();
$paymentMethodCode = $paymentMethodIdentifier::getPaymentMethodCode();

$donationToken = $transaction->getCustomFields()['donationToken'];
$pspReference = $transaction->getCustomFields()['originalPspReference'];

// Set donation token as null after first call.
$storedTransactionCustomFields = $transaction->getCustomFields();
$storedTransactionCustomFields[PaymentResponseHandler::DONATION_TOKEN] = null;

$orderTransactionId = $transaction->getId();
$salesChannelContext->getContext()->scope(
Context::SYSTEM_SCOPE,
function (Context $salesChannelContext) use ($orderTransactionId, $storedTransactionCustomFields) {
$this->orderTransactionRepository->update([
[
'id' => $orderTransactionId,
'customFields' => $storedTransactionCustomFields,
]
], $salesChannelContext);
}
);

try {
$this->donationService->donate(
$salesChannelContext,
$donationToken,
$currency,
$value,
$returnUrl,
$pspReference,
$paymentMethodCode
);
} catch (AdyenException $e) {
$this->logger->error($e->getMessage());
return new JsonResponse('An unknown error occurred', $e->getCode());
}

return new JsonResponse('Donation completed successfully.');
}
}
5 changes: 3 additions & 2 deletions src/Handlers/AbstractPaymentMethodHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use Adyen\Shopware\Storefront\Controller\RedirectResultController;
use Adyen\Util\Currency;
use Psr\Log\LoggerInterface;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStateHandler;
use Shopware\Core\Checkout\Payment\Cart\AsyncPaymentTransactionStruct;
use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\AsynchronousPaymentHandlerInterface;
Expand Down Expand Up @@ -564,8 +565,8 @@ protected function preparePaymentsRequest(
//Getting line price
$price = $orderLine->getPrice();

// Skip promotion line items.
if (empty($orderLine->getProductId()) && $orderLine->getType() === self::PROMOTION) {

if (empty($orderLine->getProductId()) || $orderLine->getType() !== LineItem::PRODUCT_LINE_ITEM_TYPE) {
continue;
}

Expand Down
32 changes: 30 additions & 2 deletions src/Handlers/PaymentResponseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Adyen\Shopware\Exception\PaymentCancelledException;
use Adyen\Shopware\Exception\PaymentFailedException;
use Adyen\Shopware\Service\CaptureService;
use Adyen\Shopware\Service\ConfigurationService;
use Psr\Log\LoggerInterface;
use Adyen\Shopware\Service\PaymentResponseService;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
Expand Down Expand Up @@ -56,7 +57,7 @@ class PaymentResponseHandler
const ORIGINAL_PSP_REFERENCE = 'originalPspReference';
const ADDITIONAL_DATA = 'additionalData';
const ACTION = 'action';

const DONATION_TOKEN = 'donationToken';

// Merchant reference parameter in return GET parameters list
const ADYEN_MERCHANT_REFERENCE = 'adyenMerchantReference';
Expand Down Expand Up @@ -88,25 +89,41 @@ class PaymentResponseHandler
*/
private $orderTransactionRepository;

/**
* @var ConfigurationService
*/
private $configurationService;

/**
* @var CaptureService
*/
private $captureService;

/**
* @param LoggerInterface $logger
* @param PaymentResponseService $paymentResponseService
* @param OrderTransactionStateHandler $transactionStateHandler
* @param PaymentResponseHandlerResult $paymentResponseHandlerResult
* @param EntityRepositoryInterface $orderTransactionRepository
* @param CaptureService $captureService
* @param ConfigurationService $configurationService
*/
public function __construct(
LoggerInterface $logger,
PaymentResponseService $paymentResponseService,
OrderTransactionStateHandler $transactionStateHandler,
PaymentResponseHandlerResult $paymentResponseHandlerResult,
EntityRepositoryInterface $orderTransactionRepository,
CaptureService $captureService
CaptureService $captureService,
ConfigurationService $configurationService
) {
$this->logger = $logger;
$this->paymentResponseService = $paymentResponseService;
$this->transactionStateHandler = $transactionStateHandler;
$this->paymentResponseHandlerResult = $paymentResponseHandlerResult;
$this->orderTransactionRepository = $orderTransactionRepository;
$this->captureService = $captureService;
$this->configurationService = $configurationService;
}

/**
Expand Down Expand Up @@ -145,6 +162,11 @@ public function handlePaymentResponse(
$this->paymentResponseHandlerResult->setAdditionalData($response[self::ADDITIONAL_DATA]);
}

// Set Donation Token if response contains it
if (isset($response[self::DONATION_TOKEN])) {
$this->paymentResponseHandlerResult->setDonationToken($response[self::DONATION_TOKEN]);
}

// Store response for cart until the payment is finalised
$this->paymentResponseService->insertPaymentResponse(
$response,
Expand Down Expand Up @@ -226,6 +248,12 @@ public function handleShopwareApis(
$transactionCustomFields[self::ORIGINAL_PSP_REFERENCE] = $pspReference;
}

$donationToken = $paymentResponseHandlerResult->getDonationToken();
if (isset($donationToken) &&
$this->configurationService->isAdyenGivingEnabled($salesChannelContext->getSalesChannelId())) {
$transactionCustomFields[self::DONATION_TOKEN] = $donationToken;
}

// Only store action for the transaction if this is the first action
$action = $this->paymentResponseHandlerResult->getAction();
if (empty($storedTransactionCustomFields[self::ACTION]) && !empty($action)) {
Expand Down
22 changes: 22 additions & 0 deletions src/Handlers/PaymentResponseHandlerResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class PaymentResponseHandlerResult
private $pspReference;
private $action;
private $additionalData;
private $donationToken;

/**
* @param PaymentResponseEntity $paymentResponse
Expand Down Expand Up @@ -49,6 +50,11 @@ public function createFromPaymentResponse(PaymentResponseEntity $paymentResponse
$this->setAction($response['action']);
}

// Set donation token if exists
if (!empty($response['donationToken'])) {
$this->setDonationToken($response['donationToken']);
}

// Set additional data if exists
if (!empty($response['additionalData'])) {
$this->setAdditionalData($response['additionalData']);
Expand Down Expand Up @@ -152,4 +158,20 @@ public function setAdditionalData($additionalData): void
{
$this->additionalData = $additionalData;
}

/**
* @param string $donationToken
*/
public function setDonationToken(string $donationToken): void
{
$this->donationToken = $donationToken;
}

/**
* @return null|string
*/
public function getDonationToken(): ?string
{
return $this->donationToken;
}
}

Large diffs are not rendered by default.

Loading

0 comments on commit 6a9616e

Please sign in to comment.