Skip to tutorial content
By Chason ArthurRevision 116.024Updated

Secure Merchant Checkout

Static tutorial companion. The complete offline Laravel demo is available in the original project download. No backend or real Stripe adapter is activated by this page.

Read local setup instructions

Lab discussion / Secure Merchant Checkout

What would you try next?

Ask about the code, compare an approach, or describe an edge case. Reader comments are reviewed before publication.

All lab discussions

Reader comments

Open the discussion to read and add reader comments.

Read or write a comment →

Code walkthrough

Follow the implementation in code.

Use these examples alongside the steps on this page. Read the explanation before copying a snippet; source excerpts depend on the surrounding implementation, and illustrative examples are labeled.

Example 01 · Source excerpt

Reject fields outside the checkout contract

The offline PHP example allows a small request shape. Clients send a product selection and an operation key; they cannot add their own trusted payment or order fields.

if (array_diff(array_keys($input), [
    'operation', 'currency', 'items', 'scenario'
])) {
    throw new Problem('untrusted_fields');
}

Inspect MerchantCheckout.php

Example 02 · Source excerpt

Bind a retry to the same request

The operation key alone is insufficient. Inside the checkout transaction, the stored fingerprint must match before the original operation can be returned.

if ($existing) {
    if (!hash_equals($existing['fingerprint'], $fingerprint)) {
        throw new Problem('idempotency_mismatch', 409);
    }
    return $existing;
}

Inspect the complete transaction

Example 03 · Source excerpt

Calculate the amount from the selected product

The server-side fixture reads the product and its currency before this calculation. Whole minor units avoid accepting a browser-supplied price.

if ($product['currency'] !== $currency) {
    throw new Problem('currency_mismatch');
}
$amount += $product['unit_minor'] * $item['quantity'];

Inspect product lookup and snapshot creation

Example 04 · Source excerpt

Verify the original signed payload

This is the signature-verification call in the downloadable adapter. The surrounding class also bounds payload size and validates the decoded event. The public companion does not activate a Stripe endpoint.

try {
    Webhook::constructEvent($raw, $signature, $secret, 300);
} catch (\UnexpectedValueException $error) {
    throw new Problem('invalid_payload', 400);
} catch (Throwable $error) {
    throw new Problem('invalid_signature', 400);
}

Inspect SignedEnvelope.php

Example 05 · Source excerpt

Deduplicate an incoming event by identity and body

The offline inbox records provider event identity and a body hash. A repeated matching event returns its existing receipt; a reused identity with different bytes is rejected. Processing happens separately.

$hash = hash('sha256', $raw);
$existing = $this->store->one(
    'SELECT * FROM inbox WHERE provider_event_id = ?',
    [$event['id']]
);
if ($existing) {
    if (!hash_equals($existing['body_hash'], $hash)) {
        throw new Problem('event_id_collision', 409);
    }
    return ['id' => $existing['id'], 'duplicate' => true];
}

Inspect Inbox.php