Adding refunds

This commit is contained in:
2021-06-12 07:08:36 -07:00
parent eb3fd17db8
commit ef049de347
3 changed files with 84 additions and 4 deletions

View File

@@ -161,7 +161,38 @@
{:db/ident :charge/processor
:db/doc "Which food app processed this order"
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one}]]}})
:db/cardinality :db.cardinality/one}]]}
:add-refunds {:txes [[{:db/ident :sales-refund/external-id
:db/doc "The external id for the refund"
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/unique :db.unique/identity}
{:db/ident :sales-refund/client
:db/doc "The client for the refund"
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one}
{:db/ident :sales-refund/location
:db/doc "The location of the refund"
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one}
{:db/ident :sales-refund/date
:db/doc "The date the refund was initiated"
:db/valueType :db.type/instant
:db/cardinality :db.cardinality/one}
{:db/ident :sales-refund/total
:db/doc "The total amount on the deposit"
:db/valueType :db.type/double
:db/cardinality :db.cardinality/one}
{:db/ident :sales-refund/fee
:db/doc "The total fee on the refund"
:db/valueType :db.type/double
:db/cardinality :db.cardinality/one}]]}})

View File

@@ -34,6 +34,7 @@
(not (env :run-web? )) (into [#'jetty])
(not (env :run-background?)) (into [#'square/square-loader
#'square/square-settlement-loader
#'square/square-refund-loader
#'vendor/refresh-vendor-usages-worker
#'ledger/touch-broken-ledger-worker
#'ledger/process-txes-worker

View File

@@ -78,8 +78,7 @@
}}})
(defn pc [d]
{"query" {"filter" {"date_time_filter"
(defn pc [d] {"query" {"filter" {"date_time_filter"
{
"created_at" {
"start_at" (f/unparse (f/formatter "YYYY-MM-dd'T'HH:mm:ssZZ") (time/to-time-zone (coerce/to-date-time d) (time/time-zone-for-id "America/Los_Angeles")))
@@ -127,7 +126,7 @@
"SCX0Y8CTGM1S0" ["NGE1" "UC"]
"FNH5VRT890WK8" ["NGMJ" "SC"]
"AMQ0NPA8FGDEF" ["NGPG" "SZ"]
"4X8T65741AEPS" ["NGVZ" "NP"]
"ACNTYY8WVZ6DV" ["NGVZ" "NP"]
"KMVFQ9CRCXJ10" ["NGZO" "VT"]} location))
;; to get totals:
@@ -243,6 +242,30 @@
:date (-> (:initiated_at settlement)
(coerce/to-date))})))))
(defn refunds [l]
(let [refunds (:refunds (:body (client/get (str "https://connect.squareup.com/v2/refunds?location_id=" l)
{:headers {"Square-Version" "2021-05-13"
"Authorization" "Bearer EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"
"Content-Type" "application/json"}
:as :json
:retry-handler (fn [ex try-count http-context]
(log/warn "Retrying after failure " ex)
(if (> try-count 4) false true))})))]
(->> refunds
(map (fn [r]
(let [[client location] (location_id->client-location l)]
#:sales-refund {:external-id (str "square/refund/" (:id r))
:total (amount->money (:amount_money r))
:fee (transduce
(comp (map :amount_money) (map amount->money))
+
0.0
(:processing_fee r))
:client [:client/code client]
:location location
:date (coerce/to-date (:created_at r))
}))))))
(defn upsert
([]
(upsert nil))
@@ -283,6 +306,27 @@
(log/error e)))
(log/info "Done loading settlements")))
(defn upsert-refunds []
(doseq [{location :id} (locations)]
(when (location_id->client-location location)
(lc/with-context {:source (str "Square refunds loading for " location)}
(try
(let [existing (->> (d/query {:query {:find ['?external-id]
:in ['$]
:where ['[_ :sales-refund/external-id ?external-id]]}
:args [(d/db conn)]})
(map first)
set)
_ (log/info (count existing) "refunds already exist")
to-create (filter #(not (existing (:sales-refund/external-id %)))
(refunds location))]
(doseq [x (partition-all 20 to-create)]
(log/info "Loading refund" (count x))
@(d/transact conn x)))
(catch Exception e
(log/error e)))
(log/info "Done loading refunds")))))
(defn reset []
(->>
(d/query {:query {:find ['?e]
@@ -300,6 +344,10 @@
:start (scheduler/every (* 14 60 1000) upsert-settlements)
:stop (scheduler/stop square-settlement-loader))
(mount/defstate square-refund-loader
:start (scheduler/every (* 30 60 1000) upsert-refunds)
:stop (scheduler/stop square-refund-loader))
(comment
(daily-results)