Create a payment
Record a successful payment and attribute it to the visit that led to it.
POST https://usedatasense.com/api/v1/payments
Takes a website key, or an account token with payments:write and websiteId in the body or query. Server-side only: there's no CORS.
sh
curl -X POST https://usedatasense.com/api/v1/payments \
-H "Authorization: Bearer $DATASENSE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"amount": 29.99,
"currency": "USD",
"transaction_id": "pi_3PZx…",
"datasense_session_id": "lq2k9x…"
}'| Field | Type | Required | Notes |
|---|---|---|---|
amount |
number | Yes | Major units (29.99, not 2999). Zero or more. |
currency |
string | Yes | 3-letter code such as USD. Converted into the site's reporting currency at the day's rate. |
transaction_id |
string | Yes | The payment's ID at your provider. Repeats are de-duplicated. |
datasense_session_id |
string | No | The datasense_sid cookie, or window.datasense.sessionId() at checkout. Credits the payment to that visit. |
datasense_visitor_id |
string | No | The datasense_vid cookie (cookie script). Attributes to the visitor's latest visit when there's no session. |
customer_id |
string | No | Your provider's customer ID. A renewal without a session is credited to the visit that won the customer. |
email |
string | No | The customer's email, matched against datasense("payment", { email }) calls. Stored only as a hash. |
renewal |
boolean | No | true for subscription renewals. |
refunded |
boolean | No | Send the payment again with true to record a refund. |
is_free_trial |
boolean | No | true for a $0 trial start. |
timestamp |
string | No | ISO 8601. Defaults to now; can't be in the future. |
websiteId |
string | With an account token | The website the payment belongs to. |
Response
json
{ "message": "Payment recorded and attributed", "transaction_id": "pi_3PZx…", "attributed": true, "matched_by": "metadata" }message is one of "Payment recorded and attributed", "Payment recorded without attribution", "Payment updated", "Refund recorded", "Duplicate payment ignored" or "Refund ignored" (no payment with that transaction_id). matched_by says how the visit was found: metadata, return_page, customer, email, or empty. Errors answer { "error": "…" } with 400, 401, 403, 429 or 503.