|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Display a notice to merchants to promote BNPL (Buy Now Pay Later) payment methods. |
| 4 | + * |
| 5 | + * @package WooCommerce\Payments\Admin |
| 6 | + */ |
| 7 | + |
| 8 | +use Automattic\WooCommerce\Admin\Notes\NoteTraits; |
| 9 | +use Automattic\WooCommerce\Admin\Notes\Note; |
| 10 | +use Automattic\WooCommerce\Admin\Notes\WC_Admin_Note; |
| 11 | + |
| 12 | +defined( 'ABSPATH' ) || exit; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class WC_Stripe_BNPL_Promotion_Note |
| 16 | + */ |
| 17 | +class WC_Stripe_BNPL_Promotion_Note { |
| 18 | + use NoteTraits; |
| 19 | + |
| 20 | + /** |
| 21 | + * Name of the note for use in the database. |
| 22 | + */ |
| 23 | + const NOTE_NAME = 'wc-stripe-bnpl-promotion-note'; |
| 24 | + |
| 25 | + /** |
| 26 | + * Link to enable the UPE in store. |
| 27 | + */ |
| 28 | + const LEARN_MORE_LINK = 'https://woocommerce.com/document/stripe/setup-and-configuration/additional-payment-methods/'; |
| 29 | + |
| 30 | + /** |
| 31 | + * Get the note. |
| 32 | + */ |
| 33 | + public static function get_note() { |
| 34 | + $note_class = self::get_note_class(); |
| 35 | + $note = new $note_class(); |
| 36 | + |
| 37 | + $note->set_title( __( 'Offer more ways to pay with Buy Now, Pay Later', 'woocommerce-gateway-stripe' ) ); |
| 38 | + $message = __( 'Flexible pay-over-time options can boost revenue by up to 14%.* Affirm and Klarna payments are auto-enabled with Stripe for eligible merchants.', 'woocommerce-gateway-stripe' ); |
| 39 | + $message .= '<br /><br />'; |
| 40 | + $message .= __( '*Source: Stripe 2024', 'woocommerce-gateway-stripe' ); |
| 41 | + $note->set_content( $message ); |
| 42 | + $note->set_type( $note_class::E_WC_ADMIN_NOTE_MARKETING ); |
| 43 | + $note->set_name( self::NOTE_NAME ); |
| 44 | + $note->set_source( 'woocommerce-gateway-stripe' ); |
| 45 | + $note->add_action( |
| 46 | + self::NOTE_NAME, |
| 47 | + __( 'Learn more', 'woocommerce-gateway-stripe' ), |
| 48 | + self::LEARN_MORE_LINK, |
| 49 | + $note_class::E_WC_ADMIN_NOTE_UNACTIONED, |
| 50 | + true |
| 51 | + ); |
| 52 | + |
| 53 | + return $note; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Get the class type to be used for the note. |
| 58 | + * |
| 59 | + * @return string |
| 60 | + */ |
| 61 | + private static function get_note_class() { |
| 62 | + if ( class_exists( 'Automattic\WooCommerce\Admin\Notes\Note' ) ) { |
| 63 | + return Note::class; |
| 64 | + } else { |
| 65 | + return WC_Admin_Note::class; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Init BNPL promotion notification |
| 71 | + * |
| 72 | + * @param WC_Stripe_Payment_Gateway $gateway |
| 73 | + * |
| 74 | + * @return void |
| 75 | + * @throws \Automattic\WooCommerce\Admin\Notes\NotesUnavailableException |
| 76 | + */ |
| 77 | + public static function init( WC_Stripe_Payment_Gateway $gateway ) { |
| 78 | + /** |
| 79 | + * No need to display the admin inbox note when |
| 80 | + * - Below version 9.7 |
| 81 | + * - Store has any BNPLs enabled |
| 82 | + * - Other BNPL extensions are active |
| 83 | + * - Stripe is not enabled |
| 84 | + */ |
| 85 | + if ( ! defined( 'WC_STRIPE_VERSION' ) || version_compare( WC_STRIPE_VERSION, '9.7', '<' ) ) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + $available_upe_payment_methods = $gateway->get_upe_enabled_payment_method_ids(); |
| 90 | + foreach ( WC_Stripe_Payment_Methods::BNPL_PAYMENT_METHODS as $bnpl_payment_method ) { |
| 91 | + if ( in_array( $bnpl_payment_method, $available_upe_payment_methods, true ) ) { |
| 92 | + return; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + $has_other_bnpl_plugins_active = false; |
| 97 | + $available_payment_gateways = WC()->payment_gateways->payment_gateways; |
| 98 | + $other_bnpl_gateway_ids = [ 'affirm', 'klarna_payments' ]; |
| 99 | + foreach ( $available_payment_gateways as $available_payment_gateway ) { |
| 100 | + if ( in_array( $available_payment_gateway->id, $other_bnpl_gateway_ids, true ) && 'yes' === $available_payment_gateway->enabled ) { |
| 101 | + $has_other_bnpl_plugins_active = true; |
| 102 | + break; |
| 103 | + } |
| 104 | + } |
| 105 | + if ( $has_other_bnpl_plugins_active ) { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); |
| 110 | + $stripe_enabled = isset( $stripe_settings['enabled'] ) && 'yes' === $stripe_settings['enabled']; |
| 111 | + if ( ! $stripe_enabled ) { |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + self::possibly_add_note(); |
| 116 | + } |
| 117 | +} |
0 commit comments