← All changes
|
src/EventTickets/Actions/GenerateTicketsFromPurchaseData.php
+15
-7
4.16.5
→
4.16.9
View file →
| @@ -4,8 +4,9 @@ | ||
| 4 | 4 | |
| 5 | 5 | use Give\Donations\Models\Donation; |
| 6 | 6 | use Give\EventTickets\DataTransferObjects\TicketPurchaseData; |
| 7 | 7 | use Give\EventTickets\Models\EventTicket; |
| 8 | +use Give\Framework\Exceptions\Primitives\RuntimeException; | |
| 8 | 9 | use Give\Framework\Support\ValueObjects\Money; |
| 9 | 10 | |
| 10 | 11 | /** |
| 11 | 12 | * @since 3.6.0 |
| @@ -26,8 +27,9 @@ | ||
| 26 | 27 | $this->donation = $donation; |
| 27 | 28 | } |
| 28 | 29 | |
| 29 | 30 | /** |
| 31 | + * @since 4.16.8.1 Stop relying on a count taken once before the loop — EventTicketRepository::insert() now enforces remaining capacity itself, atomically with each insert, closing a race that let concurrent purchases jointly oversell a ticket type. | |
| 30 | 32 | * @since 4.6.0 Add support for currency conversion |
| 31 | 33 | * @since 3.20.0 Add "amount" to the array of props |
| 32 | 34 | * @since 3.6.0 |
| 33 | 35 | */ |
| @@ -32,9 +34,11 @@ | ||
| 32 | 34 | * @since 3.6.0 |
| 33 | 35 | */ |
| 34 | 36 | public function __invoke(TicketPurchaseData $data) |
| 35 | 37 | { |
| 36 | - for($i = 0; $i < $data->quantity; $i++) { | |
| 38 | + $quantity = max(0, $data->quantity); | |
| 39 | + | |
| 40 | + for($i = 0; $i < $quantity; $i++) { | |
| 37 | 41 | $amount = $data->ticketType->price; |
| 38 | 42 | |
| 39 | 43 | if ($this->donation->amount->getCurrency() !== $data->ticketType->price->getCurrency()) { |
| 40 | 44 | $amount = new Money($data->ticketType->price->multiply($this->donation->exchangeRate)->getAmount(), $this->donation->amount->getCurrency()); |
| @@ -39,13 +43,17 @@ | ||
| 39 | 43 | if ($this->donation->amount->getCurrency() !== $data->ticketType->price->getCurrency()) { |
| 40 | 44 | $amount = new Money($data->ticketType->price->multiply($this->donation->exchangeRate)->getAmount(), $this->donation->amount->getCurrency()); |
| 41 | 45 | } |
| 42 | 46 | |
| 43 | - EventTicket::create([ | |
| 44 | - 'eventId' => $data->ticketType->eventId, | |
| 45 | - 'ticketTypeId' => $data->ticketType->id, | |
| 46 | - 'donationId' => $this->donation->id, | |
| 47 | - 'amount' => $amount, | |
| 48 | - ]); | |
| 47 | + try { | |
| 48 | + EventTicket::create([ | |
| 49 | + 'eventId' => $data->ticketType->eventId, | |
| 50 | + 'ticketTypeId' => $data->ticketType->id, | |
| 51 | + 'donationId' => $this->donation->id, | |
| 52 | + 'amount' => $amount, | |
| 53 | + ]); | |
| 54 | + } catch (RuntimeException $exception) { | |
| 55 | + break; | |
| 56 | + } | |
| 49 | 57 | } |
| 50 | 58 | } |
| 51 | 59 | } |