0

我正在使用 Paymill 作为我的支付提供商,但我被 Webhooks 卡住了。

目前,我按照以下通过电子邮件收到 webhook,但我想在页面中选择它并将其发布到我的数据库中。我知道该怎么做,但是我不确定我是如何拿起个别帖子的。

{
    "event_type":"transaction.created",
    "event_resource":{
        "id":"tran_9x2x8xex5x7xexex0x8x9xexexbx",
        "amount":"9980",
        "origin_amount":9980,
        "status":"open",
        "description":"9xax1x7x5x8x2xaxaxcx4x5xfx8x2x2x",
        "livemode":true,
        "refunds":null,
        "currency":"GBP",
        "created_at":1375307922,
        "updated_at":1375307922,
        "response_code":10001,
        "short_id":null,
        "is_fraud":false,
        "invoices":[

        ],
        "app_id":null,
        "fees":[

        ],
        "payment":{
            "id":"pay_7xdxex9xfxcx6x3x9x5x7xcx",
            "type":"creditcard",
            "client":"client_2x2xex0x2x4xx3bx3x6x",
            "card_type":"mastercard",
            "country":null,
            "expire_month":"10",
            "expire_year":"2015",
            "card_holder":"XXX XXXXXX",
            "last4":"8XX8",
            "created_at":1375307921,
            "updated_at":1375307922,
            "app_id":null
        },
        "client":{
            "id":"client_2x2xex0x2x4xfx3x3x6x",
            "email":null,
            "description":null,
            "created_at":1375307922,
            "updated_at":1375307922,
            "app_id":null,
            "payment":[

            ],
            "subscription":null
        },
        "preauthorization":null
    },
    "created_at":1375307922,
    "app_id":null
}

那么如何使用 php 提取 event_resource id?

通常我会做<?php $_POST['event_resource]; ?>

4

2 回答 2

1

这看起来像一个 JSON 对象,所以用json_decode()它来将它转换成一个不错的 PHP 对象。

$reply = json_decode( $data_from_hook );

示例代码:

<?php


    $t = '{
    "event_type":"transaction.created",
    "event_resource":{
        "id":"tran_9x2x8xex5x7xexex0x8x9xexexbx",
        "amount":"9980",
        "origin_amount":9980,
        "status":"open",
        "description":"9xax1x7x5x8x2xaxaxcx4x5xfx8x2x2x",
        "livemode":true,
        "refunds":null,
        "currency":"GBP",
        "created_at":1375307922,
        "updated_at":1375307922,
        "response_code":10001,
        "short_id":null,
        "is_fraud":false,
        "invoices":[

        ],
        "app_id":null,
        "fees":[

        ],
        "payment":{
            "id":"pay_7xdxex9xfxcx6x3x9x5x7xcx",
            "type":"creditcard",
            "client":"client_2x2xex0x2x4xx3bx3x6x",
            "card_type":"mastercard",
            "country":null,
            "expire_month":"10",
            "expire_year":"2015",
            "card_holder":"XXX XXXXXX",
            "last4":"8XX8",
            "created_at":1375307921,
            "updated_at":1375307922,
            "app_id":null
        },
        "client":{
            "id":"client_2x2xex0x2x4xfx3x3x6x",
            "email":null,
            "description":null,
            "created_at":1375307922,
            "updated_at":1375307922,
            "app_id":null,
            "payment":[

            ],
            "subscription":null
        },
        "preauthorization":null
    },
    "created_at":1375307922,
    "app_id":null
}';





echo PHP_EOL;
$u = json_decode($t);
print_r( $u );

echo 'Event Rosource ID = ' . $u->event_resource->id;

结果:

stdClass Object
(
    [event_type] => transaction.created
    [event_resource] => stdClass Object
        (
            [id] => tran_9x2x8xex5x7xexex0x8x9xexexbx
            [amount] => 9980
            [origin_amount] => 9980
            [status] => open
            [description] => 9xax1x7x5x8x2xaxaxcx4x5xfx8x2x2x
            [livemode] => 1
            [refunds] =>
            [currency] => GBP
            [created_at] => 1375307922
            [updated_at] => 1375307922
            [response_code] => 10001
            [short_id] =>
            [is_fraud] =>
            [invoices] => Array
                (
                )

            [app_id] =>
            [fees] => Array
                (
                )

            [payment] => stdClass Object
                (
                    [id] => pay_7xdxex9xfxcx6x3x9x5x7xcx
                    [type] => creditcard
                    [client] => client_2x2xex0x2x4xx3bx3x6x
                    [card_type] => mastercard
                    [country] =>
                    [expire_month] => 10
                    [expire_year] => 2015
                    [card_holder] => XXX XXXXXX
                    [last4] => 8XX8
                    [created_at] => 1375307921
                    [updated_at] => 1375307922
                    [app_id] =>
                )

            [client] => stdClass Object
                (
                    [id] => client_2x2xex0x2x4xfx3x3x6x
                    [email] =>
                    [description] =>
                    [created_at] => 1375307922
                    [updated_at] => 1375307922
                    [app_id] =>
                    [payment] => Array
                        (
                        )

                    [subscription] =>
                )

            [preauthorization] =>
        )

    [created_at] => 1375307922
    [app_id] =>
)


Event Rosource ID = tran_9x2x8xex5x7xexex0x8x9xexexbx
于 2013-08-01T12:42:57.343 回答
0

这将为您提供 json:

$body = @file_get_contents('php://input');
$json = json_decode($body, true);
于 2014-03-19T00:08:55.527 回答