1

我试图通过 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"
    }
}

当客户点击“立即购买”时,klarna_validation.php 脚本运行,并向 Klarna 发送返回 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']; 是空的,我已将它包含在 too_young.php 页面的 URL 中,就像这样 (too_young.php?$pno)。当登陆 too_young.php 时,$pno 是空的!(网址看起来像这样 too_young.php?)

有谁知道我做错了什么?

4

1 回答 1

2

最后我们让它工作了!

我们只需要将这段代码添加到验证文件中:
$post_data = json_decode(file_get_contents('php://input'), true);

像这样:

<?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);
}
?>
于 2016-05-12T05:44:25.220 回答