Skip to content

Commit eca96f1

Browse files
authored
feature #65 Update order address if changed during completion (SirDomin, Zales0123)
This PR was merged into the 1.0-dev branch. Discussion ---------- As webhooks arent working yet, it changes address when completing the order Commits ------- 2ae1fff provder + spec e824c24 code standards 147e04c webhook removed, address is changed while completing order 9a04e78 removed unused service 592f05c Improve PayPalAddressProcessor specs ddfc5fc Fix typo in javascript resulting failing payment-page payment functionality
2 parents ad963dd + ddfc5fc commit eca96f1

File tree

9 files changed

+170
-2
lines changed

9 files changed

+170
-2
lines changed

psalm.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<MixedArgument>
3030
<errorLevel type="info">
3131
<file name="src/Controller/ProcessPayPalOrderAction.php"/>
32+
<file name="src/Payum/Action/CompleteOrderAction.php"/>
3233
</errorLevel>
3334
</MixedArgument>
3435

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace spec\Sylius\PayPalPlugin\Processor;
6+
7+
use Doctrine\Persistence\ObjectManager;
8+
use PhpSpec\ObjectBehavior;
9+
use Sylius\Component\Core\Model\AddressInterface;
10+
use Sylius\Component\Core\Model\OrderInterface;
11+
use Sylius\PayPalPlugin\Processor\PayPalAddressProcessorInterface;
12+
13+
final class PayPalAddressProcessorSpec extends ObjectBehavior
14+
{
15+
function let(ObjectManager $objectManager): void
16+
{
17+
$this->beConstructedWith($objectManager);
18+
}
19+
20+
function it_implements_pay_pal_address_processor_interface(): void
21+
{
22+
$this->shouldImplement(PayPalAddressProcessorInterface::class);
23+
}
24+
25+
function it_updates_order_address(
26+
OrderInterface $order,
27+
AddressInterface $orderAddress,
28+
ObjectManager $objectManager
29+
): void {
30+
$order->getShippingAddress()->willReturn($orderAddress);
31+
32+
$orderAddress->setCity('New York')->shouldBeCalled();
33+
$orderAddress->setStreet('Main St. 123')->shouldBeCalled();
34+
$orderAddress->setPostcode('10001')->shouldBeCalled();
35+
$orderAddress->setCountryCode('US')->shouldBeCalled();
36+
37+
$objectManager->flush()->shouldBeCalled();
38+
39+
$this->process(
40+
[
41+
'address_line_1' => 'Main St. 123',
42+
'admin_area_2' => 'New York',
43+
'postal_code' => '10001',
44+
'country_code' => 'US',
45+
],
46+
$order
47+
);
48+
}
49+
50+
function it_updates_order_address_with_two_address_lines(
51+
OrderInterface $order,
52+
AddressInterface $orderAddress,
53+
ObjectManager $objectManager
54+
): void {
55+
$order->getShippingAddress()->willReturn($orderAddress);
56+
57+
$orderAddress->setCity('New York')->shouldBeCalled();
58+
$orderAddress->setStreet('Main St. 123')->shouldBeCalled();
59+
$orderAddress->setPostcode('10001')->shouldBeCalled();
60+
$orderAddress->setCountryCode('US')->shouldBeCalled();
61+
62+
$objectManager->flush()->shouldBeCalled();
63+
64+
$this->process(
65+
[
66+
'address_line_1' => 'Main St.',
67+
'address_line_2' => '123',
68+
'admin_area_2' => 'New York',
69+
'postal_code' => '10001',
70+
'country_code' => 'US',
71+
],
72+
$order
73+
);
74+
}
75+
76+
function it_throws_an_exception_if_address_data_is_missing(
77+
OrderInterface $order,
78+
AddressInterface $orderAddress,
79+
ObjectManager $objectManager
80+
): void {
81+
$order->getShippingAddress()->willReturn($orderAddress);
82+
83+
$objectManager->flush()->shouldNotBeCalled();
84+
$this->shouldThrow(\InvalidArgumentException::class)->during('process', [[], $order]);
85+
}
86+
}

src/Payum/Action/CompleteOrderAction.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Sylius\PayPalPlugin\Api\CompleteOrderApiInterface;
2424
use Sylius\PayPalPlugin\Api\UpdateOrderApiInterface;
2525
use Sylius\PayPalPlugin\Payum\Request\CompleteOrder;
26+
use Sylius\PayPalPlugin\Processor\PayPalAddressProcessor;
2627

2728
final class CompleteOrderAction implements ActionInterface
2829
{
@@ -35,14 +36,19 @@ final class CompleteOrderAction implements ActionInterface
3536
/** @var CompleteOrderApiInterface */
3637
private $completeOrderApi;
3738

39+
/** @var PayPalAddressProcessor */
40+
private $payPalAddressProcessor;
41+
3842
public function __construct(
3943
AuthorizeClientApiInterface $authorizeClientApi,
4044
UpdateOrderApiInterface $updateOrderApi,
41-
CompleteOrderApiInterface $completeOrderApi
45+
CompleteOrderApiInterface $completeOrderApi,
46+
PayPalAddressProcessor $payPalAddressProcessor
4247
) {
4348
$this->authorizeClientApi = $authorizeClientApi;
4449
$this->updateOrderApi = $updateOrderApi;
4550
$this->completeOrderApi = $completeOrderApi;
51+
$this->payPalAddressProcessor = $payPalAddressProcessor;
4652
}
4753

4854
/** @param CompleteOrder $request */
@@ -86,6 +92,8 @@ public function execute($request): void
8692
'paypal_order_id' => $content['id'],
8793
'paypal_payment_id' => $content['purchase_units'][0]['payments']['captures'][0]['id'],
8894
]);
95+
96+
$this->payPalAddressProcessor->process($content['purchase_units'][0]['shipping']['address'], $order);
8997
}
9098
}
9199

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sylius\PayPalPlugin\Processor;
6+
7+
use Doctrine\Persistence\ObjectManager;
8+
use Sylius\Component\Core\Model\AddressInterface;
9+
use Sylius\Component\Core\Model\OrderInterface;
10+
use Webmozart\Assert\Assert;
11+
12+
final class PayPalAddressProcessor implements PayPalAddressProcessorInterface
13+
{
14+
/** @var ObjectManager */
15+
private $objectManager;
16+
17+
public function __construct(ObjectManager $objectManager)
18+
{
19+
$this->objectManager = $objectManager;
20+
}
21+
22+
/**
23+
* @param array<string, string> $address
24+
*/
25+
public function process(array $address, OrderInterface $order): void
26+
{
27+
/** @var AddressInterface $orderAddress */
28+
$orderAddress = $order->getShippingAddress();
29+
30+
Assert::keyExists($address, 'admin_area_2');
31+
Assert::keyExists($address, 'address_line_1');
32+
Assert::keyExists($address, 'postal_code');
33+
Assert::keyExists($address, 'country_code');
34+
35+
$street = $address['address_line_1'] . (isset($address['address_line_2']) ? ' ' . $address['address_line_2'] : '');
36+
37+
$orderAddress->setCity($address['admin_area_2']);
38+
$orderAddress->setStreet($street);
39+
$orderAddress->setPostcode($address['postal_code']);
40+
$orderAddress->setCountryCode($address['country_code']);
41+
42+
$this->objectManager->flush();
43+
}
44+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sylius\PayPalPlugin\Processor;
6+
7+
use Sylius\Component\Core\Model\OrderInterface;
8+
9+
interface PayPalAddressProcessorInterface
10+
{
11+
/**
12+
* @param array<string, string> $address
13+
*/
14+
public function process(array $address, OrderInterface $order): void;
15+
}

src/Resources/config/services.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@
5555
<argument type="service" id="sylius.context.channel" />
5656
</service>
5757

58+
<service
59+
id="Sylius\PayPalPlugin\Provider\PayPalOrderProviderInterface"
60+
class="Sylius\PayPalPlugin\Provider\PayPalOrderProvider"
61+
>
62+
<argument type="service" id="Sylius\PayPalPlugin\Provider\PaymentProviderInterface" />
63+
</service>
64+
5865
<service id="Sylius\PayPalPlugin\Resolver\CapturePaymentResolverInterface" class="Sylius\PayPalPlugin\Resolver\CapturePaymentResolver">
5966
<argument type="service" id="payum" />
6067
</service>
@@ -146,5 +153,9 @@
146153
<argument>%sylius.form.type.checkout_select_payment.validation_groups%</argument>
147154
<tag name="form.type" />
148155
</service>
156+
157+
<service id="Sylius\PayPalPlugin\Processor\PayPalAddressProcessor">
158+
<argument type="service" id="doctrine.orm.entity_manager" />
159+
</service>
149160
</services>
150161
</container>

src/Resources/config/services/controller.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
<argument type="service" id="Sylius\PayPalPlugin\Manager\PaymentStateManagerInterface" />
2929
<argument type="service" id="router" />
3030
<argument type="service" id="Sylius\PayPalPlugin\Provider\OrderProviderInterface" />
31+
<argument type="service" id="Sylius\PayPalPlugin\Api\AuthorizeClientApiInterface" />
32+
<argument type="service" id="Sylius\PayPalPlugin\Api\CompleteOrderApiInterface" />
3133
</service>
3234

3335
<service id="Sylius\PayPalPlugin\Controller\CreatePayPalOrderFromPaymentPageAction" public="true">

src/Resources/config/services/payum.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<argument type="service" id="Sylius\PayPalPlugin\Api\AuthorizeClientApiInterface" />
2525
<argument type="service" id="Sylius\PayPalPlugin\Api\UpdateOrderApiInterface" />
2626
<argument type="service" id="Sylius\PayPalPlugin\Api\CompleteOrderApiInterface" />
27+
<argument type="service" id="Sylius\PayPalPlugin\Processor\PayPalAddressProcessor" />
2728
<tag name="payum.action" factory="sylius.pay_pal" alias="payum.action.complete_order" />
2829
</service>
2930

src/Resources/views/payFromPaymentPage.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
method: 'post'
2121
}).then(function(res) {
2222
return res.status === 400 ? window.location.reload() : res.json();
23-
}).then(data => data.orderID);
23+
}).then(data => data.order_id);
2424
},
2525
onApprove: function(data, actions) {
2626
return fetch(completePayPalOrderFromPaymentPageUrl, {

0 commit comments

Comments
 (0)