1
votes

Using laravel to do a webhook for shopify on order creation

I specify my url in the webhook to my site.

I use postcatcher to receive the data from the webhook call so that I know what data they are passing in.

the data looks something like this

{
    "customer": {
        "default_address": {
            "default": true,
            "country_name": "Singapore",
            "country_code": "SG",
            "province_code": null,
            "name": "Own Card",
            "zip": "234567",
            "province": "Singapore",
            "phone": "",
            "last_name": "Card",
            "id": 275454813,
            "first_name": "Own",
            "country": "Singapore",
            "company": "",
            "city": "Singapore",
            "address2": "",
            "address1": ""
        },
        "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": "[email protected]",
        "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": "c30217b1565a1f69ca5c1cb944ee6769c39de0f9acc8d6685975d551a2ff246f",
        "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.151376",
        "latitude": "1.2646",
        "last_name": "Card",
        "first_name": "Own",
        "country": "Singapore",
        "company": "",
        "city": "Singapore",
        "address2": "",
        "address1": ""
    },
    "billing_address": {
        "province_code": null,
        "country_code": "SG",
        "name": "Own Card",
        "zip": "312133",
        "province": "Singapore",
        "phone": "",
        "longitude": "103.351376",
        "latitude": "1.33146",
        "last_name": "Card",
        "first_name": "Own",
        "country": "Singapore",
        "company": "",
        "city": "Singapore",
        "address2": "",
        "address1": ""
    },
    "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": 315650553,
            "title": "test",
            "taxable": true,
            "sku": "1",
            "requires_shipping": true,
            "quantity": 1,
            "product_id": 265080025,
            "price": "12.00",
            "id": 432417197,
            "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.122",
    "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": "[email protected]",
    "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
}

How do I actually access it from my php laravel function? I did something like this but when using POSTMAN to send this over i dont get anything from $c.

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

Anyone can help?

1
This data looks like its being passed as a json body?majidarif

1 Answers

0
votes

It seems like you are passing the data as a json body. The code you've shown could've worked but lets try this.

First off do this:

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

Now lets try:

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

Does it return anything?

Finally lets try:

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

Based on the docs:

Some JavaScript libraries such as Backbone may send input to the application as JSON. You may access this data via Input::get like normal.


on postman you have to post that request as a json body.

Select the raw tab then change text to json... and copy paste the whole request into the textarea.


So the actual query you are looking for is:

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

now you have everything that is inside the customer in $inputs.

access it like:

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

You can even wrap it around:

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

update

try this:

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