Skip to content

Commit

Permalink
Merge branch 'release/v6.3.0' into 'master'
Browse files Browse the repository at this point in the history
release/v6.3.0 into master

See merge request agence-dnd/marketplace/magento-2/external/module-checkout-magento2-plugin!225
  • Loading branch information
DnD-Behou committed Aug 8, 2024
2 parents 914508f + 9371ad7 commit 3c8d781
Show file tree
Hide file tree
Showing 13 changed files with 1,214 additions and 36 deletions.
126 changes: 126 additions & 0 deletions Controller/Klarna/Context.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

declare(strict_types=1);
/**
* Checkout.com
* Authorized and regulated as an electronic money institution
* by the UK Financial Conduct Authority (FCA) under number 900816.
*
* PHP version 7
*
* @category Magento2
* @package Checkout.com
* @author Platforms Development Team <[email protected]>
* @copyright 2010-present Checkout.com
* @license https://opensource.org/licenses/mit-license.html MIT License
* @link https://docs.checkout.com/
*/

namespace CheckoutCom\Magento2\Controller\Klarna;

use Checkout\CheckoutApiException;
use Checkout\Common\AccountHolder;
use Checkout\Common\Address;
use Checkout\Payments\Request\Source\AbstractRequestSource;
use Checkout\Payments\Request\Source\Contexts\PaymentContextsKlarnaSource;
use CheckoutCom\Magento2\Helper\Logger;
use CheckoutCom\Magento2\Model\Service\PaymentContextRequestService;
use CheckoutCom\Magento2\Model\Service\QuoteHandlerService;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Phrase;
use Magento\Framework\Serialize\SerializerInterface;

class Context implements HttpPostActionInterface
{
protected JsonFactory $resultJsonFactory;
protected PaymentContextRequestService $paymentContextRequestService;
protected RequestInterface $request;
protected QuoteHandlerService $quoteHandlerService;
protected Logger $logger;
protected SerializerInterface $serializer;

public function __construct(
JsonFactory $resultJsonFactory,
PaymentContextRequestService $paymentContextRequestService,
RequestInterface $request,
QuoteHandlerService $quoteHandlerService,
SerializerInterface $serializer,
Logger $logger
) {
$this->resultJsonFactory = $resultJsonFactory;
$this->paymentContextRequestService = $paymentContextRequestService;
$this->request = $request;
$this->quoteHandlerService = $quoteHandlerService;
$this->logger = $logger;
$this->serializer = $serializer;
}

/**
* @inheritDoc
*/
public function execute(): Json
{
$resultJson = $this->resultJsonFactory->create();

try {
$resultJson->setData(
[
'content' => $this->paymentContextRequestService
->setShippingFeesAsItem(true)
->collectDiscountAmountOnItemUnitPrice(false)
->setForceAuthorizeMode((bool)$this->request->getParam('forceAuthorizeMode'))
->makePaymentContextRequests(
$this->getKlarnaContextSource()
),
]
);
} catch (CheckoutApiException $apiException) {
$this->logger->write(sprintf('Error happen while requesting klarna context: %s', $apiException->getMessage()));
$resultJson->setData(
[
'content' => [
'error' => $this->resolveExceptionErrorMessage($apiException),
],
]
);
}

return $resultJson;
}

private function resolveExceptionErrorMessage(CheckoutApiException $apiException): Phrase
{
$errorDetails = !$apiException->error_details ? [] : $apiException->error_details;
$errorCodes = $errorDetails['error_codes'] ?? [];

return in_array('billing_country_invalid', $errorCodes) ?
__('This payment method is not available in your country') :
(
in_array('currency_not_supported', $errorCodes) ?
__('Currency is not supported for your country') : __('This payment method is not available')
);
}

private function getKlarnaContextSource(): AbstractRequestSource
{
$klarnaRequestSource = new PaymentContextsKlarnaSource();
$accountHolder = new AccountHolder();
$billingAddress = new Address();
$billingAddress->country = $this->getCountryId();
$accountHolder->billing_address = $billingAddress;
$klarnaRequestSource->account_holder = $accountHolder;

return $klarnaRequestSource;
}

/**
* If a country id is given on request parameters give it first, if no use the quote one
*/
private function getCountryId(): string
{
return (string)$this->serializer->unserialize((string)$this->request->getContent())['country'] ?: $this->quoteHandlerService->getBillingAddress()->getCountry();
}
}
67 changes: 67 additions & 0 deletions Controller/Klarna/GetCustomerDatas.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);
/**
* Checkout.com
* Authorized and regulated as an electronic money institution
* by the UK Financial Conduct Authority (FCA) under number 900816.
*
* PHP version 7
*
* @category Magento2
* @package Checkout.com
* @author Platforms Development Team <[email protected]>
* @copyright 2010-present Checkout.com
* @license https://opensource.org/licenses/mit-license.html MIT License
* @link https://docs.checkout.com/
*/

namespace CheckoutCom\Magento2\Controller\Klarna;

use CheckoutCom\Magento2\Model\Service\QuoteHandlerService;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;

class GetCustomerDatas implements HttpPostActionInterface
{
protected JsonFactory $resultJsonFactory;
protected RequestInterface $request;
protected QuoteHandlerService $quoteHandlerService;

public function __construct(
JsonFactory $resultJsonFactory,
RequestInterface $request,
QuoteHandlerService $quoteHandlerService
) {
$this->resultJsonFactory = $resultJsonFactory;
$this->request = $request;
$this->quoteHandlerService = $quoteHandlerService;
}

/**
* @inheritDoc
*/
public function execute(): Json
{
// Get the request data
$quoteId = $this->request->getParam('quote_id');
$storeId = $this->request->getParam('store_id');

// Try to load a quote
$quote = $this->quoteHandlerService->getQuote([
'entity_id' => $quoteId,
'store_id' => $storeId,
]);

$resultJson = $this->resultJsonFactory->create();
$resultJson->setData(
[
'billing' => $this->quoteHandlerService->getBillingAddress()->toArray(),
]
);

return $resultJson;
}
}
19 changes: 15 additions & 4 deletions Controller/Paypal/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
* @license https://opensource.org/licenses/mit-license.html MIT License
* @link https://docs.checkout.com/
*/

namespace CheckoutCom\Magento2\Controller\Paypal;

use Checkout\Payments\Request\Source\AbstractRequestSource;
use Checkout\Payments\Request\Source\Contexts\PaymentContextsPayPalSource;
use CheckoutCom\Magento2\Model\Service\PaymentContextRequestService;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\RequestInterface;
Expand Down Expand Up @@ -47,13 +50,21 @@ public function execute(): Json
$resultJson = $this->resultJsonFactory->create();
$resultJson->setData(
[
'content' => $this->paymentContextRequestService->makePaymentContextRequests(
'paypal',
(bool)$this->request->getParam('forceAuthorizeMode')
)
'content' => $this->paymentContextRequestService
->setShippingFeesAsItem(false)
->collectDiscountAmountOnItemUnitPrice(true)
->setForceAuthorizeMode((bool)$this->request->getParam('forceAuthorizeMode'))
->makePaymentContextRequests(
$this->getPaypalContext()
),
]
);

return $resultJson;
}

private function getPaypalContext(): AbstractRequestSource
{
return new PaymentContextsPayPalSource();
}
}
Loading

0 comments on commit 3c8d781

Please sign in to comment.