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

[ECP-8972] GraphQL implementation of new giftcard flow #2559

Merged
merged 22 commits into from
Mar 22, 2024
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
8 changes: 5 additions & 3 deletions Api/AdyenStateDataInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
interface AdyenStateDataInterface
{
/**
* Persist the Adyen state data for the quote so it can be used in the payment request
* Persist the Adyen state data for the quote and returns the stateDataId.
* So it can be used in the payment request.
*
*
* @param string $stateData
* @param int $cartId
* @return void
* @return int
*/
public function save(string $stateData, int $cartId): void;
public function save(string $stateData, int $cartId): int;

/**
* Removes the Adyen state data with the given entity id
Expand Down
7 changes: 4 additions & 3 deletions Api/GuestAdyenStateDataInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
interface GuestAdyenStateDataInterface
{
/**
* Persist the Adyen state data for the quote so it can be used in the payment request
* Persist the Adyen state data for the quote and returns the stateDataId.
* So it can be used in the payment request.
*
* @param string $stateData
* @param string $cartId
* @return void
* @return int
*/
public function save(string $stateData, string $cartId): void;
public function save(string $stateData, string $cartId): int;

/**
* Removes the Adyen state data with the given entity id
Expand Down
14 changes: 12 additions & 2 deletions Helper/StateData.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
use Adyen\Payment\Logger\AdyenLogger;
use Adyen\Payment\Model\ResourceModel\StateData as StateDataResourceModel;
use Adyen\Payment\Model\ResourceModel\StateData\Collection as StateDataCollection;
use Adyen\Payment\Model\StateData as AdyenStateData;
use Adyen\Payment\Model\StateDataFactory;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;

Expand Down Expand Up @@ -80,7 +82,14 @@ public function getPaymentMethodVariant(int $quoteId): string
return $stateDataByQuoteId['paymentMethod']['type'];
}

public function saveStateData(string $stateData, int $quoteId): void
/**
* @param string $stateData
* @param int $quoteId
* @return AdyenStateData
* @throws LocalizedException
* @throws AlreadyExistsException
*/
public function saveStateData(string $stateData, int $quoteId): AdyenStateData
{
// Decode payload from frontend
$stateData = json_decode($stateData, true);
Expand All @@ -92,10 +101,11 @@ public function saveStateData(string $stateData, int $quoteId): void

$stateData = json_encode($this->checkoutStateDataValidator->getValidatedAdditionalData($stateData));

/** @var \Adyen\Payment\Model\StateData $stateDataObj */
$stateDataObj = $this->stateDataFactory->create();
$stateDataObj->setQuoteId($quoteId)->setStateData((string)$stateData);
$this->stateDataResourceModel->save($stateDataObj);

return $stateDataObj;
}

/**
Expand Down
9 changes: 5 additions & 4 deletions Model/Api/AdyenStateData.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ public function __construct(
$this->stateDataHelper = $stateDataHelper;
}

public function save(string $stateData, int $quoteId): void
public function save(string $stateData, int $cartId): int
{
$this->stateDataHelper->saveStateData($stateData, $quoteId);
$stateData = $this->stateDataHelper->saveStateData($stateData, $cartId);
return $stateData->getEntityId();
}

public function remove(int $stateDataId, int $quoteId): bool
public function remove(int $stateDataId, int $cartId): bool
{
return $this->stateDataHelper->removeStateData($stateDataId, $quoteId);
return $this->stateDataHelper->removeStateData($stateDataId, $cartId);
}
}
10 changes: 8 additions & 2 deletions Model/Api/GuestAdyenStateData.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use Adyen\Payment\Api\GuestAdyenStateDataInterface;
use Adyen\Payment\Helper\StateData as StateDataHelper;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
Expand All @@ -33,14 +34,19 @@ public function __construct(
}

/**
* @param string $stateData
* @param string $cartId
* @return int
* @throws InputException
* @throws LocalizedException
* @throws AlreadyExistsException
*/
public function save(string $stateData, string $cartId): void
public function save(string $stateData, string $cartId): int
{
$quoteId = $this->getQuoteIdFromMaskedCartId($cartId);
$stateData = $this->stateDataHelper->saveStateData($stateData, $quoteId);

$this->stateDataHelper->saveStateData($stateData, $quoteId);
return $stateData->getEntityId();
}

/**
Expand Down
76 changes: 76 additions & 0 deletions Model/Resolver/GetAdyenPaymentMethodsBalance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
*
* Adyen Payment Module
*
* Copyright (c) 2024 Adyen N.V.
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*
* Author: Adyen <magento@adyen.com>
*/
declare(strict_types=1);

namespace Adyen\Payment\Model\Resolver;

use Adyen\Payment\Exception\GraphQlAdyenException;
use Adyen\Payment\Model\Api\AdyenPaymentMethodsBalance;
use Exception;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

class GetAdyenPaymentMethodsBalance implements ResolverInterface
{
/**
* @var AdyenPaymentMethodsBalance
*/
private AdyenPaymentMethodsBalance $balance;

/**
* @param AdyenPaymentMethodsBalance $balance
*/
public function __construct(
AdyenPaymentMethodsBalance $balance
) {
$this->balance = $balance;
}

/**
* @param Field $field
* @param $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
* @return array
* @throws GraphQlAdyenException
* @throws GraphQlInputException
* @throws Exception
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
): array {
if (empty($args['payload'])) {
throw new GraphQlInputException(__('Required parameter "payload" is missing'));
}

$payload = $args['payload'];
try {
$balanceResponse = $this->balance->getBalance($payload);
} catch (Exception $e) {
throw new GraphQlAdyenException(
__('An error occurred while fetching the payment method balance.'),
$e
);
}

return ['balanceResponse' => $balanceResponse];
}
}


99 changes: 99 additions & 0 deletions Model/Resolver/GetAdyenRedeemedGiftcards.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/**
*
* Adyen Payment Module
*
* Copyright (c) 2024 Adyen N.V.
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*
* Author: Adyen <magento@adyen.com>
*/
declare(strict_types=1);

namespace Adyen\Payment\Model\Resolver;

use Adyen\Payment\Exception\GraphQlAdyenException;
use Adyen\Payment\Helper\GiftcardPayment;
use Magento\Quote\Model\QuoteIdMaskFactory;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\Serialize\Serializer\Json;

class GetAdyenRedeemedGiftcards implements ResolverInterface
{
/**
* @var GiftcardPayment
*/
private GiftcardPayment $giftcardPayment;

/**
* @var Json
*/
private Json $jsonSerializer;

/**
* @var QuoteIdMaskFactory
*/
private QuoteIdMaskFactory $quoteIdMaskFactory;

/**
* @param GiftcardPayment $giftcardPayment
* @param Json $jsonSerializer
* @param QuoteIdMaskFactory $quoteIdMaskFactory
*/
public function __construct(
GiftcardPayment $giftcardPayment,
Json $jsonSerializer,
QuoteIdMaskFactory $quoteIdMaskFactory
) {
$this->giftcardPayment = $giftcardPayment;
$this->jsonSerializer = $jsonSerializer;
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
}

/**
* @param Field $field
* @param $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
* @return array
* @throws GraphQlAdyenException
* @throws GraphQlInputException
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
if (empty($args['cartId'])) {
throw new GraphQlInputException(__('Required parameter "cartId" is missing'));
}

$cartId = $args['cartId'];
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
$quoteId = $quoteIdMask->getQuoteId();
$quoteId = (int)$quoteId;

try {
$redeemedGiftcardsJson = $this->giftcardPayment->fetchRedeemedGiftcards($quoteId);
} catch (\Exception $e) {
throw new GraphQlAdyenException(
__('An error occurred while fetching redeemed gift cards: %1', $e->getMessage())
);
}

$redeemedGiftcardsData = $this->jsonSerializer->unserialize($redeemedGiftcardsJson);

return [
'redeemedGiftcards' => $redeemedGiftcardsData['redeemedGiftcards'],
'remainingAmount' => $redeemedGiftcardsData['remainingAmount'],
'totalDiscount' => $redeemedGiftcardsData['totalDiscount']
];
}
}
93 changes: 93 additions & 0 deletions Model/Resolver/RemoveAdyenStateData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
*
* Adyen Payment Module
*
* Copyright (c) 2024 Adyen B.V.
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*
* Author: Adyen <magento@adyen.com>
*/
declare(strict_types=1);

namespace Adyen\Payment\Model\Resolver;

use Adyen\Payment\Exception\GraphQlAdyenException;
use Adyen\Payment\Model\Api\AdyenStateData;
use Exception;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Model\QuoteIdMaskFactory;

class RemoveAdyenStateData implements ResolverInterface
{
/**
* @var AdyenStateData
*/
private AdyenStateData $adyenStateData;

/**
* @var QuoteIdMaskFactory
*/
private QuoteIdMaskFactory $quoteIdMaskFactory;

/**
* @param AdyenStateData $adyenStateData
* @param QuoteIdMaskFactory $quoteIdMaskFactory
*/
public function __construct(
AdyenStateData $adyenStateData,
QuoteIdMaskFactory $quoteIdMaskFactory
) {
$this->adyenStateData = $adyenStateData;
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
}

/**
* @param Field $field
* @param $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
* @return array
* @throws GraphQlAdyenException
* @throws GraphQlInputException
* @throws LocalizedException
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
): array {
if (empty($args['stateDataId'])) {
throw new GraphQlInputException(__('Required parameter "stateDataId" is missing'));
}

if (empty($args['cartId'])) {
throw new GraphQlInputException(__('Required parameter "cartId" is missing'));
}

$quoteIdMask = $this->quoteIdMaskFactory->create()->load($args['cartId'], 'masked_id');
$quoteId = $quoteIdMask->getQuoteId();

try {
$result = $this->adyenStateData->remove((int) $args['stateDataId'], (int) $quoteId);
} catch (Exception $e) {
throw new GraphQlAdyenException(__('An error occurred while removing the state data.'), $e);
}

if (!$result) {
throw new LocalizedException(__('An error occurred while removing the state data.'));
}

return ['stateDataId' => $args['stateDataId']];
}
}


Loading
Loading