Skip to content

Commit

Permalink
Feature: update field scopes to support subscriptions (#6920)
Browse files Browse the repository at this point in the history
Co-authored-by: Jon Waldstein <[email protected]>
  • Loading branch information
jonwaldstein and Jon Waldstein authored Sep 11, 2023
1 parent 607545c commit 5de60a4
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
use Give\DonationForms\Listeners\StoreCustomFields;
use Give\DonationForms\Listeners\TemporarilyReplaceLegacySuccessPageUri;
use Give\Donations\Models\Donation;
use Give\Framework\FieldsAPI\Exceptions\NameCollisionException;
use Give\Subscriptions\Models\Subscription;

class DispatchDonateControllerDonationCreatedListeners
{
/**
* @since 3.0.0
* @throws NameCollisionException
*/
public function __invoke(DonateControllerData $formData, Donation $donation)
public function __invoke(DonateControllerData $formData, Donation $donation, ?Subscription $subscription)
{
(new StoreCustomFields())($formData->getDonationForm(), $donation, $formData->getCustomFields());
(new StoreCustomFields())($formData->getDonationForm(), $donation, $subscription, $formData->getCustomFields());
(new TemporarilyReplaceLegacySuccessPageUri())($formData, $donation);
(new AddRedirectUrlsToGatewayData())($formData, $donation);
}
Expand Down
5 changes: 3 additions & 2 deletions src/DonationForms/Controllers/DonateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Give\Framework\PaymentGateways\Exceptions\PaymentGatewayException;
use Give\Framework\PaymentGateways\PaymentGateway;
use Give\PaymentGateways\Actions\GetGatewayDataFromRequest;
use Give\Subscriptions\Models\Subscription;

/**
* @since 3.0.0
Expand Down Expand Up @@ -37,7 +38,7 @@ public function donate(DonateControllerData $formData, PaymentGateway $gateway)
$donation = $formData->toDonation($donor->id);
$donation->save();

do_action('givewp_donate_controller_donation_created', $formData, $donation);
do_action('givewp_donate_controller_donation_created', $formData, $donation, null);

$gatewayData = apply_filters(
"givewp_create_payment_gateway_data_{$gateway::id()}",
Expand All @@ -58,7 +59,7 @@ public function donate(DonateControllerData $formData, PaymentGateway $gateway)
$donation = $formData->toInitialSubscriptionDonation($donor->id, $subscription->id);
$donation->save();

do_action('givewp_donate_controller_donation_created', $formData, $donation);
do_action('givewp_donate_controller_donation_created', $formData, $donation, $subscription);

do_action('givewp_donate_controller_subscription_created', $formData, $subscription, $donation);

Expand Down
18 changes: 18 additions & 0 deletions src/DonationForms/DataTransferObjects/DonateControllerData.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,22 @@ public function getBillingAddress(): BillingAddress
'zip' => $this->zip,
]);
}

/**
* @since 3.0.0
*/
public function has(string $name): bool
{
return isset($this->{$name});
}

/**
* @since 3.0.0
*
* @return mixed|null
*/
public function get(string $name)
{
return property_exists($this, $name) ? $this->{$name} : null;
}
}
33 changes: 23 additions & 10 deletions src/DonationForms/Listeners/StoreCustomFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Give\Framework\FieldsAPI\Field;
use Give\Framework\FieldsAPI\File;
use Give\Framework\FieldsAPI\Types;
use Give\Subscriptions\Models\Subscription;

class StoreCustomFields
{
Expand All @@ -22,10 +23,10 @@ class StoreCustomFields
* @return void
* @throws NameCollisionException
*/
public function __invoke(DonationForm $form, Donation $donation, array $customFields)
public function __invoke(DonationForm $form, Donation $donation, ?Subscription $subscription, array $customFields)
{
$form->schema()->walkFields(
function (Field $field) use ($customFields, $donation) {
function (Field $field) use ($customFields, $donation, $subscription) {
$fieldName = $field->getName();

if (!array_key_exists($fieldName, $customFields)) {
Expand All @@ -42,22 +43,21 @@ function (Field $field) use ($customFields, $donation) {
}

foreach ($fileIds as $fileId) {
$this->persistFieldScope($field, $fileId, $donation);
$this->persistFieldScope($field, $fileId, $donation, $subscription);
}
} else {
$value = $customFields[$fieldName];

$this->persistFieldScope($field, $value, $donation);
$this->persistFieldScope($field, $value, $donation, $subscription);
}
}
);
}

/**
* @since 3.0.0
* @return array|null
*/
protected function handleFileUpload(File $field)
protected function handleFileUpload(File $field): ?array
{
if (!isset($_FILES[$field->getName()])) {
return null;
Expand All @@ -69,30 +69,43 @@ protected function handleFileUpload(File $field)
/**
* @since 3.0.0
*/
protected function storeAsDonorMeta(int $donorId, string $metaKey, $value)
protected function storeAsDonorMeta(int $donorId, string $metaKey, $value): void
{
give()->donor_meta->update_meta($donorId, $metaKey, $value);
}

/**
* @since 3.0.0
*/
protected function storeAsDonationMeta(int $donationId, string $metaKey, $value)
protected function storeAsDonationMeta(int $donationId, string $metaKey, $value): void
{
give()->payment_meta->update_meta($donationId, $metaKey, $value);
}

/**
* @since 3.0.0
*/
protected function persistFieldScope(Field $field, $value, Donation $donation)
protected function storeAsSubscriptionMeta(int $subscriptionId, string $metaKey, $value): void
{
give()->subscription_meta->update_meta($subscriptionId, $metaKey, $value);
}


/**
* @since 3.0.0
*/
protected function persistFieldScope(Field $field, $value, Donation $donation, ?Subscription $subscription): void
{
if ($field->getScope()->isDonor()) {
$this->storeAsDonorMeta($donation->donorId, $field->getMetaKey() ?? $field->getName(), $value);
} elseif ($field->getScope()->isDonation()) {
$this->storeAsDonationMeta($donation->id, $field->getMetaKey() ?? $field->getName(), $value);
} elseif ($field->getScope()->isSubscription()) {
if ($subscription) {
$this->storeAsSubscriptionMeta($subscription->id, $field->getMetaKey() ?? $field->getName(), $value);
}
} elseif ($field->getScope()->isCallback()) {
$field->getScopeCallback()($field, $value, $donation);
$field->getScopeCallback()($field, $value, $donation, $subscription);
} else {
do_action(
"givewp_donation_form_persist_field_scope_{$field->getScopeValue()}",
Expand Down
2 changes: 1 addition & 1 deletion src/DonationForms/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private function dispatchDonateControllerListeners()
DispatchDonateControllerDonationCreatedListeners::class,
'__invoke',
10,
2
3
);

Hooks::addAction(
Expand Down
23 changes: 20 additions & 3 deletions src/Framework/FieldsAPI/ValueObjects/PersistenceScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
*/
class PersistenceScope
{
const DONATION = 'donation';
const DONOR = 'donor';
const CALLBACK = 'callback';
public const DONATION = 'donation';
public const SUBSCRIPTION = 'subscription';
public const DONOR = 'donor';
public const CALLBACK = 'callback';

/**
* @var string
Expand All @@ -36,6 +37,14 @@ public static function donor(): self
return new self(self::DONOR);
}

/**
* @since 3.0.0
*/
public static function subscription(): self
{
return new self(self::SUBSCRIPTION);
}

/**
* @since 2.32.0
*/
Expand All @@ -60,6 +69,14 @@ public function isDonation(): bool
return $this->scope === self::DONATION;
}

/**
* @since 3.0.0
*/
public function isSubscription(): bool
{
return $this->scope === self::SUBSCRIPTION;
}

/**
* @since 2.32.0
*/
Expand Down
49 changes: 45 additions & 4 deletions tests/Feature/Actions/StoreCustomFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
use Give\DonationForms\Listeners\StoreCustomFields;
use Give\DonationForms\Models\DonationForm;
use Give\Donations\Models\Donation;
use Give\FormAPI\Form\Text;
use Give\Framework\Blocks\BlockCollection;
use Give\Framework\Blocks\BlockModel;
use Give\Framework\Database\DB;
use Give\Framework\Exceptions\Primitives\Exception;
use Give\Framework\FieldsAPI\ValueObjects\PersistenceScope;
use Give\Subscriptions\Models\Subscription;
use Give\Tests\TestCase;
use Give\Tests\TestTraits\RefreshDatabase;

Expand All @@ -22,7 +25,7 @@ class StoreCustomFieldsTest extends TestCase
* @return void
* @throws Exception
*/
public function testShouldStoreAsDonorMeta()
public function testShouldStoreAsDonorMeta(): void
{
/** @var DonationForm $form */
$form = DonationForm::factory()->create();
Expand Down Expand Up @@ -54,7 +57,7 @@ public function testShouldStoreAsDonorMeta()

$action = new StoreCustomFields();

$action($form, $donation, ['custom_text_block_meta' => 'Custom Text Block Value']);
$action($form, $donation, null, ['custom_text_block_meta' => 'Custom Text Block Value']);

$query = DB::table('give_donormeta')
->select('meta_value')
Expand All @@ -71,7 +74,7 @@ public function testShouldStoreAsDonorMeta()
* @return void
* @throws Exception
*/
public function testShouldStoreAsDonationMeta()
public function testShouldStoreAsDonationMeta(): void
{
/** @var DonationForm $form */
$form = DonationForm::factory()->create();
Expand Down Expand Up @@ -103,7 +106,7 @@ public function testShouldStoreAsDonationMeta()

$action = new StoreCustomFields();

$action($form, $donation, ['custom_text_block_meta' => 'Custom Text Block Value']);
$action($form, $donation,null, ['custom_text_block_meta' => 'Custom Text Block Value']);

$query = DB::table('give_donationmeta')
->select('meta_value')
Expand All @@ -113,4 +116,42 @@ public function testShouldStoreAsDonationMeta()

$this->assertSame('Custom Text Block Value', $query->meta_value);
}

/**
* @since 3.0.0
*
* @return void
* @throws Exception
* @throws \Exception
*/
public function testShouldStoreAsSubscriptionMeta(): void
{
$field = \Give\Framework\FieldsAPI\Text::make('custom_subscription_field')
->metaKey('custom_subscription_field_meta')
->showInAdmin()
->defaultValue('Custom Subscription Field Value')
->scope(PersistenceScope::SUBSCRIPTION);

add_action('givewp_donation_form_schema', static function (\Give\Framework\FieldsAPI\DonationForm $form) use ($field) {
$form->insertAfter('email', $field);
});

/** @var DonationForm $form */
$form = DonationForm::factory()->create();

/** @var Subscription $subscription */
$subscription = Subscription::factory()->createWithDonation(['donationFormId' => $form->id]);

$action = new StoreCustomFields();

$action($form, $subscription->initialDonation(), $subscription, [$field->getName() => $field->getDefaultValue()]);

$query = DB::table('give_subscriptionmeta')
->select('meta_value')
->where('subscription_id', $subscription->id)
->where('meta_key', $field->getMetaKey())
->get();

$this->assertSame($field->getDefaultValue(), $query->meta_value);
}
}
2 changes: 1 addition & 1 deletion tests/Unit/CustomFields/Views/TestDonationDetailsView.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function testRenderShouldReturnCustomFieldsHtml()
/** @var Donation $donation */
$donation = Donation::factory()->create(['formId' => $form->id]);

(new StoreCustomFields())($form, $donation, ['custom_text_block_meta' => 'Custom Text Block Value']);
(new StoreCustomFields())($form, $donation,null, ['custom_text_block_meta' => 'Custom Text Block Value']);

$form = DonationForm::find($form->id);

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/CustomFields/Views/TestDonorDetailsView.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function testRenderShouldReturnCustomFieldsHtml()
/** @var Donation $donation */
$donation = Donation::factory()->create(['formId' => $form->id, 'donorId' => $donor->id]);

(new StoreCustomFields())($form, $donation, ['custom_text_block_meta' => 'Custom Text Block Value']);
(new StoreCustomFields())($form, $donation, null, ['custom_text_block_meta' => 'Custom Text Block Value']);

$form = DonationForm::find($form->id);

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Framework/Receipts/TestDonationReceipt.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public function testToArrayReturnsExpectedArrayShapeWithCustomFields()
'formId' => $donationForm->id,
]);

(new StoreCustomFields())($donationForm, $donation, ['custom_text_block_meta' => 'Custom Text Block Value']);
(new StoreCustomFields())($donationForm, $donation, null, ['custom_text_block_meta' => 'Custom Text Block Value']);

$receipt = new DonationReceipt($donation);

Expand Down

0 comments on commit 5de60a4

Please sign in to comment.