id = self::GATEWAY_ID;
$this->method_title = __( 'Apple Pay / Google Pay', 'revolut-gateway-for-woocommerce' );
/* translators:%1s: %$2s: */
$this->method_description = sprintf( __( 'Accept Apple and Google payments easily and securely via %1$sRevolut%2$s.', 'revolut-gateway-for-woocommerce' ), '', '' );
$this->tab_title = __( 'Apple Pay / Google Pay', 'revolut-gateway-for-woocommerce' );
$this->title = __( 'Digital Wallet (ApplePay/GooglePay)', 'revolut-gateway-for-woocommerce' );
parent::__construct();
add_filter( 'wc_revolut_settings_nav_tabs', array( $this, 'admin_nav_tab' ), 2 );
add_action( 'wc_ajax_revolut_payment_request_add_to_cart', array( $this, 'revolut_payment_request_ajax_add_to_cart' ) );
add_action( 'wc_ajax_revolut_payment_request_get_shipping_options', array( $this, 'revolut_payment_request_ajax_get_shipping_options' ) );
add_action( 'wc_ajax_revolut_payment_request_update_shipping_method', array( $this, 'revolut_payment_request_ajax_update_shipping_method' ) );
add_action( 'wc_ajax_revolut_payment_request_update_payment_total', array( $this, 'revolut_payment_request_update_revolut_order_with_cart_total' ) );
add_action( 'wc_ajax_revolut_payment_request_create_order', array( $this, 'revolut_payment_request_ajax_create_order' ) );
add_action( 'wc_ajax_revolut_payment_request_get_payment_request_params', array( $this, 'revolut_payment_request_ajax_get_payment_request_params' ) );
if ( 'yes' !== $this->enabled || $this->api_settings->get_option( 'mode' ) === 'sandbox' ) {
return;
}
add_action( 'woocommerce_after_add_to_cart_quantity', array( $this, 'display_payment_request_button_html' ), 2 );
add_action( 'woocommerce_proceed_to_checkout', array( $this, 'display_payment_request_button_html' ), 2 );
add_action( 'wp_enqueue_scripts', array( $this, 'revolut_enqueue_payment_request_scripts' ) );
}
/**
* Get express checkout params
*/
public function revolut_payment_request_ajax_get_payment_request_params() {
check_ajax_referer( 'wc-revolut-get-payment-request-params', 'security' );
try {
wp_send_json(
array(
'success' => true,
'revolut_public_id' => $this->create_express_checkout_public_id(),
'checkout_nonce' => wp_create_nonce( 'woocommerce-process_checkout' ),
)
);
} catch ( Exception $e ) {
wp_send_json( array( 'success' => false ) );
$this->log_error( $e );
}
}
/**
* Add required scripts
*/
public function revolut_enqueue_payment_request_scripts() {
try {
wp_localize_script(
'revolut-woocommerce',
'revolut_payment_request_button_style',
array(
'payment_request_button_title' => $this->get_option( 'title' ),
'payment_request_button_type' => $this->get_option( 'payment_request_button_type' ),
'payment_request_button_theme' => $this->get_option( 'payment_request_button_theme' ),
'payment_request_button_radius' => $this->get_option( 'payment_request_button_radius' ),
'payment_request_button_size' => $this->get_option( 'payment_request_button_size' ),
)
);
if ( ! $this->page_supports_payment_request_button( $this->get_option( 'payment_request_button_locations' ) ) ) {
return false;
}
wp_register_script( 'revolut-core', $this->api_client->base_url . '/embed.js', false, WC_GATEWAY_REVOLUT_VERSION, true );
wp_register_script(
'revolut-woocommerce-payment-request',
plugins_url( 'assets/js/revolut-payment-request.js', WC_REVOLUT_MAIN_FILE ),
array(
'revolut-core',
'jquery',
),
WC_GATEWAY_REVOLUT_VERSION,
true
);
wp_localize_script(
'revolut-woocommerce-payment-request',
'wc_revolut_payment_request_params',
$this->get_wc_revolut_payment_request_params()
);
wp_enqueue_script( 'revolut-woocommerce-payment-request' );
} catch ( Exception $e ) {
$this->log_error( $e->getMessage() );
}
}
/**
* Check if the Revolut Pay Fast checkout payments active.
*/
public function is_revolut_pay_fast_checkout_active() {
$revolut_cc_gateway_options = get_option( 'woocommerce_revolut_pay_settings' );
return isset( $revolut_cc_gateway_options['revolut_pay_button_locations'] ) && $this->page_supports_payment_request_button( $revolut_cc_gateway_options['revolut_pay_button_locations'] ) && $this->is_shipping_required();
}
/**
* Update Revolut order with cart total amount
*
* @param string $revolut_public_id Revolut public id.
*
* @param bool $is_revolut_pay indicator.
*
* @throws Exception Exception.
*/
public function update_revolut_order_with_cart_total( $revolut_public_id, $is_revolut_pay = false ) {
$revolut_order_id = $this->get_revolut_order_by_public_id( $revolut_public_id );
$revolut_order = $this->api_client->get( "/orders/$revolut_order_id" );
$revolut_order_shipping_total = $this->get_revolut_order_total_shipping( $revolut_order );
$cart_subtotal = round( (float) ( WC()->cart->get_subtotal() + WC()->cart->get_subtotal_tax() + $revolut_order_shipping_total ), 2 );
$this->log_info(
array(
'is_revolut_pay' => $is_revolut_pay,
'cart_total_without_shipping' => $cart_subtotal,
'cart_total' => WC()->cart->get_total( '' ),
'wc_shipping_total' => WC()->cart->get_shipping_total(),
'revolut_shipping_total' => $revolut_order_shipping_total,
)
);
$descriptor = new WC_Revolut_Order_Descriptor( $is_revolut_pay ? $cart_subtotal : WC()->cart->get_total( '' ), get_woocommerce_currency(), null );
$public_id = $this->update_revolut_order( $descriptor, $revolut_public_id, true );
if ( $public_id !== $revolut_public_id ) {
throw new Exception( 'Can not update the Order' );
}
$revolut_order_id = $this->get_revolut_order_by_public_id( $public_id );
$revolut_order = $this->api_client->get( "/orders/$revolut_order_id" );
$this->log_info( 'revolut_order' );
$this->log_info( $revolut_order );
return $this->get_revolut_order_amount( $revolut_order );
}
/**
* Check is payment method available
*/
public function is_available() {
if ( ( 'yes' === $this->enabled && is_product() ) || ( $this->check_is_post_data_submitted( 'payment_method' ) && $this->get_post_request_data( 'payment_method' ) === $this->id ) ) {
return true;
}
$payment_request_button_locations = $this->get_option( 'payment_request_button_locations' );
if ( empty( $payment_request_button_locations ) ) {
$payment_request_button_locations = array();
}
if ( is_checkout() ) {
return 'yes' === $this->enabled && in_array( 'checkout', $payment_request_button_locations, true ) && ! $this->api_settings->is_sandbox();
}
return false;
}
/**
* Initialize Gateway Settings Form Fields
*/
public function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'revolut-gateway-for-woocommerce' ),
'label' => sprintf(
/* translators:%1s: %$2s: %3$s: %4$s: */
__( 'Enable Payment Request Buttons. (Apple Pay/Google Pay) %1$sBy using Apple Pay, you agree with %2$sApple\'s%3$s terms of service. (Apple Pay domain verification is performed automatically in live mode.) %4$s', 'revolut-gateway-for-woocommerce' ),
'
',
'',
'',
$this->check_authentication_required() ? '
' . __( 'Payment with Apple/Google Pay buttons wont be possible for guest users until Guest checkout is enabled', 'revolut-gateway-for-woocommerce' ) . '
' : '' ), 'type' => 'checkbox', 'description' => __( 'If enabled, users will be able to pay using Apple Pay (Safari & iOS), Google Pay (Chrome & Android) or native W3C Payment Requests if supported by the browser.', 'revolut-gateway-for-woocommerce' ), 'default' => 'yes', 'desc_tip' => true, ), 'title' => array( 'title' => __( 'Title', 'revolut-gateway-for-woocommerce' ), 'type' => 'text', 'description' => __( 'This controls the title that the user sees during checkout. Plugin will add the button\'s name (Apple Pay or Google Pay) before this title.', 'revolut-gateway-for-woocommerce' ), 'default' => '(via Revolut)', 'desc_tip' => true, ), 'payment_request_button_type' => array( 'title' => __( 'Payment Request Button Action', 'revolut-gateway-for-woocommerce' ), 'label' => __( 'Button Action', 'revolut-gateway-for-woocommerce' ), 'type' => 'select', 'description' => __( 'Select the button type you would like to show.', 'revolut-gateway-for-woocommerce' ), 'default' => 'buy', 'desc_tip' => true, 'options' => array( 'buy' => __( 'Buy', 'revolut-gateway-for-woocommerce' ), 'donate' => __( 'Donate', 'revolut-gateway-for-woocommerce' ), 'pay' => __( 'Pay', 'revolut-gateway-for-woocommerce' ), ), ), 'payment_request_button_theme' => array( 'title' => __( 'Payment Request Button Theme', 'revolut-gateway-for-woocommerce' ), 'label' => __( 'Button Theme', 'revolut-gateway-for-woocommerce' ), 'type' => 'select', 'description' => __( 'Select the button theme you would like to show.', 'revolut-gateway-for-woocommerce' ), 'default' => 'dark', 'desc_tip' => true, 'options' => array( 'dark' => __( 'Dark', 'revolut-gateway-for-woocommerce' ), 'light' => __( 'Light', 'revolut-gateway-for-woocommerce' ), 'light-outlined' => __( 'Light-Outline', 'revolut-gateway-for-woocommerce' ), ), ), 'payment_request_button_radius' => array( 'title' => __( 'Payment Request Button Radius', 'revolut-gateway-for-woocommerce' ), 'label' => __( 'Button Radius', 'revolut-gateway-for-woocommerce' ), 'type' => 'select', 'description' => __( 'Select the button radius you would like to show.', 'revolut-gateway-for-woocommerce' ), 'default' => 'none', 'desc_tip' => true, 'options' => array( 'none' => __( 'None', 'revolut-gateway-for-woocommerce' ), 'small' => __( 'Small', 'revolut-gateway-for-woocommerce' ), 'large' => __( 'Large', 'revolut-gateway-for-woocommerce' ), ), ), 'payment_request_button_size' => array( 'title' => __( 'Payment Request Button Size', 'revolut-gateway-for-woocommerce' ), 'label' => __( 'Button Size', 'revolut-gateway-for-woocommerce' ), 'type' => 'select', 'description' => __( 'Select the button size you would like to show.', 'revolut-gateway-for-woocommerce' ), 'default' => 'large', 'desc_tip' => true, 'options' => array( 'small' => __( 'Small', 'revolut-gateway-for-woocommerce' ), 'large' => __( 'Large', 'revolut-gateway-for-woocommerce' ), ), ), 'payment_request_button_locations' => array( 'title' => __( 'Payment Request Button Locations', 'revolut-gateway-for-woocommerce' ), 'type' => 'multiselect', 'description' => __( 'Select where you would like Payment Request Buttons to be displayed', 'revolut-gateway-for-woocommerce' ), 'desc_tip' => true, 'class' => 'wc-enhanced-select', 'options' => array( 'product' => __( 'Product', 'revolut-gateway-for-woocommerce' ), 'cart' => __( 'Cart', 'revolut-gateway-for-woocommerce' ), 'checkout' => __( 'Checkout', 'revolut-gateway-for-woocommerce' ), ), 'default' => array( 'product', 'cart' ), 'custom_attributes' => array( 'data-placeholder' => __( 'Select pages', 'revolut-gateway-for-woocommerce' ), ), ), ); if ( $this->get_option( 'apple_pay_merchant_onboarded' ) === 'no' ) { $this->form_fields['onboard_applepay'] = array( 'title' => __( 'Onboard shop domain for Apple Pay', 'revolut-gateway-for-woocommerce' ), 'type' => 'text', 'description' => 'Seems that there is a problem automatically onboarding your website for Apple Pay. You can also set this up manually by downloading this file and adding it to the root folder of your shop with the following uri /.well-known/apple-developer-merchantid-domain-association. To learn more about how to do this, visit our documentation