-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
90 changed files
with
3,400 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\ReCaptchaCheckout\Model; | ||
|
||
use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface; | ||
use Magento\ReCaptchaUi\Model\ValidationConfigResolverInterface; | ||
use Magento\ReCaptchaValidationApi\Api\Data\ValidationConfigInterface; | ||
use Magento\ReCaptchaWebapiApi\Api\Data\EndpointInterface; | ||
use Magento\ReCaptchaWebapiApi\Api\WebapiValidationConfigProviderInterface; | ||
|
||
/** | ||
* Provide checkout related endpoint configuration. | ||
*/ | ||
class WebapiConfigProvider implements WebapiValidationConfigProviderInterface | ||
{ | ||
private const CAPTCHA_ID = 'place_order'; | ||
|
||
/** | ||
* @var IsCaptchaEnabledInterface | ||
*/ | ||
private $isEnabled; | ||
|
||
/** | ||
* @var ValidationConfigResolverInterface | ||
*/ | ||
private $configResolver; | ||
|
||
/** | ||
* @param IsCaptchaEnabledInterface $isEnabled | ||
* @param ValidationConfigResolverInterface $configResolver | ||
*/ | ||
public function __construct(IsCaptchaEnabledInterface $isEnabled, ValidationConfigResolverInterface $configResolver) | ||
{ | ||
$this->isEnabled = $isEnabled; | ||
$this->configResolver = $configResolver; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getConfigFor(EndpointInterface $endpoint): ?ValidationConfigInterface | ||
{ | ||
//phpcs:disable Magento2.PHP.LiteralNamespaces | ||
if ($endpoint->getServiceMethod() === 'savePaymentInformationAndPlaceOrder' | ||
|| $endpoint->getServiceClass() === 'Magento\QuoteGraphQl\Model\Resolver\SetPaymentAndPlaceOrder' | ||
|| $endpoint->getServiceClass() === 'Magento\QuoteGraphQl\Model\Resolver\PlaceOrder' | ||
) { | ||
if ($this->isEnabled->isCaptchaEnabledFor(self::CAPTCHA_ID)) { | ||
return $this->configResolver->get(self::CAPTCHA_ID); | ||
} | ||
} | ||
//phpcs:enable Magento2.PHP.LiteralNamespaces | ||
|
||
return null; | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
ReCaptchaCheckout/Test/Api/GuestPaymentInformationManagementTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\ReCaptchaCheckout\Test\Api; | ||
|
||
use Magento\Framework\Webapi\Rest\Request; | ||
use Magento\Quote\Model\Quote; | ||
use Magento\Quote\Model\QuoteFactory; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
use Magento\TestFramework\TestCase\WebapiAbstract; | ||
|
||
/** | ||
* Test that checkout APIs are covered with ReCaptcha | ||
*/ | ||
class GuestPaymentInformationManagementTest extends WebapiAbstract | ||
{ | ||
private const API_ROUTE = '/V1/guest-carts/%s/payment-information'; | ||
|
||
/** | ||
* @var QuoteFactory | ||
*/ | ||
private $quoteFactory; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->_markTestAsRestOnly(); | ||
$objectManager = Bootstrap::getObjectManager(); | ||
$this->quoteFactory = $objectManager->get(QuoteFactory::class); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_check_payment.php | ||
* @magentoConfigFixture default_store customer/captcha/enable 0 | ||
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key | ||
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key | ||
* @magentoConfigFixture base_website recaptcha_frontend/type_for/place_order invisible | ||
*/ | ||
public function testRequired(): void | ||
{ | ||
$this->expectException(\Throwable::class); | ||
$this->expectExceptionCode(400); | ||
$this->expectExceptionMessage('{"message":"ReCaptcha validation failed, please try again"}'); | ||
|
||
/** @var Quote $quote */ | ||
$quote = $this->quoteFactory->create(); | ||
$quote->load('test_order_1', 'reserved_order_id'); | ||
$cartId = $quote->getId(); | ||
$payment = $quote->getPayment(); | ||
$address = $quote->getBillingAddress(); | ||
$addressData = []; | ||
$addressProperties = [ | ||
'city', 'company', 'countryId', 'firstname', 'lastname', 'postcode', | ||
'region', 'regionCode', 'regionId', 'saveInAddressBook', 'street', 'telephone', 'email' | ||
]; | ||
foreach ($addressProperties as $property) { | ||
$method = 'get' . $property; | ||
$addressData[$property] = $address->$method(); | ||
} | ||
|
||
$serviceInfo = [ | ||
'rest' => [ | ||
'resourcePath' => sprintf(self::API_ROUTE, $cartId), | ||
'httpMethod' => Request::HTTP_METHOD_POST, | ||
'token' => null | ||
], | ||
]; | ||
$requestData = [ | ||
'cart_id' => $cartId, | ||
'billingAddress' => $addressData, | ||
'email' => $quote->getCustomerEmail(), | ||
'paymentMethod' => [ | ||
'additional_data' => $payment->getAdditionalData(), | ||
'method' => $payment->getMethod(), | ||
'po_number' => $payment->getPoNumber() | ||
] | ||
]; | ||
|
||
$this->_webApiCall($serviceInfo, $requestData); | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
ReCaptchaCheckout/Test/Api/PaymentInformationManagementTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\ReCaptchaCheckout\Test\Api; | ||
|
||
use Magento\Framework\Webapi\Rest\Request; | ||
use Magento\Integration\Api\CustomerTokenServiceInterface; | ||
use Magento\Quote\Model\Quote; | ||
use Magento\Quote\Model\QuoteFactory; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
use Magento\TestFramework\TestCase\WebapiAbstract; | ||
|
||
/** | ||
* Test that checkout APIs are covered with ReCaptcha | ||
*/ | ||
class PaymentInformationManagementTest extends WebapiAbstract | ||
{ | ||
private const API_ROUTE = '/V1/carts/mine/payment-information'; | ||
|
||
/** | ||
* @var QuoteFactory | ||
*/ | ||
private $quoteFactory; | ||
|
||
/** | ||
* @var CustomerTokenServiceInterface | ||
*/ | ||
private $tokenService; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->_markTestAsRestOnly(); | ||
$objectManager = Bootstrap::getObjectManager(); | ||
$this->quoteFactory = $objectManager->get(QuoteFactory::class); | ||
$this->tokenService = $objectManager->get(CustomerTokenServiceInterface::class); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Checkout/_files/customer_quote_ready_for_order.php | ||
* @magentoConfigFixture default_store customer/captcha/enable 0 | ||
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key | ||
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key | ||
* @magentoConfigFixture base_website recaptcha_frontend/type_for/place_order invisible | ||
*/ | ||
public function testRequired(): void | ||
{ | ||
$this->expectException(\Throwable::class); | ||
$this->expectExceptionCode(400); | ||
$this->expectExceptionMessage('{"message":"ReCaptcha validation failed, please try again"}'); | ||
|
||
/** @var Quote $quote */ | ||
$quote = $this->quoteFactory->create(); | ||
$quote->load('55555555', 'reserved_order_id'); | ||
$cartId = $quote->getId(); | ||
$payment = $quote->getPayment(); | ||
$address = $quote->getBillingAddress(); | ||
$addressData = []; | ||
$addressProperties = [ | ||
'city', 'company', 'countryId', 'firstname', 'lastname', 'postcode', | ||
'region', 'regionCode', 'regionId', 'saveInAddressBook', 'street', 'telephone', 'email' | ||
]; | ||
foreach ($addressProperties as $property) { | ||
$method = 'get' . $property; | ||
$addressData[$property] = $address->$method(); | ||
} | ||
$token = $this->tokenService->createCustomerAccessToken('customer@example.com', 'password'); | ||
|
||
$serviceInfo = [ | ||
'rest' => [ | ||
'resourcePath' => self::API_ROUTE, | ||
'httpMethod' => Request::HTTP_METHOD_POST, | ||
'token' => $token | ||
], | ||
]; | ||
$requestData = [ | ||
'cart_id' => $cartId, | ||
'billingAddress' => $addressData, | ||
'email' => $quote->getCustomerEmail(), | ||
'paymentMethod' => [ | ||
'additional_data' => $payment->getAdditionalData(), | ||
'method' => $payment->getMethod(), | ||
'po_number' => $payment->getPoNumber() | ||
] | ||
]; | ||
|
||
$this->_webApiCall($serviceInfo, $requestData); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"> | ||
<system> | ||
<section id="recaptcha_frontend"> | ||
<group id="type_for"> | ||
<field id="place_order" translate="label" type="select" sortOrder="175" showInDefault="1" | ||
showInWebsite="1" showInStore="0" canRestore="1"> | ||
<label>Enable for Checkout/Placing Order</label> | ||
<source_model>Magento\ReCaptchaAdminUi\Model\OptionSource\Type</source_model> | ||
</field> | ||
</group> | ||
</section> | ||
</system> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"> | ||
<default> | ||
<recaptcha_frontend> | ||
<type_for> | ||
<place_order/> | ||
</type_for> | ||
</recaptcha_frontend> | ||
</default> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<type name="Magento\ReCaptchaWebapiApi\Model\CompositeWebapiValidationConfigProvider"> | ||
<arguments> | ||
<argument name="providers" xsi:type="array"> | ||
<item name="checkout" xsi:type="object">Magento\ReCaptchaCheckout\Model\WebapiConfigProvider</item> | ||
</argument> | ||
</arguments> | ||
</type> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.