| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\EventTickets\Actions; |
| 4 |
|
| 5 |
use Give\Donations\Models\Donation; |
| 6 |
use Give\Donations\Models\DonationNote; |
| 7 |
use Give\EventTickets\Repositories\EventTicketRepository; |
| 8 |
|
| 9 |
/** |
| 10 |
* A minted event ticket is a paid perk granted on the assumption its donation succeeds. Without |
| 11 |
* this, a donor could purchase a ticket and then have the donation cancelled/refunded/failed and |
| 12 |
* still keep the ticket — access granted without a completed payment behind it. This deletes any |
| 13 |
* tickets tied to a donation whose status means that payment didn't happen (or was reversed), so |
| 14 |
* the perk is revoked along with it. |
| 15 |
* |
| 16 |
* Runs on every donation status change (see ServiceProvider's givewp_donation_updated listener) |
| 17 |
* and is a no-op unless the new status is one of excludesFromSales(). |
| 18 |
* |
| 19 |
* @since 4.16.8.1 |
| 20 |
*/ |
| 21 |
class ReleaseEventTicketsForDonation |
| 22 |
{ |
| 23 |
/** |
| 24 |
* @since 4.16.8.1 |
| 25 |
*/ |
| 26 |
public function __invoke(Donation $donation) |
| 27 |
{ |
| 28 |
if (!$this->excludesFromSales($donation)) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
$tickets = give(EventTicketRepository::class)->queryByDonationId($donation->id)->getAll() ?? []; |
| 33 |
|
| 34 |
// Each delete() runs in its own transaction (matches the per-row transaction style elsewhere |
| 35 |
// in EventTicketRepository); a mid-loop failure can leave a release partially applied. |
| 36 |
foreach ($tickets as $ticket) { |
| 37 |
$ticket->delete(); |
| 38 |
|
| 39 |
// The ticket row is gone after delete(), so the note carries its identifying details |
| 40 |
// itself rather than a ticket ID nothing will resolve afterward. |
| 41 |
DonationNote::create([ |
| 42 |
'donationId' => $donation->id, |
| 43 |
'content' => sprintf( |
| 44 |
/* translators: 1: event ID, 2: ticket type ID, 3: donation status */ |
| 45 |
__('Event ticket released (event #%1$d, ticket type #%2$d) because the donation status changed to "%3$s".', 'give'), |
| 46 |
$ticket->eventId, |
| 47 |
$ticket->ticketTypeId, |
| 48 |
$donation->status->label() |
| 49 |
), |
| 50 |
]); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @since 4.16.8.1 |
| 56 |
*/ |
| 57 |
private function excludesFromSales(Donation $donation): bool |
| 58 |
{ |
| 59 |
$status = $donation->status; |
| 60 |
|
| 61 |
return $status->isCancelled() |
| 62 |
|| $status->isRefunded() |
| 63 |
|| $status->isFailed() |
| 64 |
|| $status->isAbandoned() |
| 65 |
|| $status->isRevoked(); |
| 66 |
} |
| 67 |
} |
| 68 |
|