Skip to content

Commit

Permalink
Enable the Payment Request feature by default for new merchants only (#…
Browse files Browse the repository at this point in the history
…1404)

* Do not display error notice when account is not live, even if cached setting has `'apple_pay_domain_set' => 'no'`

* Remove redundant check now that the notice is only visible from WCPay settings

* Remove gateway from Apple Pay registration class

* Enable by default for new merchants and refactor default settings

* Refactor leftover code and change hook initialization

* Remove redundant getters since default values are always set in the array

* Add return before Apple Pay hooks if feature is disabled

* Remove now unnecessary is_enabled checks

* Remove default settings function and replace gateway_settings with get_option

* Restore is_enabled checks
  • Loading branch information
ricardo authored Mar 19, 2021
1 parent 2fc06a4 commit 452e72a
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 193 deletions.
16 changes: 7 additions & 9 deletions includes/class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,7 @@ public function __construct(
// TODO: Remove this check and inject contents of `$payment_request_fields` into `$this->form_fields`
// after `saved_cards` ahead of Apple Pay release.
if ( 'yes' === get_option( '_wcpay_feature_payment_request' ) ) {
// Default values for Payment Request.
$payment_request_default_settings = WC_Payments_Payment_Request_Button_Handler::get_default_settings();
$payment_request_fields = [
$payment_request_fields = [
'payment_request' => [
'title' => __( 'Payment Request Button', 'woocommerce-payments' ),
'label' => sprintf(
Expand All @@ -185,15 +183,15 @@ public function __construct(
),
'type' => 'checkbox',
'description' => __( 'If enabled, users will be able to pay using Apple Pay or Chrome Payment Request if supported by the browser.', 'woocommerce-payments' ),
'default' => $payment_request_default_settings['payment_request'],
'default' => empty( get_option( 'woocommerce_woocommerce_payments_settings' ) ) ? 'yes' : 'no', // Enable by default for new installations only.
'desc_tip' => true,
],
'payment_request_button_type' => [
'title' => __( 'Payment Request Button Type', 'woocommerce-payments' ),
'label' => __( 'Button Type', 'woocommerce-payments' ),
'type' => 'select',
'description' => __( 'Select the button type you would like to show.', 'woocommerce-payments' ),
'default' => $payment_request_default_settings['payment_request_button_type'],
'default' => 'buy',
'desc_tip' => true,
'options' => [
'default' => __( 'Default', 'woocommerce-payments' ),
Expand All @@ -208,7 +206,7 @@ public function __construct(
'label' => __( 'Button Theme', 'woocommerce-payments' ),
'type' => 'select',
'description' => __( 'Select the button theme you would like to show.', 'woocommerce-payments' ),
'default' => $payment_request_default_settings['payment_request_button_theme'],
'default' => 'dark',
'desc_tip' => true,
'options' => [
'dark' => __( 'Dark', 'woocommerce-payments' ),
Expand All @@ -221,23 +219,23 @@ public function __construct(
'label' => __( 'Button Height', 'woocommerce-payments' ),
'type' => 'text',
'description' => __( 'Enter the height you would like the button to be in pixels. Width will always be 100%.', 'woocommerce-payments' ),
'default' => $payment_request_default_settings['payment_request_button_height'],
'default' => '44',
'desc_tip' => true,
],
'payment_request_button_label' => [
'title' => __( 'Payment Request Button Label', 'woocommerce-payments' ),
'label' => __( 'Button Label', 'woocommerce-payments' ),
'type' => 'text',
'description' => __( 'Enter the custom text you would like the button to have.', 'woocommerce-payments' ),
'default' => $payment_request_default_settings['payment_request_button_label'],
'default' => __( 'Buy now', 'woocommerce-payments' ),
'desc_tip' => true,
],
'payment_request_button_branded_type' => [
'title' => __( 'Payment Request Branded Button Label Format', 'woocommerce-payments' ),
'label' => __( 'Branded Button Label Format', 'woocommerce-payments' ),
'type' => 'select',
'description' => __( 'Select the branded button label format.', 'woocommerce-payments' ),
'default' => $payment_request_default_settings['payment_request_button_branded_type'],
'default' => 'long',
'desc_tip' => true,
'options' => [
'short' => __( 'Logo only', 'woocommerce-payments' ),
Expand Down
91 changes: 46 additions & 45 deletions includes/class-wc-payments-apple-pay-registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ class WC_Payments_Apple_Pay_Registration {
*/
private $payments_api_client;

/**
* The WCPay gateway object.
*
* @var WC_Payment_Gateway_WCPay
*/
private $gateway;

/**
* The WCPay account object.
*
Expand All @@ -45,11 +38,11 @@ class WC_Payments_Apple_Pay_Registration {
private $account;

/**
* Gateway settings.
* WC_Payment_Gateway_WCPay instance.
*
* @var array
* @var WC_Payment_Gateway_WCPay
*/
private $gateway_settings;
private $gateway;

/**
* Current domain name.
Expand All @@ -68,12 +61,27 @@ class WC_Payments_Apple_Pay_Registration {
/**
* Initialize class actions.
*
* @param WC_Payments_API_Client $payments_api_client WooCommerce Payments API client.
* @param WC_Payment_Gateway_WCPay $gateway WooCommerce Payments gateway.
* @param WC_Payments_Account $account WooCommerce Payments account.
* @param WC_Payments_API_Client $payments_api_client WooCommerce Payments API client.
* @param WC_Payments_Account $account WooCommerce Payments account.
*/
public function __construct( WC_Payments_API_Client $payments_api_client, WC_Payment_Gateway_WCPay $gateway, WC_Payments_Account $account ) {
add_action( 'init', [ $this, 'add_domain_association_rewrite_rule' ] );
public function __construct( WC_Payments_API_Client $payments_api_client, WC_Payments_Account $account ) {
$this->domain_name = $_SERVER['HTTP_HOST'] ?? str_replace( [ 'https://', 'http://' ], '', get_site_url() ); // @codingStandardsIgnoreLine
$this->apple_pay_verify_notice = '';
$this->payments_api_client = $payments_api_client;
$this->account = $account;

add_action( 'init', [ $this, 'init' ] );
}

/**
* Initialize hooks.
*
* @return void
*/
public function init() {
$this->gateway = WC_Payments::get_gateway();
$this->add_domain_association_rewrite_rule();

add_action( 'admin_init', [ $this, 'verify_domain_on_domain_name_change' ] );
add_filter( 'query_vars', [ $this, 'whitelist_domain_association_query_param' ], 10, 1 );
add_action( 'parse_request', [ $this, 'parse_domain_association_request' ], 10, 1 );
Expand All @@ -83,38 +91,35 @@ public function __construct( WC_Payments_API_Client $payments_api_client, WC_Pay
add_action( 'woocommerce_woocommerce_payments_updated', [ $this, 'verify_domain_if_configured' ] );
add_action( 'add_option_woocommerce_woocommerce_payments_settings', [ $this, 'verify_domain_on_new_settings' ], 10, 2 );
add_action( 'update_option_woocommerce_woocommerce_payments_settings', [ $this, 'verify_domain_on_updated_settings' ], 10, 2 );

// Add default settings in case the payment request options are missing.
$this->gateway_settings = array_merge( WC_Payments_Payment_Request_Button_Handler::get_default_settings(), get_option( 'woocommerce_woocommerce_payments_settings', [] ) );
$this->domain_name = $_SERVER['HTTP_HOST'] ?? str_replace( [ 'https://', 'http://' ], '', get_site_url() ); // @codingStandardsIgnoreLine
$this->apple_pay_verify_notice = '';
$this->payments_api_client = $payments_api_client;
$this->gateway = $gateway;
$this->account = $account;
}

/**
* Whether the gateway and Payment Request Button (prerequisites for Apple Pay) are enabled.
*
* @param array|null $settings Gateway settings.
*
* @return string Whether Apple Pay required settings are enabled.
* @return bool Whether Apple Pay required settings are enabled.
*/
private function is_enabled( $settings = null ) {
// Use given settings array or use default cached settings.
$settings = $settings ?? $this->gateway_settings;

$gateway_enabled = 'yes' === ( $settings['enabled'] ?? 'no' );
$payment_request_enabled = 'yes' === ( $settings['payment_request'] ?? 'yes' );
private function is_enabled() {
return $this->gateway->is_enabled() && 'yes' === $this->gateway->get_option( 'payment_request' );
}

/**
* Whether the gateway and Payment Request Button were enabled in previous settings.
*
* @param array|null $prev_settings Gateway settings.
*
* @return bool Whether Apple Pay required settings are enabled.
*/
private function was_enabled( $prev_settings ) {
$gateway_enabled = 'yes' === ( $prev_settings['enabled'] ?? 'no' );
$payment_request_enabled = 'yes' === ( $prev_settings['payment_request'] ?? 'no' );
return $gateway_enabled && $payment_request_enabled;
}

/**
* Trigger Apple Pay registration upon domain name change.
*/
public function verify_domain_on_domain_name_change() {
$verified_domain = $this->gateway_settings['apple_pay_verified_domain'] ?? '';
$verified_domain = $this->gateway->get_option( 'apple_pay_verified_domain' );
if ( $this->domain_name !== $verified_domain ) {
$this->verify_domain_if_configured();
}
Expand Down Expand Up @@ -248,9 +253,8 @@ public function register_domain_with_apple() {
$registration_response = $this->payments_api_client->register_domain_with_apple( $this->domain_name );

if ( isset( $registration_response['id'] ) ) {
$this->gateway_settings['apple_pay_verified_domain'] = $this->domain_name;
$this->gateway_settings['apple_pay_domain_set'] = 'yes';
update_option( 'woocommerce_woocommerce_payments_settings', $this->gateway_settings );
$this->gateway->update_option( 'apple_pay_verified_domain', $this->domain_name );
$this->gateway->update_option( 'apple_pay_domain_set', 'yes' );

Logger::log( __( 'Your domain has been verified with Apple Pay!', 'woocommerce-payments' ) );
Tracker::track_admin(
Expand All @@ -272,9 +276,8 @@ public function register_domain_with_apple() {
// Display error message in notice.
$this->apple_pay_verify_notice = $error;

$this->gateway_settings['apple_pay_verified_domain'] = $this->domain_name;
$this->gateway_settings['apple_pay_domain_set'] = 'no';
update_option( 'woocommerce_woocommerce_payments_settings', $this->gateway_settings );
$this->gateway->update_option( 'apple_pay_verified_domain', $this->domain_name );
$this->gateway->update_option( 'apple_pay_domain_set', 'no' );

Logger::log( 'Error registering domain with Apple: ' . $error );
Tracker::track_admin(
Expand Down Expand Up @@ -325,10 +328,8 @@ public function verify_domain_on_new_settings( $option, $settings ) {
* @param array $settings Settings after update.
*/
public function verify_domain_on_updated_settings( $prev_settings, $settings ) {
$this->gateway_settings = $settings;

// If Gateway or Payment Request Button wasn't enabled, then might need to verify now.
if ( ! $this->is_enabled( $prev_settings ) ) {
// If Gateway or Payment Request Button weren't enabled, then might need to verify now.
if ( ! $this->was_enabled( $prev_settings ) ) {
$this->verify_domain_if_configured();
}
}
Expand All @@ -355,12 +356,12 @@ public function display_live_account_notice() {
* Display Apple Pay registration errors.
*/
public function display_error_notice() {
if ( ! $this->is_enabled() || ! current_user_can( 'manage_woocommerce' ) ) {
if ( ! $this->is_enabled() || ! $this->account->get_is_live() ) {
return;
}

$empty_notice = empty( $this->apple_pay_verify_notice );
$domain_set = $this->gateway_settings['apple_pay_domain_set'] ?? '';
$domain_set = $this->gateway->get_option( 'apple_pay_domain_set' );
// Don't display error notice if verification notice is empty and
// apple_pay_domain_set option equals to '' or 'yes'.
if ( $empty_notice && 'no' !== $domain_set ) {
Expand Down
Loading

0 comments on commit 452e72a

Please sign in to comment.