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

Handle the Stripe connect redirect on specific pages #7223

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,28 @@ public function __construct(Settings $settings)
}

/**
* @unreleased Handle Stripe connect account on-boarding redirect on specific pages.
*
* @since 2.13.0
*/
public function __invoke()
{
if (!current_user_can('manage_give_settings')) {
if (! current_user_can('manage_give_settings')) {
return;
}

if (wp_doing_ajax() || ! $this->canProcessRequestOnCurrentPage($_SERVER['REQUEST_URI'])) {
return;
}

$requestedData = NewStripeAccountOnBoardingDto::fromArray(give_clean($_GET));

if (!$requestedData->hasValidateData()) {
if (! $requestedData->hasValidateData()) {
return;
}

$stripe_accounts = give_stripe_get_all_accounts();
$secret_key = !give_is_test_mode() ? $requestedData->stripeAccessToken : $requestedData->stripeAccessTokenTest;
$secret_key = ! give_is_test_mode() ? $requestedData->stripeAccessToken : $requestedData->stripeAccessTokenTest;

Stripe::setApiKey($secret_key);

Expand All @@ -69,15 +75,15 @@ public function __invoke()
return;
}

$account_name = !empty($account_details->business_profile->name) ?
$account_name = ! empty($account_details->business_profile->name) ?
$account_details->business_profile->name :
$account_details->settings->dashboard->display_name;
$account_slug = $account_details->id;
$account_email = $account_details->email;
$account_country = $account_details->country;

// Set first Stripe account as default.
if (!$stripe_accounts) {
if (! $stripe_accounts) {
give_update_option('_give_stripe_default_account', $account_slug);
}

Expand All @@ -101,7 +107,7 @@ public function __invoke()
$this->settings->addNewStripeAccount($accountDetailModel);

if ($requestedData->formId) {
if (!Settings::getDefaultStripeAccountSlugForDonationForm($requestedData->formId)) {
if (! Settings::getDefaultStripeAccountSlugForDonationForm($requestedData->formId)) {
Settings::setDefaultStripeAccountSlugForDonationForm(
$requestedData->formId,
$accountDetailModel->accountSlug
Expand Down Expand Up @@ -146,4 +152,48 @@ public function __invoke()
return;
}
}

/**
* Check if the request can be processed on the current page.
*
* Admin redirect to following page:
* 1. GiveWP stripe settings page.
* 2. V2 donation form edit form.
*
* @unreleased
*/
protected function canProcessRequestOnCurrentPage(string $url): bool
{
// Check if request is from edit.php or post.php page.
if (false === strpos($url, 'wp-admin/post.php') && false === strpos($url, 'wp-admin/edit.php')) {
return false;
}

$path = wp_parse_url($url);

// Result should be in array and should have query string.
if (! is_array($path) || ! isset($path['query'])) {
return false;
}

$queryParams = wp_parse_args($path['query']);

if (empty($queryParams)) {
return false;
}

// Check if request is from V2 donation form edit page.
$isDonationFormPage = isset($queryParams['post'], $queryParams['give_tab'])
&& get_post_type(absint($queryParams['post'])) === 'give_forms'
&& $queryParams['give_tab'] === 'stripe_form_account_options';

// Check if request is from GiveWP stripe settings page.
$isSettingPage = isset($queryParams['page'], $queryParams['post_type'], $queryParams['tab'], $queryParams['section'])
&& $queryParams['post_type'] === 'give_forms'
&& $queryParams['page'] === 'give-settings'
&& $queryParams['tab'] === 'gateways'
&& $queryParams['section'] === 'stripe-settings';

return $isDonationFormPage || $isSettingPage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Give\Tests\Feature\Controllers;

use Give\DonationForms\Models\DonationForm;
use Give\Donations\Models\Donation;
use Give\PaymentGateways\Stripe\Controllers\NewStripeAccountOnBoardingController;
use Give\Tests\TestCase;
use Give\Tests\TestTraits\RefreshDatabase;

class NewStripeAccountOnBoardingControllerTest extends TestCase
{
use RefreshDatabase;

/**
* @var NewStripeAccountOnBoardingController
*/
private $newStripeAccountOnBoardingController;

/**
* @throws \ReflectionException
*/
public function setUp()
{
parent::setUp();

$this->newStripeAccountOnBoardingController = give(NewStripeAccountOnBoardingController::class);
$this->canProcessRequestOnCurrentPageMethod = (new \ReflectionObject(
$this->newStripeAccountOnBoardingController
))->getMethod('canProcessRequestOnCurrentPage');
$this->canProcessRequestOnCurrentPageMethod->setAccessible(true);
}

public function testShouldReturnTrueForV2DonationFomEditPage(): void
{
/** @var DonationForm $form */
$form = DonationForm::factory()->create();
$url = "wp-admin/post.php?post=$form->id&action=edit&give_tab=stripe_form_account_options";
$result = $this->canProcessRequestOnCurrentPageMethod->invokeArgs(
$this->newStripeAccountOnBoardingController,
[$url]
);
$this->assertTrue($result);
}

public function testShouldReturnTrueGlobalStripeSettingPage(): void
{
$url = 'wp-admin/edit.php?post_type=give_forms&page=give-settings&tab=gateways&section=stripe-settings';
$result = $this->canProcessRequestOnCurrentPageMethod->invokeArgs(
$this->newStripeAccountOnBoardingController,
[$url]
);
$this->assertTrue($result);
}

public function testShouldReturnFalseOtherPage(): void
{
$url = 'wp-admin/edit.php?post_type=give_forms&page=give-settings&tab=gateways&section=paypal-settings';
$result = $this->canProcessRequestOnCurrentPageMethod->invokeArgs(
$this->newStripeAccountOnBoardingController,
[$url]
);
$this->assertFalse($result);

/** @var Donation $donation */
$donation = Donation::factory()->create();
$url = "wp-admin/post.php?post=$donation->id&action=edit&give_tab=stripe_form_account_options";
$result = $this->canProcessRequestOnCurrentPageMethod->invokeArgs(
$this->newStripeAccountOnBoardingController,
[$url]
);
$this->assertFalse($result);
}
}
Loading