Handle continuation mode in blocks

This commit is contained in:
Alex P 2023-04-07 15:58:59 +03:00
parent b0a0eea752
commit 493933a1c1
No known key found for this signature in database
GPG key ID: 54487A734A204D71
6 changed files with 107 additions and 12 deletions

View file

@ -368,3 +368,14 @@ namespace Automattic\WooCommerce\Blocks\Payments {
}
}
}
/**
* Registers and validates payment requirements callbacks.
*
* @see Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema::register_payment_requirements()
*
* @param array $args Args to pass to register_payment_requirements.
* @returns boolean|\WP_Error True on success, WP_Error on fail.
*/
function woocommerce_store_api_register_payment_requirements( $args ) {
}

View file

@ -1,5 +1,5 @@
import {useEffect, useState} from '@wordpress/element';
import {registerExpressPaymentMethod} from '@woocommerce/blocks-registry';
import {registerExpressPaymentMethod, registerPaymentMethod} from '@woocommerce/blocks-registry';
import {paypalOrderToWcShippingAddress, paypalPayerToWc} from "./Helper/Address";
import {loadPaypalScript} from '../../../ppcp-button/resources/js/modules/Helper/ScriptLoading'
@ -116,6 +116,17 @@ const PayPalComponent = ({
}
const unsubscribeProcessing = onPaymentSetup(() => {
if (config.scriptData.continuation) {
return {
type: responseTypes.SUCCESS,
meta: {
paymentMethodData: {
'paypal_order_id': config.scriptData.continuation.order_id,
},
},
};
}
const shippingAddress = paypalOrderToWcShippingAddress(paypalOrder);
let billingAddress = paypalPayerToWc(paypalOrder.payer);
// no billing address, such as if billing address retrieval is not allowed in the merchant account
@ -139,6 +150,14 @@ const PayPalComponent = ({
};
}, [onPaymentSetup, paypalOrder, activePaymentMethod]);
if (config.scriptData.continuation) {
return (
<div dangerouslySetInnerHTML={{__html: config.scriptData.continuation.cancel.html}}>
</div>
)
}
if (!loaded) {
return null;
}
@ -157,7 +176,14 @@ const PayPalComponent = ({
);
}
registerExpressPaymentMethod({
const features = ['products'];
let registerMethod = registerExpressPaymentMethod;
if (config.scriptData.continuation) {
features.push('ppcp_continuation');
registerMethod = registerPaymentMethod;
}
registerMethod({
name: config.id,
label: <div dangerouslySetInnerHTML={{__html: config.title}}/>,
content: <PayPalComponent/>,
@ -165,6 +191,6 @@ registerExpressPaymentMethod({
ariaLabel: config.title,
canMakePayment: () => config.enabled,
supports: {
features: ['products'],
features: features,
},
});

View file

@ -30,7 +30,9 @@ return array(
$container->get( 'button.smart-button' ),
$container->get( 'wcgateway.settings' ),
$container->get( 'wcgateway.settings.status' ),
$container->get( 'wcgateway.paypal-gateway' )
$container->get( 'wcgateway.paypal-gateway' ),
$container->get( 'session.cancellation.view' ),
$container->get( 'session.handler' )
);
},
);

View file

@ -10,6 +10,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Blocks;
use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry;
use WooCommerce\PayPalCommerce\Button\Assets\SmartButton;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface;
@ -40,6 +41,21 @@ class BlocksModule implements ModuleInterface {
$payment_method_registry->register( $c->get( 'blocks.method' ) );
}
);
woocommerce_store_api_register_payment_requirements(
array(
'data_callback' => function() use ( $c ): array {
$smart_button = $c->get( 'button.smart-button' );
assert( $smart_button instanceof SmartButton );
if ( isset( $smart_button->script_data()['continuation'] ) ) {
return array( 'ppcp_continuation' );
}
return array();
},
)
);
}
}

View file

@ -11,6 +11,9 @@ namespace WooCommerce\PayPalCommerce\Blocks;
use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
use WooCommerce\PayPalCommerce\Button\Assets\SmartButtonInterface;
use WooCommerce\PayPalCommerce\Session\Cancellation\CancelController;
use WooCommerce\PayPalCommerce\Session\Cancellation\CancelView;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
@ -61,6 +64,20 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
*/
private $gateway;
/**
* The cancellation view.
*
* @var CancelView
*/
private $cancellation_view;
/**
* The Session handler.
*
* @var SessionHandler
*/
private $session_handler;
/**
* Assets constructor.
*
@ -70,6 +87,8 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
* @param Settings $plugin_settings The settings.
* @param SettingsStatus $settings_status The Settings status helper.
* @param PayPalGateway $gateway The WC gateway.
* @param CancelView $cancellation_view The cancellation view.
* @param SessionHandler $session_handler The Session handler.
*/
public function __construct(
string $module_url,
@ -77,15 +96,19 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
SmartButtonInterface $smart_button,
Settings $plugin_settings,
SettingsStatus $settings_status,
PayPalGateway $gateway
PayPalGateway $gateway,
CancelView $cancellation_view,
SessionHandler $session_handler
) {
$this->name = PayPalGateway::ID;
$this->module_url = $module_url;
$this->version = $version;
$this->smart_button = $smart_button;
$this->plugin_settings = $plugin_settings;
$this->settings_status = $settings_status;
$this->gateway = $gateway;
$this->name = PayPalGateway::ID;
$this->module_url = $module_url;
$this->version = $version;
$this->smart_button = $smart_button;
$this->plugin_settings = $plugin_settings;
$this->settings_status = $settings_status;
$this->gateway = $gateway;
$this->cancellation_view = $cancellation_view;
$this->session_handler = $session_handler;
}
/**
@ -126,6 +149,14 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
public function get_payment_method_data() {
$script_data = $this->smart_button->script_data();
if ( isset( $script_data['continuation'] ) ) {
$url = add_query_arg( array( CancelController::NONCE => wp_create_nonce( CancelController::NONCE ) ), wc_get_checkout_url() );
$script_data['continuation']['cancel'] = array(
'html' => $this->cancellation_view->render_session_cancellation( $url, $this->session_handler->funding_source() ),
);
}
return array(
'id' => $this->gateway->id,
'title' => $this->gateway->title,

View file

@ -927,6 +927,15 @@ class SmartButton implements SmartButtonInterface {
$localize['button']['style']['tagline'] = false;
}
if ( $this->is_paypal_continuation() ) {
$order = $this->session_handler->order();
assert( $order !== null );
$localize['continuation'] = array(
'order_id' => $order->id(),
);
}
$this->request_data->dequeue_nonce_fix();
return $localize;
}