' . __( 'You Stripe Publishable and Secret Keys are not set correctly. You can connect to Stripe and correct them from here.', 'checkout-plugins-stripe-woo' ) . '
' . __( 'Your Stripe account has been connected to your WooCommerce store. You may now accept payments in live and test mode.', 'checkout-plugins-stripe-woo' ) . '
' . __( 'Error: The current user doesn\'t have sufficient permissions to perform this action. Please reload the page and try again.', 'checkout-plugins-stripe-woo' ) . '
' );
}
/**
* This method is used to update stripe options to the database.
*
* @since 1.0.0
*
* @param array $options settings array of the stripe.
*/
public function update_options( $options ) {
if ( ! is_array( $options ) ) {
return false;
}
if ( ! current_user_can( 'manage_options' ) ) {
return false;
}
foreach ( $options as $key => $value ) {
update_option( $key, $value );
}
}
/**
* This method is used to retrieve webhooks.
*
* @since 1.5.0
*
* @param string $id Webhook id.
*
* @return obj
*/
public function retrieve_webhooks( $id ) {
$stripe_api = new Stripe_Api();
$response = $stripe_api->webhooks( 'retrieve', [ $id ] );
if ( isset( $response['success'] ) && 1 === absint( $response['success'] ) && isset( $response['data'] ) ) {
return $response['data'];
} else {
Logger::error( sprintf( 'Error retrieve Stripe webhook. Reason: %2$s', Helper::get_payment_mode(), $response['message'] ), true );
return false;
}
}
/**
* This method is used to delete webhooks.
*
* @since 1.5.0
*
* @param string $id Webhook id.
* @param string $mode Webhook mode.
*
* @return void
*/
public function delete_webhooks( $id, $mode = '' ) {
if ( empty( $id ) ) {
return;
}
if ( ! empty( $mode ) && Helper::get_payment_mode() !== $mode ) {
$this->secret_key = '';
if ( 'live' === $mode ) {
$this->secret_key = Helper::get_setting( 'cpsw_secret_key' );
} else {
$this->secret_key = Helper::get_setting( 'cpsw_test_secret_key' );
}
if ( ! empty( $this->secret_key ) ) {
add_filter(
'cpsw_get_secret_key',
function() {
return $this->secret_key;
},
99
);
}
}
$stripe_api = new Stripe_Api();
$response = $stripe_api->webhooks( 'delete', [ $id ] );
if ( isset( $response['success'] ) && 1 === absint( $response['success'] ) && isset( $response['data'] ) ) {
$this->remove_webhook_secret_settings_keys( $mode );
return true;
} else {
Logger::error( sprintf( 'Error deleting Stripe webhook. Reason: %2$s', Helper::get_payment_mode(), $response['message'] ), true );
$this->remove_webhook_secret_settings_keys( $mode );
return $response['message'];
}
}
/**
* Remove webhook secret settings keys
*
* @since 1.5.0
* @param string $mode Webhook mode.
* @return void
*/
public function remove_webhook_secret_settings_keys( $mode ) {
if ( 'live' === $mode ) {
update_option( 'cpsw_live_webhook_secret', '' );
update_option( 'cpsw_live_webhook_id', '' );
} elseif ( 'test' === $mode ) {
update_option( 'cpsw_test_webhook_secret', '' );
update_option( 'cpsw_test_webhook_id', '' );
}
}
/**
* This method is used to create webhooks.
*
* @since 1.5.0
*
* @param string $type Create webhook type.
* @param string $mode Create webhook mode.
* @return void
*/
public function create_webhooks( $type, $mode = '' ) {
if ( ! isset( $type ) ) {
return;
}
if ( false === $this->is_stripe_connected() ) {
return;
}
if ( empty( $mode ) ) {
$modes = [ 'test', 'live' ];
} else {
$modes = [ $mode ];
}
$webhooks_created = 0;
$error_message = '';
foreach ( $modes as $mode ) {
if ( Helper::get_payment_mode() !== $mode ) {
$this->secret_key = '';
if ( 'live' === $mode ) {
$this->secret_key = Helper::get_setting( 'cpsw_secret_key' );
} else {
$this->secret_key = Helper::get_setting( 'cpsw_test_secret_key' );
}
if ( ! empty( $this->secret_key ) ) {
add_filter(
'cpsw_get_secret_key',
function() {
return $this->secret_key;
},
99
);
}
}
$stripe_api = new Stripe_Api();
$data = [
'api_version' => '2020-03-02',
'url' => esc_url( get_home_url() . '/wp-json/cpsw/v1/webhook' ),
'enabled_events' => [
'charge.failed',
'charge.succeeded',
'source.chargeable',
'payment_intent.succeeded',
'charge.refunded',
'charge.dispute.created',
'charge.dispute.closed',
'review.opened',
'review.closed',
],
];
$response = $stripe_api->webhooks( 'create', [ $data ] );
if ( isset( $response['success'] ) && 1 === absint( $response['success'] ) && isset( $response['data'] ) ) {
if ( 'live' === $mode ) {
update_option( 'cpsw_live_webhook_secret', $response['data']->secret );
update_option( 'cpsw_live_webhook_id', $response['data']->id );
} elseif ( 'test' === $mode ) {
update_option( 'cpsw_test_webhook_secret', $response['data']->secret );
update_option( 'cpsw_test_webhook_id', $response['data']->id );
}
$webhooks_created++;
} else {
$error_message = $response['message'];
Logger::error( sprintf( 'Error creating Stripe webhook. Mode: %1$s. Reason: %2$s', $mode, $response['message'] ), true );
}
}
if ( count( $modes ) === $webhooks_created ) {
return true;
}
return $error_message;
}
/**
* This method is used to stripe connect button.
*
* @since 1.0.0
*
* @param string $value Field name in string.
*/
public function stripe_connect( $value ) {
if ( true === $this->is_stripe_connected() ) {
return;
}
$label = __( 'Connect with Stripe', 'checkout-plugins-stripe-woo' );
$label_status = __( 'We make it easy to connect Stripe to your site. Click the Connect button to go through our connect flow.', 'checkout-plugins-stripe-woo' );
$sec_var = '';
$manual_link = true;
/**
* Action before conection with stripe.
*
* @since 1.3.0
*/
do_action( 'cpsw_before_connection_with_stripe' );
?>
' . $tooltip_html . '';
} elseif ( $tooltip_html ) {
$tooltip_html = wc_help_tip( $tooltip_html );
}
if ( 'live' === Helper::get_payment_mode() && ! empty( Helper::get_setting( 'cpsw_pub_key' ) ) ) {
$label = __( 'Re-Connect to Stripe', 'checkout-plugins-stripe-woo' );
$sec_var = '&rec=yes';
$label_status = ' ' . __( 'Your Stripe account has been connected. You can now accept Live and Test payments. You can Re-Connect if you want to recycle your API keys for security.', 'checkout-plugins-stripe-woo' );
} elseif ( 'test' === Helper::get_payment_mode() && ! empty( Helper::get_setting( 'cpsw_test_pub_key' ) ) ) {
$label = __( 'Re-Connect to Stripe', 'checkout-plugins-stripe-woo' );
$sec_var = '&rec=yes';
$label_status = ' ' . __( 'Your Stripe account has been connected. You can now accept Live and Test payments. You can Re-Connect if you want to recycle your API keys for security.', 'checkout-plugins-stripe-woo' );
} else {
$label = __( 'Connect to Stripe', 'checkout-plugins-stripe-woo' );
$label_status = __( 'We make it easy to connect Stripe to your site. Click the Connect button to go through our connect flow.', 'checkout-plugins-stripe-woo' );
$sec_var = '';
$manual_link = true;
}
/**
* Action before stripe connect button with stripe.
*
* @since 1.3.0
*
* @param array $value Connect button values.
* @param array $data Field description data.
*/
do_action( 'cpsw_before_stripe_connect_button', $value, $data );
?>
get_settings()
*
* @return void
*/
public function settings_tab() {
woocommerce_admin_fields( $this->get_settings() );
}
/**
* Uses the WooCommerce options API to save settings via the @see woocommerce_update_options() function.
*
* @since 1.0.0
*
* @return void
*/
public function update_settings() {
woocommerce_update_options( $this->get_settings() );
}
/**
* Generates Stripe Authorization URL for onboarding process
*
* @param boolean $redirect_url destination url to redirect after stripe connect.
* @return string
* @since 1.3.0
*/
public function get_stripe_connect_url( $redirect_url = false ) {
if ( ! $redirect_url ) {
$redirect_url = admin_url( 'admin.php?page=wc-settings&tab=cpsw_api_settings' );
}
$client_id = 'ca_KOXfLe7jv1m4L0iC4KNEMc5fT8AXWWuL';
return OAuth::authorizeUrl(
apply_filters(
'cpsw_stripe_connect_url_data',
[
'response_type' => 'code',
'client_id' => $client_id,
'stripe_landing' => 'login',
'always_prompt' => 'true',
'scope' => 'read_write',
// need to use base64_encode to encode data to be sent to server.
'state' => base64_encode( //phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
wp_json_encode(
[
'redirect' => add_query_arg( 'cpsw_connect_nonce', wp_create_nonce( 'stripe-connect' ), $redirect_url ),
]
)
),
]
)
);
}
/**
* This method is used to initialize all stripe configuration fields.
*
* @since 1.0.0
*
* @return mixed
*/
public function get_settings() {
$settings = [
'section_title' => [
'name' => __( 'Stripe API Settings', 'checkout-plugins-stripe-woo' ),
'type' => 'title',
'id' => 'cpsw_title',
],
'connection_status' => [
'name' => __( 'Stripe Connect', 'checkout-plugins-stripe-woo' ),
'type' => 'cpsw_stripe_connect',
'value' => '--',
'class' => 'wc_cpsw_connect_btn',
'id' => 'cpsw_stripe_connect',
],
'account_id' => [
'name' => __( 'Connection Status', 'checkout-plugins-stripe-woo' ),
'type' => 'cpsw_account_id',
'value' => '--',
'class' => 'account_id',
'desc_tip' => __( 'This is your Stripe Connect ID and serves as a unique identifier.', 'checkout-plugins-stripe-woo' ),
'desc' => __( 'This is your Stripe Connect ID and serves as a unique identifier.', 'checkout-plugins-stripe-woo' ),
'id' => 'cpsw_account_id',
],
'account_keys' => [
'name' => __( 'Stripe Account Keys', 'checkout-plugins-stripe-woo' ),
'type' => 'cpsw_account_keys',
'class' => 'wc_stripe_acc_keys',
'desc' => __( 'This will disable any connection to Stripe.', 'checkout-plugins-stripe-woo' ),
'id' => 'cpsw_account_keys',
],
'connect_button' => [
'name' => __( 'Connect Stripe Account', 'checkout-plugins-stripe-woo' ),
'type' => 'cpsw_connect_btn',
'class' => 'wc_cpsw_connect_btn',
'desc' => __( 'We make it easy to connect Stripe to your site. Click the Connect button to go through our connect flow.', 'checkout-plugins-stripe-woo' ),
'id' => 'cpsw_connect_btn',
],
'live_pub_key' => [
'name' => __( 'Live Publishable Key', 'checkout-plugins-stripe-woo' ),
'type' => 'text',
'desc_tip' => __( 'Your publishable key is used to initialize Stripe assets.', 'checkout-plugins-stripe-woo' ),
'id' => 'cpsw_pub_key',
],
'live_secret_key' => [
'name' => __( 'Live Secret Key', 'checkout-plugins-stripe-woo' ),
'type' => 'text',
'desc_tip' => __( 'Your secret key is used to authenticate Stripe requests.', 'checkout-plugins-stripe-woo' ),
'id' => 'cpsw_secret_key',
],
'test_pub_key' => [
'name' => __( 'Test Publishable Key', 'checkout-plugins-stripe-woo' ),
'type' => 'text',
'desc_tip' => __( 'Your test publishable key is used to initialize Stripe assets.', 'checkout-plugins-stripe-woo' ),
'id' => 'cpsw_test_pub_key',
],
'test_secret_key' => [
'name' => __( 'Test Secret Key', 'checkout-plugins-stripe-woo' ),
'type' => 'text',
'desc_tip' => __( 'Your test secret key is used to authenticate Stripe requests for testing purposes.', 'checkout-plugins-stripe-woo' ),
'id' => 'cpsw_test_secret_key',
],
'test_mode' => [
'name' => __( 'Mode', 'checkout-plugins-stripe-woo' ),
'type' => 'select',
'options' => [
'test' => 'Test',
'live' => 'Live',
],
'desc' => __( 'No live transactions are processed in test mode. To fully use test mode, you must have a sandbox (test) account for the payment gateway you are testing.', 'checkout-plugins-stripe-woo' ),
'id' => 'cpsw_mode',
'desc_tip' => true,
],
'webhook_url' => [
'name' => __( 'Webhook URL', 'checkout-plugins-stripe-woo' ),
'type' => 'cpsw_webhook_url',
'class' => 'wc_cpsw_webhook_url',
/* translators: %1$1s - %2$2s HTML markup */
'desc' => sprintf( __( 'Important: the webhook URL is called by Stripe when events occur in your account, like a source becomes chargeable. Refer to the %1$1sWebhook Guide%2$2s or create webhook on %3$3sstripe dashboard%4$4s', 'checkout-plugins-stripe-woo' ), '', '', '', '' ),
'id' => 'cpsw_webhook_url',
],
'create_webhook' => [
'name' => __( 'Create Test Webhook', 'checkout-plugins-stripe-woo' ),
'type' => 'cpsw_create_webhook',
'class' => 'wc_cpsw_create_webhook',
'id' => 'cpsw_create_webhook',
],
'delete_webhook' => [
'name' => __( 'Delete Test Webhook', 'checkout-plugins-stripe-woo' ),
'type' => 'cpsw_delete_webhook',
'class' => 'wc_cpsw_delete_webhook',
'id' => 'cpsw_delete_webhook',
'desc' => Webhook::get_webhook_interaction_message( 'test' ),
],
'live_create_webhook' => [
'name' => __( 'Create Live Webhook', 'checkout-plugins-stripe-woo' ),
'type' => 'cpsw_live_create_webhook',
'class' => 'wc_cpsw_create_webhook',
'id' => 'cpsw_live_create_webhook',
],
'live_delete_webhook' => [
'name' => __( 'Delete Live Webhook', 'checkout-plugins-stripe-woo' ),
'type' => 'cpsw_live_delete_webhook',
'class' => 'wc_cpsw_delete_webhook',
'id' => 'cpsw_live_delete_webhook',
'desc' => Webhook::get_webhook_interaction_message( 'live' ),
],
'debug_log' => [
'name' => __( 'Debug Log', 'checkout-plugins-stripe-woo' ),
'type' => 'checkbox',
'desc' => __( 'Log debug messages', 'checkout-plugins-stripe-woo' ),
'description' => __( 'Your publishable key is used to initialize Stripe assets.', 'checkout-plugins-stripe-woo' ),
'id' => 'cpsw_debug_log',
],
'section_end' => [
'type' => 'sectionend',
'id' => 'cpsw_api_settings_section_end',
],
];
$settings = apply_filters( 'cpsw_settings', $settings );
return $settings;
}
/**
* This method is used to display block for create webhook field.
*
* @param string $value Name of the field.
*/
public function create_webhook_field( $value ) {
$data = WC_Admin_Settings::get_field_description( $value );
$tooltip_html = $data['tooltip_html'];
if ( ! empty( get_option( 'cpsw_test_webhook_id' ) ) ) {
return;
}
?>
message;
} elseif ( property_exists( $error, 'raw' ) ) {
$message = $error->raw->message;
} else {
$message = __( 'Please try again.', 'checkout-plugins-stripe-woo' );
}
$this->settings['cpsw_con_status'] = 'failed';
$this->settings['cpsw_test_con_status'] = 'failed';
$this->update_options( $this->settings );
$redirect_url = add_query_arg( 'cpsw_call', 'failed', $redirect_url );
wp_safe_redirect( $redirect_url );
exit();
} elseif ( isset( $_GET['response'] ) ) {
// $response elements are getting sanitized in later stage.
$response = json_decode( base64_decode( $_GET['response'] ) ); //phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( ! empty( $response->live->stripe_publishable_key ) && ! empty( $response->test->stripe_publishable_key ) ) {
$this->settings['cpsw_pub_key'] = sanitize_text_field( $response->live->stripe_publishable_key );
$this->settings['cpsw_secret_key'] = sanitize_text_field( $response->live->access_token );
$this->settings['cpsw_test_pub_key'] = sanitize_text_field( $response->test->stripe_publishable_key );
$this->settings['cpsw_test_secret_key'] = sanitize_text_field( $response->test->access_token );
$this->settings['cpsw_account_id'] = sanitize_text_field( $response->live->stripe_user_id );
$this->settings['cpsw_mode'] = 'test';
$this->settings['cpsw_con_status'] = 'success';
$this->settings['cpsw_test_con_status'] = 'success';
$redirect_url = add_query_arg( 'cpsw_call', 'success', $redirect_url );
} else {
$this->settings['cpsw_pub_key'] = '';
$this->settings['cpsw_secret_key'] = '';
$this->settings['cpsw_test_pub_key'] = '';
$this->settings['cpsw_test_secret_key'] = '';
$this->settings['cpsw_account_id'] = '';
$this->settings['cpsw_con_status'] = 'failed';
$this->settings['cpsw_test_con_status'] = 'failed';
$redirect_url = add_query_arg( 'cpsw_call', 'failed', $redirect_url );
}
$this->settings['cpsw_auto_connect'] = 'yes';
$this->settings['cpsw_debug_log'] = 'yes';
$this->update_options( $this->settings );
do_action( 'cpsw_after_connect_with_stripe', $this->settings['cpsw_con_status'] );
wp_safe_redirect( $redirect_url );
exit();
}
}
/**
* Perform a connection test
*
* @since 1.0.0
*
* @return void
*/
public function connection_test() {
if ( ! isset( $_GET['_security'] ) || ! wp_verify_nonce( sanitize_text_field( $_GET['_security'] ), 'cpsw_admin_nonce' ) ) {
wp_send_json_error( [ 'message' => __( 'Error: Sorry, the nonce security check didn\'t pass. Please reload the page and try again.', 'checkout-plugins-stripe-woo' ) ] );
}
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( [ 'message' => __( 'Error: The current user doesn\'t have sufficient permissions to perform this action. Please reload the page and try again.', 'checkout-plugins-stripe-woo' ) ] );
}
$results = [];
$keys = [];
if ( ! empty( $_GET['cpsw_test_sec_key'] ) ) {
$keys['test'] = sanitize_text_field( $_GET['cpsw_test_sec_key'] );
}
if ( empty( $keys['test'] ) ) {
$results['test']['mode'] = __( 'Test Mode:', 'checkout-plugins-stripe-woo' );
$results['test']['status'] = 'invalid';
$results['test']['message'] = __( 'Please enter secret key to test.', 'checkout-plugins-stripe-woo' );
}
if ( ! empty( $_GET['cpsw_secret_key'] ) ) {
$keys['live'] = sanitize_text_field( $_GET['cpsw_secret_key'] );
}
if ( empty( $keys['live'] ) ) {
$results['live']['mode'] = __( 'Live Mode:', 'checkout-plugins-stripe-woo' );
$results['live']['status'] = 'invalid';
$results['live']['message'] = __( 'Please enter secret key to live.', 'checkout-plugins-stripe-woo' );
}
if ( empty( $keys ) ) {
wp_send_json_error( [ 'message' => __( 'Error: Empty String provided for keys', 'checkout-plugins-stripe-woo' ) ] );
}
foreach ( $keys as $mode => $key ) {
$stripe = new \Stripe\StripeClient(
$key
);
try {
$response = $stripe->customers->create(
[
/* translators: %1$1s mode */
'description' => sprintf( __( 'My first %1s customer (created for API docs)', 'checkout-plugins-stripe-woo' ), $mode ),
]
);
if ( ! is_wp_error( $response ) ) {
$results[ $mode ]['status'] = 'success';
$results[ $mode ]['message'] = __( 'Connected to Stripe successfully', 'checkout-plugins-stripe-woo' );
}
} catch ( \Stripe\Exception\CardException $e ) {
$results[ $mode ]['status'] = 'failed';
$results[ $mode ]['message'] = $e->getError()->message;
} catch ( \Stripe\Exception\RateLimitException $e ) {
// Too many requests made to the API too quickly.
$results[ $mode ]['status'] = 'failed';
$results[ $mode ]['message'] = $e->getError()->message;
} catch ( \Stripe\Exception\InvalidRequestException $e ) {
// Invalid parameters were supplied to Stripe's API.
$results[ $mode ]['status'] = 'failed';
$results[ $mode ]['message'] = $e->getError()->message;
} catch ( \Stripe\Exception\AuthenticationException $e ) {
// Authentication with Stripe's API failed.
// (maybe you changed API keys recently).
$results[ $mode ]['status'] = 'failed';
$results[ $mode ]['message'] = $e->getError()->message;
} catch ( \Stripe\Exception\ApiConnectionException $e ) {
// Network communication with Stripe failed.
$results[ $mode ]['status'] = 'failed';
$results[ $mode ]['message'] = $e->getError()->message;
} catch ( \Stripe\Exception\ApiErrorException $e ) {
$results[ $mode ]['status'] = 'failed';
$results[ $mode ]['message'] = $e->getError()->message;
// Display a very generic error to the user, and maybe send.
// yourself an email.
} catch ( Exception $e ) {
// Something else happened, completely unrelated to Stripe.
$results[ $mode ]['status'] = 'failed';
$results[ $mode ]['message'] = $e->getError()->message;
}
switch ( $mode ) {
case 'test':
$results[ $mode ]['mode'] = __( 'Test Mode:', 'checkout-plugins-stripe-woo' );
break;
case 'live':
$results[ $mode ]['mode'] = __( 'Live Mode:', 'checkout-plugins-stripe-woo' );
break;
default:
break;
}
}
update_option( 'cpsw_auto_connect', 'no' );
wp_send_json_success( [ 'data' => apply_filters( 'cpsw_connection_test_results', $results ) ] );
}
/**
* Checks for response after stripe onboarding process
*
* @since 1.0.0
*
* @return void
*/
public function disconnect_account() {
if ( ! isset( $_GET['_security'] ) || ! wp_verify_nonce( sanitize_text_field( $_GET['_security'] ), 'cpsw_admin_nonce' ) ) {
wp_send_json_error( [ 'message' => __( 'Error: Sorry, the nonce security check didn\’t pass. Please reload the page and try again.', 'checkout-plugins-stripe-woo' ) ] );
}
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( [ 'message' => __( 'Error: The current user doesn\'t have sufficient permissions to perform this action. Please reload the page and try again.', 'checkout-plugins-stripe-woo' ) ] );
}
if ( $this->delete_webhooks( get_option( 'cpsw_test_webhook_id', true ), 'test' ) && $this->delete_webhooks( get_option( 'cpsw_live_webhook_id', true ), 'live' ) ) {
$this->remove_settings_keys();
wp_send_json_success( [ 'message' => __( 'Your Stripe account has been disconnected and webhook secret key deleted successfully', 'checkout-plugins-stripe-woo' ) ] );
} else {
$this->remove_settings_keys();
wp_send_json_success( [ 'message' => __( 'Your Stripe account has been disconnected.', 'checkout-plugins-stripe-woo' ) ] );
}
}
/**
* Remove settings keys
*
* @return void
*/
public function remove_settings_keys() {
foreach ( $this->settings_keys as $key ) {
update_option( $key, '' );
}
wp_send_json_success( [ 'message' => __( 'Stripe keys are reset successfully.', 'checkout-plugins-stripe-woo' ) ] );
}
/**
* Delete webhook secret key manually
*
* @return void
*/
public function delete_webhook_action() {
if ( ! isset( $_GET['_security'] ) || ! wp_verify_nonce( sanitize_text_field( $_GET['_security'] ), 'cpsw_admin_nonce' ) ) {
wp_send_json_error( [ 'message' => __( 'Error: Sorry, the nonce security check didn’t pass. Please reload the page and try again.', 'checkout-plugins-stripe-woo' ) ] );
}
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( [ 'message' => __( 'Error: The current user doesn’t have sufficient permissions to perform this action. Please reload the page and try again.', 'checkout-plugins-stripe-woo' ) ] );
}
$deletion_response = ! empty( $_GET['webhook_key'] ) && ! empty( $_GET['mode'] ) ? $this->delete_webhooks( sanitize_text_field( $_GET['webhook_key'] ), sanitize_text_field( $_GET['mode'] ) ) : false;
if ( true === $deletion_response ) {
wp_send_json_success( [ 'message' => __( 'Webhook secret key deleted successfully.', 'checkout-plugins-stripe-woo' ) ] );
} else {
// translators: %s - Error reason sent from stripe.
wp_send_json_error( [ 'message' => sprintf( __( 'Error: Unable to delete Webhook.%s', 'checkout-plugins-stripe-woo' ), PHP_EOL . $deletion_response ) ] );
}
}
/**
* Create webhook secret key manually
*
* @return void
*/
public function create_webhook_action() {
if ( ! isset( $_GET['_security'] ) || ! wp_verify_nonce( sanitize_text_field( $_GET['_security'] ), 'cpsw_admin_nonce' ) ) {
wp_send_json_error( [ 'message' => __( 'Error: Sorry, the nonce security check didn’t pass. Please reload the page and try again.', 'checkout-plugins-stripe-woo' ) ] );
}
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( [ 'message' => __( 'Error: The current user doesn’t have sufficient permissions to perform this action. Please reload the page and try again.', 'checkout-plugins-stripe-woo' ) ] );
}
$creation_response = isset( $_GET['mode'] ) ? $this->create_webhooks( 'manually', sanitize_text_field( $_GET['mode'] ) ) : false;
if ( true === $creation_response ) {
wp_send_json_success( [ 'message' => __( 'Webhook secret key created successfully.', 'checkout-plugins-stripe-woo' ) ] );
} else {
// translators: %s - Error reason sent from stripe.
wp_send_json_error( [ 'message' => sprintf( __( 'Error: Webhook secret key not created.%s', 'checkout-plugins-stripe-woo' ), PHP_EOL . $creation_response ) ] );
}
exit();
}
/**
* Logs js errors
*
* @since 1.0.0
*
* @return void
*/
public function js_errors() {
if ( ! isset( $_POST['_security'] ) || ! wp_verify_nonce( sanitize_text_field( $_POST['_security'] ), 'cpsw_js_error_nonce' ) ) {
wp_send_json_error( [ 'message' => __( 'Invalid Nonce', 'checkout-plugins-stripe-woo' ) ] );
}
if ( isset( $_POST['error'] ) ) {
$error = wc_clean( $_POST['error'] );
$error_message = $error['message'] . ' (' . $error['type'] . ')';
$error_message = Helper::get_localized_messages( $error['code'], $error_message );
Logger::error( $error_message, true );
wp_send_json_success( [ 'message' => $error_message ] );
}
exit();
}
/**
* This method is used get account information from stripe.
*
* @since 1.0.0
*
* @param string $account_id Account ID of a stripe user.
*/
public function get_account_info( $account_id = '' ) {
if ( empty( $account_id ) ) {
return false;
}
$stripe_api = new Stripe_Api();
$response = $stripe_api->accounts( 'retrieve', [ $account_id ] );
if ( $response['success'] ) {
$response = $response['data'];
return $response->settings->dashboard->display_name;
} else {
return '';
}
}
/**
* Apply filters on cpsw_settings var to filter settings fields.
*
* @since 1.0.0
*
* @param array $array cpsw_settings values array.
* @return $array array It returns cpsw_settings array.
*/
public function filter_settings_fields( $array = [] ) {
if ( 'success' !== Helper::get_setting( 'cpsw_con_status' ) && 'success' !== Helper::get_setting( 'cpsw_test_con_status' ) ) {
unset( $array['test_mode'] );
unset( $array['webhook_url'] );
unset( $array['create_webhook'] );
unset( $array['delete_webhook'] );
unset( $array['live_create_webhook'] );
unset( $array['live_delete_webhook'] );
unset( $array['debug_log'] );
unset( $array['test_conn_button'] );
$webhook_options = apply_filters(
'cpsw_webhook_options',
[
'cpsw_live_webhook_began_at',
'cpsw_live_webhook_last_success_at',
'cpsw_live_webhook_last_failure_at',
'cpsw_live_webhook_last_error',
'cpsw_test_webhook_began_at',
'cpsw_test_webhook_last_success_at',
'cpsw_test_webhook_last_failure_at',
'cpsw_test_webhook_last_error',
]
);
array_map( 'delete_option', $webhook_options );
}
return $array;
}
/**
* Checks for response after stripe onboarding process
*
* @return $mixed
*/
public function are_keys_set() {
if ( ( 'live' === $this->settings['cpsw_mode']
&& empty( $this->settings['cpsw_pub_key'] )
&& empty(
$this->settings['cpsw_secret_key']
) )
|| ( 'test' === $this->settings['cpsw_mode']
&& empty( $this->settings['cpsw_test_pub_key'] )
&& empty( $this->settings['cpsw_test_secret_key'] )
)
|| ( empty( $this->settings['cpsw_mode'] )
&& empty( $this->settings['cpsw_secret_key'] )
&& empty( $this->settings['cpsw_test_secret_key'] )
) ) {
return false;
}
return true;
}
/**
* Checks if stripe is connected or not.
*
* @since 1.0.0
*
* @return $mixed
*/
public function is_stripe_connected() {
if ( 'success' === Helper::get_setting( 'cpsw_con_status' ) || 'success' === Helper::get_setting( 'cpsw_test_con_status' ) ) {
return true;
}
return false;
}
/**
* Checks if stripe is connected or not.
*
* @since 1.0.0
*
* @return void
*/
public function check_connection_on_updates() {
if ( 'yes' === Helper::get_setting( 'cpsw_auto_connect' ) ) {
return;
}
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
$test_key_test = false;
// This function only saves static string in database nonce verification not required.
if ( ! empty( $_POST['cpsw_test_secret_key'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing
$stripe = new \Stripe\StripeClient(
sanitize_text_field( $_POST['cpsw_test_secret_key'] ) //phpcs:ignore WordPress.Security.NonceVerification.Missing
);
try {
$response = $stripe->customers->create(
[
'description' => __( 'My First Test Customer (created for API docs)', 'checkout-plugins-stripe-woo' ),
]
);
if ( ! is_wp_error( $response ) ) {
$test_key_test = true;
}
} catch ( \Stripe\Exception\CardException $e ) {
$test_key_test = false;
} catch ( \Stripe\Exception\RateLimitException $e ) {
// Too many requests made to the API too quickly.
$test_key_test = false;
} catch ( \Stripe\Exception\InvalidRequestException $e ) {
// Invalid parameters were supplied to Stripe's API.
$test_key_test = false;
} catch ( \Stripe\Exception\AuthenticationException $e ) {
// Authentication with Stripe's API failed.
// (maybe you changed API keys recently).
$test_key_test = false;
} catch ( \Stripe\Exception\ApiConnectionException $e ) {
// Network communication with Stripe failed.
$test_key_test = false;
} catch ( \Stripe\Exception\ApiErrorException $e ) {
$test_key_test = false;
// Display a very generic error to the user, and maybe send.
// yourself an email.
} catch ( Exception $e ) {
// Something else happened, completely unrelated to Stripe.
$test_key_test = false;
}
} else {
$test_key_test = false;
}
if ( true === $test_key_test ) {
update_option( 'cpsw_test_con_status', 'success' );
update_option( 'cpsw_mode', 'test' );
}
$live_key_test = false;
// This function only saves static string in database nonce verification not required.
if ( isset( $_POST['cpsw_secret_key'] ) && ! empty( $_POST['cpsw_secret_key'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing
$stripe = new \Stripe\StripeClient(
sanitize_text_field( $_POST['cpsw_secret_key'] ) //phpcs:ignore WordPress.Security.NonceVerification.Missing
);
try {
$response = $stripe->customers->create(
[
'description' => __( 'My First Live Customer (created for API docs)', 'checkout-plugins-stripe-woo' ),
]
);
if ( ! is_wp_error( $response ) ) {
$live_key_test = true;
}
} catch ( \Stripe\Exception\CardException $e ) {
$live_key_test = false;
} catch ( \Stripe\Exception\RateLimitException $e ) {
// Too many requests made to the API too quickly.
$live_key_test = false;
} catch ( \Stripe\Exception\InvalidRequestException $e ) {
// Invalid parameters were supplied to Stripe's API.
$live_key_test = false;
} catch ( \Stripe\Exception\AuthenticationException $e ) {
// Authentication with Stripe's API failed.
// (maybe you changed API keys recently).
$live_key_test = false;
} catch ( \Stripe\Exception\ApiConnectionException $e ) {
// Network communication with Stripe failed.
$live_key_test = false;
} catch ( \Stripe\Exception\ApiErrorException $e ) {
$live_key_test = false;
// Display a very generic error to the user, and maybe send.
// yourself an email.
} catch ( Exception $e ) {
// Something else happened, completely unrelated to Stripe.
$live_key_test = false;
}
} else {
$live_key_test = false;
}
if ( true === $live_key_test ) {
update_option( 'cpsw_con_status', 'success' );
update_option( 'cpsw_mode', 'live' );
}
}
/**
* Update the stripe payment mode on submit.
*
* @since 1.0.0
*
* @param string $old_value Old value of the option.
* @param string $value New value of the option.
*
* @return void
*/
public function update_mode( $old_value, $value ) {
if ( 'yes' === Helper::get_setting( 'cpsw_auto_connect' ) ) {
return;
}
if ( ! empty( Helper::get_setting( 'cpsw_secret_key' ) ) && empty( Helper::get_setting( 'cpsw_test_secret_key' ) ) ) {
update_option( 'cpsw_mode', 'live' );
} elseif ( ! empty( Helper::get_setting( 'cpsw_test_secret_key' ) ) && empty( Helper::get_setting( 'cpsw_secret_key' ) ) ) {
update_option( 'cpsw_mode', 'test' );
}
}
/**
* Adds custom css to hide navigation menu item.
*
* @since 1.0.0
*
* @return void
*/
public function add_custom_css() {
?>
navigation ) ) {
?>
navigation as $key => $value ) {
$current_class = '';
$separator = '';
// Generates html content for Admin UI, nonce verification not required.
if ( isset( $_GET['tab'] ) && 'cpsw_api_settings' === $_GET['tab'] && 'cpsw_api_settings' === $key ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
$current_class = 'current';
echo wp_kses_post( '