Skip to content

Commit

Permalink
bug #153 Inform about empty billing address after PayPal checkout (Za…
Browse files Browse the repository at this point in the history
…les0123)

This PR was merged into the 1.0-dev branch.

Discussion
----------

It maybe not fixes #139, but somehow handles the problem. We indeed have no place to get the billing address so it should be passed manually in the addressing step (or should be taken from the default customer address).

<img width="1155" alt="Zrzut ekranu 2020-10-29 o 23 44 22" src="https://user-images.githubusercontent.com/6212718/97640272-af86ac80-1a40-11eb-9b77-78e1dc607e63.png">


Commits
-------

c9da7b8 Inform about empty billing address after PayPal checkout
ec68023 Check order billing address missing with twig function
  • Loading branch information
SirDomin authored Nov 6, 2020
2 parents d51d2c4 + ec68023 commit bba8b3d
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 15 deletions.
13 changes: 1 addition & 12 deletions src/Api/CreateOrderApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,6 @@ public function create(string $token, PaymentInterface $payment, string $referen
'payee' => [
'merchant_id' => $config['merchant_id'],
],
'shipping' => [
'name' => ['full_name' => 'John Doe'],
'address' => [
'address_line_1' => 'Test St. 123',
'address_line_2' => '6',
'admin_area_1' => 'CA',
'admin_area_2' => 'New York',
'postal_code' => '32000',
'country_code' => 'US',
],
],
'soft_descriptor' => 'Sylius PayPal Payment',
'items' => $payPalItemData['items'],
],
Expand All @@ -109,7 +98,7 @@ public function create(string $token, PaymentInterface $payment, string $referen
];

$address = $order->getShippingAddress();
if ($address !== null) {
if ($address !== null && $order->isShippingRequired()) {
$data['purchase_units'][0]['shipping'] = [
'name' => ['full_name' => $address->getFullName()],
'address' => [
Expand Down
9 changes: 6 additions & 3 deletions src/Controller/ProcessPayPalOrderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,12 @@ public function __invoke(Request $request): Response
} else {
$address->setFirstName($customer->getFirstName());
$address->setLastName($customer->getLastName());
$address->setStreet('');
$address->setCity('');
$address->setPostcode('');

$defaultAddress = $customer->getDefaultAddress();

$address->setStreet($defaultAddress ? $defaultAddress->getStreet() : '');
$address->setCity($defaultAddress ? $defaultAddress->getCity() : '');
$address->setPostcode($defaultAddress ? $defaultAddress->getPostcode() : '');
$address->setCountryCode($data['payer']['address']['country_code']);
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_ADDRESS);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -260,5 +260,9 @@
<service id="Sylius\PayPalPlugin\Twig\PayPalExtension">
<tag name="twig.extension" />
</service>

<service id="Sylius\PayPalPlugin\Twig\OrderAddressExtension">
<tag name="twig.extension" />
</service>
</services>
</container>
2 changes: 2 additions & 0 deletions src/Resources/translations/messages.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ sylius:
sftp_username: 'SFTP Username'
share_data_consent_confirmation: "By clicking Yes, you accept PayPal share data consent"
tender_type: 'Refunded to the PayPal wallet'
missing_billing_address_header: 'We could not fetch any billing address from PayPal'
missing_billing_address_content: 'Please, go back to the Addressing step and complete it'
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<div class="ui segment">
<div class="ui {% if order.isShippingRequired() %}two{% else %}one{% endif %} column divided stackable grid">
<div class="column" id="sylius-billing-address" {{ sylius_test_html_attribute('billing-address') }}>
<div class="ui small dividing header">{{ 'sylius.ui.billing_address'|trans }}</div>
{% include '@SyliusShop/Common/_address.html.twig' with {'address': order.billingAddress} %}
</div>
{% if order.isShippingRequired() %}
<div class="column" id="sylius-shipping-address" {{ sylius_test_html_attribute('shipping-address') }}>
<div class="ui small dividing header">{{ 'sylius.ui.shipping_address'|trans }}</div>
{% include '@SyliusShop/Common/_address.html.twig' with {'address': order.shippingAddress} %}
</div>
{% endif %}
</div>
</div>
{% if sylius_is_billing_address_missing(order) %}
<div class="ui icon message">
<i class="address card icon"></i>
<div class="content">
<div class="header">{{ 'sylius.pay_pal.missing_billing_address_header'|trans }}</div>
<p>{{ 'sylius.pay_pal.missing_billing_address_content'|trans }}</p>
</div>
</div>
{% endif %}
33 changes: 33 additions & 0 deletions src/Twig/OrderAddressExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Sylius\PayPalPlugin\Twig;

use Sylius\Component\Core\Model\AddressInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

final class OrderAddressExtension extends AbstractExtension
{
public function getFunctions(): array
{
return [
new TwigFunction('sylius_is_billing_address_missing', [$this, 'isBillingAddressMissing']),
];
}

public function isBillingAddressMissing(OrderInterface $order): bool
{
/** @var AddressInterface $billingAddress */
$billingAddress = $order->getBillingAddress();

return
!$order->isShippingRequired() &&
$billingAddress->getStreet() === '' &&
$billingAddress->getPostcode() === '' &&
$billingAddress->getCity() === ''
;
}
}

0 comments on commit bba8b3d

Please sign in to comment.