Skip to content

Commit

Permalink
Merge branch 2.3-develop into ENGCOM-4342-magento-magento2-21363
Browse files Browse the repository at this point in the history
  • Loading branch information
magento-engcom-team committed Feb 27, 2019
2 parents c264369 + 908ed15 commit 6f64793
Show file tree
Hide file tree
Showing 28 changed files with 1,607 additions and 392 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Fedex\Plugin\Block\DataProviders\Tracking;

use Magento\Fedex\Model\Carrier;
use Magento\Shipping\Model\Tracking\Result\Status;
use Magento\Shipping\Block\DataProviders\Tracking\DeliveryDateTitle as Subject;

/**
* Plugin to change delivery date title with FedEx customized value
*/
class ChangeTitle
{
/**
* Title modification in case if FedEx used as carrier
*
* @param Subject $subject
* @param \Magento\Framework\Phrase|string $result
* @param Status $trackingStatus
* @return \Magento\Framework\Phrase|string
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGetTitle(Subject $subject, $result, Status $trackingStatus)
{
if ($trackingStatus->getCarrier() === Carrier::CODE) {
$result = __('Expected Delivery:');
}
return $result;
}
}
54 changes: 54 additions & 0 deletions app/code/Magento/Fedex/Plugin/Block/Tracking/PopupDeliveryDate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Fedex\Plugin\Block\Tracking;

use Magento\Shipping\Block\Tracking\Popup;
use Magento\Fedex\Model\Carrier;
use Magento\Shipping\Model\Tracking\Result\Status;

/**
* Plugin to update delivery date value in case if Fedex used
*/
class PopupDeliveryDate
{
/**
* Show only date for expected delivery in case if Fedex is a carrier
*
* @param Popup $subject
* @param string $result
* @param string $date
* @param string $time
* @return string
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterFormatDeliveryDateTime(Popup $subject, $result, $date, $time)
{
if ($this->getCarrier($subject) === Carrier::CODE) {
$result = $subject->formatDeliveryDate($date);
}
return $result;
}

/**
* Retrieve carrier name from tracking info
*
* @param Popup $subject
* @return string
*/
private function getCarrier(Popup $subject): string
{
foreach ($subject->getTrackingInfo() as $trackingData) {
foreach ($trackingData as $trackingInfo) {
if ($trackingInfo instanceof Status) {
$carrier = $trackingInfo->getCarrier();
return $carrier;
}
}
}
return '';
}
}
6 changes: 6 additions & 0 deletions app/code/Magento/Fedex/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@
</argument>
</arguments>
</type>
<type name="Magento\Shipping\Block\DataProviders\Tracking\DeliveryDateTitle">
<plugin name="update_delivery_date_title" type="Magento\Fedex\Plugin\Block\DataProviders\Tracking\ChangeTitle"/>
</type>
<type name="Magento\Shipping\Block\Tracking\Popup">
<plugin name="update_delivery_date_value" type="Magento\Fedex\Plugin\Block\Tracking\PopupDeliveryDate"/>
</type>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function execute(QuoteAddress $address): array
$addressData['model'] = $address;

$addressData = array_merge($addressData, [
'address_id' => $address->getId(),
'country' => [
'code' => $address->getCountryId(),
'label' => $address->getCountry()
Expand Down
10 changes: 5 additions & 5 deletions app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public function __construct(
* Get cart for user
*
* @param string $cartHash
* @param int|null $userId
* @param int|null $customerId
* @return Quote
* @throws GraphQlAuthorizationException
* @throws GraphQlNoSuchEntityException
*/
public function execute(string $cartHash, ?int $userId): Quote
public function execute(string $cartHash, ?int $customerId): Quote
{
try {
$cartId = $this->maskedQuoteIdToQuoteId->execute($cartHash);
Expand All @@ -69,14 +69,14 @@ public function execute(string $cartHash, ?int $userId): Quote
);
}

$customerId = (int)$cart->getCustomerId();
$cartCustomerId = (int)$cart->getCustomerId();

/* Guest cart, allow operations */
if (!$customerId) {
if (!$cartCustomerId && null === $customerId) {
return $cart;
}

if ($customerId !== $userId) {
if ($cartCustomerId !== $customerId) {
throw new GraphQlAuthorizationException(
__(
'The current user cannot perform operations on cart "%masked_cart_id"',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public function execute(ContextInterface $context, CartInterface $cart, array $b
);
}
if (null === $customerAddressId) {
$addressInput['country_id'] = $addressInput['country_code'] ?? '';
$billingAddress = $this->addressModel->addData($addressInput);
} else {
$this->checkCustomerAccount->execute($context->getUserId(), $context->getUserType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
);
}
if (null === $customerAddressId) {
$addressInput['country_id'] = $addressInput['country_code'] ?? '';
$shippingAddress = $this->addressModel->addData($addressInput);
} else {
$this->checkCustomerAccount->execute($context->getUserId(), $context->getUserType());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Resolver;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

/**
* @inheritdoc
*/
class SelectedPaymentMethod implements ResolverInterface
{
/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
if (!isset($value['model'])) {
throw new LocalizedException(__('"model" value should be specified'));
}

/** @var \Magento\Quote\Model\Quote $cart */
$cart = $value['model'];

$payment = $cart->getPayment();
if (!$payment) {
return [];
}

return [
'code' => $payment->getMethod(),
'purchase_order_number' => $payment->getPoNumber(),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Resolver;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\Stdlib\ArrayManager;
use Magento\Quote\Api\Data\PaymentInterface;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
use Magento\Quote\Api\Data\PaymentInterfaceFactory;
use Magento\Quote\Api\PaymentMethodManagementInterface;

/**
* Mutation resolver for setting payment method for shopping cart
*/
class SetPaymentMethodOnCart implements ResolverInterface
{
/**
* @var GetCartForUser
*/
private $getCartForUser;

/**
* @var ArrayManager
*/
private $arrayManager;

/**
* @var PaymentMethodManagementInterface
*/
private $paymentMethodManagement;

/**
* @var PaymentInterfaceFactory
*/
private $paymentFactory;

/**
* @param GetCartForUser $getCartForUser
* @param ArrayManager $arrayManager
* @param PaymentMethodManagementInterface $paymentMethodManagement
* @param PaymentInterfaceFactory $paymentFactory
*/
public function __construct(
GetCartForUser $getCartForUser,
ArrayManager $arrayManager,
PaymentMethodManagementInterface $paymentMethodManagement,
PaymentInterfaceFactory $paymentFactory
) {
$this->getCartForUser = $getCartForUser;
$this->arrayManager = $arrayManager;
$this->paymentMethodManagement = $paymentMethodManagement;
$this->paymentFactory = $paymentFactory;
}

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
$maskedCartId = (string)$this->arrayManager->get('input/cart_id', $args);
if (!$maskedCartId) {
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
}

$paymentMethod = $this->arrayManager->get('input/payment_method', $args);
if (!$paymentMethod) {
throw new GraphQlInputException(__('Required parameter "payment_method" is missing'));
}

$paymentMethodCode = (string) $this->arrayManager->get('input/payment_method/code', $args);
if (!$paymentMethodCode) {
throw new GraphQlInputException(__('Required parameter payment "code" is missing'));
}

$poNumber = $this->arrayManager->get('input/payment_method/purchase_order_number', $args);

$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
$payment = $this->paymentFactory->create([
'data' => [
PaymentInterface::KEY_METHOD => $paymentMethodCode,
PaymentInterface::KEY_PO_NUMBER => $poNumber,
PaymentInterface::KEY_ADDITIONAL_DATA => [],
]
]);

try {
$this->paymentMethodManagement->set($cart->getId(), $payment);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()));
}

return [
'cart' => [
'cart_id' => $maskedCartId,
'model' => $cart,
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\QuoteGraphQl\Model\Resolver;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
Expand Down Expand Up @@ -67,7 +68,7 @@ public function __construct(
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
$shippingAddresses = $this->arrayManager->get('input/shipping_addresses', $args);
$maskedCartId = (string) $this->arrayManager->get('input/cart_id', $args);
$maskedCartId = (string)$this->arrayManager->get('input/cart_id', $args);

if (!$maskedCartId) {
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
Expand All @@ -79,7 +80,11 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value

$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());

$this->setShippingAddressesOnCart->execute($context, $cart, $shippingAddresses);
try {
$this->setShippingAddressesOnCart->execute($context, $cart, $shippingAddresses);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()));
}

return [
'cart' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
if (!$shippingMethod['cart_address_id']) {
throw new GraphQlInputException(__('Required parameter "cart_address_id" is missing'));
}
if (!$shippingMethod['shipping_carrier_code']) {
if (!$shippingMethod['carrier_code']) {
throw new GraphQlInputException(__('Required parameter "shipping_carrier_code" is missing'));
}
if (!$shippingMethod['shipping_method_code']) {
if (!$shippingMethod['method_code']) {
throw new GraphQlInputException(__('Required parameter "shipping_method_code" is missing'));
}

Expand All @@ -85,8 +85,8 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
$this->setShippingMethodOnCart->execute(
$cart,
$shippingMethod['cart_address_id'],
$shippingMethod['shipping_carrier_code'],
$shippingMethod['shipping_method_code']
$shippingMethod['carrier_code'],
$shippingMethod['method_code']
);

return [
Expand Down
Loading

0 comments on commit 6f64793

Please sign in to comment.