Skip to content

Commit

Permalink
Feature: add Razorpay per-form settings migration step (#7438)
Browse files Browse the repository at this point in the history
  • Loading branch information
glaubersilva authored Jul 11, 2024
1 parent f47aec8 commit 4cd7a20
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/FormMigration/FormMetaDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1022,4 +1022,12 @@ public function getCurrencySwitcherSupportedCurrencies(): array
{
return (array)$this->getMeta('cs_supported_currency', []);
}

/**
* @unreleased
*/
public function isRazorpayPerFormSettingsEnabled(): bool
{
return give_is_setting_enabled($this->getMeta('razorpay_per_form_account_options'));
}
}
1 change: 1 addition & 0 deletions src/FormMigration/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function register()
Steps\ActiveCampaign::class,
Steps\DoubleTheDonation::class,
Steps\CurrencySwitcher::class,
Steps\RazorpayPerFormSettings::class,
]);
});
}
Expand Down
56 changes: 56 additions & 0 deletions src/FormMigration/Steps/RazorpayPerFormSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Give\FormMigration\Steps;

use Give\FormMigration\Contracts\FormMigrationStep;

/**
* @unreleased
*/
class RazorpayPerFormSettings extends FormMigrationStep
{
/**
* @unreleased
*/
public function canHandle(): bool
{
return $this->formV2->isRazorpayPerFormSettingsEnabled();
}

/**
* @unreleased
*/
public function process()
{
$oldFormId = $this->formV2->id;

$paymentGatewaysBlock = $this->fieldBlocks->findByName('givewp/payment-gateways');

$paymentGatewaysBlock->setAttribute('razorpayUseGlobalSettings',
$this->getMetaValue($oldFormId, 'razorpay_per_form_account_options', 'global') === 'global');

$paymentGatewaysBlock->setAttribute('razorpayLiveKeyId',
$this->getMetaValue($oldFormId, 'razorpay_per_form_live_merchant_key_id', ''));
$paymentGatewaysBlock->setAttribute('razorpayLiveSecretKey',
$this->getMetaValue($oldFormId, 'razorpay_per_form_live_merchant_secret_key', ''));

$paymentGatewaysBlock->setAttribute('razorpayTestKeyId',
$this->getMetaValue($oldFormId, 'razorpay_per_form_test_merchant_key_id', ''));
$paymentGatewaysBlock->setAttribute('razorpayTestSecretKey',
$this->getMetaValue($oldFormId, 'razorpay_per_form_test_merchant_secret_key', ''));
}

/**
* @unreleased
*/
private function getMetaValue(int $formId, string $metaKey, $defaultValue)
{
$metaValue = give()->form_meta->get_meta($formId, $metaKey, true);

if ( ! $metaValue) {
return $defaultValue;
}

return $metaValue;
}
}
82 changes: 82 additions & 0 deletions tests/Feature/FormMigration/Steps/TestRazorpayPerFormSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Feature\FormMigration\Steps;

use Give\FormMigration\DataTransferObjects\FormMigrationPayload;
use Give\FormMigration\Steps\RazorpayPerFormSettings;
use Give\Tests\TestCase;
use Give\Tests\TestTraits\RefreshDatabase;
use Give\Tests\Unit\DonationForms\TestTraits\LegacyDonationFormAdapter;

/**
* @unreleased
*/
class TestRazorpayPerFormSettings extends TestCase
{
use RefreshDatabase, LegacyDonationFormAdapter;

/**
* @unreleased
*/
public function testCanHandleShouldReturnFalse()
{
$meta = [
'razorpay_per_form_account_options' => 'global',
];
$formV2 = $this->createSimpleDonationForm(['meta' => $meta]);
$payload = FormMigrationPayload::fromFormV2($formV2);
$razorpayPerFormSettings = new RazorpayPerFormSettings($payload);

$this->assertNotTrue($razorpayPerFormSettings->canHandle());
}

/**
* @unreleased
*/
public function testCanHandleShouldReturnTrue()
{
$meta = [
'razorpay_per_form_account_options' => 'enabled',
];
$formV2 = $this->createSimpleDonationForm(['meta' => $meta]);
$payload = FormMigrationPayload::fromFormV2($formV2);
$razorpayPerFormSettings = new RazorpayPerFormSettings($payload);

$this->assertTrue($razorpayPerFormSettings->canHandle());
}

/**
* @unreleased
*/
public function testProcessShouldUpdatePaymentGatewaysBlockAttributes(): void
{
$liveKeyId = 'live_12304567890';
$liveSecretKey = 'live_0123456789';
$testKeyId = 'test_12304567890';
$testSecretKey = 'test_0123456789';

$meta = [
'razorpay_per_form_account_options' => 'enabled',
'razorpay_per_form_live_merchant_key_id' => $liveKeyId,
'razorpay_per_form_live_merchant_secret_key' => $liveSecretKey,
'razorpay_per_form_test_merchant_key_id' => $testKeyId,
'razorpay_per_form_test_merchant_secret_key' => $testSecretKey,
];

$formV2 = $this->createSimpleDonationForm(['meta' => $meta]);

$payload = FormMigrationPayload::fromFormV2($formV2);

$razorpayPerFormSettings = new RazorpayPerFormSettings($payload);
$razorpayPerFormSettings->process();

$paymentGatewaysBlock = $payload->formV3->blocks->findByName('givewp/payment-gateways');

$this->assertSame(false, $paymentGatewaysBlock->getAttribute('razorpayUseGlobalSettings'));
$this->assertSame($liveKeyId, $paymentGatewaysBlock->getAttribute('razorpayLiveKeyId'));
$this->assertSame($liveSecretKey, $paymentGatewaysBlock->getAttribute('razorpayLiveSecretKey'));
$this->assertSame($testKeyId, $paymentGatewaysBlock->getAttribute('razorpayTestKeyId'));
$this->assertSame($testSecretKey, $paymentGatewaysBlock->getAttribute('razorpayTestSecretKey'));
}
}

0 comments on commit 4cd7a20

Please sign in to comment.