-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: add Razorpay per-form settings migration step (#7438)
- Loading branch information
1 parent
f47aec8
commit 4cd7a20
Showing
4 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
82
tests/Feature/FormMigration/Steps/TestRazorpayPerFormSettings.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} | ||
|