File download system after payment

Our company is engaged in the development, support and maintenance of sites of any complexity. From simple one-page sites to large-scale cluster systems built on micro services. Experience of developers is confirmed by certificates from vendors.
Development and maintenance of all types of websites:
Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Showing 1 of 1 servicesAll 2065 services
File download system after payment
Medium
~2-3 business days
FAQ
Our competencies:
Development stages
Latest works
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1161
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1041
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    822
  • image_crm_chasseurs_493_0.webp
    CRM development for Chasseurs
    847
  • image_website-sbh_0.png
    Website development for SBH Partners
    999
  • image_website-_0.png
    Website development for Red Pear
    451

Implementing File Download System After Payment

File download after payment is a critical path in digital goods sales. The file must become available within seconds of payment confirmation, the link must be single-use or limited, and the file itself should never be served directly from a public directory.

Data Flow

Payment confirmed (webhook from payment processor)
  ↓
PaymentController::webhook()
  ↓
PaymentConfirmedEvent → CreateDownloadLinksListener
  ↓
foreach (order.items as item where item.is_digital):
  CreateDigitalDownloadAction::execute(item)
    → DigitalOrderDownload (token, limits, expiry)
  ↓
DigitalDownloadReadyMail → customer receives email
  ↓
Customer clicks link → /download/{token}
  ↓
DigitalDownloadController::download(token)
  → token validation
  → file streaming

Handling Payment Webhook

class PaymentWebhookController
{
    public function handle(Request $request, string $provider): JsonResponse
    {
        $handler = PaymentHandlerFactory::make($provider);

        // Verify webhook signature
        if (!$handler->verifySignature($request)) {
            Log::warning('Invalid payment webhook signature', ['provider' => $provider]);
            abort(400);
        }

        $paymentResult = $handler->parse($request);

        if ($paymentResult->isSuccessful()) {
            $order = Order::where('payment_id', $paymentResult->transactionId)->firstOrFail();

            DB::transaction(function () use ($order, $paymentResult) {
                $order->update([
                    'status'     => 'paid',
                    'paid_at'    => now(),
                    'payment_id' => $paymentResult->transactionId,
                ]);

                event(new PaymentConfirmedEvent($order));
            });
        }

        return response()->json(['ok' => true]);
    }
}

Synchronous vs. Asynchronous Link Creation

Synchronously (inline in Listener) — customer receives email 1–2 seconds after payment. Works for small number of items.

Asynchronously (via Queue) — more reliable under high load. Email may delay a few seconds, but won't delay webhook HTTP response.

class CreateDownloadLinksListener implements ShouldQueue
{
    public $queue = 'digital-downloads';
    public $tries = 5;
    public $backoff = [5, 15, 30, 60, 120];

    public function handle(PaymentConfirmedEvent $event): void
    {
        $order = $event->order;

        $digitalItems = $order->items->filter(
            fn($item) => $item->product->digitalProduct !== null
        );

        foreach ($digitalItems as $item) {
            app(CreateDigitalDownloadAction::class)->execute($item);
        }
    }
}

Streaming Large Files

When serving files from PHP, don't load entire file into memory. Laravel's Storage::download() uses streaming automatically, but for very large files (>500 MB) use X-Accel-Redirect (nginx) or presigned URL (S3):

// Option 1: X-Accel-Redirect (nginx serves file directly, PHP only authorizes)
public function downloadViaAccel(DigitalOrderDownload $download): Response
{
    $this->validateDownload($download);
    $this->recordDownload($download);

    $internalPath = '/private-files/' . $download->digitalProduct->storage_path;

    return response('', 200, [
        'X-Accel-Redirect'       => $internalPath,
        'Content-Type'           => $download->digitalProduct->mime_type,
        'Content-Disposition'    => 'attachment; filename="' . $download->digitalProduct->original_filename . '"',
        'X-Content-Type-Options' => 'nosniff',
    ]);
}
# nginx config
location /private-files/ {
    internal;
    alias /var/www/storage/app/private/;
}
// Option 2: S3 Presigned URL (for large files, CDN delivery)
public function downloadViaS3(DigitalOrderDownload $download): RedirectResponse
{
    $this->validateDownload($download);
    $this->recordDownload($download);

    $url = Storage::disk('s3')->temporaryUrl(
        path: $download->digitalProduct->storage_path,
        expiration: now()->addMinutes(10),
        options: [
            'ResponseContentDisposition' => sprintf(
                'attachment; filename="%s"',
                $download->digitalProduct->original_filename
            ),
        ]
    );

    return redirect($url);
}

Email with Download Link

class DigitalDownloadReadyMail extends Mailable
{
    use Queueable, SerializesModels;

    public function __construct(
        private DigitalOrderDownload $download,
    ) {}

    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Your Digital Product is Ready to Download',
        );
    }

    public function content(): Content
    {
        return new Content(
            view: 'mails.digital-download-ready',
            with: [
                'downloadUrl' => route('downloads.show', $this->download->token),
                'expiresAt'   => $this->download->expires_at,
                'remainingDownloads' => $this->download->remaining_downloads,
            ],
        );
    }
}

Timeline

Basic implementation: webhook handling + single-use links + email — 2–3 days. Complete system: multiple file types, streaming, presigned URLs, download limits, expiry management — 4–6 days.