Merge pull request #793 from woocommerce/PCP-155-tracking-api

Tracking API
This commit is contained in:
Emili Castells 2022-08-19 09:30:57 +02:00 committed by GitHub
commit 8d6ce555fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 3994 additions and 12 deletions

View file

@ -12,6 +12,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway;
use Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PayUponInvoiceOrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
@ -944,6 +945,20 @@ return array(
'requirements' => array(),
'gateway' => 'paypal',
),
'tracking_enabled' => array(
'title' => __( 'Tracking', 'woocommerce-paypal-payments' ),
'type' => 'checkbox',
'desc_tip' => true,
'label' => __( 'Enable tracking', 'woocommerce-paypal-payments' ),
'description' => __( 'Enable tracking', 'woocommerce-paypal-payments' ),
'default' => false,
'screens' => array(
State::STATE_ONBOARDED,
),
'requirements' => array(),
'gateway' => array( 'paypal' ),
'input_class' => $container->get( 'wcgateway.settings.should-disable-tracking-checkbox' ) ? array( 'ppcp-disabled-checkbox' ) : array(),
),
// General button styles.
'button_style_heading' => array(
@ -2287,7 +2302,9 @@ return array(
},
'wcgateway.pay-upon-invoice-helper' => static function( ContainerInterface $container ): PayUponInvoiceHelper {
return new PayUponInvoiceHelper(
$container->get( 'wcgateway.checkout-helper' )
$container->get( 'wcgateway.checkout-helper' ),
$container->get( 'api.shop.country' ),
$container->get( 'wcgateway.pay-upon-invoice-product-status' )
);
},
'wcgateway.pay-upon-invoice-product-status' => static function( ContainerInterface $container ): PayUponInvoiceProductStatus {
@ -2418,4 +2435,31 @@ return array(
(bool) $settings->get( 'allow_card_button_gateway' ) :
$container->get( 'wcgateway.settings.allow_card_button_gateway.default' );
},
'order-tracking.is-tracking-available' => static function ( ContainerInterface $container ): bool {
try {
$bearer = $container->get( 'api.bearer' );
assert( $bearer instanceof Bearer );
$token = $bearer->bearer();
return $token->is_tracking_available();
} catch ( RuntimeException $exception ) {
return false;
}
},
'wcgateway.settings.should-disable-tracking-checkbox' => static function ( ContainerInterface $container ): bool {
$pui_helper = $container->get( 'wcgateway.pay-upon-invoice-helper' );
assert( $pui_helper instanceof PayUponInvoiceHelper );
$is_tracking_available = $container->get( 'order-tracking.is-tracking-available' );
if ( ! $is_tracking_available ) {
return true;
}
if ( $pui_helper->is_pui_ready_in_admin() ) {
return true;
}
return false;
},
);

View file

@ -23,13 +23,32 @@ class PayUponInvoiceHelper {
*/
protected $checkout_helper;
/**
* The selected shop country.
*
* @var string
*/
protected $shop_country;
/**
* The PUI seller product status.
*
* @var PayUponInvoiceProductStatus
*/
protected $pui_product_status;
/**
* PayUponInvoiceHelper constructor.
*
* @param CheckoutHelper $checkout_helper The checkout helper.
* @param CheckoutHelper $checkout_helper The checkout helper.
* @param string $shop_country The selected shop country.
* @param PayUponInvoiceProductStatus $pui_product_status The PUI seller product status.
*/
public function __construct( CheckoutHelper $checkout_helper ) {
$this->checkout_helper = $checkout_helper;
public function __construct( CheckoutHelper $checkout_helper, string $shop_country, PayUponInvoiceProductStatus $pui_product_status ) {
$this->checkout_helper = $checkout_helper;
$this->shop_country = $shop_country;
$this->pui_product_status = $pui_product_status;
}
/**
@ -78,4 +97,17 @@ class PayUponInvoiceHelper {
return false;
}
/**
* Checks whether PUI is ready in admin screen.
*
* @return bool
*/
public function is_pui_ready_in_admin(): bool {
if ( $this->shop_country === 'DE' && $this->pui_product_status->pui_is_active() ) {
return true;
}
return false;
}
}

View file

@ -235,7 +235,7 @@ class SettingsListener {
*
* @throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting was not found.
*/
public function listen() {
public function listen(): void {
if ( ! $this->is_valid_update_request() ) {
return;
@ -473,4 +473,36 @@ class SettingsListener {
}
return true;
}
/**
* Prevent enabling tracking if it is not enabled for merchant account.
*/
public function listen_for_tracking_enabled(): void {
if ( State::STATE_ONBOARDED !== $this->state->current_state() ) {
return;
}
try {
$token = $this->bearer->bearer();
if ( ! $token->is_tracking_available() ) {
$this->settings->set( 'tracking_enabled', false );
$this->settings->persist();
return;
}
} catch ( RuntimeException $exception ) {
$this->settings->set( 'tracking_enabled', false );
$this->settings->persist();
add_action(
'admin_notices',
function () use ( $exception ) {
printf(
'<div class="notice notice-error"><p>%1$s</p><p>%2$s</p></div>',
esc_html__( 'Authentication with PayPal failed: ', 'woocommerce-paypal-payments' ) . esc_attr( $exception->getMessage() ),
wp_kses_post( __( 'Please verify your API Credentials and try again to connect your PayPal business account. Visit the <a href="https://docs.woocommerce.com/document/woocommerce-paypal-payments/" target="_blank">plugin documentation</a> for more information about the setup.', 'woocommerce-paypal-payments' ) )
);
}
);
}
}
}

View file

@ -386,6 +386,7 @@ class WCGatewayModule implements ModuleInterface {
*/
$listener->listen_for_merchant_id();
$listener->listen_for_vaulting_enabled();
$listener->listen_for_tracking_enabled();
}
);