使用laravel 4从shopify中进行Webhook

时间:2014-03-15 05:53:48

标签: laravel-4 shopify

使用laravel在订单创建时使用webhook进行shopify

我将webhook中的网址指定到我的网站。

我使用postcatcher从webhook调用接收数据,以便我知道他们传入了哪些数据。

数据看起来像这样

{
    "customer": {
        "default_address": {
            "default": true,
            "country_name": "Singapore",
            "country_code": "SG",
            "province_code": null,
            "name": "Own Card",
            "zip": "234567",
            "province": "Singapore",
            "phone": "92965040",
            "last_name": "Card",
            "id": 275454813,
            "first_name": "Own",
            "country": "Singapore",
            "company": "",
            "city": "Singapore",
            "address2": "",
            "address1": "Toa Block 233 #10-254"
        },
        "last_order_name": null,
        "tags": "",
        "verified_email": true,
        "updated_at": "2014-03-14T06:10:59-04:00",
        "total_spent": "0.00",
        "state": "disabled",
        "orders_count": 0,
        "note": null,
        "multipass_identifier": null,
        "last_order_id": null,
        "last_name": "Koh",
        "id": 219085425,
        "first_name": "Kwang",
        "email": "kwanghock@a.com.sg",
        "created_at": "2014-03-14T05:55:55-04:00",
        "accepts_marketing": true
    },
    "client_details": {
        "user_agent": "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36",
        "session_hash": "c30217b1565a1f69ca5c1cb906ee6769c39de0f9acc8d6685975d551a2ff246f",
        "browser_ip": "118.200.236.161",
        "accept_language": "en-GB,en-US;q=0.8,en;q=0.6"
    },
    "fulfillments": [],
    "shipping_address": {
        "province_code": null,
        "country_code": "SG",
        "name": "Own Card",
        "zip": "123456",
        "province": "Singapore",
        "phone": "12345676",
        "longitude": "1.851376",
        "latitude": "1.3646",
        "last_name": "Card",
        "first_name": "Own",
        "country": "Singapore",
        "company": "",
        "city": "Singapore",
        "address2": "",
        "address1": "Toa  8 Block 111 #10-254"
    },
    "billing_address": {
        "province_code": null,
        "country_code": "SG",
        "name": "Own Card",
        "zip": "310233",
        "province": "Singapore",
        "phone": "92965040",
        "longitude": "103.851376",
        "latitude": "1.33646",
        "last_name": "Card",
        "first_name": "Own",
        "country": "Singapore",
        "company": "",
        "city": "Singapore",
        "address2": "",
        "address1": "Toa fff Block a #10-254"
    },
    "payment_details": {
        "credit_card_company": "Bogus",
        "credit_card_number": "XXXX-XXXX-XXXX-1",
        "cvv_result_code": null,
        "credit_card_bin": "1",
        "avs_result_code": null
    },
    "shipping_lines": [
        {
            "tax_lines": [],
            "title": "Standard Shipping",
            "source": "shopify",
            "price": "10.00",
            "code": "Standard Shipping"
        }
    ],
    "line_items": [
        {
            "tax_lines": [],
            "product_exists": true,
            "properties": [],
            "variant_inventory_management": null,
            "name": "test",
            "vendor": "Kwang",
            "variant_title": "",
            "variant_id": 615650553,
            "title": "test",
            "taxable": true,
            "sku": "1",
            "requires_shipping": true,
            "quantity": 1,
            "product_id": 265080025,
            "price": "12.00",
            "id": 432497197,
            "grams": 0,
            "fulfillment_status": null,
            "fulfillment_service": "manual"
        }
    ],
    "tags": "",
    "tax_lines": [],
    "checkout_id": 221740105,
    "processing_method": "direct",
    "note_attributes": [],
    "discount_codes": [],
    "order_number": 1003,
    "landing_site_ref": null,
    "browser_ip": "118.200.236.161",
    "user_id": null,
    "updated_at": "2014-03-14T06:10:59-04:00",
    "total_weight": 0,
    "total_tax": "0.00",
    "total_price_usd": "17.37",
    "total_price": "22.00",
    "total_line_items_price": "12.00",
    "total_discounts": "0.00",
    "token": "3c71c6a830eee3b7cb0c8627e4d48e03",
    "test": true,
    "taxes_included": false,
    "subtotal_price": "12.00",
    "source_url": null,
    "source_name": "web",
    "source_identifier": null,
    "source": "browser",
    "referring_site": "",
    "reference": null,
    "number": 3,
    "note": null,
    "name": "#1003",
    "location_id": null,
    "landing_site": "/products/test",
    "id": 244075413,
    "gateway": "bogus",
    "fulfillment_status": null,
    "financial_status": "authorized",
    "email": "kwanghock@a.com.sg",
    "currency": "SGD",
    "created_at": "2014-03-14T06:10:58-04:00",
    "confirmed": true,
    "closed_at": null,
    "checkout_token": "95dc1ed9b867df5b1ecdf770de4eba3b",
    "cart_token": "ab1795435caebccb6d96ee69f716a4c9",
    "cancelled_at": null,
    "cancel_reason": null,
    "buyer_accepts_marketing": true
}

如何从我的php laravel函数实际访问它?我做了类似这样的事情但是当使用POSTMAN发送它时我没有从$ c获得任何东西。

    $inputs = Input::all();
    $c = $inputs ['customer'];

任何人都可以提供帮助吗?

1 个答案:

答案 0 :(得分:0)

您似乎将数据作为json正文传递。您展示的代码可能已经有效,但我们可以尝试一下。

首先执行此操作:

$inputs = Input::all();
$c = $inputs->customer;

现在让我们试试:

$inputs = Input::json()->all();
$c = $inputs->customer;

它会返回什么吗?

最后让我们试试:

$c = Input::get('customer');
$cd = Input::get('customer.default_address');

基于文档:

  

某些JavaScript库(如Backbone)可能会将输入作为JSON发送到应用程序。您可以通过输入::获取访问此数据,就像正常一样。


邮递员必须将该请求作为json正文发布。

选择raw标签,然后将text更改为json...,然后将整个请求复制粘贴到textarea中。


所以您要查找的实际查询是:

$customer = Input::get('customer'); 

现在,您拥有$ input中客户内部的所有内容。

访问它:

$default_address = $customer->default_address;  // returns object
$state = $customer->state; // returns string

你甚至可以把它包起来:

if (Request::isJson())
{
    // make sure it is from a json body
}

<强>更新

试试这个:

$inputs = Input::all();
dd($inputs); // what does this return?