-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'release/v6.3.0' into 'master'
release/v6.3.0 into master See merge request agence-dnd/marketplace/magento-2/external/module-checkout-magento2-plugin!225
- Loading branch information
Showing
13 changed files
with
1,214 additions
and
36 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
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(); | ||
} | ||
} |
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,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; | ||
} | ||
} |
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.