Use shipping callback

This commit is contained in:
Alex P. 2025-06-20 10:43:52 +03:00
parent b16ef8e333
commit 6691ee5a21
No known key found for this signature in database
GPG key ID: 54487A734A204D71
5 changed files with 44 additions and 12 deletions

View file

@ -16,7 +16,8 @@ return array(
'wcgateway.builder.experience-context' => static function ( ContainerInterface $container ): ExperienceContextBuilder { 'wcgateway.builder.experience-context' => static function ( ContainerInterface $container ): ExperienceContextBuilder {
return new ExperienceContextBuilder( return new ExperienceContextBuilder(
$container->get( 'wcgateway.settings' ) $container->get( 'wcgateway.settings' ),
$container->get( 'wcgateway.shipping.callback.factory.url' )
); );
}, },
); );

View file

@ -11,9 +11,11 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
use WC_AJAX; use WC_AJAX;
use WC_Order; use WC_Order;
use WooCommerce\PayPalCommerce\ApiClient\Entity\CallbackConfig;
use WooCommerce\PayPalCommerce\ApiClient\Entity\ExperienceContext; use WooCommerce\PayPalCommerce\ApiClient\Entity\ExperienceContext;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\WcGateway\Endpoint\ReturnUrlEndpoint; use WooCommerce\PayPalCommerce\WcGateway\Endpoint\ReturnUrlEndpoint;
use WooCommerce\PayPalCommerce\WcGateway\Shipping\ShippingCallbackUrlFactory;
/** /**
* Class ExperienceContextBuilder * Class ExperienceContextBuilder
@ -30,15 +32,16 @@ class ExperienceContextBuilder {
*/ */
private ContainerInterface $settings; private ContainerInterface $settings;
/** private ShippingCallbackUrlFactory $shipping_callback_url_factory;
* ExperienceContextBuilder constructor.
* public function __construct(
* @param ContainerInterface $settings The settings. ContainerInterface $settings,
*/ ShippingCallbackUrlFactory $shipping_callback_url_factory
public function __construct( ContainerInterface $settings ) { ) {
$this->experience_context = new ExperienceContext(); $this->experience_context = new ExperienceContext();
$this->settings = $settings; $this->settings = $settings;
$this->shipping_callback_url_factory = $shipping_callback_url_factory;
} }
/** /**
@ -161,6 +164,23 @@ class ExperienceContextBuilder {
return $builder; return $builder;
} }
/**
* Uses the server-side shipping callback configuration.
*/
public function with_shipping_callback(): ExperienceContextBuilder {
$builder = clone $this;
$builder->experience_context = $builder->experience_context
->with_order_update_callback_config(
new CallbackConfig(
array( CallbackConfig::EVENT_SHIPPING_ADDRESS, CallbackConfig::EVENT_SHIPPING_OPTIONS ),
$this->shipping_callback_url_factory->create()
)
);
return $builder;
}
/** /**
* Returns the ExperienceContext. * Returns the ExperienceContext.
*/ */

View file

@ -352,6 +352,8 @@ export const PayPalComponent = ( {
); );
const getOnShippingOptionsChange = ( fundingSource ) => { const getOnShippingOptionsChange = ( fundingSource ) => {
return null;
if ( fundingSource === 'venmo' ) { if ( fundingSource === 'venmo' ) {
return null; return null;
} }
@ -364,6 +366,8 @@ export const PayPalComponent = ( {
}; };
const getOnShippingAddressChange = ( fundingSource ) => { const getOnShippingAddressChange = ( fundingSource ) => {
return null;
if ( fundingSource === 'venmo' ) { if ( fundingSource === 'venmo' ) {
return null; return null;
} }

View file

@ -446,12 +446,15 @@ class CreateOrderEndpoint implements EndpointInterface {
$payment_source_key = $funding_source; $payment_source_key = $funding_source;
} }
$experience_context = $this->experience_context_builder->with_default_paypal_config( $shipping_preference, $action );
if ( $shipping_preference === ExperienceContext::SHIPPING_PREFERENCE_GET_FROM_FILE ) {
$experience_context = $experience_context->with_shipping_callback();
}
$payment_source = new PaymentSource( $payment_source = new PaymentSource(
$payment_source_key, $payment_source_key,
(object) array( (object) array(
'experience_context' => $this->experience_context_builder 'experience_context' => $experience_context->build()->to_array(),
->with_default_paypal_config( $shipping_preference, $action )
->build()->to_array(),
) )
); );

View file

@ -8,6 +8,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Factory\ExperienceContextBuilder;
use WooCommerce\PayPalCommerce\TestCase; use WooCommerce\PayPalCommerce\TestCase;
use WooCommerce\PayPalCommerce\WcGateway\Endpoint\ReturnUrlEndpoint; use WooCommerce\PayPalCommerce\WcGateway\Endpoint\ReturnUrlEndpoint;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WooCommerce\PayPalCommerce\WcGateway\Shipping\ShippingCallbackUrlFactory;
use function Brain\Monkey\Functions\expect; use function Brain\Monkey\Functions\expect;
use Mockery; use Mockery;
@ -15,6 +16,8 @@ class ExperienceContextBuilderTest extends TestCase
{ {
private $settings; private $settings;
private $shipping_callback_url_factory;
private $sut; private $sut;
public function setUp(): void public function setUp(): void
@ -22,8 +25,9 @@ class ExperienceContextBuilderTest extends TestCase
parent::setUp(); parent::setUp();
$this->settings = Mockery::mock(Settings::class); $this->settings = Mockery::mock(Settings::class);
$this->shipping_callback_url_factory = Mockery::mock(ShippingCallbackUrlFactory::class);
$this->sut = new ExperienceContextBuilder($this->settings); $this->sut = new ExperienceContextBuilder($this->settings, $this->shipping_callback_url_factory);
} }
public function testOrderReturnUrls() public function testOrderReturnUrls()