Skip to content

Commit 0d25d47

Browse files
GSadeeZales0123
authored andcommitted
[API] Implement managing payment state feature
1 parent d305957 commit 0d25d47

File tree

10 files changed

+141
-6
lines changed

10 files changed

+141
-6
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@
222222
"spec\\Sylius\\Bundle\\AddressingBundle\\": "src/Sylius/Bundle/AddressingBundle/spec/",
223223
"spec\\Sylius\\Bundle\\AdminApiBundle\\": "src/Sylius/Bundle/AdminApiBundle/spec/",
224224
"spec\\Sylius\\Bundle\\AdminBundle\\": "src/Sylius/Bundle/AdminBundle/spec/",
225+
"spec\\Sylius\\Bundle\\ApiBundle\\": "src/Sylius/Bundle/ApiBundle/spec/",
225226
"spec\\Sylius\\Bundle\\AttributeBundle\\": "src/Sylius/Bundle/AttributeBundle/spec/",
226227
"spec\\Sylius\\Bundle\\ChannelBundle\\": "src/Sylius/Bundle/ChannelBundle/spec/",
227228
"spec\\Sylius\\Bundle\\CoreBundle\\": "src/Sylius/Bundle/CoreBundle/spec/",

features/payment/managing_payments/managing_of_payment_status.feature

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ Feature: Completing a payment from its list
99
And the store ships everywhere for free
1010
And the store has a product "Apple"
1111
And the store allows paying with "Cash on Delivery"
12-
And there is a customer "[email protected]" that placed an order "#00000001" in channel "United States"
13-
And there is a "New" "#00000001" order with "Apple" product
12+
And there is an "#00000001" order with "Apple" product
1413
And I am logged in as an administrator
1514

16-
@ui
15+
@ui @api
1716
Scenario: Completing a payment from payments index
1817
When I browse payments
1918
And I complete the payment of order "#00000001"

phpspec.yml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ suites:
2020
AddressingBundle: { namespace: Sylius\Bundle\AddressingBundle, psr4_prefix: Sylius\Bundle\AddressingBundle, spec_path: src/Sylius/Bundle/AddressingBundle, src_path: src/Sylius/Bundle/AddressingBundle }
2121
AdminBundle: { namespace: Sylius\Bundle\AdminBundle, psr4_prefix: Sylius\Bundle\AdminBundle, spec_path: src/Sylius/Bundle/AdminBundle, src_path: src/Sylius/Bundle/AdminBundle }
2222
AdminApiBundle: { namespace: Sylius\Bundle\AdminApiBundle, psr4_prefix: Sylius\Bundle\AdminApiBundle, spec_path: src/Sylius/Bundle/AdminApiBundle, src_path: src/Sylius/Bundle/AdminApiBundle }
23+
ApiBundle: { namespace: Sylius\Bundle\ApiBundle, psr4_prefix: Sylius\Bundle\ApiBundle, spec_path: src/Sylius/Bundle/ApiBundle, src_path: src/Sylius/Bundle/ApiBundle }
2324
AttributeBundle: { namespace: Sylius\Bundle\AttributeBundle, psr4_prefix: Sylius\Bundle\AttributeBundle, spec_path: src/Sylius/Bundle/AttributeBundle, src_path: src/Sylius/Bundle/AttributeBundle }
2425
ChannelBundle: { namespace: Sylius\Bundle\ChannelBundle, psr4_prefix: Sylius\Bundle\ChannelBundle, spec_path: src/Sylius/Bundle/ChannelBundle, src_path: src/Sylius/Bundle/ChannelBundle }
2526
CoreBundle: { namespace: Sylius\Bundle\CoreBundle, psr4_prefix: Sylius\Bundle\CoreBundle, spec_path: src/Sylius/Bundle/CoreBundle, src_path: src/Sylius/Bundle/CoreBundle }

src/Sylius/Behat/Context/Api/Admin/ManagingPaymentsContext.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Sylius\Behat\Client\ApiClientInterface;
1818
use Sylius\Component\Core\Formatter\StringInflector;
1919
use Sylius\Component\Core\Model\CustomerInterface;
20+
use Sylius\Component\Core\Model\OrderInterface;
21+
use Sylius\Component\Payment\PaymentTransitions;
2022
use Webmozart\Assert\Assert;
2123

2224
final class ManagingPaymentsContext implements Context
@@ -38,6 +40,21 @@ public function iAmBrowsingPayments(): void
3840
$this->client->index('payments');
3941
}
4042

43+
/**
44+
* @When I complete the payment of order :order
45+
*/
46+
public function iCompleteThePaymentOfOrder(OrderInterface $order): void
47+
{
48+
$payment = $order->getLastPayment();
49+
Assert::notNull($payment);
50+
51+
$this->client->applyTransition(
52+
'payments',
53+
(string) $payment->getId(),
54+
PaymentTransitions::TRANSITION_COMPLETE
55+
);
56+
}
57+
4158
/**
4259
* @Then I should see a single payment in the list
4360
* @Then I should see :count payments in the list
@@ -77,4 +94,24 @@ public function iShouldSeePaymentForTheOrderInTheList(string $orderNumber, int $
7794
$this->client->hasItemOnPositionWithValue($position - 1, 'order', sprintf('/new-api/orders/%s', $orderNumber))
7895
);
7996
}
97+
98+
/**
99+
* @Then I should be notified that the payment has been completed
100+
*/
101+
public function iShouldBeNotifiedThatThePaymentHasBeenCompleted(): void
102+
{
103+
Assert::true($this->client->isUpdateSuccessful());
104+
}
105+
106+
/**
107+
* @Then I should see the payment of order :order as :paymentState
108+
*/
109+
public function iShouldSeeThePaymentOfOrderAs(OrderInterface $order, string $paymentState): void
110+
{
111+
$payment = $order->getLastPayment();
112+
Assert::notNull($payment);
113+
114+
$this->client->show('payments', (string) $payment->getId());
115+
Assert::true($this->client->responseHasValue('state', StringInflector::nameToLowercaseCode($paymentState)));
116+
}
80117
}

src/Sylius/Behat/Resources/config/suites/api/payment/managing_payments.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ default:
77
contexts:
88
- sylius.behat.context.hook.doctrine_orm
99

10+
- sylius.behat.context.transform.channel
1011
- sylius.behat.context.transform.customer
1112
- sylius.behat.context.transform.order
1213
- sylius.behat.context.transform.product
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Paweł Jędrzejewski
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\Bundle\ApiBundle\Applicator;
15+
16+
use SM\Factory\FactoryInterface as StateMachineFactoryInterface;
17+
use Sylius\Component\Payment\Model\PaymentInterface;
18+
use Sylius\Component\Payment\PaymentTransitions;
19+
20+
final class PaymentStateMachineTransitionApplicator
21+
{
22+
/** @var StateMachineFactoryInterface $stateMachineFactory */
23+
private $stateMachineFactory;
24+
25+
public function __construct(StateMachineFactoryInterface $stateMachineFactory)
26+
{
27+
$this->stateMachineFactory = $stateMachineFactory;
28+
}
29+
30+
public function complete(PaymentInterface $data): PaymentInterface
31+
{
32+
$this->applyTransition($data, PaymentTransitions::TRANSITION_COMPLETE);
33+
34+
return $data;
35+
}
36+
37+
private function applyTransition(PaymentInterface $payment, string $transition): void
38+
{
39+
$stateMachine = $this->stateMachineFactory->get($payment, PaymentTransitions::GRAPH);
40+
$stateMachine->apply($transition);
41+
}
42+
}

src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Payment.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,21 @@
3030

3131
<itemOperations>
3232
<itemOperation name="get" />
33+
<itemOperation name="complete_payment">
34+
<attribute name="method">PATCH</attribute>
35+
<attribute name="path">/payments/{id}/complete</attribute>
36+
<attribute name="controller">sylius.api.payment_state_machine_transition_applicator:complete</attribute>
37+
</itemOperation>
3338
</itemOperations>
3439

3540
<property name="id" identifier="true" writable="false" />
3641
<property name="createdAt" writable="false" />
3742
<property name="updatedAt" writable="false" />
43+
<property name="method" writable="false" />
44+
<property name="currencyCode" writable="false" />
45+
<property name="amount" writable="false" />
46+
<property name="state" writable="false" />
47+
<property name="details" writable="false" />
48+
<property name="order" writable="false" />
3849
</resource>
3950
</resources>

src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/ProductReview.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
<itemOperation name="accept_product_review">
3434
<attribute name="method">PATCH</attribute>
3535
<attribute name="path">/product_reviews/{id}/accept</attribute>
36-
<attribute name="controller">sylius.api.state_machine_transition_applicator:accept</attribute>
36+
<attribute name="controller">sylius.api.product_review_state_machine_transition_applicator:accept</attribute>
3737
</itemOperation>
3838
<itemOperation name="reject_product_review">
3939
<attribute name="method">PATCH</attribute>
4040
<attribute name="path">/product_reviews/{id}/reject</attribute>
41-
<attribute name="controller">sylius.api.state_machine_transition_applicator:reject</attribute>
41+
<attribute name="controller">sylius.api.product_review_state_machine_transition_applicator:reject</attribute>
4242
</itemOperation>
4343
</itemOperations>
4444
<property name="createdAt" writable="false" />

src/Sylius/Bundle/ApiBundle/Resources/config/services.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
</imports>
1818

1919
<services>
20-
<service id="sylius.api.state_machine_transition_applicator" class="Sylius\Bundle\ApiBundle\Applicator\ProductReviewStateMachineTransitionApplicator" public="true">
20+
<service id="sylius.api.payment_state_machine_transition_applicator" class="Sylius\Bundle\ApiBundle\Applicator\PaymentStateMachineTransitionApplicator" public="true">
21+
<argument id="sm.factory" type="service" />
22+
</service>
23+
24+
<service id="sylius.api.product_review_state_machine_transition_applicator" class="Sylius\Bundle\ApiBundle\Applicator\ProductReviewStateMachineTransitionApplicator" public="true">
2125
<argument id="sm.factory" type="service" />
2226
</service>
2327
</services>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Paweł Jędrzejewski
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace spec\Sylius\Bundle\ApiBundle\Applicator;
15+
16+
use PhpSpec\ObjectBehavior;
17+
use SM\Factory\FactoryInterface as StateMachineFactoryInterface;
18+
use SM\StateMachine\StateMachine;
19+
use Sylius\Component\Core\Model\PaymentInterface;
20+
use Sylius\Component\Payment\PaymentTransitions;
21+
22+
final class PaymentStateMachineTransitionApplicatorSpec extends ObjectBehavior
23+
{
24+
function let(StateMachineFactoryInterface $stateMachineFactory)
25+
{
26+
$this->beConstructedWith($stateMachineFactory);
27+
}
28+
29+
function it_completes_payment(
30+
StateMachineFactoryInterface $stateMachineFactory,
31+
PaymentInterface $payment,
32+
StateMachine $stateMachine
33+
): void {
34+
$stateMachineFactory->get($payment, PaymentTransitions::GRAPH)->willReturn($stateMachine);
35+
$stateMachine->apply(PaymentTransitions::TRANSITION_COMPLETE)->shouldBeCalled();
36+
37+
$this->complete($payment);
38+
}
39+
}

0 commit comments

Comments
 (0)