Skip to content

Commit 0c10be4

Browse files
authored
Add inbox notification for Link PM (#2462)
1 parent b5e6aa3 commit 0c10be4

9 files changed

+230
-41
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* Tweak - Remove remaining traces of old Stripe settings.
99
* Add - Add Boleto expiration setting.
1010
* Add - Declare incompatibility with HPOS.
11+
* Add - Add inbox notification for Link payment method.
1112

1213
= 6.8.0 - 2022-09-28 =
1314
* Fix - Minor adjustments for Custom Order Tables compatibility.

includes/admin/class-wc-rest-stripe-settings-controller.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,9 +528,12 @@ private function update_is_upe_enabled( WP_REST_Request $request ) {
528528
update_option( 'woocommerce_stripe_settings', $settings );
529529

530530
// including the class again because otherwise it's not present.
531-
if ( WC_Stripe_UPE_Compatibility::are_inbox_notes_supported() ) {
531+
if ( WC_Stripe_Inbox_Notes::are_inbox_notes_supported() ) {
532532
require_once WC_STRIPE_PLUGIN_PATH . '/includes/notes/class-wc-stripe-upe-availability-note.php';
533533
WC_Stripe_UPE_Availability_Note::possibly_delete_note();
534+
535+
require_once WC_STRIPE_PLUGIN_PATH . '/includes/notes/class-wc-stripe-upe-stripelink-note.php';
536+
WC_Stripe_UPE_StripeLink_Note::possibly_delete_note();
534537
}
535538
}
536539

includes/admin/class-wc-stripe-inbox-notes.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,9 @@ class WC_Stripe_Inbox_Notes {
1717
const CAMPAIGN_2020_CLEANUP_ACTION = 'wc_stripe_apple_pay_2020_cleanup';
1818

1919
public function __construct() {
20-
if ( ! WC_Stripe_UPE_Compatibility::are_inbox_notes_supported() ) {
21-
return;
22-
}
23-
2420
add_action( self::POST_SETUP_SUCCESS_ACTION, [ self::class, 'create_marketing_note' ] );
2521
add_action( self::CAMPAIGN_2020_CLEANUP_ACTION, [ self::class, 'cleanup_campaign_2020' ] );
26-
add_action( 'admin_init', [ self::class, 'create_upe_availability_note' ] );
22+
add_action( 'admin_init', [ self::class, 'create_upe_notes' ] );
2723

2824
// Schedule a 2020 holiday campaign cleanup action if needed.
2925
// First, check to see if we are still before the cutoff.
@@ -36,9 +32,30 @@ public function __construct() {
3632
}
3733
}
3834

39-
public static function create_upe_availability_note() {
35+
public static function are_inbox_notes_supported() {
36+
if ( ! class_exists( 'WC_Data_Store' ) ) {
37+
return false;
38+
}
39+
40+
try {
41+
WC_Data_Store::load( 'admin-note' );
42+
} catch ( Exception $e ) {
43+
return false;
44+
}
45+
46+
return trait_exists( 'Automattic\WooCommerce\Admin\Notes\NoteTraits' ) && class_exists( 'Automattic\WooCommerce\Admin\Notes\Note' );
47+
}
48+
49+
public static function create_upe_notes() {
50+
if ( ! self::are_inbox_notes_supported() ) {
51+
return;
52+
}
53+
4054
require_once WC_STRIPE_PLUGIN_PATH . '/includes/notes/class-wc-stripe-upe-availability-note.php';
4155
WC_Stripe_UPE_Availability_Note::init();
56+
57+
require_once WC_STRIPE_PLUGIN_PATH . '/includes/notes/class-wc-stripe-upe-stripelink-note.php';
58+
WC_Stripe_UPE_StripeLink_Note::init( new WC_Stripe_UPE_Payment_Gateway() );
4259
}
4360

4461
public static function get_campaign_2020_cutoff() {
@@ -121,7 +138,7 @@ public static function should_show_marketing_note() {
121138
*/
122139
public static function create_marketing_note() {
123140
// Make sure conditions for this note still hold.
124-
if ( ! self::should_show_marketing_note() ) {
141+
if ( ! self::should_show_marketing_note() || ! self::are_inbox_notes_supported() ) {
125142
return;
126143
}
127144

@@ -169,6 +186,10 @@ public static function create_failure_note() {
169186
* on/about 2020 Dec 22.
170187
*/
171188
public static function cleanup_campaign_2020() {
189+
if ( ! self::are_inbox_notes_supported() ) {
190+
return;
191+
}
192+
172193
$admin_notes_class = WC_Stripe_Woo_Compat_Utils::get_notes_class();
173194
if ( ! class_exists( $admin_notes_class ) || ! class_exists( 'WC_Data_Store' ) ) {
174195
return;

includes/admin/class-wc-stripe-rest-upe-flag-toggle-controller.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,12 @@ public function set_flag( WP_REST_Request $request ) {
7878
update_option( 'woocommerce_stripe_settings', $settings );
7979

8080
// including the class again because otherwise it's not present.
81-
if ( WC_Stripe_UPE_Compatibility::are_inbox_notes_supported() ) {
81+
if ( WC_Stripe_Inbox_Notes::are_inbox_notes_supported() ) {
8282
require_once WC_STRIPE_PLUGIN_PATH . '/includes/notes/class-wc-stripe-upe-availability-note.php';
8383
WC_Stripe_UPE_Availability_Note::possibly_delete_note();
84+
85+
require_once WC_STRIPE_PLUGIN_PATH . '/includes/notes/class-wc-stripe-upe-stripelink-note.php';
86+
WC_Stripe_UPE_StripeLink_Note::possibly_delete_note();
8487
}
8588

8689
return new WP_REST_Response( [ 'result' => 'success' ], 200 );

includes/class-wc-stripe-upe-compatibility.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,4 @@ public static function is_wp_supported() {
1515
public static function is_wc_supported() {
1616
return version_compare( WC_VERSION, self::MIN_WC_VERSION, '>=' );
1717
}
18-
19-
public static function are_inbox_notes_supported() {
20-
if ( ! class_exists( 'WC_Data_Store' ) ) {
21-
return false;
22-
}
23-
24-
try {
25-
WC_Data_Store::load( 'admin-note' );
26-
} catch ( Exception $e ) {
27-
return false;
28-
}
29-
30-
return trait_exists( 'Automattic\WooCommerce\Admin\Notes\NoteTraits' ) && class_exists( 'Automattic\WooCommerce\Admin\Notes\Note' );
31-
}
3218
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/**
3+
* Display a notice to merchants to inform about Stripe Link.
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_UPE_StripeLink_Note
16+
*/
17+
class WC_Stripe_UPE_StripeLink_Note {
18+
use NoteTraits;
19+
20+
/**
21+
* Name of the note for use in the database.
22+
*/
23+
const NOTE_NAME = 'wc-stripe-upe-stripelink-note';
24+
25+
/**
26+
* Link to Stripe Link documentation.
27+
*/
28+
const NOTE_DOCUMENTATION_URL = 'https://woocommerce.com/document/stripe/#stripe-link';
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( __( 'Increase conversion at checkout', 'woocommerce-gateway-stripe' ) );
38+
$note->set_content( __( 'Reduce cart abandonment and create a frictionless checkout experience with Link by Stripe. Link autofills your customer’s payment and shipping details so they can check out in just six seconds with the Link optimized experience.', 'woocommerce-gateway-stripe' ) );
39+
40+
$note->set_type( $note_class::E_WC_ADMIN_NOTE_INFORMATIONAL );
41+
$note->set_name( self::NOTE_NAME );
42+
$note->set_source( 'woocommerce-gateway-stripe' );
43+
$note->add_action(
44+
self::NOTE_NAME,
45+
__( 'Set up now', 'woocommerce-gateway-stripe' ),
46+
self::NOTE_DOCUMENTATION_URL,
47+
$note_class::E_WC_ADMIN_NOTE_UNACTIONED,
48+
true
49+
);
50+
51+
return $note;
52+
}
53+
54+
/**
55+
* Get the class type to be used for the note.
56+
*
57+
* @return string
58+
*/
59+
private static function get_note_class() {
60+
if ( class_exists( 'Automattic\WooCommerce\Admin\Notes\Note' ) ) {
61+
return Note::class;
62+
} else {
63+
return WC_Admin_Note::class;
64+
}
65+
}
66+
67+
/**
68+
* Init Link payment method notification
69+
*
70+
* @param WC_Stripe_UPE_Payment_Gateway $gateway
71+
*
72+
* @return void
73+
* @throws \Automattic\WooCommerce\Admin\Notes\NotesUnavailableException
74+
*/
75+
public static function init( WC_Stripe_UPE_Payment_Gateway $gateway ) {
76+
if ( ! WC_Stripe_Feature_Flags::is_upe_checkout_enabled() ) {
77+
return;
78+
}
79+
80+
// Check if Link payment is available.
81+
$available_upe_payment_methods = $gateway->get_upe_available_payment_methods();
82+
83+
if ( ! in_array( WC_Stripe_UPE_Payment_Method_Link::STRIPE_ID, $available_upe_payment_methods, true ) ) {
84+
return;
85+
}
86+
87+
// If store currency is not USD, skip
88+
if ( 'USD' !== get_woocommerce_currency() ) {
89+
return;
90+
}
91+
92+
// Retrieve enabled payment methods at checkout.
93+
$enabled_payment_methods = $gateway->get_upe_enabled_at_checkout_payment_method_ids();
94+
// If card payment method is not enabled, skip. If Link payment method is enabled, skip.
95+
if (
96+
! in_array( WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID, $enabled_payment_methods, true ) ||
97+
in_array( WC_Stripe_UPE_Payment_Method_Link::STRIPE_ID, $enabled_payment_methods, true )
98+
) {
99+
return;
100+
}
101+
102+
self::possibly_add_note();
103+
}
104+
}

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,6 @@ If you get stuck, you can ask for help in the Plugin Forum.
131131
= 7.0.0 - 2022-xx-xx =
132132
* Add - Auto-complete first and last name on checkout form when using Link payment method.
133133
* Add - Allow subscription orders to be paid with Link payment method.
134+
* Add - Add inbox notification for Link payment method.
134135

135136
[See changelog for all versions](https://raw.githubusercontent.com/woocommerce/woocommerce-gateway-stripe/trunk/changelog.txt).

tests/phpunit/test-class-wc-stripe-notes.php

Lines changed: 84 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,35 @@ public function tear_down() {
4848
}
4949

5050
public function test_create_upe_availability_note() {
51-
WC_Stripe_Inbox_Notes::create_upe_availability_note();
5251

53-
$note_id = WC_Stripe_UPE_Availability_Note::NOTE_NAME;
52+
WC_Stripe_Inbox_Notes::create_upe_notes();
5453
$admin_note_store = WC_Data_Store::load( 'admin-note' );
55-
$this->assertSame( 1, count( $admin_note_store->get_notes_with_name( $note_id ) ) );
54+
$this->assertSame( 1, count( $admin_note_store->get_notes_with_name( WC_Stripe_UPE_Availability_Note::NOTE_NAME ) ) );
5655
}
5756

58-
public function test_create_upe_availability_note_does_not_create_note_when_upe_preview_is_disabled() {
57+
public function test_create_upe_stripelink_note() {
58+
update_option(
59+
'woocommerce_stripe_settings',
60+
[
61+
'enabled' => 'yes',
62+
'upe_checkout_experience_enabled' => 'yes',
63+
]
64+
);
65+
WC_Stripe_Inbox_Notes::create_upe_notes();
66+
$admin_note_store = WC_Data_Store::load( 'admin-note' );
67+
$this->assertSame( 1, count( $admin_note_store->get_notes_with_name( WC_Stripe_UPE_StripeLink_Note::NOTE_NAME ) ) );
68+
}
69+
70+
public function test_create_upe_notes_does_not_create_note_when_upe_preview_is_disabled() {
5971
update_option( '_wcstripe_feature_upe', 'no' );
6072

61-
WC_Stripe_Inbox_Notes::create_upe_availability_note();
73+
WC_Stripe_Inbox_Notes::create_upe_notes();
6274

63-
$note_id = WC_Stripe_UPE_Availability_Note::NOTE_NAME;
6475
$admin_note_store = WC_Data_Store::load( 'admin-note' );
65-
$this->assertSame( 0, count( $admin_note_store->get_notes_with_name( $note_id ) ) );
76+
$this->assertSame( 0, count( $admin_note_store->get_notes_with_name( WC_Stripe_UPE_Availability_Note::NOTE_NAME ) ) );
6677
}
6778

68-
public function test_create_upe_availability_note_does_not_create_note_when_upe_is_enbled() {
79+
public function test_create_upe_notes_does_not_create_availability_note_when_upe_is_enbled() {
6980
update_option(
7081
'woocommerce_stripe_settings',
7182
[
@@ -74,14 +85,14 @@ public function test_create_upe_availability_note_does_not_create_note_when_upe_
7485
]
7586
);
7687

77-
WC_Stripe_Inbox_Notes::create_upe_availability_note();
88+
WC_Stripe_Inbox_Notes::create_upe_notes();
7889

79-
$note_id = WC_Stripe_UPE_Availability_Note::NOTE_NAME;
8090
$admin_note_store = WC_Data_Store::load( 'admin-note' );
81-
$this->assertSame( 0, count( $admin_note_store->get_notes_with_name( $note_id ) ) );
91+
$this->assertSame( 0, count( $admin_note_store->get_notes_with_name( WC_Stripe_UPE_Availability_Note::NOTE_NAME ) ) );
92+
$this->assertSame( 1, count( $admin_note_store->get_notes_with_name( WC_Stripe_UPE_StripeLink_Note::NOTE_NAME ) ) );
8293
}
8394

84-
public function test_create_upe_availability_note_does_not_create_note_when_stripe_is_disabled() {
95+
public function test_create_upe_notes_does_not_create_note_when_stripe_is_disabled() {
8596
update_option(
8697
'woocommerce_stripe_settings',
8798
[
@@ -90,14 +101,14 @@ public function test_create_upe_availability_note_does_not_create_note_when_stri
90101
]
91102
);
92103

93-
WC_Stripe_Inbox_Notes::create_upe_availability_note();
104+
WC_Stripe_Inbox_Notes::create_upe_notes();
94105

95-
$note_id = WC_Stripe_UPE_Availability_Note::NOTE_NAME;
96106
$admin_note_store = WC_Data_Store::load( 'admin-note' );
97-
$this->assertSame( 0, count( $admin_note_store->get_notes_with_name( $note_id ) ) );
107+
$this->assertSame( 0, count( $admin_note_store->get_notes_with_name( WC_Stripe_UPE_Availability_Note::NOTE_NAME ) ) );
108+
$this->assertSame( 0, count( $admin_note_store->get_notes_with_name( WC_Stripe_UPE_StripeLink_Note::NOTE_NAME ) ) );
98109
}
99110

100-
public function test_create_upe_availability_note_does_not_create_note_when_upe_has_been_manually_disabled() {
111+
public function test_create_upe_notes_does_not_create_note_when_upe_has_been_manually_disabled() {
101112
update_option(
102113
'woocommerce_stripe_settings',
103114
[
@@ -106,9 +117,65 @@ public function test_create_upe_availability_note_does_not_create_note_when_upe_
106117
]
107118
);
108119

109-
WC_Stripe_Inbox_Notes::create_upe_availability_note();
120+
WC_Stripe_Inbox_Notes::create_upe_notes();
110121

111122
$admin_note_store = WC_Data_Store::load( 'admin-note' );
112123
$this->assertSame( 0, count( $admin_note_store->get_notes_with_name( WC_Stripe_UPE_Availability_Note::NOTE_NAME ) ) );
124+
$this->assertSame( 0, count( $admin_note_store->get_notes_with_name( WC_Stripe_UPE_StripeLink_Note::NOTE_NAME ) ) );
125+
}
126+
127+
public function test_create_stripelink_note_unavailable_if_cc_not_enabled() {
128+
update_option(
129+
'woocommerce_stripe_settings',
130+
[
131+
'enabled' => 'yes',
132+
'upe_checkout_experience_enabled' => 'yes',
133+
]
134+
);
135+
136+
$this->set_enabled_payment_methods( [ 'test' ] );
137+
WC_Stripe_Inbox_Notes::create_upe_notes();
138+
139+
$admin_note_store = WC_Data_Store::load( 'admin-note' );
140+
$this->assertSame( 0, count( $admin_note_store->get_notes_with_name( WC_Stripe_UPE_StripeLink_Note::NOTE_NAME ) ) );
141+
}
142+
143+
public function test_create_stripelink_note_unavailable_link_enabled() {
144+
update_option(
145+
'woocommerce_stripe_settings',
146+
[
147+
'enabled' => 'yes',
148+
'upe_checkout_experience_enabled' => 'yes',
149+
'testmode' => 'yes',
150+
]
151+
);
152+
153+
$this->set_enabled_payment_methods( [ 'card', 'link' ] );
154+
155+
update_option(
156+
'woocommerce_stripe_settings',
157+
array_merge(
158+
get_option( 'woocommerce_stripe_settings' ),
159+
[
160+
'upe_checkout_experience_accepted_payments' => [ 'card', 'link' ],
161+
]
162+
)
163+
);
164+
WC_Stripe_Inbox_Notes::create_upe_notes();
165+
166+
$admin_note_store = WC_Data_Store::load( 'admin-note' );
167+
$this->assertSame( 0, count( $admin_note_store->get_notes_with_name( WC_Stripe_UPE_StripeLink_Note::NOTE_NAME ) ) );
168+
}
169+
170+
private function set_enabled_payment_methods( $payment_methods ) {
171+
update_option(
172+
'woocommerce_stripe_settings',
173+
array_merge(
174+
get_option( 'woocommerce_stripe_settings' ),
175+
[
176+
'upe_checkout_experience_accepted_payments' => $payment_methods,
177+
]
178+
)
179+
);
113180
}
114181
}

woocommerce-gateway-stripe.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,11 +721,14 @@ function add_woocommerce_inbox_variant() {
721721
function wcstripe_deactivated() {
722722
// admin notes are not supported on older versions of WooCommerce.
723723
require_once WC_STRIPE_PLUGIN_PATH . '/includes/class-wc-stripe-upe-compatibility.php';
724-
if ( WC_Stripe_UPE_Compatibility::are_inbox_notes_supported() ) {
724+
if ( WC_Stripe_Inbox_Notes::are_inbox_notes_supported() ) {
725725
// requirements for the note
726726
require_once WC_STRIPE_PLUGIN_PATH . '/includes/class-wc-stripe-feature-flags.php';
727727
require_once WC_STRIPE_PLUGIN_PATH . '/includes/notes/class-wc-stripe-upe-availability-note.php';
728728
WC_Stripe_UPE_Availability_Note::possibly_delete_note();
729+
730+
require_once WC_STRIPE_PLUGIN_PATH . '/includes/notes/class-wc-stripe-upe-stripelink-note.php';
731+
WC_Stripe_UPE_StripeLink_Note::possibly_delete_note();
729732
}
730733
}
731734
register_deactivation_hook( __FILE__, 'wcstripe_deactivated' );

0 commit comments

Comments
 (0)