Jump to content

Raffle refund function


Recommended Posts

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->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();

        // clone recurring raffle
        if ($raffle->recurring) {

        event(new RaffleCompleted($raffle));


Link to comment
Share on other sites

Not a whole lot here to work from so it'll be hard to provide more than just an educated guess...

I take it when you say "refund" you mean as an alternative to "complete"ing the raffle? You're cancelling the entire raffle and refunding the tickets, not just refunding a single ticket? I suggest picking a different verb that more accurately reflects that the raffle is not active anymore - such as "cancel".

The pieces you need are:
1. Getting all the tickets in the raffle
2. Refunding all of them
3. Closing out the raffle

#1 is apparently in $raffle->tickets. I don't know exactly what that is but I'm confident you can use it to identify all the tickets. You'll likely want a foreach loop.
#2 is going to be something in the AccountService. Possibly createTransaction as well. Inside your loop, you create a new AccountService for every ticket's account (since it depends on the account) and then calling whatever method you need according to the price of the ticket.
#3 is likely going to be a copy of the second half of your complete() except (1) using a different Raffle::STATUS, and (2) maybe or maybe not "replicate"ing the raffle, and (3) firing an event that may or may not be RaffleCompleted but should obviously be somehow different than a regular "raffle completed" event.

Side note: in this new method as well as your complete() you should make sure that the raffle is still active before continuing - wouldn't want to accidentally refund the same raffle multiple times, or one that's been completed and paid out, right? Similarly for buy() that your code shouldn't allow buying a ticket for something that isn't running anymore.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.