Skip to content

Commit

Permalink
Merge pull request #1 from mbouchottbd/fixes/201811
Browse files Browse the repository at this point in the history
Fixes/201811
  • Loading branch information
vpietri-tbd authored Nov 8, 2018
2 parents a2907b3 + 60b104a commit 08aecf0
Show file tree
Hide file tree
Showing 12 changed files with 418 additions and 104 deletions.
10 changes: 7 additions & 3 deletions Helper/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
class Constants
{
const CACHE_KEY_MERCHANT_CONTRACT_IMPORT_FLAG = 'payline_merchant_contract_import_flag';

const WEB_PAYMENT_CPT = 'payline_web_payment_cpt';

const CONFIG_PATH_PAYLINE_GENERAL_ENVIRONMENT = 'payline/general/environment';
const CONFIG_PATH_PAYLINE_GENERAL_MERCHANT_ID = 'payline/general/merchant_id';
const CONFIG_PATH_PAYLINE_GENERAL_ACCESS_KEY = 'payline/general/access_key';
const CONFIG_PATH_PAYLINE_GENERAL_DEBUG = 'payline/general/debug';
const CONFIG_PATH_PAYLINE_GENERAL_CONTRACTS = 'payline/general/contracts';

const ORDER_STATUS_PAYLINE_PENDING = 'payline_pending';
const ORDER_STATUS_PAYLINE_WAITING_CAPTURE = 'payline_waiting_capture';
const ORDER_STATUS_PAYLINE_CAPTURED = 'payline_captured';
Expand All @@ -22,4 +22,8 @@ class Constants
const ORDER_STATUS_PAYLINE_REFUSED = 'payline_refused';
const ORDER_STATUS_PAYLINE_FRAUD = 'payline_fraud';
const ORDER_STATUS_PAYLINE_WAITING_ACCEPTANCE = 'payline_waiting_acceptance';

const PAYLINE_API_USED_BY_PREFIX = 'MGT2';

const MODULE_NAME = 'Monext_Payline';
}
75 changes: 70 additions & 5 deletions Model/CartManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Monext\Payline\Model;

use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
use Magento\Checkout\Model\Cart as CheckoutCart;
use Magento\Quote\Api\CartManagementInterface;
use Magento\Quote\Api\CartRepositoryInterface;
Expand All @@ -12,17 +14,17 @@
class CartManagement
{
/**
* @var CartRepositoryInterface
* @var CartRepositoryInterface
*/
protected $cartRepository;

/**
* @var CartManagementInterface
* @var CartManagementInterface
*/
protected $cartManagement;

/**
* @var QuoteFactory
* @var QuoteFactory
*/
protected $quoteFactory;

Expand All @@ -32,23 +34,37 @@ class CartManagement
protected $orderIncrementIdTokenFactory;

/**
* @var CheckoutCart
* @var CheckoutCart
*/
protected $checkoutCart;

/**
* @var ProductCollectionFactory
*/
protected $productCollectionFactory;

/**
* @var CategoryCollectionFactory
*/
protected $categoryCollectionFactory;

public function __construct(
CartRepositoryInterface $cartRepository,
CartManagementInterface $cartManagement,
OrderIncrementIdTokenFactory $orderIncrementIdTokenFactory,
QuoteFactory $quoteFactory,
CheckoutCart $checkoutCart
CheckoutCart $checkoutCart,
ProductCollectionFactory $productCollectionFactory,
CategoryCollectionFactory $categoryCollectionFactory
)
{
$this->cartRepository = $cartRepository;
$this->cartManagement = $cartManagement;
$this->quoteFactory = $quoteFactory;
$this->orderIncrementIdTokenFactory = $orderIncrementIdTokenFactory;
$this->checkoutCart = $checkoutCart;
$this->productCollectionFactory = $productCollectionFactory;
$this->categoryCollectionFactory = $categoryCollectionFactory;
}

public function handleReserveCartOrderId($cartId, $forceReserve = false)
Expand Down Expand Up @@ -92,5 +108,54 @@ public function getCartByToken($token)
// TODO Use QuoteRepository instead of quote::load
return $this->quoteFactory->create()->load($orderIncrementId, 'reserved_order_id');
}

public function getProductCollectionFromCart($cartId)
{
$cart = $this->cartRepository->getActive($cartId);

$productIds = [];
$categoryIds = [];
$productCollection = $this->productCollectionFactory->create();
$categoryCollection = $this->categoryCollectionFactory->create();

foreach ($cart->getItems() as $item) {
$productIds[] = $item->getProductId();
}

$productCollection
->addAttributeToFilter('entity_id', ['in' => $productIds])
->addAttributeToSelect('*')
->addCategoryIds();

foreach ($productCollection as $product) {
$categoryIds = array_merge($categoryIds, $product->getCategoryIds());
}

$categoryCollection
->addAttributeToFilter('entity_id', ['in' => $categoryIds])
->addAttributeToSelect(['name', 'payline_category_mapping', 'level']);

foreach ($productCollection as $product) {
$categoryCandidate = null;

foreach ($product->getCategoryIds() as $categoryId) {
$tmpCategory = $categoryCollection->getItemById($categoryId);

if (!$tmpCategory) {
continue;
}

if (!$categoryCandidate || $tmpCategory->getLevel() > $categoryCandidate->getLevel()) {
$categoryCandidate = $tmpCategory;
}
}

$product->setPaylineCategoryMapping(
$categoryCandidate->getPaylineCategoryMapping() ? $categoryCandidate->getPaylineCategoryMapping() : $categoryCandidate->getName()
);
}

return $productCollection;
}
}

53 changes: 53 additions & 0 deletions Model/Category/Attribute/Source/CategoryMapping.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Monext\Payline\Model\Category\Attribute\Source;

use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\File\Csv;
use Monext\Payline\Helper\Constants as HelperConstants;

/**
* This class serve to map Magento Categories to Payline Categories : https://payline.atlassian.net/wiki/spaces/DT/pages/28901389/Codes+Category
*/
class CategoryMapping extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
/**
* @var \Magento\Framework\Component\ComponentRegistrar
*/
protected $componentRegistrar;

/**
* @var \Magento\Framework\File\Csv
*/
protected $csvReader;

public function __construct(
ComponentRegistrar $componentRegistrar,
Csv $csvReader
)
{
$this->componentRegistrar = $componentRegistrar;
$this->csvReader = $csvReader;
}

/**
* {@inheritdoc}
* @codeCoverageIgnore
*/
public function getAllOptions()
{
if (!$this->_options) {
$this->_options = [];

$csvFile = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, HelperConstants::MODULE_NAME) . '/fixtures/category_mapping.csv';
$rows = $this->csvReader->getData($csvFile);

foreach ($rows as $row) {
$this->_options[] = ['value' => $row[0], 'label' => __($row[1])];
}

array_unshift($this->_options, ['value' => '', 'label' => __('Please select a category mapping...')]);
}
return $this->_options;
}
}
7 changes: 6 additions & 1 deletion Model/PaymentManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Magento\Framework\Api\SortOrderBuilder;
use Magento\Framework\Data\Collection;

use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
use Magento\Checkout\Api\PaymentInformationManagementInterface as CheckoutPaymentInformationManagementInterface;
use Magento\Quote\Api\BillingAddressManagementInterface as QuoteBillingAddressManagementInterface;
use Magento\Quote\Api\CartRepositoryInterface;
Expand Down Expand Up @@ -205,6 +206,7 @@ public function wrapCallPaylineApiDoWebPaymentFacade($cartId)
{
$response = $this->callPaylineApiDoWebPaymentFacade(
$this->cartRepository->getActive($cartId),
$this->paylineCartManagement->getProductCollectionFromCart($cartId),
$this->cartTotalRepository->get($cartId),
$this->quotePaymentMethodManagement->get($cartId),
$this->quoteBillingAddressManagement->get($cartId),
Expand All @@ -219,6 +221,7 @@ public function wrapCallPaylineApiDoWebPaymentFacade($cartId)

protected function callPaylineApiDoWebPaymentFacade(
CartInterface $cart,
ProductCollection $productCollection,
TotalsInterface $totals,
PaymentInterface $payment,
AddressInterface $billingAddress,
Expand All @@ -231,7 +234,7 @@ protected function callPaylineApiDoWebPaymentFacade(
$shippingAddress = null;
}

$response = $this->callPaylineApiDoWebPayment($cart, $totals, $payment, $billingAddress, $shippingAddress);
$response = $this->callPaylineApiDoWebPayment($cart, $productCollection, $totals, $payment, $billingAddress, $shippingAddress);

if(!$response->isSuccess()) {
// TODO log
Expand All @@ -248,6 +251,7 @@ protected function callPaylineApiDoWebPaymentFacade(

protected function callPaylineApiDoWebPayment(
CartInterface $cart,
ProductCollection $productCollection,
TotalsInterface $totals,
PaymentInterface $payment,
AddressInterface $billingAddress,
Expand All @@ -257,6 +261,7 @@ protected function callPaylineApiDoWebPayment(
$request = $this->requestDoWebPaymentFactory->create();
$request
->setCart($cart)
->setProductCollection($productCollection)
->setBillingAddress($billingAddress)
->setShippingAddress($shippingAddress)
->setTotals($totals)
Expand Down
Loading

0 comments on commit 08aecf0

Please sign in to comment.