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

Express e2e failcheck #2725

Closed
wants to merge 6 commits into from
Closed
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
4 changes: 2 additions & 2 deletions .github/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ flush:
enable-express:
bin/magento module:enable Adyen_ExpressCheckout
bin/magento setup:upgrade
bin/magento config:set payment/adyen_express/show_google_pay_on "1,2,3"
bin/magento config:set payment/adyen_express/show_apple_pay_on "1,2,3"
# bin/magento config:set payment/adyen_express/show_google_pay_on "1,2,3"
# bin/magento config:set payment/adyen_express/show_apple_pay_on "1,2,3"
bin/magento cache:clean

# Full plugin setup
Expand Down
52 changes: 40 additions & 12 deletions Gateway/Http/Client/TransactionPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,28 @@ public function placeRequest(TransferInterface $transferObject): array
$responseData = [];

try {
list($requestData, $giftcardResponse) = $this->processGiftcards($requestData, $service);

/** @var PaymentResponse $giftcardResponse */
if (isset($giftcardResponse) && $this->remainingOrderAmount === 0) {
return $giftcardResponse->toArray();
$failedGiftcardResponse = null;
list($requestData, $giftcardResponseCollection) = $this->processGiftcards($requestData, $service);

/** @var PaymentResponse[] $giftcardResponseCollection */
if (!empty($giftcardResponseCollection)) {
// check if any giftcard was refused
foreach ($giftcardResponseCollection as $giftcardResponse) {
if ($giftcardResponse->getResultCode() == "Refused"){
$failedGiftcardResponse = $giftcardResponse;
break;
}
}

if ($this->remainingOrderAmount === 0) {
// if any of the giftcard was refused, return that one in response, so that the payment fails
if (!empty($failedGiftcardResponse)) {
return $failedGiftcardResponse->toArray();
}

// if none of the giftcard payments are failed, return the first successful one
return $giftcardResponseCollection[0]->toArray();
}
}

$requestData['applicationInfo'] = $this->adyenHelper->buildApplicationInfo($client);
Expand All @@ -150,7 +167,13 @@ public function placeRequest(TransferInterface $transferObject): array
$paymentResponse->setResultCode($response->getResultCode());
$paymentResponse->setMerchantReference($requestData["reference"]);
$this->paymentResponseResourceModel->save($paymentResponse);
$responseData = $response->toArray();

// if there is any failed giftcard payment use that as a response so that the payment fails
if (!empty($failedGiftcardResponse)) {
$responseData = $failedGiftcardResponse->toArray();
} else {
$responseData = $response->toArray();
}

$this->adyenHelper->logResponse($responseData);
} catch (AdyenException $e) {
Expand All @@ -165,7 +188,7 @@ public function placeRequest(TransferInterface $transferObject): array
* @param PaymentsApi $service
* @param array $redeemedGiftcards
* @param array $ordersResponse
* @return CheckoutPaymentResponse
* @return CheckoutPaymentResponse[]
* @throws AdyenException
* @throws AlreadyExistsException
*/
Expand All @@ -174,7 +197,10 @@ private function handleGiftcardPayments(
PaymentsApi $service,
array $redeemedGiftcards,
array $ordersResponse
): CheckoutPaymentResponse {
): array {

$giftCardResponseCollection = [];

foreach ($redeemedGiftcards as $giftcard) {
$stateData = json_decode($giftcard['state_data'], true);

Expand Down Expand Up @@ -204,6 +230,8 @@ private function handleGiftcardPayments(
$response = $service->payments(new PaymentRequest($giftcardPaymentRequest));
$this->adyenHelper->logResponse($response->toArray());

$giftCardResponseCollection[] = $response;

/** @var PaymentResponse $paymentResponse */
$paymentResponse = $this->paymentResponseFactory->create();
$paymentResponse->setResponse(json_encode($response));
Expand All @@ -215,7 +243,7 @@ private function handleGiftcardPayments(
$this->remainingOrderAmount -= $deductedAmount;
}

return $response;
return $giftCardResponseCollection;
}

/**
Expand All @@ -239,17 +267,17 @@ public function processGiftcards(array $request, PaymentsApi $service): array
$this->storeManager->getStore()->getId()
);

$giftcardResponse = $this->handleGiftcardPayments($request, $service, $redeemedGiftcards, $ordersResponse);
$giftcardResponseCollection = $this->handleGiftcardPayments($request, $service, $redeemedGiftcards, $ordersResponse);

$request['amount']['value'] = $this->remainingOrderAmount;
$request['order'] = [
'pspReference' => $ordersResponse['pspReference'],
'orderData' => $ordersResponse['orderData']
];
} else {
$giftcardResponse = null;
$giftcardResponseCollection = [];
}

return array($request, $giftcardResponse);
return array($request, $giftcardResponseCollection);
}
}
67 changes: 47 additions & 20 deletions Test/Unit/Gateway/Http/Client/TransactionPaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Adyen\Model\Checkout\ApplicationInfo;
use Adyen\Model\Checkout\PaymentRequest;
use Adyen\Model\Checkout\PaymentResponse as CheckoutPaymentResponse;
use Adyen\Payment\Api\Data\PaymentResponseInterface;
use Adyen\Payment\Model\PaymentResponse;
use Adyen\Payment\Test\Unit\AbstractAdyenTestCase;
Expand Down Expand Up @@ -104,7 +105,7 @@ public function testPlaceRequestGeneratesIdempotencyKey()
)
->willReturn($expectedIdempotencyKey);

$paymentResponse = new \Adyen\Model\Checkout\PaymentResponse([
$paymentResponse = new CheckoutPaymentResponse([
'reference' => 'ABC12345',
'amount' => ['value' => 100],
'resultCode' => 'Authorised'
Expand Down Expand Up @@ -146,7 +147,7 @@ public function testRequestHeadersAreAddedToPaymentsCall()

$actualHeaders = $this->adyenHelperMock->buildRequestHeaders();

$paymentResponse = new \Adyen\Model\Checkout\PaymentResponse([
$paymentResponse = new CheckoutPaymentResponse([
'reference' => 'ABC12345',
'amount' => ['value' => 100],
'resultCode' => 'Authorised'
Expand Down Expand Up @@ -179,10 +180,50 @@ public function testProcessGiftCardsWithNoGiftCards()
list($request, $giftcardResponse) = $this->transactionPayment->processGiftcards($originalRequest, $service);

$this->assertEquals($request, $originalRequest);
$this->assertNull($giftcardResponse);
$this->assertEmpty($giftcardResponse);
}

public function testProcessGiftCardsWithGiftCards()
{
$orderData = [
'pspReference' => 'pspReference!23',
'orderData' => 'orderData....'
];

list($request, $giftCardResponseCollection) = $this->doMultipleGiftCardPayments($orderData);

$this->assertEquals(
$request,
[
'reference' => '0000020',
'amount' => [
'value' => 100,
'currency' => 'EUR'
],
'order' => $orderData
]
);
}

public function testProcessGiftCardReturnsMultipleGiftCardResponses()
{
$orderData = [
'pspReference' => 'pspReference!23',
'orderData' => 'orderData....'
];

list($request, $giftCardResponseCollection) = $this->doMultipleGiftCardPayments($orderData);

// make sure processGiftcards response is an array
$this->assertIsArray($giftCardResponseCollection);
// make sure the size of response array is equal to the number of redeemed gift cards
$this->assertEquals(2, count($giftCardResponseCollection));
// make sure the values in the response array are of type Checkout PaymentResponse Objects
$this->assertEquals(get_class($giftCardResponseCollection[0]), CheckoutPaymentResponse::class);
$this->assertEquals(get_class($giftCardResponseCollection[1]), CheckoutPaymentResponse::class);
}

private function doMultipleGiftCardPayments($orderData)
{
$amount = 250;
$store = $this->createConfiguredMock(StoreInterface::class, [
Expand All @@ -203,7 +244,7 @@ public function testProcessGiftCardsWithGiftCards()
'currency' => 'EUR'
]
];
$response = new \Adyen\Model\Checkout\PaymentResponse();
$response = new CheckoutPaymentResponse();
$response->setResultCode('Authorised');
$response->setMerchantReference('PSPDMDM2222');
$serviceMock = $this->createMock(PaymentsApi::class);
Expand All @@ -217,26 +258,12 @@ public function testProcessGiftCardsWithGiftCards()
$reflector = new \ReflectionProperty(TransactionPayment::class, 'remainingOrderAmount');
$reflector->setAccessible(true);
$reflector->setValue($this->transactionPayment, $amount);
$orderData = [
'pspReference' => 'pspReference!23',
'orderData' => 'orderData....'
];

$this->orderApiHelperMock
->expects($this->once())
->method('createOrder')
->willReturn($orderData);

list($request, $giftCardResponse) = $this->transactionPayment->processGiftcards($originalRequest, $serviceMock);
$this->assertEquals(
$request,
[
'reference' => '0000020',
'amount' => [
'value' => 100,
'currency' => 'EUR'
],
'order' => $orderData
]
);
return $this->transactionPayment->processGiftcards($originalRequest, $serviceMock);
}
}
Loading