knitr::opts_chunk$set( fig.align = 'center', collapse = TRUE, comment = "#>", fig.path = "man/figures/README-")
squareupr is an R package that connects to the Square APIs (Connect v1 & v2).
sq_auth()
)sq_list_locations()
, sq_get_location()
)sq_list_transactions()
, sq_get_transaction()
)sq_list_customers()
, sq_search_customers()
, sq_get_customer()
, sq_create_customer()
, sq_update_customer()
, sq_delete_customer()
sq_list_payments()
, sq_get_payment()
)sq_list_items()
, sq_get_item()
, sq_create_item()
, sq_update_item()
, sq_delete_item()
sq_list_fees()
, sq_list_categories()
, sq_list_modifiers()
,
sq_list_discounts()
)# This package is not yet available on CRAN so you must install from GitHub # install.packages("devtools") devtools::install_github("StevenMMortimer/squareupr")
If you encounter a clear bug, please file a minimal reproducible example on GitHub.
First, load the squareupr package and authenticate. There are two ways to authenticate:
library(dplyr) library(squareupr) settings <- readRDS(here::here("tests", "testthat", "squareupr_test_settings.rds")) suppressMessages(sq_auth(personal_access_token = settings$personal_access_token, verbose = FALSE))
library(dplyr) library(squareupr) # Using Personal Access Token (PAT) sq_auth(personal_access_token = "sq-Th1s1sMyPers0nalAcessT0ken") # Using OAuth 2.0 authentication sq_auth()
NOTE: Before using OAuth 2.0 authentication it is necessary that you set up your own Connected App in the Square dashboard. An App ID and App Secret will be provided, then you will be able to plug into your script like so:
options(squareupr.app_id = "sq0-99-thisisatest99connected33app22id") options(squareupr.app_secret = "sq0-Th1s1sMyAppS3cr3t") sq_auth()
OAuth 2.0 credentials will be cached locally in a file entitled ".httr-oauth-squareupr"
in the current working directory so that a new token is not needed each session.
Transactions are organized by location. With the v2 Locations endpoint you can pull
information regarding all locations first to obtain the location IDs. Then with the
sq_list_transactions()
function you can provide the location and timeframe to search.
The function defaults to pulling transactions from the previous day using Sys.Date() - 1
.
Once you obtain the transactions the tenders
field lists all methods of payment
used to pay in the transaction.
# list all locations our_locations <- sq_list_locations() our_transactions <- sq_list_transactions(location = our_locations$id[2], begin_time = as.Date('2019-07-09'), end_time = as.Date('2019-07-10')) our_transactions
Once you pull data about transactions you can take the customer_id from the transaction
tenders
field and match that up with customer details. In Square customers can
be placed into groups that allow for the analysis of transactions at a group-level.
# list customers created in the last 30 days created_start <- format(Sys.Date() - 30, '%Y-%m-%dT00:00:00-00:00') created_end <- format(Sys.Date(), '%Y-%m-%dT00:00:00-00:00') our_customers <- sq_search_customers(query = list(filter= list(created_at= list(start_at=created_start, end_at=created_end)))) our_customers$given_name <- "{HIDDEN}" our_customers$family_name <- "{HIDDEN}" our_customers %>% select(id, created_at, updated_at, given_name, family_name, preferences, groups) # show the groups that each customer belongs to # filter to the groups designated automatically by Square sq_extract_cust_groups(our_customers) %>% filter(grepl("^CQ689YH4KCJMY", groups.id))
This application uses other open source software components. The authentication components are mostly verbatim copies of the routines established in the googlesheets package (https://github.com/jennybc/googlesheets). We acknowledge and are grateful to these developers for their contributions to open source.
This package makes requests best formatted to match what the APIs require as input. This articulation is not perfect and continued progress will be made to add and improve functionality. For details on formatting, attributes, and methods please refer to Square's documentation as they are explained better there.
More information is also available on the pkgdown
site at
https://StevenMMortimer.github.io/squareupr.
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.