<?php
function awardPoints(array $order): ?array {
$points = (int) round($order["total_usd"] * 100); // 100 points per USD
$ch = curl_init("https://api.rigaly.com/api/v1/points/issue");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
"Authorization: Bearer " . getenv("RIGALY_API_KEY"),
"Content-Type: application/json",
"Idempotency-Key: order-" . $order["id"],
],
CURLOPT_POSTFIELDS => json_encode([
"identifier" => $order["customer_email"],
"points" => $points,
"description" => "Order #" . $order["id"],
]),
]);
$body = json_decode(curl_exec($ch), true);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) === 404) {
return null; // customer has no Rigaly account yet
}
return $body["data"];
}