Hi guys,
I have a raffle script that I'd like to write a "refund" function. The "complete" function will distribute the the raffle pot size to one user at random.
What I'm trying to do is create a function that will return the amount a user paid for a raffle ticket to each user that bought one.
I've tried this $accountService->createTransaction($winningTicket, $raffle->ticket_price); and it would refund the amount to the user who has the "winning" ticket but I'm not sure how to go about refunding to all users who bought a ticket.
Any help would be greatly appreciated. :)
/**
* Buy raffle ticket(s)
*
* @param int $quantity
* @throws \Exception
*/
public function buy(int $quantity = 1)
{
if ($quantity < 1) {
throw new \Exception('Quantity can not be less than 1.');
}
$accountService = new AccountService($this->user->account);
for ($i=1; $i <= $quantity; $i++) {
// create a RaffleTicket model
$raffleTicket = $this->raffle->tickets()->create([
'raffle_id' => $this->raffle->id,
'account_id' => $this->user->account->id
]);
// create account transaction
$accountService->createTransaction($raffleTicket, -$this->raffle->ticket_price);
}
event(new RaffleTicketsBought($this->raffle, $this->user, $quantity));
}
/**
* Complete raffle
*
* @param Raffle $raffle
*/
public static function complete(Raffle $raffle)
{
// if some tickets were purchased
if ($raffle->pot_size > 0) {
// draw a random ticket
$winningTicket = $raffle->tickets->random();
// update raffle
$raffle->winningTicket()->associate($winningTicket);
$raffle->win = $raffle->pot_size;
// create account transaction
$accountService = new AccountService($winningTicket->account);
$accountService->createTransaction($winningTicket, $raffle->pot_size);
}
// complete the raffle
$raffle->status = Raffle::STATUS_COMPLETED;
// set end date for raffles that don't have end date (finish after all tickets are purchased)
$raffle->end_date = $raffle->end_date ?: Carbon::now();
$raffle->save();
// clone recurring raffle
if ($raffle->recurring) {
self::replicate($raffle);
}
event(new RaffleCompleted($raffle));
}