mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Use address from PayPal as preset in checkout PCP-28
This commit is contained in:
parent
ad61709660
commit
9fe6ee4fb7
3 changed files with 141 additions and 1 deletions
|
@ -9,6 +9,7 @@ use Inpsyde\PayPalCommerce\ApiClient\Entity\ApplicationContext;
|
|||
use Inpsyde\PayPalCommerce\Onboarding\State;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Admin\OrderTablePaymentStatusColumn;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Admin\PaymentStatusOrderDetail;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Checkout\CheckoutPayPalAddressPreset;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Checkout\DisableGateways;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Gateway\WcGateway;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
|
||||
|
@ -544,4 +545,11 @@ return [
|
|||
],
|
||||
];
|
||||
},
|
||||
|
||||
'wcgateway.checkout.address-preset' => static function(ContainerInterface $container): CheckoutPayPalAddressPreset {
|
||||
|
||||
return new CheckoutPayPalAddressPreset(
|
||||
$container->get('session.handler')
|
||||
);
|
||||
},
|
||||
];
|
||||
|
|
|
@ -0,0 +1,114 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\WcGateway\Checkout;
|
||||
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Shipping;
|
||||
use Inpsyde\PayPalCommerce\Session\SessionHandler;
|
||||
|
||||
/**
|
||||
* Service that fills checkout address fields
|
||||
* with address selected via PayPal
|
||||
*/
|
||||
class CheckoutPayPalAddressPreset
|
||||
{
|
||||
|
||||
private $shippingCache = [];
|
||||
|
||||
/**
|
||||
* @var SessionHandler
|
||||
*/
|
||||
private $sessionHandler;
|
||||
|
||||
/**
|
||||
* @param SessionHandler $sessionHandler
|
||||
*/
|
||||
public function __construct(SessionHandler $sessionHandler)
|
||||
{
|
||||
$this->sessionHandler = $sessionHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* @wp-hook woocommerce_checkout_get_value
|
||||
* @param string|null
|
||||
* @param string $fieldId
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function filterCheckoutFiled($defaultValue, $fieldId): ?string
|
||||
{
|
||||
if(! is_string($defaultValue)) {
|
||||
$defaultValue = null;
|
||||
}
|
||||
|
||||
if(!is_string($fieldId)) {
|
||||
return $defaultValue;
|
||||
}
|
||||
|
||||
return $this->readPresetForField($fieldId) ?? $defaultValue;
|
||||
}
|
||||
|
||||
private function readPresetForField(string $fieldId): ?string
|
||||
{
|
||||
if(!$order = $this->sessionHandler->order()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$shipping = $this->readShippingFromOrder();
|
||||
$payer = $order->payer();
|
||||
|
||||
$addressMap = [
|
||||
'billing_address_1' => 'addressLine1',
|
||||
'billing_address_2' => 'addressLine2',
|
||||
'billing_postcode' => 'postalCode',
|
||||
'billing_country' => 'countryCode',
|
||||
'billing_city' => 'adminArea2',
|
||||
'billing_state' => 'adminArea1'
|
||||
];
|
||||
$payerNameMap = [
|
||||
'billing_last_name' => 'surname',
|
||||
'billing_first_name' => 'givenName',
|
||||
];
|
||||
$payerMap = [
|
||||
'billing_email' => 'emailAddress',
|
||||
'billing_phone' => 'phone',
|
||||
];
|
||||
|
||||
if(array_key_exists($fieldId, $addressMap) && $shipping) {
|
||||
return $shipping->address()->{$addressMap[$fieldId]}() ?: null;
|
||||
}
|
||||
|
||||
if(array_key_exists($fieldId, $payerNameMap) && $payer) {
|
||||
return $payer->name()->{$payerNameMap[$fieldId]}() ?: null;
|
||||
}
|
||||
|
||||
if(array_key_exists($fieldId, $payerMap) && $payer) {
|
||||
return $payer->{$payerMap[$fieldId]}() ?: null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function readShippingFromOrder(): ?Shipping
|
||||
{
|
||||
if(!$order = $this->sessionHandler->order()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if(array_key_exists($order->id(), $this->shippingCache)) {
|
||||
return $this->shippingCache[$order->id()];
|
||||
}
|
||||
|
||||
$shipping = null;
|
||||
foreach($this->sessionHandler->order()->purchaseUnits() as $unit) {
|
||||
if($shipping = $unit->shipping()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$this->shippingCache[$order->id()] = $shipping;
|
||||
|
||||
return $shipping;
|
||||
}
|
||||
}
|
|
@ -10,9 +10,9 @@ use Inpsyde\PayPalCommerce\AdminNotices\Repository\Repository;
|
|||
use Inpsyde\PayPalCommerce\WcGateway\Admin\OrderDetail;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Admin\OrderTablePaymentStatusColumn;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Admin\PaymentStatusOrderDetail;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Checkout\CheckoutPayPalAddressPreset;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Checkout\DisableGateways;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Gateway\WcGateway;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Notice\ConnectAdminNotice;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsRenderer;
|
||||
|
@ -34,6 +34,7 @@ class WcGatewayModule implements ModuleInterface
|
|||
$this->registerPaymentGateway($container);
|
||||
$this->registerOrderFunctionality($container);
|
||||
$this->registerColumns($container);
|
||||
$this->registerCheckoutAddressPreset($container);
|
||||
|
||||
add_filter(
|
||||
Repository::NOTICES_FILTER,
|
||||
|
@ -212,4 +213,21 @@ class WcGatewayModule implements ModuleInterface
|
|||
2
|
||||
);
|
||||
}
|
||||
|
||||
private function registerCheckoutAddressPreset(ContainerInterface $container): void
|
||||
{
|
||||
add_filter(
|
||||
'woocommerce_checkout_get_value',
|
||||
static function (...$args) use ($container) {
|
||||
// It's important to not instantiate the service to early
|
||||
// as it depends on SessionHandler and WooCommerce Session
|
||||
/* @var CheckoutPayPalAddressPreset $service */
|
||||
$service = $container->get('wcgateway.checkout.address-preset');
|
||||
|
||||
return $service->filterCheckoutFiled(...$args);
|
||||
},
|
||||
10,
|
||||
2
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue