Skip to content

Commit

Permalink
feature #65 Update order address if changed during completion (SirDom…
Browse files Browse the repository at this point in the history
…in, 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
  • Loading branch information
Zales0123 authored Aug 31, 2020
2 parents ad963dd + ddfc5fc commit eca96f1
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 2 deletions.
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<MixedArgument>
<errorLevel type="info">
<file name="src/Controller/ProcessPayPalOrderAction.php"/>
<file name="src/Payum/Action/CompleteOrderAction.php"/>
</errorLevel>
</MixedArgument>

Expand Down
86 changes: 86 additions & 0 deletions spec/Processor/PayPalAddressProcessorSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

namespace spec\Sylius\PayPalPlugin\Processor;

use Doctrine\Persistence\ObjectManager;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\AddressInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\PayPalPlugin\Processor\PayPalAddressProcessorInterface;

final class PayPalAddressProcessorSpec extends ObjectBehavior
{
function let(ObjectManager $objectManager): void
{
$this->beConstructedWith($objectManager);
}

function it_implements_pay_pal_address_processor_interface(): void
{
$this->shouldImplement(PayPalAddressProcessorInterface::class);
}

function it_updates_order_address(
OrderInterface $order,
AddressInterface $orderAddress,
ObjectManager $objectManager
): void {
$order->getShippingAddress()->willReturn($orderAddress);

$orderAddress->setCity('New York')->shouldBeCalled();
$orderAddress->setStreet('Main St. 123')->shouldBeCalled();
$orderAddress->setPostcode('10001')->shouldBeCalled();
$orderAddress->setCountryCode('US')->shouldBeCalled();

$objectManager->flush()->shouldBeCalled();

$this->process(
[
'address_line_1' => 'Main St. 123',
'admin_area_2' => 'New York',
'postal_code' => '10001',
'country_code' => 'US',
],
$order
);
}

function it_updates_order_address_with_two_address_lines(
OrderInterface $order,
AddressInterface $orderAddress,
ObjectManager $objectManager
): void {
$order->getShippingAddress()->willReturn($orderAddress);

$orderAddress->setCity('New York')->shouldBeCalled();
$orderAddress->setStreet('Main St. 123')->shouldBeCalled();
$orderAddress->setPostcode('10001')->shouldBeCalled();
$orderAddress->setCountryCode('US')->shouldBeCalled();

$objectManager->flush()->shouldBeCalled();

$this->process(
[
'address_line_1' => 'Main St.',
'address_line_2' => '123',
'admin_area_2' => 'New York',
'postal_code' => '10001',
'country_code' => 'US',
],
$order
);
}

function it_throws_an_exception_if_address_data_is_missing(
OrderInterface $order,
AddressInterface $orderAddress,
ObjectManager $objectManager
): void {
$order->getShippingAddress()->willReturn($orderAddress);

$objectManager->flush()->shouldNotBeCalled();
$this->shouldThrow(\InvalidArgumentException::class)->during('process', [[], $order]);
}
}
10 changes: 9 additions & 1 deletion src/Payum/Action/CompleteOrderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Sylius\PayPalPlugin\Api\CompleteOrderApiInterface;
use Sylius\PayPalPlugin\Api\UpdateOrderApiInterface;
use Sylius\PayPalPlugin\Payum\Request\CompleteOrder;
use Sylius\PayPalPlugin\Processor\PayPalAddressProcessor;

final class CompleteOrderAction implements ActionInterface
{
Expand All @@ -35,14 +36,19 @@ final class CompleteOrderAction implements ActionInterface
/** @var CompleteOrderApiInterface */
private $completeOrderApi;

/** @var PayPalAddressProcessor */
private $payPalAddressProcessor;

public function __construct(
AuthorizeClientApiInterface $authorizeClientApi,
UpdateOrderApiInterface $updateOrderApi,
CompleteOrderApiInterface $completeOrderApi
CompleteOrderApiInterface $completeOrderApi,
PayPalAddressProcessor $payPalAddressProcessor
) {
$this->authorizeClientApi = $authorizeClientApi;
$this->updateOrderApi = $updateOrderApi;
$this->completeOrderApi = $completeOrderApi;
$this->payPalAddressProcessor = $payPalAddressProcessor;
}

/** @param CompleteOrder $request */
Expand Down Expand Up @@ -86,6 +92,8 @@ public function execute($request): void
'paypal_order_id' => $content['id'],
'paypal_payment_id' => $content['purchase_units'][0]['payments']['captures'][0]['id'],
]);

$this->payPalAddressProcessor->process($content['purchase_units'][0]['shipping']['address'], $order);
}
}

Expand Down
44 changes: 44 additions & 0 deletions src/Processor/PayPalAddressProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Sylius\PayPalPlugin\Processor;

use Doctrine\Persistence\ObjectManager;
use Sylius\Component\Core\Model\AddressInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Webmozart\Assert\Assert;

final class PayPalAddressProcessor implements PayPalAddressProcessorInterface
{
/** @var ObjectManager */
private $objectManager;

public function __construct(ObjectManager $objectManager)
{
$this->objectManager = $objectManager;
}

/**
* @param array<string, string> $address
*/
public function process(array $address, OrderInterface $order): void
{
/** @var AddressInterface $orderAddress */
$orderAddress = $order->getShippingAddress();

Assert::keyExists($address, 'admin_area_2');
Assert::keyExists($address, 'address_line_1');
Assert::keyExists($address, 'postal_code');
Assert::keyExists($address, 'country_code');

$street = $address['address_line_1'] . (isset($address['address_line_2']) ? ' ' . $address['address_line_2'] : '');

$orderAddress->setCity($address['admin_area_2']);
$orderAddress->setStreet($street);
$orderAddress->setPostcode($address['postal_code']);
$orderAddress->setCountryCode($address['country_code']);

$this->objectManager->flush();
}
}
15 changes: 15 additions & 0 deletions src/Processor/PayPalAddressProcessorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Sylius\PayPalPlugin\Processor;

use Sylius\Component\Core\Model\OrderInterface;

interface PayPalAddressProcessorInterface
{
/**
* @param array<string, string> $address
*/
public function process(array $address, OrderInterface $order): void;
}
11 changes: 11 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@
<argument type="service" id="sylius.context.channel" />
</service>

<service
id="Sylius\PayPalPlugin\Provider\PayPalOrderProviderInterface"
class="Sylius\PayPalPlugin\Provider\PayPalOrderProvider"
>
<argument type="service" id="Sylius\PayPalPlugin\Provider\PaymentProviderInterface" />
</service>

<service id="Sylius\PayPalPlugin\Resolver\CapturePaymentResolverInterface" class="Sylius\PayPalPlugin\Resolver\CapturePaymentResolver">
<argument type="service" id="payum" />
</service>
Expand Down Expand Up @@ -146,5 +153,9 @@
<argument>%sylius.form.type.checkout_select_payment.validation_groups%</argument>
<tag name="form.type" />
</service>

<service id="Sylius\PayPalPlugin\Processor\PayPalAddressProcessor">
<argument type="service" id="doctrine.orm.entity_manager" />
</service>
</services>
</container>
2 changes: 2 additions & 0 deletions src/Resources/config/services/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
<argument type="service" id="Sylius\PayPalPlugin\Manager\PaymentStateManagerInterface" />
<argument type="service" id="router" />
<argument type="service" id="Sylius\PayPalPlugin\Provider\OrderProviderInterface" />
<argument type="service" id="Sylius\PayPalPlugin\Api\AuthorizeClientApiInterface" />
<argument type="service" id="Sylius\PayPalPlugin\Api\CompleteOrderApiInterface" />
</service>

<service id="Sylius\PayPalPlugin\Controller\CreatePayPalOrderFromPaymentPageAction" public="true">
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services/payum.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<argument type="service" id="Sylius\PayPalPlugin\Api\AuthorizeClientApiInterface" />
<argument type="service" id="Sylius\PayPalPlugin\Api\UpdateOrderApiInterface" />
<argument type="service" id="Sylius\PayPalPlugin\Api\CompleteOrderApiInterface" />
<argument type="service" id="Sylius\PayPalPlugin\Processor\PayPalAddressProcessor" />
<tag name="payum.action" factory="sylius.pay_pal" alias="payum.action.complete_order" />
</service>

Expand Down
2 changes: 1 addition & 1 deletion src/Resources/views/payFromPaymentPage.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
method: 'post'
}).then(function(res) {
return res.status === 400 ? window.location.reload() : res.json();
}).then(data => data.orderID);
}).then(data => data.order_id);
},
onApprove: function(data, actions) {
return fetch(completePayPalOrderFromPaymentPageUrl, {
Expand Down

0 comments on commit eca96f1

Please sign in to comment.