Lesson 115 – Implementation: Implementing the Buy Now Auction Completion Workflow


Introduction

In the previous lesson, we outlined how the Buy Now feature should behave from a business perspective. In this implementation lesson, we transform that design into working code.

Rather than introducing a separate purchase engine, the implementation builds upon the auction infrastructure already developed throughout the project. The result is a cleaner architecture where a Buy Now purchase is simply a special case of a successful bid that immediately concludes the auction.


Step 1 – Detect Buy Now Bids

A new helper method was introduced:

Flipnzee_Bid_Manager::is_buy_now_bid()

This method retrieves the configured Buy Now price for the auction and compares it against the submitted bid amount.

If the bid is equal to or greater than the Buy Now price, the method returns true.

Keeping this logic separate makes the bid placement code easier to understand and allows future enhancements without modifying the core bidding workflow.


Step 2 – Update the Bid Handler

After a successful bid is recorded, the bid handler now performs an additional check:

$is_buy_now = Flipnzee_Bid_Manager::is_buy_now_bid(
    $auction_id,
    $bid_amount
);

For ordinary bids, execution continues exactly as before.

For Buy Now bids, the workflow branches into an immediate auction completion sequence.


Step 3 – Close the Auction

A new method was added to the Auction Manager:

Flipnzee_Auction_Manager::close_auction(
    $auction_id
);

This method:

  • updates the auction status to closed,
  • records the closing timestamp,
  • returns whether the update succeeded.

Centralising this behaviour inside the Auction Manager keeps auction state management in a single location.


Step 4 – Determine the Winner Immediately

Once the auction is closed, the existing winner determination logic is reused:

Flipnzee_Bid_Manager::determine_winner(
    $auction_id
);

No duplicate winner-selection logic is required.

The plugin simply performs the same process that would normally occur after the scheduled auction expiry.


Step 5 – Reuse Existing Hooks

Because winner determination already fires the existing action hook:

do_action(
    'flipnzee_auction_winner_determined',
    $auction_id,
    $winner
);

the following systems continue working automatically:

  • Buyer notification
  • Seller notification
  • Administrator notification
  • Transaction creation

This demonstrates one of the benefits of designing around WordPress actions rather than tightly coupled method calls.


Step 6 – Automatically Create the Transaction

The existing Transaction Manager now creates the purchase transaction immediately after the winner is determined.

This removes the delay that previously existed between auction completion and payment.

The buyer is now ready to proceed directly to the payment stage.


Step 7 – Integrate the External Provider Workflow

During implementation, the transaction workflow also creates an associated external provider record for future integrations such as Escrow.com.

This lays the foundation for supporting external payment and escrow services without altering the auction workflow itself.


Debugging the Workflow

This lesson involved significantly more debugging than implementation.

Extensive logging was added throughout the Buy Now workflow to verify each stage executed correctly.

Typical log entries included:

  • Buy Now detection
  • Auction closure
  • Winner determination
  • Notification dispatch
  • Transaction creation
  • External provider creation

These logs made it possible to isolate failures quickly and verify that each subsystem executed in the expected order.


Issues Encountered

Several issues surfaced while implementing this workflow:

  • Buy Now bids behaved like normal bids.
  • Auctions remained active after reaching the Buy Now price.
  • Winner determination was not triggered immediately.
  • Transaction creation exposed a missing class loading issue for the External Provider Manager.
  • Front-end auction state required refreshing after administrative changes because the database status remained closed until explicitly reopened.

Resolving these issues reinforced the importance of validating the complete workflow rather than assuming each individual component behaved correctly in isolation.


Final Workflow

After completing Lesson 115, the Buy Now process now follows this sequence:

Buyer submits Buy Now bid
        │
        ▼
Bid accepted
        │
        ▼
Buy Now detected
        │
        ▼
Auction closed
        │
        ▼
Winner determined
        │
        ▼
Notifications sent
        │
        ▼
Transaction created
        │
        ▼
External provider record created
        │
        ▼
Buyer proceeds to payment

Conclusion

With this lesson complete, the Flipnzee Auctions plugin now supports an end-to-end Buy Now workflow. A qualifying bid no longer waits for the auction timer to expire; instead, it immediately concludes the auction, determines the winner, creates the transaction, and launches the payment process.

This represents a major architectural milestone. The plugin has evolved from handling bids and scheduled auction endings to supporting immediate purchases through a unified auction lifecycle, providing a solid foundation for future enhancements such as escrow integrations, automated transfers, and richer post-sale workflows.

https://github.com/SplendidDigital/flipnzee-auctions/releases/tag/lesson-115-stable

Lesson 115: Buy Now Auction Completion Workflow (Planning)

Introduction

Until now, the Flipnzee Auctions plugin has treated every bid in the same way. Regardless of the bid amount, the auction remains active until its scheduled end time, where a scheduled process later determines the winner.

However, this behavior is not how a traditional Buy Now feature is expected to work.

When a bidder agrees to pay the Buy Now price, they are effectively accepting the seller’s asking price. At that point there should be no reason to keep the auction running or allow additional bids.

This lesson focuses on transforming Buy Now from a simple display price into an action that immediately completes the auction.


Current Problem

Suppose an auction has:

  • Start Price: $120
  • Current Bid: $1,200
  • Buy Now Price: $5,000

If a buyer places a bid of exactly $5,000, the plugin currently:

  • accepts the bid,
  • updates the current bid,
  • leaves the auction active,
  • allows other users to continue bidding.

This defeats the purpose of having a Buy Now option.


Expected Behaviour

The expected workflow should become:

Buyer submits Buy Now bid
        │
        ▼
Bid is accepted
        │
        ▼
Buy Now condition detected
        │
        ▼
Auction closes immediately
        │
        ▼
Winner determined
        │
        ▼
Winner notifications sent
        │
        ▼
Transaction created
        │
        ▼
Buyer redirected to payment

Instead of waiting until the scheduled auction end, the auction lifecycle should complete immediately.


Why This Matters

This change transforms the auction from a passive bidding system into an actual marketplace transaction.

It establishes a complete workflow where:

  • bidding,
  • winner determination,
  • transaction creation,
  • payment,
  • ownership transfer

all become part of a single automated process.

Without this behaviour, Buy Now is merely another bid amount rather than an instant purchase mechanism.


Design Considerations

Rather than scattering Buy Now logic throughout the plugin, we will introduce a clear sequence of responsibilities.

The bid handler should remain responsible for accepting bids.

Once a valid bid has been recorded, it should ask one simple question:

“Did this bid satisfy the Buy Now price?”

If the answer is yes, the auction manager will immediately close the auction and the existing winner determination workflow can continue unchanged.

This approach reuses the infrastructure already built in previous lessons instead of creating an entirely separate purchase system.


Objectives

By the end of this lesson we will:

  • Detect when a submitted bid reaches the Buy Now price.
  • Close the auction immediately.
  • Determine the winning bidder instantly.
  • Reuse the existing notification system.
  • Automatically create the buyer transaction.
  • Launch the payment workflow without waiting for auction expiry.

What We’ll Build

At the end of this lesson, the auction lifecycle will look like this:

Auction Created
        │
        ▼
Buyer Places Bid
        │
        ▼
Buy Now Price Reached
        │
        ▼
Auction Closed Immediately
        │
        ▼
Winner Determined
        │
        ▼
Notifications Sent
        │
        ▼
Transaction Created
        │
        ▼
Buyer Payment Page

This is one of the most important milestones in the Flipnzee Auctions project, as it connects bidding with the complete post-auction purchase workflow.