Skip to content

Commit

Permalink
[API][Payments] change endpoints and add missing specs
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamKasp committed Oct 20, 2020
1 parent 3788aae commit 41582c7
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 242 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ Feature: Changing the method after order confirmation
Then I should see the thank you page

@api
Scenario: Changing the payment method after placed order
Scenario: Changing the payment method for placed order
Given I added product "PHP T-Shirt" to the cart
When I complete addressing step with email "john@example.com" and "United States" based billing address
And I select "Free" shipping method
And I select "Cash on delivery" payment method
And I confirm my order
And I change payment method to "Offline"
Then I should have chosen "offline" payment method for my order
Then I should have chosen "Offline" payment method for my order
22 changes: 1 addition & 21 deletions src/Sylius/Behat/Context/Api/Shop/CheckoutContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ public function iCompleteTheShippingStepWithFirstShippingMethod(): void
* @When /^the (?:customer|visitor) proceed with ("[^"]+" payment)$/
* @Given /^the (?:customer|visitor) has proceeded ("[^"]+" payment)$/
* @When I try to change payment method to :paymentMethod payment
* @When I change payment method to :paymentMethod
*/
public function iChoosePaymentMethod(PaymentMethodInterface $paymentMethod): void
{
Expand Down Expand Up @@ -823,27 +824,6 @@ public function iTryToChangeQuantityToOfProductFromTheCart(int $quantity, Produc
$this->putProductToCart($product, $tokenValue, $quantity);
}

/**
* @When I change payment method to :paymentMethod
*/
public function iChangePaymentMethodTo(PaymentMethodInterface $paymentMethod): void
{
$this->client->request(
Request::METHOD_PATCH,
\sprintf(
'/new-api/shop/orders/%s/change-payments/%s',
$this->sharedStorage->get('cart_token'),
(string) $this->iriConverter->getItemFromIri($this->getCart()['payments'][0])->getId()
),
[],
[],
$this->getHeaders(),
json_encode([
'paymentMethodCode' => $paymentMethod->getCode(),
], \JSON_THROW_ON_ERROR)
);
}

/**
* @Then I should be informed that cart is no longer available
*/
Expand Down
3 changes: 2 additions & 1 deletion src/Sylius/Behat/Context/Api/Shop/OrderContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,9 @@ public function theShouldHaveUnitPriceDiscountedFor(ProductInterface $product, i
public function iShouldHaveChosenPaymentMethodForMyOrder(PaymentMethod $paymentMethod): void
{
$payment = $this->iriConverter->getItemFromIri(
$this->responseChecker->getValue($this->client->getLastResponse(), 'payments')[0]
$this->responseChecker->getValue($this->client->show($this->sharedStorage->get('cart_token')), 'payments')[0]
);

Assert::same(
$this->iriConverter->getIriFromItem($paymentMethod),
$this->iriConverter->getIriFromItem($payment->getMethod())
Expand Down
65 changes: 0 additions & 65 deletions src/Sylius/Bundle/ApiBundle/Command/ChangePaymentMethod.php

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
use Sylius\Bundle\ApiBundle\Command\Checkout\ChoosePaymentMethod;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Core\OrderCheckoutStates;
use Sylius\Component\Core\OrderCheckoutTransitions;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface;
use Sylius\Component\Core\Repository\PaymentRepositoryInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use Sylius\Component\Payment\Model\PaymentInterface;
use Webmozart\Assert\Assert;

/** @experimental */
Expand Down Expand Up @@ -58,13 +60,6 @@ public function __invoke(ChoosePaymentMethod $choosePaymentMethod): OrderInterfa

Assert::notNull($cart, 'Cart has not been found.');

$stateMachine = $this->stateMachineFactory->get($cart, OrderCheckoutTransitions::GRAPH);

Assert::true($stateMachine->can(
OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT),
'Order cannot have payment method assigned.'
);

/** @var PaymentMethodInterface|null $paymentMethod */
$paymentMethod = $this->paymentMethodRepository->findOneBy([
'code' => $choosePaymentMethod->paymentMethodCode,
Expand All @@ -74,8 +69,29 @@ public function __invoke(ChoosePaymentMethod $choosePaymentMethod): OrderInterfa
$payment = $this->paymentRepository->findOneByOrderId($choosePaymentMethod->paymentId, $cart->getId());
Assert::notNull($payment, 'Can not find payment with given identifier.');

if (in_array(
$cart->getCheckoutState(),
[OrderCheckoutStates::STATE_SHIPPING_SELECTED, OrderCheckoutStates::STATE_SHIPPING_SKIPPED, OrderCheckoutStates::STATE_PAYMENT_SELECTED],
true)) {
$stateMachine = $this->stateMachineFactory->get($cart, OrderCheckoutTransitions::GRAPH);

Assert::true($stateMachine->can(
OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT),
'Order cannot have payment method assigned.'
);

$payment->setMethod($paymentMethod);
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT);

return $cart;
}

Assert::same(
$payment->getState(),
PaymentInterface::STATE_NEW,
'Can not change payment method for this payment'
);
$payment->setMethod($paymentMethod);
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT);

return $cart;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,35 @@ public function applyToItem(
$rootAlias = $queryBuilder->getRootAliases()[0];
$user = $this->userContext->getUser();

$this->applyUserRulesToItem($user, $queryBuilder, $rootAlias, $httpRequestMethodType);
$this->applyUserRulesToItem($user, $queryBuilder, $rootAlias, $httpRequestMethodType, $operationName);
}

private function applyUserRulesToItem(
?UserInterface $user,
QueryBuilder $queryBuilder,
string $rootAlias,
string $httpRequestMethodType
string $httpRequestMethodType,
string $operationName
): void {
if ($user === null && $operationName === 'shop_select_payment_method') {
$queryBuilder
->leftJoin(sprintf('%s.customer', $rootAlias), 'customer')
->leftJoin('customer.user', 'user')
->andWhere('user IS NULL')
->orWhere(sprintf('%s.customer IS NULL', $rootAlias))
;

return;
}

if ($user === null) {
$queryBuilder
->leftJoin(sprintf('%s.customer', $rootAlias), 'customer')
->leftJoin('customer.user', 'user')
->andWhere('user IS NULL')
->orWhere(sprintf('%s.customer IS NULL', $rootAlias))
// ->andWhere(sprintf('%s.state = :state', $rootAlias))
// ->setParameter('state', OrderInterface::STATE_CART)
->andWhere(sprintf('%s.state = :state', $rootAlias))
->setParameter('state', OrderInterface::STATE_CART)
;

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,40 +187,6 @@
</attribute>
</itemOperation>

<itemOperation name="shop_change_payment_method">
<attribute name="method">PATCH</attribute>
<attribute name="path">/shop/orders/{id}/change-payments/{paymentId}</attribute>
<attribute name="messenger">input</attribute>
<attribute name="input">Sylius\Bundle\ApiBundle\Command\ChangePaymentMethod</attribute>
<attribute name="denormalization_context">
<attribute name="groups">order:change_payment_method</attribute>
</attribute>
<attribute name="normalization_context">
<attribute name="groups">order:read</attribute>
</attribute>
<attribute name="openapi_context">
<attribute name="summary">Selects payment methods for particular payment</attribute>
<attribute name="parameters">
<attribute>
<attribute name="name">id</attribute>
<attribute name="in">path</attribute>
<attribute name="required">true</attribute>
<attribute name="schema">
<attribute name="type">string</attribute>
</attribute>
</attribute>
<attribute>
<attribute name="name">paymentId</attribute>
<attribute name="in">path</attribute>
<attribute name="required">true</attribute>
<attribute name="schema">
<attribute name="type">string</attribute>
</attribute>
</attribute>
</attribute>
</attribute>
</itemOperation>

<itemOperation name="shop_complete">
<attribute name="method">PATCH</attribute>
<attribute name="path">/shop/orders/{id}/complete</attribute>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,6 @@
<tag name="messenger.message_handler" />
</service>

<service id="Sylius\Bundle\ApiBundle\CommandHandler\ChangePaymentMethodHandler">
<argument type="service" id="sylius.repository.order"/>
<argument type="service" id="sylius.repository.payment_method"/>
<argument type="service" id="sylius.repository.payment"/>
<argument type="service" id="sm.factory" />
<tag name="messenger.message_handler" />
</service>

<service id="Sylius\Bundle\ApiBundle\CommandHandler\Checkout\CompleteOrderHandler">
<argument type="service" id="sylius.repository.order"/>
<argument type="service" id="sm.factory" />
Expand Down
Loading

0 comments on commit 41582c7

Please sign in to comment.