Skip to content
Open
2 changes: 1 addition & 1 deletion classes/helpers/FrmAppHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class FrmAppHelper {
*
* @var int
*/
public static $db_version = 108;
public static $db_version = 109;

/**
* Used by the API add-on.
Expand Down
12 changes: 12 additions & 0 deletions classes/models/FrmMigrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ private function migrate_to_108() {
if ( is_string( $css ) && '' !== $css ) {
add_option( 'frm_saved_css_hash', hash( 'sha256', $css ), '', false );
}

delete_option( 'frmpro_css' );
}

Expand All @@ -569,6 +570,17 @@ private function migrate_to_107() {
}
}

/**
* Save the site URL for Stripe connections made before it was tracked, so a copied database can be detected.
*
* @since x.x
*
* @return void
*/
private function migrate_to_109() {
FrmStrpLiteConnectHelper::save_missing_connected_site_urls();
}

/**
* Add new wp_options row for custom setting.
*
Expand Down
2 changes: 1 addition & 1 deletion stripe/controllers/FrmStrpLiteAppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static function uninstall() {
);

$modes = array( 'test', 'live' );
$option_name_keys = array( 'account_id', 'client_password', 'server_password', 'details_submitted' );
$option_name_keys = array( 'account_id', 'client_password', 'server_password', 'details_submitted', 'site_url' );

foreach ( $modes as $mode ) {
foreach ( $option_name_keys as $key ) {
Expand Down
86 changes: 75 additions & 11 deletions stripe/helpers/FrmStrpLiteConnectHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private static function get_active_mode_option_name_suffix( $mode = 'auto' ) {
}

/**
* @param string $key 'account_id', 'client_password', 'server_password', 'details_submitted'.
* @param string $key 'account_id', 'client_password', 'server_password', 'details_submitted', 'site_url'.
* @param string $mode either 'auto', 'live', or 'test'.
*
* @return string
Expand Down Expand Up @@ -193,6 +193,55 @@ private static function get_stripe_details_submitted_option_name( $mode = 'auto'
return self::get_strp_connect_option_name( 'details_submitted', $mode );
}

/**
* Save the current site URL as the URL this Stripe connection belongs to.
*
* @since x.x
*
* @param string $mode 'live' or 'test'.
*
* @return void
*/
private static function save_connected_site_url( $mode ) {
update_option( self::get_strp_connect_option_name( 'site_url', $mode ), self::get_site_url_for_connect_server(), false );
}

/**
* Save the current site URL for connections made before it was tracked. This runs from the database upgrade.
* It is best-effort, since a database copied before the update records the URL of the copy instead.
*
* @since x.x
*
* @return void
*/
public static function save_missing_connected_site_urls() {
foreach ( array( 'test', 'live' ) as $mode ) {
if ( self::get_account_id( $mode ) && ! get_option( self::get_strp_connect_option_name( 'site_url', $mode ) ) ) {
self::save_connected_site_url( $mode );
}
}
}

/**
* Get the site URL saved on connect when it no longer matches, usually because the database was copied from another site.
*
* @since x.x
*
* @param string $mode 'live' or 'test'.
*
* @return false|string The site URL the account was connected on, or false when it matches or no URL is saved. A trailing slash (WPML can add one) is ignored.
*/
public static function get_mismatched_site_url( $mode ) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: this adds no PHPUnit coverage. The logic is plain option handling, so it is cheap to test. tests/phpunit/stripe/ has no test for FrmStrpLiteConnectHelper yet. Worth covering in get_mismatched_site_url():

  • no account ID returns false;
  • no saved URL returns false and saves the current URL (the backfill);
  • a matching URL returns false, including when one side has a trailing slash;
  • a different URL returns the saved one;
  • reset_stripe_connect_integration() deletes the site_url option;
  • FrmStrpLiteAppController::uninstall() deletes frm_strp_connect_site_url_test and _live.

$connected_url = self::get_account_id( $mode ) ? get_option( self::get_strp_connect_option_name( 'site_url', $mode ) ) : false;

// The database upgrade saves a URL for older connections. Without one there is nothing to compare.
if ( ! $connected_url || ! is_string( $connected_url ) ) {
return false;
}

return untrailingslashit( $connected_url ) === untrailingslashit( self::get_site_url_for_connect_server() ) ? false : $connected_url;
}

/**
* @return string
*/
Expand Down Expand Up @@ -221,6 +270,7 @@ public static function reset_stripe_connect_integration() {
delete_option( self::get_server_side_token_option_name( $mode ) );
delete_option( self::get_client_side_token_option_name( $mode ) );
delete_option( self::get_stripe_details_submitted_option_name( $mode ) );
delete_option( self::get_strp_connect_option_name( 'site_url', $mode ) );
}

/**
Expand Down Expand Up @@ -331,6 +381,7 @@ private static function check_server_for_oauth_account_id() {

if ( is_object( $data ) && ! empty( $data->account_id ) ) {
update_option( self::get_account_id_option_name( $mode ), $data->account_id, false );
self::save_connected_site_url( $mode );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: this line is the only place a new connection gets its saved URL, and no test covers it. Deleting it leaves all 7 new tests green, so a regression here would silently turn off the warning for every new connection. check_server_for_oauth_account_id() calls post_to_connect_server(), so a test needs that stubbed, for example with a pre_http_request filter returning an oauth_account_status response with an account_id, then asserting frm_strp_connect_site_url_test equals get_site_url_for_connect_server().


if ( ! empty( $data->details_submitted ) ) {
self::set_stripe_details_as_submitted( $mode );
Expand Down Expand Up @@ -473,18 +524,29 @@ private static function build_headers_for_post() {
$password = 'lite_' . self::get_uuid();
}

$site_url = self::get_site_url_for_connect_server();

// $password is either a Pro license or a uuid (See FrmUsage::uuid).
return array(
'Authorization' => 'Basic ' . base64_encode( $site_url . ':' . $password ),
);
}

/**
* Get the site URL the way the connect server identifies this site.
*
* @since x.x
*
* @return string
*/
private static function get_site_url_for_connect_server() {
$site_url = home_url();
$site_url = self::maybe_fix_wpml_url( $site_url );
// Remove protocol from url (our url cannot include the colon).
$site_url = preg_replace( '#^https?://#', '', $site_url );
// Remove port from url (mostly helpful in development).
$site_url = preg_replace( '/:[0-9]+/', '', $site_url );
$site_url = self::strip_lang_from_url( $site_url );

// $password is either a Pro license or a uuid (See FrmUsage::uuid).
return array(
'Authorization' => 'Basic ' . base64_encode( $site_url . ':' . $password ),
);
return self::strip_lang_from_url( $site_url );
}

/**
Expand Down Expand Up @@ -579,10 +641,12 @@ private static function render_settings() {
$html = '';

foreach ( $modes as $mode ) {
$account_id = self::get_account_id( $mode );
$connected = self::stripe_connect_is_setup( $mode );
$test = 'test' === $mode;
$title = $test ? __( 'TEST', 'formidable' ) : __( 'LIVE', 'formidable' );
$account_id = self::get_account_id( $mode );
$connected = self::stripe_connect_is_setup( $mode );
$test = 'test' === $mode;
$title = $test ? __( 'TEST', 'formidable' ) : __( 'LIVE', 'formidable' );
$mismatched_url = self::get_mismatched_site_url( $mode );
$current_url = self::get_site_url_for_connect_server();

ob_start();
require FrmStrpLiteAppHelper::plugin_path() . '/views/settings/connect.php';
Expand Down
12 changes: 12 additions & 0 deletions stripe/views/settings/connect.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@
or
<?php } ?>
<a class="frm_disconnect_stripe" href="#" style="font-size:13px"><?php esc_html_e( 'Disconnect', 'formidable' ); ?></a>
<?php if ( $mismatched_url ) { ?>
<div class="frm_error_style frm_strp_site_url_mismatch" role="alert" style="margin-top:10px;">
<?php
printf(
/* translators: %1$s: The site URL Stripe was connected on, %2$s: The current site URL */
esc_html__( 'This Stripe account was connected on %1$s, but this site is %2$s. This usually happens when a database is copied from one site to another. Please disconnect and reconnect Stripe so payments are linked to this site.', 'formidable' ),
'<strong>' . esc_html( $mismatched_url ) . '</strong>',
'<strong>' . esc_html( $current_url ) . '</strong>'
);
?>
</div>
<?php } ?>
<?php } else { ?>
<br/><br/>
<a class="frm_connect_with_oauth button-primary frm-button-primary">
Expand Down
16 changes: 16 additions & 0 deletions tests/phpunit/database/test_FrmMigrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,22 @@ public function test_migrate_to_108() {
$this->assertFalse( get_option( 'frm_saved_css_hash' ) );
}

/**
* @covers FrmMigrate::migrate_to_109
*/
public function test_migrate_to_109() {
update_option( 'frm_strp_connect_account_id_live', 'acct_live' );
delete_option( 'frm_strp_connect_site_url_live' );
delete_option( 'frm_strp_connect_account_id_test' );
delete_option( 'frm_strp_connect_site_url_test' );

$frmdb = new FrmMigrate();
$this->run_private_method( array( $frmdb, 'migrate_to_109' ), array() );

$this->assertNotEmpty( get_option( 'frm_strp_connect_site_url_live' ), 'An existing Stripe connection should get the current site URL.' );
$this->assertFalse( get_option( 'frm_strp_connect_site_url_test' ), 'A mode with no Stripe connection should not get a site URL.' );
}

public function test_collation() {
global $wpdb;

Expand Down
Loading