Skip to content

Commit

Permalink
Feature: add receipt api for fields (#7149)
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 Jan 9, 2024
1 parent aa733cc commit 050a76c
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 10 deletions.
72 changes: 72 additions & 0 deletions src/Framework/FieldsAPI/Concerns/ShowInReceipt.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Give\Framework\FieldsAPI\Concerns;

use Closure;

/**
* @since 2.10.2
*/
Expand All @@ -13,6 +15,16 @@ trait ShowInReceipt
*/
protected $showInReceipt = false;

/**
* @var string
*/
protected $receiptLabel;

/**
* @var Closure
*/
protected $receiptValueCallback;

/**
* @since 2.10.2
*/
Expand All @@ -30,4 +42,64 @@ public function shouldShowInReceipt(): bool
{
return $this->showInReceipt;
}

/**
* @unreleased
*/
public function receiptLabel(string $label): self
{
$this->receiptLabel = $label;

return $this;
}

/**
* @unreleased
*/
public function hasReceiptLabel(): bool
{
return !empty($this->receiptLabel);
}

/**
* @unreleased
*/
public function getReceiptLabel(): string
{
return $this->receiptLabel;
}

/**
* @unreleased
*/
public function hasReceiptValue(): bool
{
return !empty($this->receiptValueCallback);
}

/**
* @unreleased
*/
public function isReceiptValueCallback(): bool
{
return is_callable($this->receiptValueCallback);
}

/**
* @unreleased
*/
public function getReceiptValue(): Closure
{
return $this->receiptValueCallback;
}

/**
* @unreleased
*/
public function receiptValue(Closure $value): self
{
$this->receiptValueCallback = $value;

return $this;
}
}
57 changes: 47 additions & 10 deletions src/Framework/Receipts/Actions/GenerateConfirmationPageReceipt.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Give\Framework\Receipts\Actions;

use Exception;
use Give\DonationForms\Models\DonationForm;
use Give\DonationForms\Repositories\DonationFormRepository;
use Give\Donations\Models\Donation;
Expand All @@ -12,6 +13,7 @@
use Give\Framework\Receipts\DonationReceipt;
use Give\Framework\Receipts\Properties\ReceiptDetail;
use Give\Framework\TemplateTags\DonationTemplateTags;
use Give\Log\Log;

class GenerateConfirmationPageReceipt
{
Expand All @@ -30,6 +32,7 @@ public function __invoke(DonationReceipt $receipt): DonationReceipt
}

/**
* @unreleased updated conditional to check for scopes and added support for retrieving values programmatically with Fields API
* @since 3.0.0
*/
protected function getCustomFields(Donation $donation): array
Expand All @@ -46,26 +49,56 @@ protected function getCustomFields(Donation $donation): array
});

$receiptDetails = [];
foreach($customFields as $field) {
foreach ($customFields as $field) {
/** @var Field|HasLabel|HasName $field */
if ($field->shouldStoreAsDonorMeta()) {
if (!metadata_exists('donor', $donation->donor->id, $field->getName()) ) {
if ($field->hasReceiptValue()) {
try {
$value = $field->isReceiptValueCallback() ? $field->getReceiptValue()($field, $donation) : null;
} catch (Exception $e) {
$value = null;

Log::error('Error getting receipt value for field', [
'field' => $field->getName(),
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
}
} elseif ($field->getScope()->isDonor()) {
if (!metadata_exists('donor', $donation->donor->id, $field->getName())) {
continue;
}

$value = give()->donor_meta->get_meta($donation->donor->id, $field->getName(), true);
} else {
if (!metadata_exists('donation', $donation->id, $field->getName()) ) {
} elseif ($field->getScope()->isDonation()) {
if (!metadata_exists('donation', $donation->id, $field->getName())) {
continue;
}

$value = give()->payment_meta->get_meta($donation->id, $field->getName(), true);
} else {
$value = null;
}

$receiptDetails[] = new ReceiptDetail(
$field->getLabel(),
$value
$value = apply_filters(
sprintf("givewp_donation_confirmation_page_field_value_for_%s", $field->getName()),
$value,
$field,
$donation
);

$label = apply_filters(
sprintf("givewp_donation_confirmation_page_field_label_for_%s", $field->getName()),
$field->hasReceiptLabel() ? $field->getReceiptLabel() : $field->getLabel(),
$field,
$donation
);

if (!empty($label)){
$receiptDetails[] = new ReceiptDetail(
$label,
$value ?? ''
);
}
}

return $receiptDetails;
Expand All @@ -83,7 +116,10 @@ private function fillDonationDetails(DonationReceipt $receipt)
$paymentMethodLabel = give_get_gateway_checkout_label($receipt->donation->gatewayId, null);

if (empty($paymentMethodLabel) || $paymentMethodLabel === $receipt->donation->gatewayId) {
$paymentMethodLabel = $paymentGatewayRegistrar->hasPaymentGateway($receipt->donation->gatewayId) ? $paymentGatewayRegistrar->getPaymentGateway($receipt->donation->gatewayId)->getPaymentMethodLabel() : $receipt->donation->gatewayId;
$paymentMethodLabel = $paymentGatewayRegistrar->hasPaymentGateway(
$receipt->donation->gatewayId
) ? $paymentGatewayRegistrar->getPaymentGateway($receipt->donation->gatewayId)->getPaymentMethodLabel(
) : $receipt->donation->gatewayId;
}

$receipt->donationDetails->addDetails([
Expand Down Expand Up @@ -142,7 +178,8 @@ private function fillDonorDetails(DonationReceipt $receipt)
__('Billing Address', 'give'),
$receipt->donation->billingAddress->address1 . ' ' . $receipt->donation->billingAddress->address2 . PHP_EOL .
$receipt->donation->billingAddress->city . ($receipt->donation->billingAddress->state ? ', ' . $receipt->donation->billingAddress->state : '') . ' ' . $receipt->donation->billingAddress->zip . PHP_EOL .
$receipt->donation->billingAddress->country . PHP_EOL);
$receipt->donation->billingAddress->country . PHP_EOL
);
}

$receipt->donorDetails->addDetails($details);
Expand Down
56 changes: 56 additions & 0 deletions tests/Unit/Framework/FieldsAPI/Concerns/ShowInReceiptTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Unit\Framework\FieldsAPI\Concerns;

use Give\Framework\FieldsAPI\Concerns\ShowInReceipt;
use Give\Tests\TestCase;


/**
* @covers ShowInReceipt
*/
class ShowInReceiptTest extends TestCase
{
/**
* @unreleased
*/
public function testSettingTheReceiptLabel(): void
{
/** @var ShowInReceipt $mock */
$mock = $this->getMockForTrait(ShowInReceipt::class);

$mock->receiptLabel('test');
$this->assertTrue($mock->hasReceiptLabel());
$this->assertSame('test', $mock->getReceiptLabel());
}

/**
* @unreleased
*/
public function testSettingTheReceiptValue(): void
{
/** @var ShowInReceipt $mock */
$mock = $this->getMockForTrait(ShowInReceipt::class);

$value = function ($field, $donation) {
return 'test';
};

$mock->receiptValue($value);
$this->assertTrue($mock->hasReceiptValue());
self::assertIsCallable($mock->getReceiptValue());
$this->assertSame($value, $mock->getReceiptValue());
}

/**
* @unreleased
*/
public function testShouldShowInReceipt(): void
{
/** @var ShowInReceipt $mock */
$mock = $this->getMockForTrait(ShowInReceipt::class);

$mock->showInReceipt();
$this->assertTrue($mock->shouldShowInReceipt());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Give\DonationForms\Models\DonationForm;
use Give\Donations\Models\Donation;
use Give\Framework\FieldsAPI\Text;
use Give\Framework\Receipts\Actions\GenerateConfirmationPageReceipt;
use Give\Framework\Receipts\Properties\ReceiptDetail;
use Give\Framework\Receipts\Properties\ReceiptDetailCollection;
Expand Down Expand Up @@ -247,4 +248,51 @@ public function testShouldGenerateReceiptForRecurringDonation()
]
);
}

/**
* @unreleased
*/
public function testShouldAddCustomFieldsToAdditionalDetails(): void
{
$field = Text::make('favorite_color')
->showInAdmin()
->defaultValue('Blue')
->scope('custom_scope')
->showInReceipt()
->receiptLabel('Your favorite color:')
->receiptValue(static function (Text $field, $donation) {
return $field->getDefaultValue();
})
;

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

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

/** @var Donation $donation */
$donation = Donation::factory()->create([
'formId' => $donationForm->id
]);

$initialReceipt = new DonationReceipt($donation);

$receipt = (new GenerateConfirmationPageReceipt())($initialReceipt);

$additionalDetails = new ReceiptDetailCollection();

$additionalDetails->addDetail(
new ReceiptDetail(
__('Your favorite color:', 'give'),
'Blue'
)
);

$this->assertContains(
$additionalDetails->toArray()[0],
$receipt->additionalDetails->toArray()
);
}
}

0 comments on commit 050a76c

Please sign in to comment.