Skip to content

Commit

Permalink
[API] refactor choose payment method chandler
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamKasp committed Oct 14, 2020
1 parent 7b70a69 commit 759fcc1
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 365 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Feature: Changing the method after order confirmation
And the store allows paying "Cash on delivery"
And the store has a product "PHP T-Shirt" priced at "$19.99"
And the store ships everywhere for free
And there is an administrator "sylius@example.com"

@ui
Scenario: Retrying the payment with offline payment
Expand Down
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
2 changes: 0 additions & 2 deletions src/Sylius/Behat/Context/Api/Shop/OrderContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,6 @@ public function iShouldHaveChosenPaymentMethodForMyOrder(PaymentMethod $paymentM
$this->responseChecker->getValue($this->client->show($this->sharedStorage->get('cart_token')), 'payments')[0]
);

$paymeth1 = $this->iriConverter->getIriFromItem($paymentMethod);
$payMeth = $this->iriConverter->getIriFromItem($payment->getMethod());
Assert::same(
$this->iriConverter->getIriFromItem($paymentMethod),
$this->iriConverter->getIriFromItem($payment->getMethod())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ default:
- sylius.behat.context.setup.product
- sylius.behat.context.setup.shipping
- sylius.behat.context.setup.shop_api_security
# only for now, should be deleted
- sylius.behat.context.setup.admin_user

- sylius.behat.context.api.shop.cart
- sylius.behat.context.api.shop.checkout
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,10 +17,12 @@
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 Sylius\Component\Payment\Model\PaymentInterface;
use Webmozart\Assert\Assert;

/** @experimental */
Expand Down Expand Up @@ -57,13 +59,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 @@ -73,8 +68,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 @@ -67,7 +67,7 @@ private function applyUserRulesToItem(
string $httpRequestMethodType,
string $operationName
): void {
if ($user === null && $operationName === 'shop_change_payment_method') {
if ($user === null && $operationName === 'shop_select_payment_method') {
$queryBuilder
->leftJoin(sprintf('%s.customer', $rootAlias), 'customer')
->leftJoin('customer.user', 'user')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,41 +232,6 @@
<attribute name="summary">Changes quantity of order item</attribute>
</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>

</itemOperations>

<subresourceOperations>
Expand Down

This file was deleted.

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

<service id="Sylius\Bundle\ApiBundle\CommandHandler\ChangePaymentMethodHandler">
<argument type="service" id="sylius.repository.payment_method"/>
<argument type="service" id="sylius.repository.payment"/>
<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 759fcc1

Please sign in to comment.