Skip to content

Commit

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

See merge request agence-dnd/marketplace/magento-2/external/module-checkout-magento2-plugin!58
  • Loading branch information
Rémi V committed Sep 7, 2022
2 parents 1796abd + 0c73775 commit b70365e
Show file tree
Hide file tree
Showing 25 changed files with 326 additions and 106 deletions.
79 changes: 79 additions & 0 deletions Block/Cart/ApplePay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?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\Block\Cart;

use Magento\Checkout\Block\Onepage;
use Magento\Checkout\Model\Cart;
use Magento\Checkout\Model\CompositeConfigProvider;
use Magento\Framework\Data\Form\FormKey;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\View\Element\Template\Context;

class ApplePay extends Onepage
{
protected Cart $cart;

/**
* @param Cart $cart
* @param Context $context
* @param FormKey $formKey
* @param CompositeConfigProvider $configProvider
* @param array $layoutProcessors
* @param array $data
* @param Json|null $serializer
* @param SerializerInterface|null $serializerInterface
*/
public function __construct(
Cart $cart,
Context $context,
FormKey $formKey,
CompositeConfigProvider $configProvider,
array $layoutProcessors = [],
array $data = [],
Json $serializer = null,
SerializerInterface $serializerInterface = null
) {
parent::__construct(
$context,
$formKey,
$configProvider,
$layoutProcessors,
$data,
$serializer,
$serializerInterface
);
$this->cart = $cart;
}

/**
*
* @return int
*/
public function getProductCount(): int
{
$productCount = 0;
if ($this->cart->getQuote()->getItemsCollection()->getSize() > 0) {
$productCount = 1;
}

return $productCount;
}
}
22 changes: 13 additions & 9 deletions Controller/Api/V1.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,20 +163,24 @@ public function execute(): Json
// Load the quote
$quote = $this->loadQuote();

// Create an order
$order = $this->orderHandler->setMethodId('checkoutcom_card_payment')->handleOrder($quote);
// Reserved an order
/** @var string $reservedOrderId */
$reservedOrderId = $this->quoteHandler->getReference($quote);

// Process the payment
if ($this->orderHandler->isOrder($order)) {
if ($this->quoteHandler->isQuote($quote) && $reservedOrderId !== null) {
// Get response and success
$response = $this->requestPayment($order);
$response = $this->requestPayment($quote);

// Get the store code
$storeCode = $this->storeManager->getStore()->getCode();

// Process the response
$api = $this->apiHandler->init($storeCode, ScopeInterface::SCOPE_STORE);
if ($api->isValidResponse($response)) {
// Create an order
$order = $this->orderHandler->setMethodId('checkoutcom_card_payment')->handleOrder($quote);

// Get the payment details
$paymentDetails = $api->getPaymentDetails($response->id);

Expand Down Expand Up @@ -213,11 +217,11 @@ public function execute(): Json
/**
* Request payment to API handler
*
* @param OrderInterface $order
* @param CartInterface $quote
*
* @return mixed
*/
protected function requestPayment(OrderInterface $order)
protected function requestPayment(CartInterface $quote)
{
// Prepare the payment request payload
$payload = [
Expand All @@ -231,9 +235,9 @@ protected function requestPayment(OrderInterface $order)
// Send the charge request
return $this->methodHandler->get('checkoutcom_card_payment')->sendPaymentRequest(
$payload,
$order->getGrandTotal(),
$order->getOrderCurrencyCode(),
$order->getIncrementId()
$quote->getGrandTotal(),
$quote->getQuoteCurrencyCode(),
$quote->getReservedOrderId()
);
}

Expand Down
38 changes: 23 additions & 15 deletions Controller/Api/V2.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,21 @@ public function init(): void
*/
protected function processPayment(): array
{
$order = $this->createOrder();
if ($this->orderHandler->isOrder($order)) {
$this->order = $order;
// Try to load a quote
$quote = $this->loadQuote();

if ($quote !== null) {
// Reserved an order
/** @var string $reservedOrderId */
$reservedOrderId = $this->quoteHandler->getReference($quote);

// Get the payment response
$response = $this->getPaymentResponse($order);
$response = $this->getPaymentResponse($quote);

if ($this->api->isValidResponse($response) && $reservedOrderId !== null) {
// Create Order
$this->order = $order = $this->createOrder();

if ($this->api->isValidResponse($response)) {
// Process the payment response
$is3ds = property_exists(
$response,
Expand Down Expand Up @@ -332,11 +340,11 @@ protected function processPayment(): array
/**
* Request payment to API handler
*
* @param $order
* @param CartInterface $quote
*
* @return mixed
*/
protected function requestPayment($order)
protected function requestPayment(CartInterface $quote)
{
// Prepare the payment request payload
$payload = [
Expand All @@ -361,9 +369,9 @@ protected function requestPayment($order)
// Send the charge request
return $this->methodHandler->get('checkoutcom_card_payment')->sendPaymentRequest(
$payload,
$order->getGrandTotal(),
$order->getOrderCurrencyCode(),
$order->getIncrementId(),
$quote->getGrandTotal(),
$quote->getQuoteCurrencyCode(),
$quote->getReservedOrderId(),
$this->quote,
true
);
Expand All @@ -372,15 +380,15 @@ protected function requestPayment($order)
/**
* Get a payment response.
*
* @return Object
* @param CartInterface $quote
*
* @return mixed
*/
public function getPaymentResponse($order)
public function getPaymentResponse(CartInterface $quote)
{
$sessionId = $this->getRequest()->getParam('cko-session-id');

return ($sessionId && !empty($sessionId)) ? $this->api->getPaymentDetails($sessionId) : $this->requestPayment(
$order
);
return ($sessionId && !empty($sessionId)) ? $this->api->getPaymentDetails($sessionId) : $this->requestPayment($quote);
}

/**
Expand Down
34 changes: 21 additions & 13 deletions Controller/Payment/PlaceOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order;
use Magento\Store\Model\ScopeInterface;
Expand Down Expand Up @@ -197,11 +198,12 @@ public function execute(): Json
if (!$this->isEmptyCardToken($data)) {
// Process the request
if ($this->getRequest()->isAjax() && $quote) {
// Create an order
$order = $this->orderHandler->setMethodId($data['methodId'])->handleOrder($quote);
// Reserved an order
/** @var string $reservedOrderId */
$reservedOrderId = $this->quoteHandler->getReference($quote);

// Process the payment
if ($this->orderHandler->isOrder($order)) {
if ($this->quoteHandler->isQuote($quote) && $reservedOrderId !== null) {
$log = false;
// Get the debug config value
$debug = $this->scopeConfig->getValue(
Expand All @@ -216,7 +218,7 @@ public function execute(): Json
);

// Get response and success
$response = $this->requestPayment($order, $data);
$response = $this->requestPayment($quote, $data);

// Logging
$this->logger->display($response);
Expand All @@ -227,6 +229,9 @@ public function execute(): Json
// Process the response
$api = $this->apiHandler->init($storeCode, ScopeInterface::SCOPE_STORE);
if ($api->isValidResponse($response)) {
// Create an order
$order = $this->orderHandler->setMethodId($data['methodId'])->handleOrder($quote);

// Add the payment info to the order
$order = $this->utilities->setPaymentData($order, $response, $data);

Expand Down Expand Up @@ -258,9 +263,6 @@ public function execute(): Json

// Restore the quote
$this->session->restoreQuote();

// Handle order on failed payment
$this->orderStatusHandler->handleFailedPayment($order);
}
} else {
// Payment failed
Expand Down Expand Up @@ -296,22 +298,28 @@ public function execute(): Json
/**
* Request payment to API handler
*
* @param $order
* @param CartInterface $quote
* @param $data
*
* @return Payment|null
*/
protected function requestPayment($order, $data): ?Payment
protected function requestPayment(CartInterface $quote, $data): ?Payment
{
if ($quote->getPayment()->getMethod() === null) {
$paymentMethod = $data['methodId'];
$quote->setPaymentMethod($paymentMethod); //payment method
$quote->getPayment()->importData(['method' => $paymentMethod]);
}

// Get the method id
$methodId = $order->getPayment()->getMethodInstance()->getCode();
$methodId = $quote->getPayment()->getMethodInstance()->getCode();

// Send the charge request
return $this->methodHandler->get($methodId)->sendPaymentRequest(
$data,
$order->getGrandTotal(),
$order->getOrderCurrencyCode(),
$order->getIncrementId()
$quote->getGrandTotal(),
$quote->getQuoteCurrencyCode(),
$quote->getReservedOrderId()
);
}

Expand Down
2 changes: 1 addition & 1 deletion Gateway/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public function getCoreValue(string $path)
public function getModuleConfig(): array
{
/** @var mixed[] $moduleConfig */
$moduleConfig = $this->scopeConfig->getValue('settings/checkoutcom_configuration', ScopeInterface::SCOPE_WEBSITE);
$moduleConfig = $this->scopeConfig->getValue('settings/checkoutcom_configuration', ScopeInterface::SCOPE_WEBSITE) ?? [];
if (array_key_exists('secret_key', $moduleConfig)) {
unset($moduleConfig['secret_key']);
}
Expand Down
36 changes: 21 additions & 15 deletions Model/Api/V3.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,21 @@ private function isValidPublicKey(): bool
*/
private function processPayment(): array
{
$order = $this->createOrder($this->data->getPaymentMethod());
if ($this->orderHandler->isOrder($order)) {
$this->order = $order;
// Try to load a quote
$quote = $this->loadQuote();

if ($quote !== null) {
// Reserved an order
/** @var string $reservedOrderId */
$reservedOrderId = $this->quoteHandler->getReference($quote);

// Get the payment response
$response = $this->getPaymentResponse($order);
$response = $this->getPaymentResponse($quote);

if ($this->api->isValidResponse($response) && $reservedOrderId !== null) {

$this->order = $order = $this->createOrder($this->data->getPaymentMethod());

if ($this->api->isValidResponse($response)) {
// Process the payment response
$is3ds = property_exists($response, '_links')
&& isset($response->_links['redirect'])
Expand Down Expand Up @@ -444,27 +452,25 @@ private function loadQuote(): ?CartInterface
/**
* Get a payment response
*
* @param OrderInterface $order
* @param CartInterface $quote
*
* @return mixed
*/
private function getPaymentResponse(OrderInterface $order)
private function getPaymentResponse(CartInterface $quote)
{
$sessionId = $this->request->getParam('cko-session-id');

return ($sessionId && !empty($sessionId)) ? $this->api->getPaymentDetails($sessionId) : $this->requestPayment(
$order
);
return ($sessionId && !empty($sessionId)) ? $this->api->getPaymentDetails($sessionId) : $this->requestPayment($quote);
}

/**
* Request payment to API handler
*
* @param OrderInterface $order
* @param CartInterface $quote
*
* @return mixed
*/
private function requestPayment(OrderInterface $order)
private function requestPayment(CartInterface $quote)
{
// Prepare the payment request payload
$payload = [];
Expand Down Expand Up @@ -512,9 +518,9 @@ private function requestPayment(OrderInterface $order)
// Send the charge request
return $this->methodHandler->get($this->data->getPaymentMethod())->sendPaymentRequest(
$payload,
$order->getGrandTotal(),
$order->getOrderCurrencyCode(),
$order->getIncrementId(),
$quote->getGrandTotal(),
$quote->getQuoteCurrencyCode(),
$quote->getReservedOrderId(),
$this->quote,
true,
$this->customer ? $this->customer->getId() : null
Expand Down
2 changes: 1 addition & 1 deletion Model/Methods/AlternativePaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ public function capture(InfoInterface $payment, $amount): AbstractMethod
}

// Process the void request
$response = $api->captureOrder($payment, $amount);
$response = $api->captureOrder($payment, (float)$amount);
if (!$api->isValidResponse($response)) {
throw new LocalizedException(
__('The capture request could not be processed.')
Expand Down
2 changes: 1 addition & 1 deletion Model/Methods/ApplePayMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ public function capture(InfoInterface $payment, $amount): AbstractMethod
}

// Process the capture request
$response = $api->captureOrder($payment, $amount);
$response = $api->captureOrder($payment, (float)$amount);
if (!$api->isValidResponse($response)) {
throw new LocalizedException(
__('The capture request could not be processed.')
Expand Down
Loading

0 comments on commit b70365e

Please sign in to comment.