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 instructionsStatic 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 instructionsLab discussion / Secure Merchant Checkout
Ask about the code, compare an approach, or describe an edge case. Reader comments are reviewed before publication.
Code walkthrough
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.
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');
}
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;
}
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'];
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);
}
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];
}