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

Adding channel parameter to /paymentMethods API call #2798

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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: 7 additions & 1 deletion Api/AdyenPaymentMethodManagementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ interface AdyenPaymentMethodManagementInterface
* @param string $cartId
* @param string|null $shopperLocale
* @param string|null $country
* @param string|null $channel
* @return string
*/
public function getPaymentMethods(string $cartId, ?string $shopperLocale = null, ?string $country = null) :string;
public function getPaymentMethods(
string $cartId,
?string $shopperLocale = null,
?string $country = null,
?string $channel = null
) :string;
}
8 changes: 7 additions & 1 deletion Api/GuestAdyenPaymentMethodManagementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ interface GuestAdyenPaymentMethodManagementInterface
* @param string $cartId
* @param string|null $shopperLocale
* @param string|null $country
* @param string|null $channel
* @return string
*/
public function getPaymentMethods(string $cartId, ?string $shopperLocale = null, ?string $country = null): string;
public function getPaymentMethods(
string $cartId,
?string $shopperLocale = null,
?string $country = null,
?string $channel = null
): string;
}
38 changes: 31 additions & 7 deletions Helper/PaymentMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class PaymentMethods extends AbstractHelper

const ADYEN_GROUP_ALTERNATIVE_PAYMENT_METHODS = 'adyen-alternative-payment-method';

const VALID_CHANNELS = ["iOS", "Android", "Web"];

/*
* Following payment methods should be enabled with their own configuration path.
*/
Expand Down Expand Up @@ -229,12 +231,18 @@ public function __construct(
* @param int $quoteId
* @param string|null $country
* @param string|null $shopperLocale
* @param string|null $channel
* @return string
* @throws AdyenException
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function getPaymentMethods(int $quoteId, ?string $country = null, ?string $shopperLocale = null): string
public function getPaymentMethods(
int $quoteId,
?string $country = null,
?string $shopperLocale = null,
?string $channel = null
): string
{
// get quote from quoteId
$quote = $this->quoteRepository->getActive($quoteId);
Expand All @@ -245,7 +253,7 @@ public function getPaymentMethods(int $quoteId, ?string $country = null, ?string

$this->setQuote($quote);

return $this->fetchPaymentMethods($country, $shopperLocale);
return $this->fetchPaymentMethods($country, $shopperLocale, $channel);
}

/**
Expand Down Expand Up @@ -327,12 +335,17 @@ public function removePaymentMethodsActivation(string $scope, int $scopeId): voi
/**
* @param string|null $country
* @param string|null $shopperLocale
* @param string|null $channel
* @return string
* @throws AdyenException
* @throws LocalizedException
* @throws NoSuchEntityException
*/
protected function fetchPaymentMethods(?string $country = null, ?string $shopperLocale = null): string
protected function fetchPaymentMethods(
?string $country = null,
?string $shopperLocale = null,
?string $channel = null
): string
{
$quote = $this->getQuote();
$store = $quote->getStore();
Expand All @@ -342,7 +355,14 @@ protected function fetchPaymentMethods(?string $country = null, ?string $shopper
return json_encode([]);
}

$requestData = $this->getPaymentMethodsRequest($merchantAccount, $store, $quote, $shopperLocale, $country);
$requestData = $this->getPaymentMethodsRequest(
$merchantAccount,
$store,
$quote,
$shopperLocale,
$country,
$channel
);
$responseData = $this->getPaymentMethodsResponse($requestData, $store);
if (empty($responseData['paymentMethods'])) {
return json_encode([]);
Expand Down Expand Up @@ -526,6 +546,7 @@ protected function getCurrentShopperReference(): ?string
* @param Quote $quote
* @param string|null $shopperLocale
* @param string|null $country
* @param string|null $channel
* @return array
* @throws AdyenException
*/
Expand All @@ -534,15 +555,18 @@ protected function getPaymentMethodsRequest(
Store $store,
Quote $quote,
?string $shopperLocale = null,
?string $country = null
?string $country = null,
?string $channel = null
): array {
$currencyCode = $this->chargedCurrency->getQuoteAmountCurrency($quote)->getCurrencyCode();

$channel = in_array($channel, self::VALID_CHANNELS, true) ? $channel : "Web";

$paymentMethodRequest = [
"channel" => "Web",
"channel" => $channel ?? "Web",
"merchantAccount" => $merchantAccount,
"countryCode" => $country ?? $this->getCurrentCountryCode($store),
"shopperLocale" => $shopperLocale ?: $this->adyenHelper->getCurrentLocaleCode($store->getId()),
"shopperLocale" => $shopperLocale ?? $this->adyenHelper->getCurrentLocaleCode($store->getId()),
"amount" => [
"currency" => $currencyCode
]
Expand Down
11 changes: 9 additions & 2 deletions Helper/PaymentMethodsFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Adyen\Payment\Model\Ui\AdyenPosCloudConfigProvider;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Framework\App\RequestInterface;

class PaymentMethodsFilter
{
Expand All @@ -31,18 +32,24 @@ class PaymentMethodsFilter
];

private PaymentMethods $paymentMethods;
private RequestInterface $request;

public function __construct(
PaymentMethods $paymentMethods
PaymentMethods $paymentMethods,
RequestInterface $request
) {
$this->paymentMethods = $paymentMethods;
$this->request = $request;
}

public function sortAndFilterPaymentMethods(array $magentoPaymentMethods, CartInterface $quote): array
{
$channel = $this->request->getParam('channel');
$adyenPaymentMethodsResponse = $this->paymentMethods->getPaymentMethods(
$quote->getId(),
$quote->getBillingAddress()->getCountryId()
$quote->getBillingAddress()->getCountryId(),
null,
$channel
);

$adyenPaymentMethodsDecoded = json_decode($adyenPaymentMethodsResponse, true);
Expand Down
9 changes: 7 additions & 2 deletions Model/Api/AdyenPaymentMethodManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ public function __construct(
$this->paymentMethodsHelper = $paymentMethodsHelper;
}

public function getPaymentMethods(string $cartId, ?string $shopperLocale = null, ?string $country = null) : string
public function getPaymentMethods(
string $cartId,
?string $shopperLocale = null,
?string $country = null,
?string $channel = null
) : string
{
return $this->paymentMethodsHelper->getPaymentMethods($cartId, $country, $shopperLocale);
return $this->paymentMethodsHelper->getPaymentMethods($cartId, $country, $shopperLocale, $channel);
}
}
9 changes: 7 additions & 2 deletions Model/Api/GuestAdyenPaymentMethodManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ public function __construct(
$this->paymentMethodsHelper = $paymentMethodsHelper;
}

public function getPaymentMethods(string $cartId, ?string $shopperLocale = null, ?string $country = null): string {
public function getPaymentMethods(
string $cartId,
?string $shopperLocale = null,
?string $country = null,
?string $channel = null
): string {
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
$quoteId = $quoteIdMask->getQuoteId();

return $this->paymentMethodsHelper->getPaymentMethods($quoteId, $country, $shopperLocale);
return $this->paymentMethodsHelper->getPaymentMethods($quoteId, $country, $shopperLocale, $channel);
}
}
4 changes: 3 additions & 1 deletion Model/Resolver/GetAdyenPaymentMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function resolve(
$maskedCartId = $args['cart_id'];
$shopperLocale = $args['shopper_locale'] ?? null;
$country = $args['country'] ?? null;
$channel = $args['channel'] ?? null;

$currentUserId = $context->getUserId();
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
Expand All @@ -68,7 +69,8 @@ public function resolve(
$adyenPaymentMethodsResponse = $this->paymentMethodsHelper->getPaymentMethods(
intval($cart->getId()),
$country,
$shopperLocale
$shopperLocale,
$channel
);

return $adyenPaymentMethodsResponse ? $this->preparePaymentMethodGraphQlResponse($adyenPaymentMethodsResponse) : [];
Expand Down
20 changes: 12 additions & 8 deletions Test/Unit/Helper/PaymentMethodsFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Sales\Model\Order\Address;
use Magento\Framework\App\RequestInterface;

class PaymentMethodsFilterTest extends AbstractAdyenTestCase
{
Expand Down Expand Up @@ -175,17 +176,20 @@ public function testSortAndFilterPaymentMethods(): void
])
]);

$cartRepositoryInterfaceMock = $this->createConfiguredMock(CartRepositoryInterface::class, [
'get' => $quoteMock
]);
$channel = 'iOS';

$paymentMethodsHelperMock = $this->createConfiguredMock(PaymentMethods::class, [
'getPaymentMethods' => self::PAYMENT_METHODS_RESPONSE
]);

$RequestInterfaceMock = $this->getMockBuilder(RequestInterface::class)
->getMockForAbstractClass();

$RequestInterfaceMock->method('getParam')->with('channel')->willReturn($channel);

$paymentMethodsFilterHelper = $this->createPaymentMethodsFilterHelper(
$paymentMethodsHelperMock,
$cartRepositoryInterfaceMock
$RequestInterfaceMock
);

$sortedMagentoPaymentMethods =
Expand All @@ -201,16 +205,16 @@ public function testSortAndFilterPaymentMethods(): void

protected function createPaymentMethodsFilterHelper(
$paymentMethodsHelperMock = null,
$cartRepositoryInterfaceMock = null
$RequestInterfaceMock = null
): PaymentMethodsFilter {
if (is_null($paymentMethodsHelperMock)) {
$paymentMethodsHelperMock = $this->createMock(PaymentMethods::class);
}

if (is_null($cartRepositoryInterfaceMock)) {
$cartRepositoryInterfaceMock = $this->createMock(CartRepositoryInterface::class);
if (is_null($RequestInterfaceMock)) {
$RequestInterfaceMock = $this->createMock(RequestInterface::class);
}

return new PaymentMethodsFilter($paymentMethodsHelperMock, $cartRepositoryInterfaceMock);
return new PaymentMethodsFilter($paymentMethodsHelperMock, $RequestInterfaceMock);
}
}
48 changes: 48 additions & 0 deletions Test/Unit/Model/Api/AdyenPaymentMethodManagementTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php


namespace Adyen\Payment\Test\Unit\Model\Api;

use Adyen\Payment\Helper\PaymentMethods;
use Adyen\Payment\Model\Api\AdyenPaymentMethodManagement;
use Adyen\Payment\Test\Unit\AbstractAdyenTestCase;
use PHPUnit\Framework\MockObject\MockObject;

class AdyenPaymentMethodManagementTest extends AbstractAdyenTestCase
{
/** @var PaymentMethods|MockObject */
private $paymentMethodsHelperMock;

/** @var AdyenPaymentMethodManagement */
private $adyenPaymentMethodManagement;

protected function setUp(): void
{
$this->paymentMethodsHelperMock = $this->createMock(PaymentMethods::class);

$this->adyenPaymentMethodManagement = new AdyenPaymentMethodManagement(
$this->paymentMethodsHelperMock
);
}

public function testGetPaymentMethods(): void
{
$cartId = '12345';
$shopperLocale = 'en_US';
$country = 'US';
$channel = 'web';

// Define the expected result
$expectedResult = '["payment_method_1", "payment_method_2"]';

$this->paymentMethodsHelperMock->expects($this->once())
->method('getPaymentMethods')
->with($cartId, $country, $shopperLocale, $channel)
->willReturn($expectedResult);

$result = $this->adyenPaymentMethodManagement->getPaymentMethods($cartId, $shopperLocale, $country, $channel);

// Assert that the result matches the expected result
$this->assertEquals($expectedResult, $result);
}
}
56 changes: 56 additions & 0 deletions Test/Unit/Model/Api/GuestAdyenPaymentMethodManagementTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Adyen\Payment\Test\Unit\Model\Api;

use Adyen\Payment\Model\Api\GuestAdyenPaymentMethodManagement;
use Adyen\Payment\Helper\PaymentMethods;
use Adyen\Payment\Test\Unit\AbstractAdyenTestCase;
use Magento\Quote\Model\QuoteIdMask;
use Magento\Quote\Model\QuoteIdMaskFactory;

class GuestAdyenPaymentMethodManagementTest extends AbstractAdyenTestCase
{
/** @var QuoteIdMaskFactory|\PHPUnit\Framework\MockObject\MockObject */
private $quoteIdMaskFactoryMock;

/** @var PaymentMethods|\PHPUnit\Framework\MockObject\MockObject */
private $paymentMethodsHelperMock;

/** @var GuestAdyenPaymentMethodManagement */
private $guestAdyenPaymentMethodManagement;

protected function setUp(): void
{
$this->quoteIdMaskFactoryMock = $this->createGeneratedMock(QuoteIdMaskFactory::class, ['create']);
$this->paymentMethodsHelperMock = $this->createMock(PaymentMethods::class);

$this->guestAdyenPaymentMethodManagement = new GuestAdyenPaymentMethodManagement(
$this->quoteIdMaskFactoryMock,
$this->paymentMethodsHelperMock
);
}

public function testGetPaymentMethods()
{
$cartId = '123';
$quoteId = 123;
$shopperLocale = 'en_US';
$country = 'US';
$channel = 'Web';
$expectedPaymentMethods = 'sample_payment_methods';

$quoteIdMaskMock = $this->createGeneratedMock(QuoteIdMask::class, ['load', 'getQuoteId']);
$quoteIdMaskMock->method('load')->willReturn($quoteIdMaskMock);
$quoteIdMaskMock->method('getQuoteId')->willReturn($quoteId);
$this->quoteIdMaskFactoryMock->method('create')->willReturn($quoteIdMaskMock);

$this->paymentMethodsHelperMock->expects($this->once())
->method('getPaymentMethods')
->with($quoteId, $country, $shopperLocale, $channel)
->willReturn($expectedPaymentMethods);

$result = $this->guestAdyenPaymentMethodManagement->getPaymentMethods($cartId, $shopperLocale, $country, $channel);

$this->assertEquals($expectedPaymentMethods, $result);
}
}
Loading
Loading