supports multi customers in square
This commit is contained in:
@@ -27,4 +27,85 @@
|
|||||||
:yodlee2-client-secret "wZQHoGEkv5AGG2ZH"
|
:yodlee2-client-secret "wZQHoGEkv5AGG2ZH"
|
||||||
:yodlee2-base-url "https://development.api.yodlee.com/ysl"
|
:yodlee2-base-url "https://development.api.yodlee.com/ysl"
|
||||||
:yodlee2-fastlink "https://fl4.preprod.yodlee.com/authenticate/USDevexPreProd2-195/fastlink/?channelAppName=usdevexpreprod2"
|
:yodlee2-fastlink "https://fl4.preprod.yodlee.com/authenticate/USDevexPreProd2-195/fastlink/?channelAppName=usdevexpreprod2"
|
||||||
|
|
||||||
|
:square-config {"NGE1"
|
||||||
|
{:square-location "SCX0Y8CTGM1S0",
|
||||||
|
:location "UC",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGOP"
|
||||||
|
{:square-location "L3GMNBFARX9GG",
|
||||||
|
:location "OP",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGDG"
|
||||||
|
{:square-location "LQTHXQY69MYB6",
|
||||||
|
:location "DB",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGPG"
|
||||||
|
{:square-location "AMQ0NPA8FGDEF",
|
||||||
|
:location "SZ",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGEZ"
|
||||||
|
{:square-location "L0Z167T2T7W7F",
|
||||||
|
:location "JS",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGLK"
|
||||||
|
{:square-location "LRC7WVD77ZM81",
|
||||||
|
:location "SM",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGA1"
|
||||||
|
{:square-location "FZ3ZYC77T3W1T",
|
||||||
|
:location "KA",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGKG"
|
||||||
|
{:square-location "8JT71V8XGYAT3",
|
||||||
|
:location "NB",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGEB"
|
||||||
|
{:square-location "LEV4GM1JPJS6R",
|
||||||
|
:location "CV",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGGG"
|
||||||
|
{:square-location "L7S9MXZBJ00HY",
|
||||||
|
:location "LM",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGRV"
|
||||||
|
{:square-location "L0J45VZKHWXVR",
|
||||||
|
:location "RV",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGSM"
|
||||||
|
{:square-location "LG5X0MHA4NZDM",
|
||||||
|
:location "SM",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGZO"
|
||||||
|
{:square-location "KMVFQ9CRCXJ10",
|
||||||
|
:location "VT",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGWL"
|
||||||
|
{:square-location "LSWNP14T0YKD9",
|
||||||
|
:location "WL",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGMJ"
|
||||||
|
{:square-location "FNH5VRT890WK8",
|
||||||
|
:location "SC",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGHG"
|
||||||
|
{:square-location "LXJCAHYGZVNEJ",
|
||||||
|
:location "DC",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGVZ"
|
||||||
|
{:square-location "ACNTYY8WVZ6DV",
|
||||||
|
:location "NP",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGAK"
|
||||||
|
{:square-location "2RVBYER6QSV7W",
|
||||||
|
:location "MH",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGGH"
|
||||||
|
{:square-location "LT322EK0S2TRD",
|
||||||
|
:location "FM",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"}
|
||||||
|
"20TY"
|
||||||
|
{:square-location "L2579ATQ0X1ET",
|
||||||
|
:location "WG",
|
||||||
|
:token "EAAAEEr749Ea6AdPTdngsmUPwIM3ETbPwcx3QQl_NS0KWuIL-JNzAg4f3W9DGQhb"}}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,4 +26,85 @@
|
|||||||
:yodlee2-fastlink "https://fl4.prod.yodlee.com/authenticate/USDevexProd2-319/fastlink/?channelAppName=usdevexprod2"
|
:yodlee2-fastlink "https://fl4.prod.yodlee.com/authenticate/USDevexProd2-319/fastlink/?channelAppName=usdevexprod2"
|
||||||
:yodlee2-proxy-host "172.31.10.83"
|
:yodlee2-proxy-host "172.31.10.83"
|
||||||
:yodlee2-proxy-port 8888
|
:yodlee2-proxy-port 8888
|
||||||
|
|
||||||
|
:square-config {"NGE1"
|
||||||
|
{:square-location "SCX0Y8CTGM1S0",
|
||||||
|
:location "UC",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGOP"
|
||||||
|
{:square-location "L3GMNBFARX9GG",
|
||||||
|
:location "OP",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGDG"
|
||||||
|
{:square-location "LQTHXQY69MYB6",
|
||||||
|
:location "DB",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGPG"
|
||||||
|
{:square-location "AMQ0NPA8FGDEF",
|
||||||
|
:location "SZ",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGEZ"
|
||||||
|
{:square-location "L0Z167T2T7W7F",
|
||||||
|
:location "JS",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGLK"
|
||||||
|
{:square-location "LRC7WVD77ZM81",
|
||||||
|
:location "SM",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGA1"
|
||||||
|
{:square-location "FZ3ZYC77T3W1T",
|
||||||
|
:location "KA",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGKG"
|
||||||
|
{:square-location "8JT71V8XGYAT3",
|
||||||
|
:location "NB",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGEB"
|
||||||
|
{:square-location "LEV4GM1JPJS6R",
|
||||||
|
:location "CV",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGGG"
|
||||||
|
{:square-location "L7S9MXZBJ00HY",
|
||||||
|
:location "LM",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGRV"
|
||||||
|
{:square-location "L0J45VZKHWXVR",
|
||||||
|
:location "RV",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGSM"
|
||||||
|
{:square-location "LG5X0MHA4NZDM",
|
||||||
|
:location "SM",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGZO"
|
||||||
|
{:square-location "KMVFQ9CRCXJ10",
|
||||||
|
:location "VT",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGWL"
|
||||||
|
{:square-location "LSWNP14T0YKD9",
|
||||||
|
:location "WL",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGMJ"
|
||||||
|
{:square-location "FNH5VRT890WK8",
|
||||||
|
:location "SC",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGHG"
|
||||||
|
{:square-location "LXJCAHYGZVNEJ",
|
||||||
|
:location "DC",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGVZ"
|
||||||
|
{:square-location "ACNTYY8WVZ6DV",
|
||||||
|
:location "NP",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGAK"
|
||||||
|
{:square-location "2RVBYER6QSV7W",
|
||||||
|
:location "MH",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"},
|
||||||
|
"NGGH"
|
||||||
|
{:square-location "LT322EK0S2TRD",
|
||||||
|
:location "FM",
|
||||||
|
:token "EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"}
|
||||||
|
"20TY"
|
||||||
|
{:square-location "L2579ATQ0X1ET",
|
||||||
|
:location "WG",
|
||||||
|
:token "EAAAEEr749Ea6AdPTdngsmUPwIM3ETbPwcx3QQl_NS0KWuIL-JNzAg4f3W9DGQhb"}}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
[clj-time.coerce :as coerce]
|
[clj-time.coerce :as coerce]
|
||||||
[clj-time.core :as time]
|
[clj-time.core :as time]
|
||||||
[clj-time.format :as f]
|
[clj-time.format :as f]
|
||||||
|
[config.core :refer [env] :as cfg ]
|
||||||
[clojure.string :as str]
|
[clojure.string :as str]
|
||||||
[clojure.data.json :as json]
|
[clojure.data.json :as json]
|
||||||
[clojure.tools.logging :as log]
|
[clojure.tools.logging :as log]
|
||||||
@@ -14,9 +15,10 @@
|
|||||||
[yang.scheduler :as scheduler]
|
[yang.scheduler :as scheduler]
|
||||||
[clojure.core.async :as async]))
|
[clojure.core.async :as async]))
|
||||||
|
|
||||||
(def base-headers {"Square-Version" "2021-08-18"
|
(defn base-headers [client]
|
||||||
"Authorization" "Bearer EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"
|
{"Square-Version" "2021-08-18"
|
||||||
"Content-Type" "application/json"})
|
"Authorization" (str "Bearer " (get-in env [:square-config client :token]))
|
||||||
|
"Content-Type" "application/json"})
|
||||||
|
|
||||||
(defn lookup-dates []
|
(defn lookup-dates []
|
||||||
(->> (clj-time.periodic/periodic-seq (time/plus (time/now) (time/days -15))
|
(->> (clj-time.periodic/periodic-seq (time/plus (time/now) (time/days -15))
|
||||||
@@ -27,21 +29,19 @@
|
|||||||
|
|
||||||
(auto-ap.time/unparse (time/plus d (time/days 5)) auto-ap.time/iso-date)]))))
|
(auto-ap.time/unparse (time/plus d (time/days 5)) auto-ap.time/iso-date)]))))
|
||||||
|
|
||||||
(defn locations []
|
(defn locations [client]
|
||||||
(->> (client/get "https://connect.squareup.com/v2/locations"
|
(->> (client/get "https://connect.squareup.com/v2/locations"
|
||||||
{:headers {"Square-Version" "2020-08-12"
|
{:headers (base-headers client)
|
||||||
"Authorization" "Bearer EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"
|
|
||||||
"Content-Type" "application/json"}
|
|
||||||
:as :json})
|
:as :json})
|
||||||
:body
|
:body
|
||||||
:locations))
|
:locations))
|
||||||
|
|
||||||
(defn fetch-catalog [i]
|
(defn fetch-catalog [client i]
|
||||||
(if i
|
(if i
|
||||||
(try
|
(try
|
||||||
(log/info "looking up catalog for" (str "https://connect.squareup.com/v2/catalog/object/" i))
|
(log/info "looking up catalog for" (str "https://connect.squareup.com/v2/catalog/object/" i))
|
||||||
(->> (client/get (str "https://connect.squareup.com/v2/catalog/object/" i)
|
(->> (client/get (str "https://connect.squareup.com/v2/catalog/object/" i)
|
||||||
{:headers base-headers
|
{:headers (base-headers client)
|
||||||
:query-params {"include_related_items" "true"}
|
:query-params {"include_related_items" "true"}
|
||||||
:as :json})
|
:as :json})
|
||||||
:body
|
:body
|
||||||
@@ -53,13 +53,13 @@
|
|||||||
|
|
||||||
(def fetch-catalog-fast (memoize fetch-catalog))
|
(def fetch-catalog-fast (memoize fetch-catalog))
|
||||||
|
|
||||||
(defn item-id->category-name [i]
|
(defn item-id->category-name [client i]
|
||||||
(let [item (fetch-catalog-fast i)]
|
(let [item (fetch-catalog-fast client i)]
|
||||||
(cond (:item_variation_data item)
|
(cond (:item_variation_data item)
|
||||||
(item-id->category-name (:item_id (:item_variation_data item)))
|
(item-id->category-name client (:item_id (:item_variation_data item)))
|
||||||
|
|
||||||
(:category_id (:item_data item))
|
(:category_id (:item_data item))
|
||||||
(:name (:category_data (fetch-catalog-fast (:category_id (:item_data item)))))
|
(:name (:category_data (fetch-catalog-fast client (:category_id (:item_data item)))))
|
||||||
|
|
||||||
(:item_data item)
|
(:item_data item)
|
||||||
"Uncategorized"
|
"Uncategorized"
|
||||||
@@ -109,14 +109,14 @@
|
|||||||
|
|
||||||
|
|
||||||
(defn search
|
(defn search
|
||||||
([l]
|
([client]
|
||||||
(search l nil))
|
(search client (get-in env [:square-config client :square-location])))
|
||||||
([l d]
|
([client l]
|
||||||
|
(search client l nil))
|
||||||
|
([client l d]
|
||||||
(log/info "Searching for" l)
|
(log/info "Searching for" l)
|
||||||
(let [result (->> (client/post "https://connect.squareup.com/v2/orders/search"
|
(let [result (->> (client/post "https://connect.squareup.com/v2/orders/search"
|
||||||
{:headers {"Square-Version" "2020-08-12"
|
{:headers (base-headers client)
|
||||||
"Authorization" "Bearer EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"
|
|
||||||
"Content-Type" "application/json"}
|
|
||||||
:body (json/write-str (cond-> {"location_ids" [l] "limit" 10000}
|
:body (json/write-str (cond-> {"location_ids" [l] "limit" 10000}
|
||||||
d (merge (pc d))))
|
d (merge (pc d))))
|
||||||
:as :json})
|
:as :json})
|
||||||
@@ -128,12 +128,10 @@
|
|||||||
|
|
||||||
(defn order
|
(defn order
|
||||||
|
|
||||||
([o]
|
([client o]
|
||||||
(log/info "Searching for" o)
|
(log/info "Searching for" o)
|
||||||
(let [result (->> (client/get (str "https://connect.squareup.com/v2/orders/" o)
|
(let [result (->> (client/get (str "https://connect.squareup.com/v2/orders/" o)
|
||||||
{:headers {"Square-Version" "2020-08-12"
|
{:headers (base-headers client)
|
||||||
"Authorization" "Bearer EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"
|
|
||||||
"Content-Type" "application/json"}
|
|
||||||
:as :json})
|
:as :json})
|
||||||
:body)]
|
:body)]
|
||||||
|
|
||||||
@@ -144,25 +142,7 @@
|
|||||||
(* 0.01 (or (:amount amt) 0.0)))
|
(* 0.01 (or (:amount amt) 0.0)))
|
||||||
|
|
||||||
(defn location_id->client-location [location]
|
(defn location_id->client-location [location]
|
||||||
({"2RVBYER6QSV7W" ["NGAK" "MH"]
|
(get-in env [:square-location location]))
|
||||||
"8JT71V8XGYAT3" ["NGKG" "NB"]
|
|
||||||
"SCX0Y8CTGM1S0" ["NGE1" "UC"]
|
|
||||||
"FNH5VRT890WK8" ["NGMJ" "SC"]
|
|
||||||
"AMQ0NPA8FGDEF" ["NGPG" "SZ"]
|
|
||||||
"ACNTYY8WVZ6DV" ["NGVZ" "NP"]
|
|
||||||
"KMVFQ9CRCXJ10" ["NGZO" "VT"]
|
|
||||||
"L0J45VZKHWXVR" ["NGRV" "RV"]
|
|
||||||
"L3GMNBFARX9GG" ["NGOP" "OP"]
|
|
||||||
"LXJCAHYGZVNEJ" ["NGHG" "DC"]
|
|
||||||
"LSWNP14T0YKD9" ["NGWL" "WL"]
|
|
||||||
"LT322EK0S2TRD" ["NGGH" "FM"]
|
|
||||||
"L0Z167T2T7W7F" ["NGEZ" "JS"]
|
|
||||||
"LEV4GM1JPJS6R" ["NGEB" "CV"]
|
|
||||||
"LQTHXQY69MYB6" ["NGDG" "DB"]
|
|
||||||
"L7S9MXZBJ00HY" ["NGGG" "LM"]
|
|
||||||
"LRC7WVD77ZM81" ["NGLK" "SM"]
|
|
||||||
"FZ3ZYC77T3W1T" ["NGA1" "KA"]
|
|
||||||
"LG5X0MHA4NZDM" ["NGSM" "SM"]} location))
|
|
||||||
|
|
||||||
;; to get totals:
|
;; to get totals:
|
||||||
(comment
|
(comment
|
||||||
@@ -178,68 +158,70 @@
|
|||||||
)))
|
)))
|
||||||
0.0))
|
0.0))
|
||||||
|
|
||||||
(defn order->sales-order [order]
|
(defn order->sales-order [client location order]
|
||||||
(let [[client loc] (location_id->client-location (:location_id order))]
|
(remove-nils
|
||||||
(remove-nils
|
#:sales-order
|
||||||
#:sales-order
|
{:date (coerce/to-date (time/to-time-zone (coerce/to-date-time (:created_at order)) (time/time-zone-for-id "America/Los_Angeles")))
|
||||||
{:date (coerce/to-date (time/to-time-zone (coerce/to-date-time (:created_at order)) (time/time-zone-for-id "America/Los_Angeles")))
|
:client [:client/code client]
|
||||||
:client [:client/code client]
|
:location location
|
||||||
:location loc
|
:external-id (str "square/order/" client "-" location "-" (:id order))
|
||||||
:external-id (str "square/order/" client "-" loc "-" (:id order))
|
:total (-> order :net_amounts :total_money amount->money)
|
||||||
:total (-> order :net_amounts :total_money amount->money)
|
:tax (-> order :net_amounts :tax_money amount->money)
|
||||||
:tax (-> order :net_amounts :tax_money amount->money)
|
:tip (-> order :net_amounts :tip_money amount->money)
|
||||||
:tip (-> order :net_amounts :tip_money amount->money)
|
:discount (-> order :net_amounts :discount_money amount->money)
|
||||||
:discount (-> order :net_amounts :discount_money amount->money)
|
:service-charge (-> order :net_amounts :service_charge_money amount->money)
|
||||||
:service-charge (-> order :net_amounts :service_charge_money amount->money)
|
:returns (+ (- (-> order :return_amounts :total_money amount->money)
|
||||||
:returns (+ (- (-> order :return_amounts :total_money amount->money)
|
(-> order :return_amounts :tax_money amount->money)
|
||||||
(-> order :return_amounts :tax_money amount->money)
|
(-> order :return_amounts :tip_money amount->money)
|
||||||
(-> order :return_amounts :tip_money amount->money)
|
(-> order :return_amounts :service_charge_money amount->money))
|
||||||
(-> order :return_amounts :service_charge_money amount->money))
|
(-> order :return_amounts :discount_money amount->money))
|
||||||
(-> order :return_amounts :discount_money amount->money))
|
:charges (->> (:tenders order)
|
||||||
:charges (->> (:tenders order)
|
(map (fn [t]
|
||||||
(map (fn [t]
|
(remove-nils
|
||||||
(remove-nils
|
#:charge
|
||||||
#:charge
|
{:type-name (:type t)
|
||||||
{:type-name (:type t)
|
:processor (condp = (some-> (:note t) str/lower-case)
|
||||||
:processor (condp = (some-> (:note t) str/lower-case)
|
"doordash" :ccp-processor/doordash
|
||||||
"doordash" :ccp-processor/doordash
|
"dd" :ccp-processor/doordash
|
||||||
"dd" :ccp-processor/doordash
|
"ubereats" :ccp-processor/uber-eats
|
||||||
"ubereats" :ccp-processor/uber-eats
|
"ue" :ccp-processor/uber-eats
|
||||||
"ue" :ccp-processor/uber-eats
|
"grubhub" :ccp-processor/grubhub
|
||||||
"grubhub" :ccp-processor/grubhub
|
"grub" :ccp-processor/grubhub
|
||||||
"grub" :ccp-processor/grubhub
|
"gh" :ccp-processor/grubhub
|
||||||
"gh" :ccp-processor/grubhub
|
(condp = (:name (:source order))
|
||||||
(condp = (:name (:source order))
|
"GRUBHUB" :ccp-processor/grubhub
|
||||||
"GRUBHUB" :ccp-processor/grubhub
|
"UBEREATS" :ccp-processor/uber-eats
|
||||||
"UBEREATS" :ccp-processor/uber-eats
|
"DOORDASH" :ccp-processor/doordash
|
||||||
"DOORDASH" :ccp-processor/doordash
|
:ccp-processor/na))
|
||||||
:ccp-processor/na))
|
:total (amount->money (:amount_money t))
|
||||||
:total (amount->money (:amount_money t))
|
:tip (amount->money (:tip_money t))}))))
|
||||||
:tip (amount->money (:tip_money t))}))))
|
:line-items (->> (:line_items order)
|
||||||
:line-items (->> (:line_items order)
|
(map (fn [li]
|
||||||
(map (fn [li]
|
(remove-nils
|
||||||
(remove-nils
|
#:order-line-item
|
||||||
#:order-line-item
|
{:item-name (:name li)
|
||||||
{:item-name (:name li)
|
:category (if (= "GIFT_CARD" (:item_type li))
|
||||||
:category (if (= "GIFT_CARD" (:item_type li))
|
"Gift Card"
|
||||||
"Gift Card"
|
(item-id->category-name client (:catalog_object_id li)))
|
||||||
(item-id->category-name (:catalog_object_id li)))
|
:total (amount->money (:total_money li))
|
||||||
:total (amount->money (:total_money li))
|
:tax (amount->money (:total_tax_money li))
|
||||||
:tax (amount->money (:total_tax_money li))
|
:discount (amount->money (:total_discount_money li))}))))}))
|
||||||
:discount (amount->money (:total_discount_money li))}))))})))
|
|
||||||
|
|
||||||
(defn daily-results [d]
|
(defn daily-results
|
||||||
(->> (locations)
|
([client]
|
||||||
(map :id)
|
(daily-results client nil))
|
||||||
(filter location_id->client-location)
|
([client d]
|
||||||
(mapcat #(search % d))
|
(daily-results client (get-in env [:square-config client :square-location]) d))
|
||||||
(filter (fn [order]
|
([client location d]
|
||||||
;; sometimes orders stay open in square. At least one payment
|
(->> (search client location d)
|
||||||
;; is needed to import, in order to avoid importing orders in-progress.
|
(filter (fn [order]
|
||||||
(and (> (count (:tenders order)) 0)
|
;; sometimes orders stay open in square. At least one payment
|
||||||
(not= #{"FAILED"}
|
;; is needed to import, in order to avoid importing orders in-progress.
|
||||||
(set (map #(:status (:card_details %)) (:tenders order)))))))
|
(and (or (> (count (:tenders order)) 0)
|
||||||
(map order->sales-order)))
|
(seq (:returns order)))
|
||||||
|
(not= #{"FAILED"}
|
||||||
|
(set (map #(:status (:card_details %)) (:tenders order)))))))
|
||||||
|
(map #(order->sales-order client (get-in env [:square-config client :location]) %)))))
|
||||||
|
|
||||||
#_(daily-results)
|
#_(daily-results)
|
||||||
|
|
||||||
@@ -255,15 +237,15 @@
|
|||||||
(retry f (inc i))))
|
(retry f (inc i))))
|
||||||
(log/error "Too many failures"))))
|
(log/error "Too many failures"))))
|
||||||
|
|
||||||
(defn get-payment [p]
|
(defn get-payment [client p]
|
||||||
(:payment (:body (retry #(client/get (str "https://connect.squareup.com/v2/payments/" p)
|
(:payment (:body (retry #(client/get (str "https://connect.squareup.com/v2/payments/" p)
|
||||||
{:headers base-headers
|
{:headers (base-headers client)
|
||||||
:as :json
|
:as :json
|
||||||
:retry-handler (fn [ex try-count http-context]
|
:retry-handler (fn [ex try-count http-context]
|
||||||
(log/warn "Retrying after failure " ex)
|
(log/warn "Retrying after failure " ex)
|
||||||
(if (> try-count 4) false true))})))))
|
(if (> try-count 4) false true))})))))
|
||||||
|
|
||||||
(defn get-settlement-sales-date [settlement]
|
(defn get-settlement-sales-date [client settlement]
|
||||||
(let [concurrent 10
|
(let [concurrent 10
|
||||||
output-chan (async/chan)]
|
output-chan (async/chan)]
|
||||||
(async/pipeline-blocking concurrent
|
(async/pipeline-blocking concurrent
|
||||||
@@ -271,15 +253,8 @@
|
|||||||
(map (fn [p]
|
(map (fn [p]
|
||||||
(lc/with-context {:source "Square settlements loading "}
|
(lc/with-context {:source "Square settlements loading "}
|
||||||
(log/info "looking up payment " p " for settlement " (:id settlement))
|
(log/info "looking up payment " p " for settlement " (:id settlement))
|
||||||
(println (or
|
|
||||||
(-> p
|
|
||||||
get-payment
|
|
||||||
:created_at
|
|
||||||
coerce/to-date)
|
|
||||||
(coerce/to-date (time/now))))
|
|
||||||
(or
|
(or
|
||||||
(-> p
|
(-> (get-payment client p)
|
||||||
get-payment
|
|
||||||
:created_at
|
:created_at
|
||||||
coerce/to-date)
|
coerce/to-date)
|
||||||
(coerce/to-date (time/now))))))
|
(coerce/to-date (time/now))))))
|
||||||
@@ -300,7 +275,7 @@
|
|||||||
(drop 2)
|
(drop 2)
|
||||||
first)))
|
first)))
|
||||||
|
|
||||||
(defn get-settlement-details [settlements] ;; pairs of [location settlement]
|
(defn get-settlement-details [client settlements] ;; pairs of [location settlement]
|
||||||
(log/info "getting settlement details for " settlements)
|
(log/info "getting settlement details for " settlements)
|
||||||
(let [concurrent 10
|
(let [concurrent 10
|
||||||
output-chan (async/chan)]
|
output-chan (async/chan)]
|
||||||
@@ -310,14 +285,14 @@
|
|||||||
(lc/with-context {:source "Square settlements loading "}
|
(lc/with-context {:source "Square settlements loading "}
|
||||||
(log/info "Looking up settlement " s " for location " location)
|
(log/info "Looking up settlement " s " for location " location)
|
||||||
(let [settlement (:body (retry #(client/get (str "https://connect.squareup.com/v1/" location "/settlements/" s)
|
(let [settlement (:body (retry #(client/get (str "https://connect.squareup.com/v1/" location "/settlements/" s)
|
||||||
{:headers base-headers
|
{:headers (base-headers client)
|
||||||
:as :json
|
:as :json
|
||||||
:retry-handler (fn [ex try-count http-context]
|
:retry-handler (fn [ex try-count http-context]
|
||||||
(log/warn "Retrying after failure " ex)
|
(log/warn "Retrying after failure " ex)
|
||||||
(if (> try-count 4) false true))})))]
|
(if (> try-count 4) false true))})))]
|
||||||
|
|
||||||
(log/info "sales date for " s " is " (get-settlement-sales-date settlement))
|
(log/info "sales date for " s " is " (get-settlement-sales-date client settlement))
|
||||||
(assoc settlement :sales-date (get-settlement-sales-date settlement) )))))
|
(assoc settlement :sales-date (get-settlement-sales-date client settlement) )))))
|
||||||
(async/to-chan settlements)
|
(async/to-chan settlements)
|
||||||
true
|
true
|
||||||
(fn [e]
|
(fn [e]
|
||||||
@@ -326,14 +301,14 @@
|
|||||||
(async/<!! (async/into [] output-chan))))
|
(async/<!! (async/into [] output-chan))))
|
||||||
|
|
||||||
(defn settlements
|
(defn settlements
|
||||||
([l] (settlements l (lookup-dates)))
|
([client l] (settlements client l (lookup-dates)))
|
||||||
([l lookup-dates]
|
([client l lookup-dates]
|
||||||
(log/info "Searching for" l)
|
(log/info "Searching for" l)
|
||||||
(->> lookup-dates
|
(->> lookup-dates
|
||||||
(mapcat (fn [[start-date end-date]]
|
(mapcat (fn [[start-date end-date]]
|
||||||
(log/info "looking up settlements for " l " on dates " start-date " to " end-date)
|
(log/info "looking up settlements for " l " on dates " start-date " to " end-date)
|
||||||
(let [settlements (->> (client/get (str "https://connect.squareup.com/v1/" l "/settlements")
|
(let [settlements (->> (client/get (str "https://connect.squareup.com/v1/" l "/settlements")
|
||||||
{:headers base-headers
|
{:headers (base-headers client)
|
||||||
:query-params {"begin_time" start-date
|
:query-params {"begin_time" start-date
|
||||||
"end_time" end-date}
|
"end_time" end-date}
|
||||||
:as :json})
|
:as :json})
|
||||||
@@ -344,77 +319,74 @@
|
|||||||
seq
|
seq
|
||||||
(map (fn [s]
|
(map (fn [s]
|
||||||
[l s]))
|
[l s]))
|
||||||
(get-settlement-details )
|
(get-settlement-details client)
|
||||||
)))
|
)))
|
||||||
|
|
||||||
(defn daily-settlements
|
(defn daily-settlements
|
||||||
([] (daily-settlements (->> (locations)
|
([client] (daily-settlements client
|
||||||
(map :id)
|
(get-in env [:square-config client :square-location])))
|
||||||
(filter location_id->client-location))))
|
([client location-id]
|
||||||
([location-ids]
|
(->> (for [settlement (settlements client location-id)]
|
||||||
(->> location-ids
|
#:expected-deposit {:external-id (str "square/settlement/" (:id settlement))
|
||||||
(mapcat (fn [l]
|
:total (amount->money (:total_money settlement))
|
||||||
(for [settlement (settlements l)
|
:client [:client/code client]
|
||||||
:let [[client loc] (location_id->client-location l)]]
|
:location (get-in env [:square-config client :location])
|
||||||
#:expected-deposit {:external-id (str "square/settlement/" (:id settlement))
|
:fee (- (reduce + 0.0 (map (fn [entry]
|
||||||
:total (amount->money (:total_money settlement))
|
(if (= (:type entry) "REFUND")
|
||||||
:client [:client/code client]
|
(- (amount->money (:fee_money entry)))
|
||||||
:location loc
|
(amount->money (:fee_money entry))))
|
||||||
:fee (- (reduce + 0.0 (map (fn [entry]
|
(:entries settlement))))
|
||||||
(if (= (:type entry) "REFUND")
|
:date (-> (:initiated_at settlement)
|
||||||
(- (amount->money (:fee_money entry)))
|
(coerce/to-date))
|
||||||
(amount->money (:fee_money entry))))
|
:sales-date (or (:sales-date settlement)
|
||||||
(:entries settlement))))
|
(-> (:initiated_at settlement)
|
||||||
:date (-> (:initiated_at settlement)
|
(coerce/to-date)))})
|
||||||
(coerce/to-date))
|
|
||||||
:sales-date (or (:sales-date settlement)
|
(filter :expected-deposit/date))))
|
||||||
(-> (:initiated_at settlement)
|
|
||||||
(coerce/to-date)))})))
|
|
||||||
(filter :expected-deposit/date)))
|
|
||||||
)
|
|
||||||
|
|
||||||
(defn refunds [l]
|
(defn refunds
|
||||||
(let [refunds (:refunds (:body (client/get (str "https://connect.squareup.com/v2/refunds?location_id=" l)
|
([client]
|
||||||
{:headers {"Square-Version" "2021-05-13"
|
(refunds client (get-in env [:square-config client :square-location])))
|
||||||
"Authorization" "Bearer EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"
|
([client l]
|
||||||
"Content-Type" "application/json"}
|
(let [refunds (:refunds (:body (client/get (str "https://connect.squareup.com/v2/refunds?location_id=" l)
|
||||||
:as :json
|
{:headers (base-headers client)
|
||||||
:retry-handler (fn [ex try-count http-context]
|
:as :json
|
||||||
(log/warn "Retrying after failure " ex)
|
:retry-handler (fn [ex try-count http-context]
|
||||||
(if (> try-count 4) false true))})))]
|
(log/warn "Retrying after failure " ex)
|
||||||
(->> refunds
|
(if (> try-count 4) false true))})))]
|
||||||
(filter (fn [r] (= "COMPLETED" (:status r))))
|
(->> refunds
|
||||||
(map (fn [r]
|
(filter (fn [r] (= "COMPLETED" (:status r))))
|
||||||
(let [[client location] (location_id->client-location l)]
|
(map (fn [r]
|
||||||
#:sales-refund {:external-id (str "square/refund/" (:id r))
|
#:sales-refund {:external-id (str "square/refund/" (:id r))
|
||||||
:total (amount->money (:amount_money r))
|
:total (amount->money (:amount_money r))
|
||||||
:fee (transduce
|
:fee (transduce
|
||||||
(comp (filter #(= "ADJUSTMENT" (:type %)))
|
(comp (filter #(= "ADJUSTMENT" (:type %)))
|
||||||
(map :amount_money)
|
(map :amount_money)
|
||||||
(map amount->money))
|
(map amount->money))
|
||||||
+
|
+
|
||||||
0.0
|
0.0
|
||||||
(:processing_fee r))
|
(:processing_fee r))
|
||||||
:client [:client/code client]
|
:client [:client/code client]
|
||||||
:location location
|
:location (get-in env [:square-config client :location])
|
||||||
:date (coerce/to-date (:created_at r))
|
:date (coerce/to-date (:created_at r))
|
||||||
:type (:source_type (get-payment (:payment_id r)))}))))))
|
:type (:source_type (get-payment client (:payment_id r)))}))))))
|
||||||
|
|
||||||
(defn upsert
|
(defn upsert
|
||||||
([]
|
([client ]
|
||||||
(upsert nil))
|
(upsert client nil))
|
||||||
([d]
|
([client d]
|
||||||
(lc/with-context {:source "Square loading"}
|
(lc/with-context {:source "Square loading"}
|
||||||
(try
|
(try
|
||||||
(let [existing (->> (d/query {:query {:find ['?external-id]
|
(let [existing (->> (d/query {:query {:find ['?external-id]
|
||||||
:in ['$]
|
:in ['$ '?client]
|
||||||
:where ['[_ :sales-order/external-id ?external-id]]}
|
:where ['[?o :sales-order/client ?client]
|
||||||
:args [(d/db conn)]})
|
'[?o :sales-order/external-id ?external-id]]}
|
||||||
|
:args [(d/db conn) [:client/code client]]})
|
||||||
(map first)
|
(map first)
|
||||||
set)
|
set)
|
||||||
_ (log/info (count existing) "Sales orders already exist")
|
_ (log/info (count existing) "Sales orders already exist")
|
||||||
to-create (filter #(not (existing (:sales-order/external-id %)))
|
to-create (filter #(not (existing (:sales-order/external-id %)))
|
||||||
(daily-results d))]
|
(daily-results client d))]
|
||||||
(doseq [x (partition-all 20 to-create)]
|
(doseq [x (partition-all 20 to-create)]
|
||||||
(log/info "Loading " (count x))
|
(log/info "Loading " (count x))
|
||||||
@(d/transact conn x)))
|
@(d/transact conn x)))
|
||||||
@@ -422,21 +394,20 @@
|
|||||||
(log/error e))))))
|
(log/error e))))))
|
||||||
|
|
||||||
(defn upsert-settlements
|
(defn upsert-settlements
|
||||||
([] (upsert-settlements nil))
|
([client] (upsert-settlements client (get-in env [:square-config client :square-location])))
|
||||||
([location-ids]
|
([client location-id]
|
||||||
(lc/with-context {:source "Square settlements loading "}
|
(lc/with-context {:source "Square settlements loading"}
|
||||||
(try
|
(try
|
||||||
(let [existing (->> (d/query {:query {:find ['?external-id]
|
(let [existing (->> (d/query {:query {:find ['?external-id]
|
||||||
:in ['$]
|
:in ['$ '?client]
|
||||||
:where ['[_ :expected-deposit/external-id ?external-id]]}
|
:where ['[?d :expected-deposit/client ?client]
|
||||||
:args [(d/db conn)]})
|
'[?d :expected-deposit/external-id ?external-id]]}
|
||||||
|
:args [(d/db conn) [:client/code client]]})
|
||||||
(map first)
|
(map first)
|
||||||
set)
|
set)
|
||||||
_ (log/info (count existing) "settlements already exist")
|
_ (log/info (count existing) "settlements already exist")
|
||||||
to-create (filter #(not (existing (:expected-deposit/external-id %)))
|
to-create (filter #(not (existing (:expected-deposit/external-id %)))
|
||||||
(if location-ids
|
(daily-settlements client location-id))]
|
||||||
(daily-settlements location-ids)
|
|
||||||
(daily-settlements)))]
|
|
||||||
(doseq [x (partition-all 20 to-create)]
|
(doseq [x (partition-all 20 to-create)]
|
||||||
(log/info "Loading expected deposit" (count x))
|
(log/info "Loading expected deposit" (count x))
|
||||||
@(d/transact conn x)))
|
@(d/transact conn x)))
|
||||||
@@ -444,26 +415,28 @@
|
|||||||
(log/error e)))
|
(log/error e)))
|
||||||
(log/info "Done loading settlements"))))
|
(log/info "Done loading settlements"))))
|
||||||
|
|
||||||
(defn upsert-refunds []
|
(defn upsert-refunds
|
||||||
(doseq [{location :id} (locations)]
|
([client] (upsert-refunds client (get-in env [:square-config client :square-location])))
|
||||||
(when (location_id->client-location location)
|
([client location]
|
||||||
(lc/with-context {:source (str "Square refunds loading for " location)}
|
(lc/with-context {:source "Loading Square Settlements"
|
||||||
(try
|
:client client}
|
||||||
(let [existing (->> (d/query {:query {:find ['?external-id]
|
(try
|
||||||
:in ['$]
|
(let [existing (->> (d/query {:query {:find ['?external-id]
|
||||||
:where ['[_ :sales-refund/external-id ?external-id]]}
|
:in ['$ '?client]
|
||||||
:args [(d/db conn)]})
|
:where ['[?r :sales-refund/client ?client]
|
||||||
(map first)
|
'[?r :sales-refund/external-id ?external-id]]}
|
||||||
set)
|
:args [(d/db conn) [:client/code client]]})
|
||||||
_ (log/info (count existing) "refunds already exist")
|
(map first)
|
||||||
to-create (filter #(not (existing (:sales-refund/external-id %)))
|
set)
|
||||||
(refunds location))]
|
_ (log/info (count existing) "refunds already exist")
|
||||||
(doseq [x (partition-all 20 to-create)]
|
to-create (filter #(not (existing (:sales-refund/external-id %)))
|
||||||
(log/info "Loading refund" (count x))
|
(refunds client location))]
|
||||||
@(d/transact conn x)))
|
(doseq [x (partition-all 20 to-create)]
|
||||||
(catch Exception e
|
(log/info "Loading refund" (count x))
|
||||||
(log/error e)))
|
@(d/transact conn x)))
|
||||||
(log/info "Done loading refunds")))))
|
(catch Exception e
|
||||||
|
(log/error e)))
|
||||||
|
(log/info "Done loading refunds"))))
|
||||||
|
|
||||||
(defn reset []
|
(defn reset []
|
||||||
(->>
|
(->>
|
||||||
@@ -474,17 +447,25 @@
|
|||||||
(map first)
|
(map first)
|
||||||
(map (fn [x] [:db/retractEntity x]))))
|
(map (fn [x] [:db/retractEntity x]))))
|
||||||
|
|
||||||
|
(defn upsert-all []
|
||||||
|
(doseq [[client-code] (get-in env [:square-config])]
|
||||||
|
(lc/with-context {:source (str "Square loading for " client-code)
|
||||||
|
:client client-code}
|
||||||
|
(log/info "Loading Orders")
|
||||||
|
(upsert client-code)
|
||||||
|
(log/info "Loading Settlements")
|
||||||
|
(upsert-settlements client-code)
|
||||||
|
(log/info "Loading refunds")
|
||||||
|
(upsert-refunds client-code)
|
||||||
|
)))
|
||||||
|
|
||||||
(mount/defstate square-loader
|
(mount/defstate square-loader
|
||||||
:start (scheduler/every (* 4 59 60 1000) upsert)
|
:start (scheduler/every (* 4 59 60 1000) upsert-all)
|
||||||
:stop (scheduler/stop square-loader))
|
:stop (scheduler/stop square-loader))
|
||||||
|
|
||||||
(mount/defstate square-settlement-loader
|
|
||||||
:start (scheduler/every (* 4 60 60 1000) upsert-settlements)
|
|
||||||
:stop (scheduler/stop square-settlement-loader))
|
|
||||||
|
|
||||||
(mount/defstate square-refund-loader
|
|
||||||
:start (scheduler/every (* 4 58 60 1000) upsert-refunds)
|
|
||||||
:stop (scheduler/stop square-refund-loader))
|
|
||||||
|
|
||||||
(comment
|
(comment
|
||||||
(daily-results)
|
(daily-results)
|
||||||
|
|||||||
Reference in New Issue
Block a user