I a trying to validate that the customer is above 20 thru Klarna Checkout.
They have a built in function for validating, see here https://developers.klarna.com/en/se/kco-v2/checkout/use-cases#validate-checkout-order
The checkout.php page looks like this
Accept: application/vnd.klarna.checkout.aggregated-order-v2+json
Authorization: Klarna pwhcueUff0MmwLShJiBE9JHA==
Content-Type: application/vnd.klarna.checkout.aggregated-order-v2+json
{
"purchase_country": "se",
"purchase_currency": "sek",
"locale": "sv-se",
"cart": {
"items": [
{
"reference": "123456789",
"name": "Klarna t-shirt",
"quantity": 2,
"unit_price": 12300,
"discount": 1000,
"tax_rate": 2500
},
{
"type": "shipping_fee",
"reference": "SHIPPING",
"name": "Shipping fee",
"quantity": 1,
"unit_price": 4900,
"tax_rate": 2500
}
]
},
"merchant": {
"id": "0",
"terms_uri": "http://example.com/terms.php",
"checkout_uri": "https://example.com/checkout.php",
"confirmation_uri": "https://example.com/thankyou.php?sid=123&klarna_order={checkout.order.uri}",
"push_uri": "https://example.com/push.php?sid=123&klarna_order={checkout.order.uri}",
"validation_uri": "https://example.com/klarna_validation.php"
}
}
When a customer cliks "buy now" the klarna_validation.php script runs, and sends a return to Klarna with a HTTP status 202 OK or 303 SEE OTHER.
Below are my klarna_validation.php
<?php
$pno = $_POST['customer']['date_of_birth'];
$birthdate = new DateTime("$pno");
$today = new DateTime();
$interval = $today->diff($birthdate);
$interval2 = $interval->format('%y');
if($interval2 <= "20"){
header("Location: https://example.com/too_young.php?$pno", true, 303);
exit;
} else {
http_response_code(200);
}
?>
According to Klarna: POST request will be sent to the merchant.validation_uri. The body of the request will contain the current order information. The structure of the order information is identical to the result of fetching the order, as you saw in render the checkout.
The thing is that i don't get any data with $_POST['customer']['date_of_birth']; it's empty.
To validate that this $_POST['customer']['date_of_birth']; is empty i have included it in the URL of the too_young.php page, like this (too_young.php?$pno). When landing on too_young.php the $pno is empty! (The urls looks like this too_young.php?)
Does anyone have an idea of what i am doing wrong?
Finally we got it to work!
We just had to add this code to the validate file:
$post_data = json_decode(file_get_contents('php://input'), true);
Like this:
<?php
$post_data = json_decode(file_get_contents('php://input'), true);
$pno = $post_data['customer']['date_of_birth'];
$birthdate = new DateTime("$pno");
$today = new DateTime();
$interval = $today->diff($birthdate);
$interval2 = $interval->format('%y');
if($interval2 < "60"){
header("Location: https://example.com/too_young.php?$pno&$interval2", true, 303);
exit;
} else {
http_response_code(200);
}
?>