Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Woocomerce Onboarding task to set up Stripe #1492

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions class-wc-calypso-bridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ public function includes() {
require_once WC_CALYPSO_BRIDGE_PLUGIN_PATH . '/includes/class-wc-calypso-bridge-smart-shipping.php';
require_once WC_CALYPSO_BRIDGE_PLUGIN_PATH . '/includes/class-wc-calypso-bridge-woo-express-introductory-offers.php';
require_once WC_CALYPSO_BRIDGE_PLUGIN_PATH . '/includes/free-trial/partners/class-wc-calypso-bridge-partner-square.php';
require_once WC_CALYPSO_BRIDGE_PLUGIN_PATH . '/includes/free-trial/partners/class-wc-calypso-bridge-partner-stripe.php';

// Experiments.
require_once WC_CALYPSO_BRIDGE_PLUGIN_PATH . '/includes/experiments/class-wc-calypso-bridge-task-list-reminderbar-experiment.php';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php

/**
* WC Calypso Bridge Partner Stripe
*
* @since 2.5.4
* @version 2.5.4
*
* This file includes customizations for the sites that were created through /start-with/stripe on wordpress.com.
* woocommerce_onboarding_profile.partner must get 'stripe'
*/
class WC_Calypso_Bridge_Partner_Stripe {

/**
* Class instance.
*
* @var WC_Calypso_Bridge_Partner_Stripe instance
*/
protected static $instance = false;

/**
* Get class instance
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}

return self::$instance;
}

/**
* Constructor.
*/
public function __construct() {
// Only for free trials.
if ( ! wc_calypso_bridge_is_ecommerce_trial_plan() ) {
return;
}
JulianBustamante marked this conversation as resolved.
Show resolved Hide resolved
$onboarding_profile = get_option( 'woocommerce_onboarding_profile', array() );
if ( ! isset( $onboarding_profile['partner'] ) ) {
return;
}

if ( $onboarding_profile['partner'] !== 'stripe' ) {
return;
}

$this->add_stripe_setup_task();
$this->add_stripe_connect_url_to_js();
$this->remove_woo_payments_from_payments_suggestions_feed();
$this->remove_payments_note();
}

/**
* Remove woo payments from the payments suggestions feed.
*
* @return void
*/
private function remove_woo_payments_from_payments_suggestions_feed() {
add_filter( 'woocommerce_admin_payment_gateway_suggestion_specs', function( $specs ) {
$keys = array(
'woocommerce_payments',
'woocommerce_payments:with-in-person-payments',
'woocommerce_payments:without-in-person-payments',
);
foreach ( $keys as $key ) {
if ( isset( $specs[ $key ] ) ) {
unset( $specs[ $key ] );
}
}

return $specs;
});
}

private function has_stripe_plugin_class() {
return class_exists( '\WC_Stripe' );
}

/**
* Add Stripe setup task to the setup onboarding tasks list.
*/
private function add_stripe_setup_task() {
add_filter( 'woocommerce_admin_experimental_onboarding_tasklists', function( $lists ) {
if ( isset( $lists['setup'] ) ) {
require_once __DIR__ . '/../../tasks/class-wc-calypso-task-get-paid-with-stripe.php';

$lists['setup']->tasks = array_filter( $lists['setup']->tasks, function( $task ) {
$removeTasks = [
'Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks\TrialPayments',
'Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks\WooCommercePayments'
];

if ( in_array( get_class( $task ), $removeTasks ) ) {
return false;
}

return true;
});

// Place it at the third position.
array_splice( $lists['setup']->tasks, 2, 0, array( new \Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks\WCBridgeGetPaidWithStripe( $lists['setup'] ) ) );
}

return $lists;
} );
}

/**
* Add Stripe connect URL to the JS.
*
* @return void
*/
private function add_stripe_connect_url_to_js() {
add_filter( 'wc_calypso_bridge_shared_params', function( $params ) {
if ( ! $this->has_stripe_plugin_class() ) {
return $params;
}

$stripe_oauth_url = woocommerce_gateway_stripe()->connect->get_oauth_url();
if ( is_wp_error( $stripe_oauth_url ) ) {
// Fallback to the settings page
return $params['stripe_connect_url'] = add_query_arg( array(
'page' => 'wc-settings',
'tab' => 'square',
), admin_url( 'admin.php' ) );
}

$params['stripe_connect_url'] = $stripe_oauth_url;
return $params;
});
}

/**
* Remove wc-admin-onboarding-payments-reminder note from the notes api endpoint.
*
* @return void
*/
private function remove_payments_note() {
add_filter( 'rest_request_after_callbacks', function( $response, $handler, $request ) {
if ( $request->get_route() === '/wc-analytics/admin/notes' ) {
$data = $response->get_data();
foreach( $data as $key=>$note ) {
if ( isset( $note['name'] ) && $note['name'] === 'wc-admin-onboarding-payments-reminder' ) {
unset( $data[$key] );
$headers = $response->get_headers();
if ( isset( $headers['X-WP-Total'] ) ) {
$headers['X-WP-Total'] = (int) $headers['X-WP-Total'] - 1;
$response->set_headers( $headers );
}
break;
}
}
$response->set_data( array_values( $data ) );
}
return $response;
}, 10, 3);
}
}

WC_Calypso_Bridge_Partner_Stripe::get_instance();
76 changes: 76 additions & 0 deletions includes/tasks/class-wc-calypso-task-get-paid-with-stripe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks;

use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task;

/**
* WCBridgeSetupWooCommerceStripe Task
*
* @since 2.5.4
* @version 2.5.4
*/
class WCBridgeGetPaidWithStripe extends Task {
/**
* ID.
*
* @return string
*/
public function get_id() {
return 'get-paid-with-stripe';
}

/**
* Title.
*
* @return string
*/
public function get_title() {
return __( 'Get paid with Stripe', 'wc-calypso-bridge' );
}

/**
* Content.
*
* @return string
*/
public function get_content() {
return __(
"Set up Stripe payments to accept credit card payments in your store. You'll need a Stripe account to get started",
'wc-calypso-bridge'
);
}

/**
* Check if the task is complete.
*
* When Stripe is connected, it sets an access token in the options table.
* Count the access token and consider the task complete if it is not empty.
*
*/
public function is_complete() {
if ( ! class_exists( '\WC_Stripe' ) ) {
return false;
}

return woocommerce_gateway_stripe()->connect->is_connected();
}

/**
* Time.
*
* @return string
*/
public function get_time() {
return __( '2 minutes', 'wc-calypso-bridge' );
}

/**
* Action label.
*
* @return string
*/
public function get_action_label() {
return __( 'Get paid with Stripe', 'wc-calypso-bridge' );
}
}
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
} from './homescreen-progress-header';
import './index.scss';
import { CalypsoBridgeHomescreenBanner } from './homescreen-banner';
import { AppearanceFill, GetPaidWithSquareFill } from './task-fills';
import { AppearanceFill, GetPaidWithSquareFill, GetPaidWithStripeFill } from './task-fills';
import './task-headers';
import './track-menu-item';
import { CalypsoBridgeIntroductoryOfferBanner } from './introductory-offer-banner';
Expand Down Expand Up @@ -176,6 +176,14 @@ if ( !! window.wcCalypsoBridge.isEcommercePlanTrial ) {
render: GetPaidWithSquareFill,
} );
}

if ( window?.wcCalypsoBridge?.stripe_connect_url ) {
// Setup Stripe task fill (Partner Aware Onboarding).
registerPlugin( 'wc-calypso-bridge-task-setup-woocommerce-stripe', {
scope: 'woocommerce-tasks',
render: GetPaidWithStripeFill,
} );
}
}

if ( !! window.wcCalypsoBridge.isEcommercePlan ) {
Expand Down
20 changes: 20 additions & 0 deletions src/task-fills/get-paid-with-stripe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* External dependencies
*/
import React from 'react';
import { WooOnboardingTaskListItem } from '@woocommerce/onboarding';

export const GetPaidWithStripeFill = () => {
return (
<WooOnboardingTaskListItem id="get-paid-with-stripe">
{ ( { defaultTaskItem: DefaultTaskItem } ) => (
<DefaultTaskItem
onClick={ () => {
window.location.href =
window.wcCalypsoBridge.stripe_connect_url;
} }
/>
) }
</WooOnboardingTaskListItem>
);
};
1 change: 1 addition & 0 deletions src/task-fills/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './appearance.js';
export * from './get-paid-with-square.js';
export * from './get-paid-with-stripe.js';
72 changes: 72 additions & 0 deletions src/task-headers/get-paid-with-stripe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* External dependencies
*/
import { Button } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { WooOnboardingTaskListHeader } from '@woocommerce/onboarding';
import { registerPlugin } from '@wordpress/plugins';

/**
* Internal dependencies
*/
import TimerImage from './assets/images/timer.svg';
import { WC_ASSET_URL } from '../utils/admin-settings';

const GetPaidWithStripeHeader = () => {
return (
<WooOnboardingTaskListHeader id="get-paid-with-stripe">
{ ( { task, goToTask } ) => {
return (
<div className="woocommerce-task-header__contents-container">
<img
alt={ __(
'Payment illustration',
'wc-calypso-bridge'
) }
src={
WC_ASSET_URL +
'images/task_list/payment-illustration.svg'
}
className="svg-background"
/>
<div className="woocommerce-task-header__contents">
<h1>
{ __(
"It's time to get paid",
'wc-calypso-bridge'
) }
</h1>
<p>
{ __(
'Accepting payments is easy with Stripe. Sell online and in person, and sync all payments, customers, items, and inventory.',
'wc-calypso-bridge'
) }
</p>
<Button
isSecondary={ task.isComplete }
isPrimary={ ! task.isComplete }
onClick={ () => {
window.location.href =
window.wcCalypsoBridge.stripe_connect_url;
} }
>
{ __( 'Set up Stripe', 'wc-calypso-bridge' ) }
</Button>
<p className="woocommerce-task-header__timer">
<img src={ TimerImage } alt="Timer" />{ ' ' }
<span>{ task.time }</span>
</p>
</div>
</div>
);
} }
</WooOnboardingTaskListHeader>
);
};

registerPlugin( 'wc-calypso-bridge-get-paid-with-stripe-task-header', {
render: GetPaidWithStripeHeader,
scope: 'woocommerce-tasks',
} );

export default GetPaidWithStripeHeader;
1 change: 1 addition & 0 deletions src/task-headers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './add-domain';
export * from './products';
export * from './appearance';
export * from './get-paid-with-square';
export * from './get-paid-with-stripe';
Loading