More2Life / m2l-server

More2Life App Server
https://m2l-server.herokuapp.com
2 stars 0 forks source link

Integrate Square product information into feed #26

Closed kingbrendann closed 7 years ago

kingbrendann commented 7 years ago

Overview

More2Life admins will create products and manage inventory and orders in the Square dashboard.

We need to integrate the Square product information into the feed response.

Reference

https://docs.connect.squareup.com/api/connect/v1/#navsection-webhooks https://docs.connect.squareup.com/api/connect/v1/#get-itemid

Change Webhook URL for testing

https://connect.squareup.com/apps/sq0idp-z5EGdwjjzQv2SNqLOYsHpg/webhooks

Design

Listen for the INVENTORY_UPDATED webhook.

That notification is sent in the following format:

{
  "merchant_id": "JGHJ0343",
  "event_type": "INVENTORY_UPDATED",
  "entity_id": "Jq74mCczmFXk1tC10GB"
}

Our listening endpoint will be:

/api/webhooks/square

We parse the payload to determine the type of notification and use that entity_id to GET the inventory item:

GET /v1/{location_id}/items/{entity_id}

Example Response:

{
  "id": "442d1344-6d2b-4238-83d0-0284dfd335d8",
  "name": "Milkshake",
  "description": "It's better than yours",
  "type": "NORMAL",
  "visibility": "PRIVATE",
  "available_online": false,
  "category": {
    "id": "36ac7016-3a4e-4934-81f1-9057ac613f2y",
    "name": "Beverages"
  },
  "variations": [
    {
      "id": "cb890728-cfdc-4690-9e03-349f964f756r",
      "name": "Small",
      "pricing_type": "FIXED_PRICING",
      "price_money": {
        "currency_code": "USD",
        "amount": 400
      },
      "ordinal": 0,
      "item_id": "442d1344-6d2b-4238-83d0-0284dfd335d8"
    },
    ...
  ],
  "modifier_lists": [
    {
      "id": "dcd7e350-c50f-421f-ada1-23a825b008b2",
      "name": "Toppings",
      "selection_type": "MULTIPLE",
      "modifier_options": [
        {
          "id": "39059fd0-ae9d-4eb3-b6e8-dd3198f019b8",
          "name": "Whipped Cream",
          "price_money": {
            "currency_code": "USD",
            "amount": 50
          },
          "on_by_default": false,
          "ordinal": 1,
          "modifier_list_id": "dcd7e350-c50f-421f-ada1-23a825b008b2"
        },
        ...
      ]
    }
    ...
  ],
  "fees": [
    {
      "id": "19498df7-3fb0-4c96-8b47-860480718abk",
      "name": "Sales tax",
      "adjustment_type": "TAX",
      "rate": "0.06",
      "calculation_phase": "FEE_SUBTOTAL_PHASE",
      "applies_to_custom_amounts": true,
      "enabled": true,
      "inclusion_type": "INCLUSIVE"
    },
    ...
  ]
}

We then place the needed data in our DB.

Important

From the docs:

Square attempts to send one notification to your application per applicable event. If a notification fails for any reason, Square does not reattempt it. If webhooks become unavailable for a period of time, notifications that would have been sent during that period are not sent, even at a later time.

We should thus periodically make a List Items call and manually check for any new items. https://docs.connect.squareup.com/api/connect/v1/#get-items

M2L Listing Model

{
    "_id" : String,
    "type" : "listing",
    "isActive" : Boolean,
    "title" : String,
    "description" : String,
    "index" : Number,
    "previewImageUrl" : String,
    "Product" : {
      "_id" : String,
      "price" : Number
    }
}