Skip to content
loomark

Documentation

Passing mk_cid to the order

For the website developer: keep the click mark and send a signed event after payment. Example in PHP / Laravel.

The idea

  1. 01

    Landing page

    ?mk_cid=… in the address

  2. 02

    Session

    Store mk_cid

  3. 03

    Order

    Save mk_cid with the order

  4. 04

    Payment

    POST a signed purchase event

mk_cid is 22 Latin letters and digits. In redirect mode it is added to the landing page address by the tracking domain; in direct and parallel modes the website gets it from the visit endpoint or sends the platform click id instead.

Step 1. Keep mk_cid in the session

app/Http/Middleware/CaptureMkCid.php
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class CaptureMkCid
{
    public function handle(Request $request, Closure $next)
    {
        $cid = $request->query('mk_cid');
        if (is_string($cid) && preg_match('/^[A-Za-z0-9]{22}$/', $cid)) {
            $request->session()->put('mk_cid', $cid);
        }
        return $next($request);
    }
}

Register the middleware in the web group so it runs on every page.

Step 2. Save it with the order

When the order is created
// Migration: $table->string('mk_cid', 22)->nullable();
$order->mk_cid = session('mk_cid');
$order->save();

Step 3. Send a signed event after payment

app/Services/Loomark.php
<?php

namespace App\Services;

use Illuminate\Support\Facades\Http;

class Loomark
{
    public static function send(array $event): void
    {
        // Sign exactly the bytes you send.
        $body = json_encode(['events' => [$event]], JSON_UNESCAPED_SLASHES);
        $ts = (string) time();
        $sig = hash_hmac('sha256', $ts . '.' . $body, config('services.loomark.secret'));

        Http::withHeaders([
            'X-S2S-Key' => config('services.loomark.key'),
            'X-S2S-Timestamp' => $ts,
            'X-S2S-Signature' => 'sha256=' . $sig,
        ])
            ->withBody($body, 'application/json')
            ->post(config('services.loomark.endpoint'));
    }
}
After a successful payment
Loomark::send([
    'event' => 'purchase',
    'event_id' => 'order-' . $order->id,
    'mk_cid' => $order->mk_cid,
    'order_id' => (string) $order->id,
    'value' => (float) $order->total,
    'currency' => 'EUR',
]);

Put the key, the secret and the events address from the source window into config/services.php through environment variables, never into the code.

Step 4. Check

  1. Step 1Add 'test' => true to the event and run a test order.
  2. Step 2Open Tracking → Events: the event appears marked “test”.
  3. Step 3Remove test and go live.
The same request from the shell
BODY='{"events":[{"event":"purchase","event_id":"order-1001","mk_cid":"<mk_cid>","order_id":"1001","value":129.9,"currency":"EUR","test":true}]}'
TS=$(date +%s)
SIG=$(printf "%s.%s" "$TS" "$BODY" | openssl dgst -sha256 -hmac '<secret>' | sed 's/^.* //')
curl -X POST <events address> -H 'Content-Type: application/json' -H 'X-S2S-Key: <key>' -H "X-S2S-Timestamp: $TS" -H "X-S2S-Signature: sha256=$SIG" -d "$BODY"

Common errors

AnswerCause
401 signature_expiredThe timestamp differs from the server time by more than 5 minutes — check NTP
401 signature_invalidThe signature was computed over different bytes than were sent, or with another secret
401 unauthorizedWrong or deleted key
400 invalid_payloadThe body is not {"events": [...]} with 1–100 events
413 payload_too_largeThe body is larger than 512 KB

A 200 answer has counters accepted, duplicates, unmatched (no click found) and a list of rejected events.