'download', 'page' => 'edd-settings', 'tab' => 'gateways', 'section' => 'edd-stripe', ), admin_url( 'edit.php' ) ); /** * Filters the URL users are returned to after using Stripe Connect oAuth. * * @since 2.8.0 * * @param $return_url URL to return to. */ $return_url = apply_filters( 'edds_stripe_connect_return_url', $return_url ); $stripe_connect_url = add_query_arg( array( 'live_mode' => (int) ! edd_is_test_mode(), 'state' => str_pad( wp_rand( wp_rand(), PHP_INT_MAX ), 100, wp_rand(), STR_PAD_BOTH ), 'customer_site_url' => esc_url_raw( $return_url ), ), 'https://easydigitaldownloads.com/?edd_gateway_connect_init=stripe_connect' ); /** * Filters the URL to start the Stripe Connect oAuth flow. * * @since 2.8.0 * * @param $stripe_connect_url URL to oAuth proxy. */ $stripe_connect_url = apply_filters( 'edds_stripe_connect_url', $stripe_connect_url ); return $stripe_connect_url; } /** * Listens for Stripe Connect completion requests and saves the Stripe API keys. * * @since 2.6.14 */ function edds_process_gateway_connect_completion() { $redirect_screen = ! empty( $_GET['redirect_screen'] ) ? sanitize_text_field( $_GET['redirect_screen'] ) : ''; // A cancelled connection doesn't contain the completion or state values, but we do need to listen for the redirect_screen for the wizard. if ( isset( $_GET['edd_gateway_connect_error'] ) && filter_var( $_GET['edd_gateway_connect_error'], FILTER_VALIDATE_BOOLEAN ) && ! empty( $redirect_screen ) ) { $error_redirect = ''; switch ( $redirect_screen ) { case 'onboarding-wizard': $error_redirect = admin_url( 'edit.php?post_type=download&page=edd-onboarding-wizard¤t_step=payment_methods' ); break; } if ( ! empty( $error_redirect ) ) { wp_safe_redirect( esc_url_raw( $error_redirect ) ); exit; } } if ( ! isset( $_GET['edd_gateway_connect_completion'] ) || 'stripe_connect' !== $_GET['edd_gateway_connect_completion'] || ! isset( $_GET['state'] ) ) { return; } if ( ! current_user_can( 'manage_shop_settings' ) ) { return; } if ( headers_sent() ) { return; } $customer_site_url = admin_url( 'edit.php?post_type=download' ); if ( ! empty( $redirect_screen ) ) { $customer_site_url = add_query_arg( 'redirect_screen', $redirect_screen, $customer_site_url ); } $edd_credentials_url = add_query_arg( array( 'live_mode' => (int) ! edd_is_test_mode(), 'state' => sanitize_text_field( $_GET['state'] ), 'customer_site_url' => urlencode( $customer_site_url ), ), 'https://easydigitaldownloads.com/?edd_gateway_connect_credentials=stripe_connect' ); $response = wp_remote_get( esc_url_raw( $edd_credentials_url ) ); if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { $message = '
' . sprintf( /* translators: %1$s Opening anchor tag, do not translate. %2$s Closing anchor tag, do not translate. */ __( 'There was an error getting your Stripe credentials. Please %1$stry again%2$s. If you continue to have this problem, please contact support.', 'easy-digital-downloads' ), '', '' ) . '
'; wp_die( $message ); } $data = json_decode( $response['body'], true ); $data = $data['data']; if ( edd_is_test_mode() ) { edd_update_option( 'test_publishable_key', sanitize_text_field( $data['publishable_key'] ) ); edd_update_option( 'test_secret_key', sanitize_text_field( $data['secret_key'] ) ); } else { edd_update_option( 'live_publishable_key', sanitize_text_field( $data['publishable_key'] ) ); edd_update_option( 'live_secret_key', sanitize_text_field( $data['secret_key'] ) ); } edd_update_option( 'stripe_connect_account_id', sanitize_text_field( $data['stripe_user_id'] ) ); $redirect_url = admin_url( 'edit.php?post_type=download&page=edd-settings&tab=gateways§ion=edd-stripe' ); if ( ! empty( $redirect_screen ) ) { switch ( $redirect_screen ) { case 'onboarding-wizard': $redirect_url = admin_url( 'edit.php?post_type=download&page=edd-onboarding-wizard¤t_step=payment_methods' ); break; } } wp_safe_redirect( esc_url_raw( $redirect_url ) ); exit; } add_action( 'admin_init', 'edds_process_gateway_connect_completion' ); /** * Returns a URL to disconnect the current Stripe Connect account ID and keys. * * @since 2.8.0 * * @return string $stripe_connect_disconnect_url URL to disconnect an account ID and keys. */ function edds_stripe_connect_disconnect_url() { $stripe_connect_disconnect_url = add_query_arg( array( 'post_type' => 'download', 'page' => 'edd-settings', 'tab' => 'gateways', 'section' => 'edd-stripe', 'edds-stripe-disconnect' => true, ), admin_url( 'edit.php' ) ); /** * Filters the URL to "disconnect" the Stripe Account. * * @since 2.8.0 * * @param $stripe_connect_disconnect_url URL to remove the associated Account ID. */ $stripe_connect_disconnect_url = apply_filters( 'edds_stripe_connect_disconnect_url', $stripe_connect_disconnect_url ); $stripe_connect_disconnect_url = wp_nonce_url( $stripe_connect_disconnect_url, 'edds-stripe-connect-disconnect' ); return $stripe_connect_disconnect_url; } /** * Removes the associated Stripe Connect Account ID and keys. * * This does not revoke application permissions from the Stripe Dashboard, * it simply allows the "Connect with Stripe" flow to run again for a different account. * * @since 2.8.0 */ function edds_stripe_connect_process_disconnect() { // Do not need to handle this request, bail. if ( ! ( isset( $_GET['page'] ) && ( 'edd-settings' === $_GET['page'] || 'edd-onboarding-wizard' === $_GET['page'] ) ) || ! isset( $_GET['edds-stripe-disconnect'] ) ) { return; } // Current user cannot handle this request, bail. if ( ! current_user_can( 'manage_options' ) ) { return; } // No nonce, bail. if ( ! isset( $_GET['_wpnonce'] ) ) { return; } // Invalid nonce, bail. if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'edds-stripe-connect-disconnect' ) ) { return; } $options = array( 'stripe_connect_account_id', 'stripe_connect_account_country', 'test_publishable_key', 'test_secret_key', 'live_publishable_key', 'live_secret_key', ); foreach ( $options as $option ) { edd_delete_option( $option ); } $redirect = remove_query_arg( array( '_wpnonce', 'edds-stripe-disconnect', ) ); return wp_redirect( esc_url_raw( $redirect ) ); } add_action( 'admin_init', 'edds_stripe_connect_process_disconnect' ); /** * Updates the `stripe_connect_account_country` setting if using Stripe Connect * and no country information is available. * * @since 2.8.7 */ function edds_stripe_connect_maybe_refresh_account_country() { // Current user cannot modify options, bail. if ( false === current_user_can( 'manage_options' ) ) { return; } // Stripe Connect has not been used, bail. $account_id = edd_get_option( 'stripe_connect_account_id', '' ); if ( empty( $account_id ) ) { return; } // Account country is already set, bail. $account_country = edd_get_option( 'stripe_connect_account_country', '' ); if ( ! empty( $account_country ) ) { return; } try { $account = edds_api_request( 'Account', 'retrieve', $account_id ); if ( isset( $account->country ) ) { $account_country = sanitize_text_field( strtolower( $account->country ) ); edd_update_option( 'stripe_connect_account_country', $account_country ); } } catch ( \Exception $e ) { // Do nothing. } } add_action( 'admin_init', 'edds_stripe_connect_maybe_refresh_account_country' ); /** * Renders custom HTML for the "Stripe Connect" setting field in the Stripe Payment Gateway * settings subtab. * * Provides a way to use Stripe Connect and manually manage API keys. * * @since 2.8.0 */ function edds_stripe_connect_setting_field() { $stripe_connect_url = edds_stripe_connect_url(); $stripe_disconnect_url = edds_stripe_connect_disconnect_url(); $stripe_connect_account_id = edd_get_option( 'stripe_connect_account_id' ); $api_key = edd_is_test_mode() ? edd_get_option( 'test_publishable_key' ) : edd_get_option( 'live_publishable_key' ); ob_start(); ?>', '' ), array( 'a' => array( 'href' => true, 'target' => true, 'rel' => true, ) ) ); ?>
%s
%s
', esc_html__( 'Start accepting payments with Stripe by connecting your account. Stripe Connect helps ensure easier setup and improved security.', 'easy-digital-downloads' ), $connect_button ), 'type' => 'info', 'dismissible' => true, ) ); // Stripe Connect reconnect. /** translators: %s Test mode status. */ $test_mode_status = edd_is_test_mode() ? _x( 'enabled', 'gateway test mode status', 'easy-digital-downloads' ) : _x( 'disabled', 'gateway test mode status', 'easy-digital-downloads' ); $registry->add( 'stripe-connect-reconnect', array( 'message' => sprintf( '%s
%s
', sprintf( /* translators: %s Test mode status. Enabled or disabled. */ __( '"Test Mode" has been %s. Please verify your Stripe connection status.', 'easy-digital-downloads' ), $test_mode_status ), $connect_button ), 'type' => 'warning', 'dismissible' => true, ) ); } catch( Exception $e ) { return new WP_Error( 'edds-invalid-notices-registration', esc_html__( $e->getMessage() ) ); }; return true; } add_action( 'admin_init', 'edds_stripe_connect_admin_notices_register' ); /** * Conditionally prints registered notices. * * @since 2.6.19 */ function edds_stripe_connect_admin_notices_print() { // Current user needs capability to dismiss notices. if ( ! current_user_can( 'manage_options' ) ) { return; } $registry = edds_get_registry( 'admin-notices' ); if ( ! $registry ) { return; } $notices = new EDD_Stripe_Admin_Notices( $registry ); wp_enqueue_script( 'edds-admin-notices' ); try { $enabled_gateways = edd_get_enabled_payment_gateways(); $api_key = true === edd_is_test_mode() ? edd_get_option( 'test_secret_key' ) : edd_get_option( 'live_secret_key' ); $mode_toggle = isset( $_GET['edd-message'] ) && 'connect-to-stripe' === $_GET['edd-message']; if ( array_key_exists( 'stripe', $enabled_gateways ) && empty( $api_key ) ) { wp_enqueue_style( 'edd-stripe-admin-styles', EDDSTRIPE_PLUGIN_URL . 'assets/css/build/admin.min.css', array(), EDD_STRIPE_VERSION ); // Stripe Connect. if ( false === $mode_toggle ) { $notices->output( 'stripe-connect' ); // Stripe Connect reconnect. } else { $notices->output( 'stripe-connect-reconnect' ); } } } catch( Exception $e ) {} } add_action( 'admin_notices', 'edds_stripe_connect_admin_notices_print' );