Skip to content

Commit ae2baf8

Browse files
joshuatflovo-h
andauthored
Add onboarding payment gateway setup methods (#1569)
* Add remote payment gateway setup methods * Add needs_setup method * Add missing return param * Filter the onboarding settings depending on types of keys entered * Use get_option to get latest updated setting values * Update connection URL method name * Only toggle test mode based on keys when referer is wc-admin * PHPCS: Use short array syntax to define arrays Before, the linter was failing the following PHPCS rule: - "Short array syntax must be used to define arrays" * PHPCS: Ignore sanitization for simple query parsing Before, PHPCS would warn that $_SERVER['HTTP_REFERRER'] was not being sanitized correctly. But, because it is being used for simple comparisons to check 'page' and 'task', the warning can be surpressed. * PHPCS: Add translators helper comment & update API details link Before, the Stripe API details link was referencing the Stripe docs when it was intended to reference the Stripe account's API details. This commit fixes that. Also, fixing a misaligned line of code. * Add comment to clarify the reason for short-circuiting early Before, the if-then block wasn't 100% clear as to what was going on. The added comment helps clarify what's going on in the code, a bit. * Code cleanup: Wrap long line into multiple lines Also, replace 'else if' with 'elseif'. * Enable Stripe payment method after connecting an account Before, the Stripe payment method was not automatically enabled after the merchant connected their Stripe account through "Connect" or by manually entering Stripe account keys. Now, the Stripe payment method is automatically enabled after the merchant connects their Stripe account through either approach. Fixes #2314 * Auto disable Stripe payment method once account keys are removed Before, if the merchant removed their Stripe account keys, the Stripe payment method would remain enabled. Now, it is automatically disabled if the account keys are removed. * Update changelog: Enable Stripe payment method after connecting Also: Add onboarding payment gateway setup methods. * Replacing "deleted" with "disconnected" in comment, for accuracy The word "deleted" implies that the Stripe account will be deleted when, in fact, it will be "disconnected". The rewording should prevent any confusion. Co-authored-by: Hector Lovo <[email protected]>
1 parent ede3e40 commit ae2baf8

File tree

5 files changed

+103
-4
lines changed

5 files changed

+103
-4
lines changed

changelog.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
*** Changelog ***
22

33
= 6.2.0 - 2022-xx-xx =
4-
4+
* Add - Add onboarding payment gateway setup methods.
5+
* Fix - Enable Stripe payment method after connecting account.
56

67
= 6.1.0 - 2022-01-26 =
78
* Tweak - Use the newly exposed LoadableMask component provided by WooCommerce Blocks to trigger the loading state for Payment Request Buttons.

includes/admin/class-wc-rest-stripe-account-keys-controller.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,15 @@ public function set_account_keys( WP_REST_Request $request ) {
193193
$settings = get_option( self::STRIPE_GATEWAY_SETTINGS_OPTION_NAME, [] );
194194

195195
// If all keys were empty, then is a new account; we need to set the test/live mode.
196-
$new_account = ! trim( $settings['publishable_key'] ) && ! trim( $settings['secret_key'] ) && ! trim( $settings['test_publishable_key'] ) && ! trim( $settings['test_secret_key'] );
196+
$new_account = ! trim( $settings['publishable_key'] )
197+
&& ! trim( $settings['secret_key'] )
198+
&& ! trim( $settings['test_publishable_key'] )
199+
&& ! trim( $settings['test_secret_key'] );
200+
// If all new keys are empty, then account is being disconnected. We should disable the payment gateway.
201+
$is_deleting_account = ! trim( $publishable_key )
202+
&& ! trim( $secret_key )
203+
&& ! trim( $test_publishable_key )
204+
&& ! trim( $test_secret_key );
197205

198206
$settings['publishable_key'] = is_null( $publishable_key ) ? $settings['publishable_key'] : $publishable_key;
199207
$settings['secret_key'] = is_null( $secret_key ) ? $settings['secret_key'] : $secret_key;
@@ -203,11 +211,14 @@ public function set_account_keys( WP_REST_Request $request ) {
203211
$settings['test_webhook_secret'] = is_null( $test_webhook_secret ) ? $settings['test_webhook_secret'] : $test_webhook_secret;
204212

205213
if ( $new_account ) {
214+
$settings['enabled'] = 'yes';
206215
if ( trim( $settings['publishable_key'] ) && trim( $settings['secret_key'] ) ) {
207216
$settings['testmode'] = 'no';
208-
} else if ( trim( $settings['test_publishable_key'] ) && trim( $settings['test_secret_key'] ) ) {
217+
} elseif ( trim( $settings['test_publishable_key'] ) && trim( $settings['test_secret_key'] ) ) {
209218
$settings['testmode'] = 'yes';
210219
}
220+
} elseif ( $is_deleting_account ) {
221+
$settings['enabled'] = 'no';
211222
}
212223

213224
update_option( self::STRIPE_GATEWAY_SETTINGS_OPTION_NAME, $settings );

includes/class-wc-gateway-stripe.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public function __construct() {
123123
add_action( 'set_logged_in_cookie', [ $this, 'set_cookie_on_current_request' ] );
124124
add_filter( 'woocommerce_get_checkout_payment_url', [ $this, 'get_checkout_payment_url' ], 10, 2 );
125125
add_filter( 'woocommerce_settings_api_sanitized_fields_' . $this->id, [ $this, 'settings_api_sanitized_fields' ] );
126+
add_filter( 'woocommerce_gateway_' . $this->id . '_settings_values', [ $this, 'update_onboarding_settings' ] );
126127

127128
// Note: display error is in the parent class.
128129
add_action( 'admin_notices', [ $this, 'display_errors' ], 9999 );
@@ -1162,4 +1163,88 @@ public function validate_account_statement_descriptor_field( $param, $value, $ma
11621163

11631164
return $value;
11641165
}
1166+
1167+
/**
1168+
* Get required setting keys for setup.
1169+
*
1170+
* @return array Array of setting keys used for setup.
1171+
*/
1172+
public function get_required_settings_keys() {
1173+
return [ 'publishable_key', 'secret_key' ];
1174+
}
1175+
1176+
/**
1177+
* Get the connection URL.
1178+
*
1179+
* @return string Connection URL.
1180+
*/
1181+
public function get_connection_url( $return_url = '' ) {
1182+
$api = new WC_Stripe_Connect_API();
1183+
$connect = new WC_Stripe_Connect( $api );
1184+
1185+
$url = $connect->get_oauth_url( $return_url );
1186+
1187+
return is_wp_error( $url ) ? null : $url;
1188+
}
1189+
1190+
/**
1191+
* Get help text to display during quick setup.
1192+
*
1193+
* @return string
1194+
*/
1195+
public function get_setup_help_text() {
1196+
return sprintf(
1197+
/* translators: %1$s Link to Stripe API details, %2$s Link to register a Stripe account */
1198+
__( 'Your API details can be obtained from your <a href="%1$s">Stripe account</a>. Don’t have a Stripe account? <a href="%2$s">Create one.</a>', 'woocommerce-gateway-stripe' ),
1199+
'https://dashboard.stripe.com/apikeys',
1200+
'https://dashboard.stripe.com/register'
1201+
);
1202+
}
1203+
1204+
/**
1205+
* Determine if the gateway still requires setup.
1206+
*
1207+
* @return bool
1208+
*/
1209+
public function needs_setup() {
1210+
return ! $this->get_option( 'publishable_key' ) || ! $this->get_option( 'secret_key' );
1211+
}
1212+
1213+
/**
1214+
* Updates the test mode based on keys provided when setting up the gateway via onboarding.
1215+
*
1216+
* @return array
1217+
*/
1218+
public function update_onboarding_settings( $settings ) {
1219+
if ( ! isset( $_SERVER['HTTP_REFERER'] ) ) {
1220+
return;
1221+
}
1222+
1223+
parse_str( wp_parse_url( $_SERVER['HTTP_REFERER'], PHP_URL_QUERY ), $queries ); // phpcs:ignore sanitization ok.
1224+
1225+
// Determine if merchant is onboarding (page='wc-admin' and task='payments').
1226+
if (
1227+
! isset( $queries ) ||
1228+
! isset( $queries['page'] ) ||
1229+
! isset( $queries['task'] ) ||
1230+
'wc-admin' !== $queries['page'] ||
1231+
'payments' !== $queries['task']
1232+
) {
1233+
return;
1234+
}
1235+
1236+
if ( ! empty( $settings['publishable_key'] ) && ! empty( $settings['secret_key'] ) ) {
1237+
if ( strpos( $settings['publishable_key'], 'pk_test_' ) === 0 || strpos( $settings['secret_key'], 'sk_test_' ) === 0 ) {
1238+
$settings['test_publishable_key'] = $settings['publishable_key'];
1239+
$settings['test_secret_key'] = $settings['secret_key'];
1240+
unset( $settings['publishable_key'] );
1241+
unset( $settings['secret_key'] );
1242+
$settings['testmode'] = 'yes';
1243+
} else {
1244+
$settings['testmode'] = 'no';
1245+
}
1246+
}
1247+
1248+
return $settings;
1249+
}
11651250
}

includes/connect/class-wc-stripe-connect.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ private function save_stripe_keys( $result ) {
128128
$prefix = $is_test ? 'test_' : '';
129129
$default_options = $this->get_default_stripe_config();
130130
$options = array_merge( $default_options, get_option( self::SETTINGS_OPTION, [] ) );
131+
$options['enabled'] = 'yes';
131132
$options['testmode'] = $is_test ? 'yes' : 'no';
132133
$options[ $prefix . 'publishable_key' ] = $result->publishableKey; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
133134
$options[ $prefix . 'secret_key' ] = $result->secretKey; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase

readme.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ If you get stuck, you can ask for help in the Plugin Forum.
129129
== Changelog ==
130130

131131
= 6.2.0 - 2022-xx-xx =
132-
132+
* Add - Add onboarding payment gateway setup methods.
133+
* Fix - Enable Stripe payment method after connecting account.
133134

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

0 commit comments

Comments
 (0)