Klarna Checkout KCO 验证
Klarna Checkout KCO validate
我试图通过 Klarna Checkout 验证客户是否超过 20 岁。
他们有一个内置的验证功能,请参阅此处https://developers.klarna.com/en/se/kco-v2/checkout/use-cases#validate-checkout-order
checkout.php 页面看起来像这样
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"
}
}
当客户点击 "buy now" 时,klarna_validation.php 脚本运行,并向 Klarna 发送 return HTTP 状态 202 OK 或 303 SEE OTHER。
下面是我的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);
}
?>
根据 Klarna 的说法: POST 请求将发送到 merchant.validation_uri。请求正文将包含当前订单信息。订单信息的结构与获取订单的结果相同,正如您在渲染结帐中看到的那样。
问题是我没有得到任何数据
$_POST['customer']['date_of_birth'];它是空的。
为了验证这个 $_POST['customer']['date_of_birth']; 是空的,我将它包含在 URL 的 too_young.php 页面,像这样 (too_young.php?$pno)。当登陆 too_young.php 时 $pno 是空的! (网址看起来像这样 too_young.php?)
有人知道我做错了什么吗?
我们终于成功了!
我们只需要将这段代码添加到验证文件中:
$post_data = json_decode(file_get_contents('php://input'), 真);
像这样:
<?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);
}
?>
我试图通过 Klarna Checkout 验证客户是否超过 20 岁。
他们有一个内置的验证功能,请参阅此处https://developers.klarna.com/en/se/kco-v2/checkout/use-cases#validate-checkout-order
checkout.php 页面看起来像这样
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"
}
}
当客户点击 "buy now" 时,klarna_validation.php 脚本运行,并向 Klarna 发送 return HTTP 状态 202 OK 或 303 SEE OTHER。
下面是我的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);
}
?>
根据 Klarna 的说法: POST 请求将发送到 merchant.validation_uri。请求正文将包含当前订单信息。订单信息的结构与获取订单的结果相同,正如您在渲染结帐中看到的那样。
问题是我没有得到任何数据 $_POST['customer']['date_of_birth'];它是空的。
为了验证这个 $_POST['customer']['date_of_birth']; 是空的,我将它包含在 URL 的 too_young.php 页面,像这样 (too_young.php?$pno)。当登陆 too_young.php 时 $pno 是空的! (网址看起来像这样 too_young.php?)
有人知道我做错了什么吗?
我们终于成功了!
我们只需要将这段代码添加到验证文件中:
$post_data = json_decode(file_get_contents('php://input'), 真);
像这样:
<?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);
}
?>