Skip to content

Commit

Permalink
Update captcha validation
Browse files Browse the repository at this point in the history
  • Loading branch information
leon-zhang-awx committed Apr 25, 2024
1 parent 74b2113 commit a725517
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
67 changes: 67 additions & 0 deletions Model/Client/Request/PaymentIntents/Get.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* This file is part of the Airwallex Payments module.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade
* to newer versions in the future.
*
* @copyright Copyright (c) 2021 Magebit, Ltd. (https://magebit.com/)
* @license GNU General Public License ("GPL") v3.0
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Airwallex\Payments\Model\Client\Request\PaymentIntents;

use Airwallex\Payments\Model\Client\AbstractClient;
use Airwallex\Payments\Model\Client\Interfaces\BearerAuthenticationInterface;
use Psr\Http\Message\ResponseInterface;

class Get extends AbstractClient implements BearerAuthenticationInterface
{
/**
* @var string
*/
private string $paymentIntentId;

/**
* @param string $id
*
* @return $this
*/
public function setPaymentIntentId(string $id): self
{
$this->paymentIntentId = $id;

return $this;
}

/**
* @return string
*/
protected function getUri(): string
{
return 'pa/payment_intents/' . $this->paymentIntentId;
}

/**
* @param ResponseInterface $request
*
* @return string
* @throws \JsonException
*/
protected function parseResponse(ResponseInterface $request): string
{
return $request->getBody()->getContents();
}

/**
* @return string
*/
protected function getMethod(): string
{
return 'GET';
}
}
4 changes: 2 additions & 2 deletions Model/Config/WebapiConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
class WebapiConfigProvider implements WebapiValidationConfigProviderInterface
{
const PROTECTED_METHODS = [
'guestSavePaymentInformationAndPlaceOrder' => true,
'savePaymentInformationAndPlaceOrder' => true
'airwallexGuestPlaceOrder' => true,
'airwallexPlaceOrder' => true
];
protected IsCaptchaEnabledInterface $isEnabled;
protected ValidationConfigResolverInterface $configResolver;
Expand Down
19 changes: 18 additions & 1 deletion Model/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Magento\Payment\Model\MethodInterface;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\PaymentInterface;
use Airwallex\Payments\Model\Client\Request\PaymentIntents\Get;

class Service implements ServiceInterface
{
Expand All @@ -44,6 +45,7 @@ class Service implements ServiceInterface
protected PaymentInformationManagementInterface $paymentInformationManagement;
protected PlaceOrderResponseInterfaceFactory $placeOrderResponseFactory;
protected CacheInterface $cache;
protected Get $intentGet;

/**
* Index constructor.
Expand All @@ -63,7 +65,8 @@ public function __construct(
GuestPaymentInformationManagementInterface $guestPaymentInformationManagement,
PaymentInformationManagementInterface $paymentInformationManagement,
PlaceOrderResponseInterfaceFactory $placeOrderResponseFactory,
CacheInterface $cache
CacheInterface $cache,
Get $intentGet
) {
$this->paymentIntents = $paymentIntents;
$this->configuration = $configuration;
Expand All @@ -72,6 +75,7 @@ public function __construct(
$this->paymentInformationManagement = $paymentInformationManagement;
$this->placeOrderResponseFactory = $placeOrderResponseFactory;
$this->cache = $cache;
$this->intentGet = $intentGet;
}

/**
Expand Down Expand Up @@ -138,6 +142,7 @@ public function airwallexGuestPlaceOrder(
'client_secret' => $intent['clientSecret']
]);
} else {
$this->checkIntent($intentId);
$orderId = $this->guestPaymentInformationManagement->savePaymentInformationAndPlaceOrder(
$cartId,
$email,
Expand Down Expand Up @@ -169,6 +174,7 @@ public function airwallexPlaceOrder(
): PlaceOrderResponseInterface {
/** @var PlaceOrderResponse $response */
$response = $this->placeOrderResponseFactory->create();

if ($intentId === null) {
$intent = $this->paymentIntents->getIntents();
$this->cache->save(1, ReCaptchaValidationPlugin::getCacheKey($intent['id']), [], 3600);
Expand All @@ -179,6 +185,7 @@ public function airwallexPlaceOrder(
'client_secret' => $intent['clientSecret']
]);
} else {
$this->checkIntent($intentId);
$orderId = $this->paymentInformationManagement->savePaymentInformationAndPlaceOrder(
$cartId,
$paymentMethod,
Expand All @@ -193,4 +200,14 @@ public function airwallexPlaceOrder(

return $response;
}

protected function checkIntent($id)
{
$resp = $this->intentGet->setPaymentIntentId($id)->send();

$respArr = json_decode($resp, true);
if (!in_array($respArr['status'],["SUCCEEDED","REQUIRES_CAPTURE"])) {
throw new \Exception("Something went wrong while processing your request. Please try again later.");
}
}
}

0 comments on commit a725517

Please sign in to comment.