Skip to content

Commit

Permalink
Merge pull request #12 from scandipwa/virtual-checkout
Browse files Browse the repository at this point in the history
Added support for getting payments without shipping method.
  • Loading branch information
alfredsgenkins authored Oct 16, 2019
2 parents 7eb599f + 46cad0d commit a656df4
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 14 deletions.
14 changes: 10 additions & 4 deletions src/Model/Resolver/GetCartForCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable;
use Magento\Catalog\Model\ProductFactory;

use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Api\CartManagementInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Api\GuestCartRepositoryInterface;
use Magento\Quote\Model\QuoteManagement;
use Magento\Webapi\Controller\Rest\ParamOverriderCustomerId;
Expand Down Expand Up @@ -53,7 +55,6 @@ public function __construct(
GuestCartRepositoryInterface $guestCartRepository,
Configurable $configurable,
ProductFactory $productFactory

)
{
parent::__construct($guestCartRepository, $overriderCustomerId, $quoteManagement);
Expand All @@ -69,8 +70,8 @@ public function __construct(
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
* @return Value|\Magento\Quote\Api\Data\CartInterface|mixed
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @return Value|CartInterface|mixed
* @throws NotFoundException
*/
public function resolve(
Field $field,
Expand Down Expand Up @@ -120,7 +121,12 @@ public function resolve(
[
'items' => $itemsData,
'tax_amount' => $tax_amount,
'discount_amount' => $discount_amount
'discount_amount' => $discount_amount,
/**
* In interface it is PHPDocumented that it returns bool,
* while in implementation it returns int.
*/
'is_virtual' => (bool)$cart->getIsVirtual()
]
);
}
Expand Down
126 changes: 126 additions & 0 deletions src/Model/Resolver/GetPaymentMethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php
/**
* ScandiPWA - Progressive Web App for Magento
*
* Copyright © Scandiweb, Inc. All rights reserved.
* See LICENSE for license details.
*
* @license OSL-3.0 (Open Software License ("OSL") v. 3.0)
* @package scandipwa/quote-graphql
* @link https://github.com/scandipwa/quote-graphql
*/

declare(strict_types=1);

namespace ScandiPWA\QuoteGraphQl\Model\Resolver;

use Magento\Checkout\Model\PaymentDetailsFactory;
use Magento\Quote\Api\PaymentMethodManagementInterface;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Api\Data\PaymentMethodInterface;
use Magento\Quote\Api\CartTotalRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Model\QuoteIdMask;
use Magento\Quote\Model\Webapi\ParamOverriderCartId;
use Magento\Quote\Model\QuoteIdMaskFactory;

/**
* Class GetPaymentMethods
*
* @package ScandiPWA\ServerTime\Model\Resolver
*/
class GetPaymentMethods implements ResolverInterface
{
/**
* @var PaymentDetailsFactory
*/
protected $paymentDetailsFactory;

/**
* @var CartTotalRepositoryInterface
*/
protected $cartTotalsRepository;

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

/**
* @var ParamOverriderCartId
*/
protected $overriderCartId;

/**
* @var QuoteIdMaskFactory
*/
protected $quoteIdMaskFactory;

/**
* GetPaymentMethods constructor.
* @param PaymentMethodManagementInterface $paymentMethodManagement
* @param PaymentDetailsFactory $paymentDetailsFactory
* @param CartTotalRepositoryInterface $cartTotalsRepository
* @param ParamOverriderCartId $overriderCartId
* @param QuoteIdMaskFactory $quoteIdMaskFactory
*/
public function __construct(
PaymentMethodManagementInterface $paymentMethodManagement,
PaymentDetailsFactory $paymentDetailsFactory,
CartTotalRepositoryInterface $cartTotalsRepository,
ParamOverriderCartId $overriderCartId,
QuoteIdMaskFactory $quoteIdMaskFactory
) {
$this->paymentDetailsFactory = $paymentDetailsFactory;
$this->paymentMethodManagement = $paymentMethodManagement;
$this->cartTotalsRepository = $cartTotalsRepository;
$this->overriderCartId = $overriderCartId;
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
}

/**
* Get payment methods
*
* @param Field $field
* @param ContextInterface $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
* @return array|Value|mixed
* @throws NoSuchEntityException
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
if (isset($args['guestCartId'])) {
/** @var $quoteIdMask QuoteIdMask */
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($args['guestCartId'], 'masked_id');
$cartId = $quoteIdMask->getQuoteId();
} else {
$cartId = $this->overriderCartId->getOverriddenValue();
}

$paymentDetails = $this->paymentDetailsFactory->create();
$paymentDetails->setPaymentMethods($this->paymentMethodManagement->getList($cartId));
$paymentDetails->setTotals($this->cartTotalsRepository->get($cartId));

return array_map(
function ($payment) {
/** @var PaymentMethodInterface $payment */
return [
'code' => $payment->getCode(),
'title' => $payment->getTitle(),
];
},
$paymentDetails->getPaymentMethods()
);
}
}
21 changes: 11 additions & 10 deletions src/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Mutation {
}

type Query {
getPaymentMethods(guestCartId: String): [PaymentMethod] @resolver(class: "\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\GetPaymentMethods")
getCartForCustomer(guestCartId: String): QuoteData @resolver(class: "\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\GetCartForCustomer")
getOrderList: OrderList @resolver(class: "\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\OrderListResolver") @doc(description: "The Sales Order query returns information about a Sales order")
getOrderById(id: Int!): Order @resolver(class: "\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\ExpandedOrderResolver") @doc(description: "The Sales Order query returns information about a Sales order")
Expand Down Expand Up @@ -104,13 +105,10 @@ input SaveAddressInformation {
}

type QuoteData implements TotalsObject {
id: Int
items_count: Int
customer_is_guest: Boolean
store_id: Int
customer_tax_class_id: Int
items_qty: Float
coupon_code: String
is_virtual: Boolean
}

type PaymentTotals implements TotalsObject {
}

type OrderIdObject {
Expand All @@ -127,10 +125,13 @@ type PaymentMethod {
title: String
}

type PaymentTotals implements TotalsObject {
}

interface TotalsObject @typeResolver(class: "\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\TotalsObject") {
items_count: Int
customer_is_guest: Boolean
store_id: Int
customer_tax_class_id: Int
items_qty: Float
coupon_code: String
grand_total: Float
base_grand_total: Float
subtotal: Float
Expand Down

0 comments on commit a656df4

Please sign in to comment.