diff --git a/psalm.xml b/psalm.xml index 426c506d..1d6e59e2 100644 --- a/psalm.xml +++ b/psalm.xml @@ -29,6 +29,7 @@ + diff --git a/spec/Processor/PayPalAddressProcessorSpec.php b/spec/Processor/PayPalAddressProcessorSpec.php new file mode 100644 index 00000000..5bd3cf24 --- /dev/null +++ b/spec/Processor/PayPalAddressProcessorSpec.php @@ -0,0 +1,86 @@ +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]); + } +} diff --git a/src/Payum/Action/CompleteOrderAction.php b/src/Payum/Action/CompleteOrderAction.php index 70fce659..a82f3228 100644 --- a/src/Payum/Action/CompleteOrderAction.php +++ b/src/Payum/Action/CompleteOrderAction.php @@ -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 { @@ -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 */ @@ -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); } } diff --git a/src/Processor/PayPalAddressProcessor.php b/src/Processor/PayPalAddressProcessor.php new file mode 100644 index 00000000..defb813b --- /dev/null +++ b/src/Processor/PayPalAddressProcessor.php @@ -0,0 +1,44 @@ +objectManager = $objectManager; + } + + /** + * @param array $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(); + } +} diff --git a/src/Processor/PayPalAddressProcessorInterface.php b/src/Processor/PayPalAddressProcessorInterface.php new file mode 100644 index 00000000..bd87fadd --- /dev/null +++ b/src/Processor/PayPalAddressProcessorInterface.php @@ -0,0 +1,15 @@ + $address + */ + public function process(array $address, OrderInterface $order): void; +} diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index eb92f12e..b7de9e7e 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -55,6 +55,13 @@ + + + + @@ -146,5 +153,9 @@ %sylius.form.type.checkout_select_payment.validation_groups% + + + + diff --git a/src/Resources/config/services/controller.xml b/src/Resources/config/services/controller.xml index df16700c..cf6afbb3 100644 --- a/src/Resources/config/services/controller.xml +++ b/src/Resources/config/services/controller.xml @@ -28,6 +28,8 @@ + + diff --git a/src/Resources/config/services/payum.xml b/src/Resources/config/services/payum.xml index 9b591168..82e17876 100644 --- a/src/Resources/config/services/payum.xml +++ b/src/Resources/config/services/payum.xml @@ -24,6 +24,7 @@ + diff --git a/src/Resources/views/payFromPaymentPage.html.twig b/src/Resources/views/payFromPaymentPage.html.twig index c4187bb7..6443ff76 100644 --- a/src/Resources/views/payFromPaymentPage.html.twig +++ b/src/Resources/views/payFromPaymentPage.html.twig @@ -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, {