Skip to content
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
9 changes: 9 additions & 0 deletions Gateway/Response/CheckoutPaymentsDetailsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ public function handle(array $handlingSubject, array $responseCollection)
$payment->setCcTransId($response['pspReference']);
$payment->setLastTransId($response['pspReference']);

//set CC Type
$ccType = $payment->getAdditionalInformation('cc_type');

if (!empty($response['additionalData']['paymentMethod']) && $ccType == null) {
$ccType = $response['additionalData']['paymentMethod'];
$payment->setAdditionalInformation('cc_type', $ccType);
$payment->setCcType($ccType);
}

// set transaction
$payment->setTransactionId($response['pspReference']);
}
Expand Down
3 changes: 2 additions & 1 deletion Helper/PaymentMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class PaymentMethods extends AbstractHelper
const ADYEN_ONE_CLICK = 'adyen_oneclick';
const ADYEN_PAY_BY_LINK = 'adyen_pay_by_link';
const ADYEN_PREFIX = 'adyen_';
const ADYEN_CC_VAULT = 'adyen_cc_vault';
const METHODS_WITH_BRAND_LOGO = [
"giftcard"
];
Expand Down Expand Up @@ -958,7 +959,7 @@ public function compareOrderAndWebhookPaymentMethods(Order $order, Notification

// Returns if the payment method is wallet like wechatpayWeb, amazonpay, applepay, paywithgoogle
$isWalletPaymentMethod = $this->isWalletPaymentMethod($paymentMethodInstance);
$isCardPaymentMethod = $order->getPayment()->getMethod() === 'adyen_cc' || $order->getPayment()->getMethod() === 'adyen_oneclick';
$isCardPaymentMethod = $order->getPayment()->getMethod() === self::ADYEN_CC || $order->getPayment()->getMethod() === self::ADYEN_ONE_CLICK;

// If it is a wallet method OR a card OR the methods match exactly, return true
if ($isWalletPaymentMethod || $isCardPaymentMethod || strcmp($notificationPaymentMethod, $orderPaymentMethod) === 0) {
Expand Down
24 changes: 21 additions & 3 deletions Helper/PaymentResponseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Adyen\Payment\Helper;

use Adyen\Model\Checkout\CancelOrderRequest;
use Adyen\Payment\Helper\Config;
use Adyen\Payment\Logger\AdyenLogger;
use Adyen\Payment\Model\ResourceModel\PaymentResponse\CollectionFactory as PaymentResponseCollectionFactory;
use Exception;
Expand All @@ -24,7 +23,6 @@
use Magento\Sales\Model\OrderRepository;
use Magento\Sales\Model\ResourceModel\Order;
use Magento\Sales\Model\Order as OrderModel;
use Adyen\Payment\Helper\Data;
use Magento\Framework\Mail\Exception\InvalidArgumentException;
use Adyen\Client;

Expand Down Expand Up @@ -65,6 +63,7 @@ class PaymentResponseHandler
private StateData $stateDataHelper;
private PaymentResponseCollectionFactory $paymentResponseCollectionFactory;
private Config $configHelper;
private PaymentMethods $paymentMethodsHelper;

public function __construct(
AdyenLogger $adyenLogger,
Expand All @@ -77,7 +76,8 @@ public function __construct(
HistoryFactory $orderHistoryFactory,
StateData $stateDataHelper,
PaymentResponseCollectionFactory $paymentResponseCollectionFactory,
Config $configHelper
Config $configHelper,
PaymentMethods $paymentMethodsHelper
) {
$this->adyenLogger = $adyenLogger;
$this->vaultHelper = $vaultHelper;
Expand All @@ -90,6 +90,7 @@ public function __construct(
$this->stateDataHelper = $stateDataHelper;
$this->paymentResponseCollectionFactory = $paymentResponseCollectionFactory;
$this->configHelper = $configHelper;
$this->paymentMethodsHelper = $paymentMethodsHelper;
}

public function formatPaymentResponse(
Expand Down Expand Up @@ -159,6 +160,12 @@ public function handlePaymentsDetailsResponse(
$this->adyenLogger->addAdyenResult('Updating the order');
$payment = $order->getPayment();

//Check magento Payment Method
$paymentMethodInstance = $payment->getMethodInstance();
$isWalletPaymentMethod = $this->paymentMethodsHelper->isWalletPaymentMethod($paymentMethodInstance);
$isCardPaymentMethod = $payment->getMethod() === PaymentMethods::ADYEN_CC ||
$payment->getMethod() === PaymentMethods::ADYEN_CC_VAULT;

$authResult = $paymentsDetailsResponse['authResult'] ?? $paymentsDetailsResponse['resultCode'] ?? null;
if (is_null($authResult)) {
// In case the result is unknown we log the request and don't update the history
Expand Down Expand Up @@ -211,6 +218,17 @@ public function handlePaymentsDetailsResponse(
$payment->setAdditionalInformation('donationToken', $paymentsDetailsResponse['donationToken']);
}

$ccType = $payment->getAdditionalInformation('cc_type');

if (!empty($paymentsDetailsResponse['additionalData']['paymentMethod']) &&
is_null($ccType) &&
($isWalletPaymentMethod || $isCardPaymentMethod)
) {
$ccType = $paymentsDetailsResponse['additionalData']['paymentMethod'];
$payment->setAdditionalInformation('cc_type', $ccType);
$payment->setCcType($ccType);
}

// Handle recurring details
$this->vaultHelper->handlePaymentResponseRecurringDetails($payment, $paymentsDetailsResponse);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,15 @@ public function testIfPartialPaymentHandlesLastPaymentResponse()
]
],
1 => [
'additionalData' => [],
'additionalData' => [
'paymentMethod' => 'VI',
],
'amount' => [],
'resultCode' => 'Authorised',
'pspReference' => 'ABC12345',
'paymentMethod' => [
'name' => 'card',
'type' => 'CreditCard',
'type' => 'VI',
]
]
];
Expand Down Expand Up @@ -146,6 +148,22 @@ public function testIfPartialPaymentHandlesLastPaymentResponse()
->method('setTransactionId')
->with('ABC12345');

$this->paymentMock
->expects($this->once())
->method('getAdditionalInformation')
->with('cc_type')
->willReturn(null);

$this->paymentMock
->expects($this->once())
->method('setAdditionalInformation')
->with('cc_type', 'VI');

$this->paymentMock
->expects($this->once())
->method('setCcType')
->with('VI');

$this->applyGenericMockExpectations();

$this->checkoutPaymentsDetailsHandler->handle($this->handlingSubject, $responseCollection);
Expand Down
44 changes: 43 additions & 1 deletion Test/Unit/Helper/PaymentResponseHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Adyen\Payment\Helper\Data;
use Adyen\Payment\Helper\Quote;
use Adyen\Payment\Helper\Order as OrderHelper;
use Adyen\Payment\Model\Method\Adapter;
use Adyen\Payment\Test\Unit\AbstractAdyenTestCase;
use Exception;
use Magento\Framework\Exception\AlreadyExistsException;
Expand All @@ -31,6 +32,7 @@
use Adyen\Payment\Model\ResourceModel\PaymentResponse\Collection;
use Adyen\Payment\Model\ResourceModel\PaymentResponse\CollectionFactory;
use Adyen\Payment\Helper\Config;
use Adyen\Payment\Helper\PaymentMethods;
use ReflectionClass;

class PaymentResponseHandlerTest extends AbstractAdyenTestCase
Expand All @@ -51,6 +53,9 @@ class PaymentResponseHandlerTest extends AbstractAdyenTestCase
protected function setUp(): void
{
$this->paymentMock = $this->createMock(Payment::class);
// Mock the payment method
$this->paymentMethodInstanceMock = $this->createMock(Adapter::class);

$this->orderMock = $this->createMock(\Magento\Sales\Model\Order::class);
$this->adyenLoggerMock = $this->createMock(AdyenLogger::class);
$this->vaultHelperMock = $this->createMock(Vault::class);
Expand All @@ -64,6 +69,7 @@ protected function setUp(): void
]);
$this->stateDataHelperMock = $this->createMock(StateData::class);
$this->configHelperMock = $this->createMock(Config::class);
$this->paymentMethodsHelperMock = $this->createMock(PaymentMethods::class);

$this->paymentResponseMockForFactory = $this->createMock(Collection::class);

Expand All @@ -80,6 +86,7 @@ protected function setUp(): void
$this->orderMock->method('getPayment')->willReturn($this->paymentMock);
$this->orderMock->method('getStatus')->willReturn('pending');
$this->orderMock->method('getIncrementId')->willReturn('00123456');
$this->paymentMock->method('getMethodInstance')->willReturn($this->paymentMethodInstanceMock);

$this->orderHelperMock->method('setStatusOrderCreation')->willReturn($this->orderMock);

Expand All @@ -94,7 +101,8 @@ protected function setUp(): void
$this->orderHistoryFactoryMock,
$this->stateDataHelperMock,
$this->paymentResponseCollectionFactoryMock,
$this->configHelperMock
$this->configHelperMock,
$this->paymentMethodsHelperMock
);
}

Expand Down Expand Up @@ -597,4 +605,38 @@ public function testOrderStatusUpdateWhenResponseIsValid()

$this->paymentResponseHandler->handlePaymentsDetailsResponse($paymentsDetailsResponse, $this->orderMock);
}

public function testHandlePaymentsDetailsResponseSetsCcType()
{

// Mock the method `isWalletPaymentMethod` in your helper if it's being checked
$this->paymentMethodsHelperMock->method('isWalletPaymentMethod')
->with($this->paymentMethodInstanceMock)
->willReturn(false); // Assuming false for this test case

// Payment details response with a payment method brand
$paymentsDetailsResponse = [
'resultCode' => PaymentResponseHandler::AUTHORISED,
'paymentMethod' => [
'brand' => 'VI'
],
'merchantReference' => '00123456'
];

// Expect the `setCcType` method to be called on the payment object with the correct value
$this->paymentMock
->method('setCcType')
->with($this->equalTo('VI'));

// Call the method under test
$result = $this->paymentResponseHandler->handlePaymentsDetailsResponse(
$paymentsDetailsResponse,
$this->orderMock
);

// Assert the response is as expected
$this->assertTrue($result);
}


}
Loading