diff --git a/Gateway/Response/CheckoutPaymentsDetailsHandler.php b/Gateway/Response/CheckoutPaymentsDetailsHandler.php index d9183c6115..1731b7db15 100644 --- a/Gateway/Response/CheckoutPaymentsDetailsHandler.php +++ b/Gateway/Response/CheckoutPaymentsDetailsHandler.php @@ -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']); } diff --git a/Helper/PaymentMethods.php b/Helper/PaymentMethods.php index f8972d18c7..ed057ca4e6 100644 --- a/Helper/PaymentMethods.php +++ b/Helper/PaymentMethods.php @@ -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" ]; @@ -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) { diff --git a/Helper/PaymentResponseHandler.php b/Helper/PaymentResponseHandler.php index ca1df99836..085224bafb 100644 --- a/Helper/PaymentResponseHandler.php +++ b/Helper/PaymentResponseHandler.php @@ -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; @@ -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; @@ -65,6 +63,7 @@ class PaymentResponseHandler private StateData $stateDataHelper; private PaymentResponseCollectionFactory $paymentResponseCollectionFactory; private Config $configHelper; + private PaymentMethods $paymentMethodsHelper; public function __construct( AdyenLogger $adyenLogger, @@ -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; @@ -90,6 +90,7 @@ public function __construct( $this->stateDataHelper = $stateDataHelper; $this->paymentResponseCollectionFactory = $paymentResponseCollectionFactory; $this->configHelper = $configHelper; + $this->paymentMethodsHelper = $paymentMethodsHelper; } public function formatPaymentResponse( @@ -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 @@ -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); diff --git a/Test/Unit/Gateway/Response/CheckoutPaymentsDetailsHandlerTest.php b/Test/Unit/Gateway/Response/CheckoutPaymentsDetailsHandlerTest.php index fa7143dea3..aa48177709 100644 --- a/Test/Unit/Gateway/Response/CheckoutPaymentsDetailsHandlerTest.php +++ b/Test/Unit/Gateway/Response/CheckoutPaymentsDetailsHandlerTest.php @@ -109,13 +109,15 @@ public function testIfPartialPaymentHandlesLastPaymentResponse() ] ], 1 => [ - 'additionalData' => [], + 'additionalData' => [ + 'paymentMethod' => 'VI', + ], 'amount' => [], 'resultCode' => 'Authorised', 'pspReference' => 'ABC12345', 'paymentMethod' => [ 'name' => 'card', - 'type' => 'CreditCard', + 'type' => 'VI', ] ] ]; @@ -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); diff --git a/Test/Unit/Helper/PaymentResponseHandlerTest.php b/Test/Unit/Helper/PaymentResponseHandlerTest.php index 0bff0bbf23..24680b46bf 100644 --- a/Test/Unit/Helper/PaymentResponseHandlerTest.php +++ b/Test/Unit/Helper/PaymentResponseHandlerTest.php @@ -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; @@ -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 @@ -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); @@ -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); @@ -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); @@ -94,7 +101,8 @@ protected function setUp(): void $this->orderHistoryFactoryMock, $this->stateDataHelperMock, $this->paymentResponseCollectionFactoryMock, - $this->configHelperMock + $this->configHelperMock, + $this->paymentMethodsHelperMock ); } @@ -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); + } + + }