Skip to content

Commit 0fed197

Browse files
authored
Merge pull request #9 from Foxy/polish/datastores
Polish/datastores
2 parents 5261a5c + e27475b commit 0fed197

File tree

10 files changed

+140
-64
lines changed

10 files changed

+140
-64
lines changed

Diff for: README.md

+80-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
# Netlify Serverless Functions for the Foxy.io API
22

3-
This repository allows you to (more) easily create serverless functions to work with the Foxy.io API, deployed using Netlify. The goal is to make this as approachable as possible, so we try to have reasonable default behaviours, and Netlify is a great choice because of the one-click deployment. Once you deploy to Netlify, you can modify your own GitHub files to customize as needed.
3+
This repository allows you to (more) easily create serverless functions to work with the Foxy.io API, deployed using Netlify.
4+
The goal is to make this as [approachable](#Built to be approachable) as possible, so we try to have reasonable default behaviours, and Netlify is a great choice because of the one-click deployment.
5+
Once you deploy to Netlify, you can modify your own GitHub files to customize as needed.
6+
7+
You may use this repository:
8+
- directly, configuring it to your needs with environment variables
9+
- as usage examples, to better understand the usage of Foxy API and FoxySDK.
410

511
## Functions
612

7-
The functions provided in this repository can be used independently, or as a reference for building your own functions. Be sure to check the README for each function in the functions folder.
13+
The functions provided in this repository are used independently.
14+
15+
Be sure to check the README for each function in the functions folder, as they provide specific documentation.
816

9-
- [cart](src/functions/cart): Converts a cart between recurring and non-recurring. Useful in an upsell flow.
10-
- [idevaffiliate-marketplace](src/functions/idevaffiliate-marketplace): A marketplace-style integration, using iDevAffiliate.
1117
- Datastore integrations:
12-
- [pre-payment-webhook-webflow](src/functions/pre-payment-webhook-webflow): Validates the price and/or availability of items against Webflow CMS collections before a payment is processed.
18+
- [datastore-integration-webflow](src/functions/pre-payment-webhook-webflow): Validates the price and/or availability of items against Webflow CMS collections before a payment is processed.
1319
- [datastore-integration-orderdesk](src/functions/datastore-integration-orderdesk): Validates the cart against OrderDesk and updates the inventory upon successful transaction.
14-
- [ShipTheory](src/functions/shiptheory): Creates shipments in your ShipTheory account upon successful transactions.
20+
- Other features:
21+
- [cart](src/functions/cart): Converts a cart between recurring and non-recurring. Useful in an upsell flow.
22+
- [idevaffiliate-marketplace](src/functions/idevaffiliate-marketplace): A marketplace-style integration, using iDevAffiliate.
23+
- [shiptheory](src/functions/shiptheory): Creates shipments in your ShipTheory account upon successful transactions.
1524

1625

1726
### Data Store Integrations
@@ -61,9 +70,74 @@ The pre-payment webhook fires before a transaction is submitted to the payment p
6170
- To do this, after the deploy is finished, click the "functions" tab, look for your webhook function and copy the **Endpoint URL**.
6271
1. Configure your prepayment webhook using your endpoint. Check the docs here: https://wiki.foxycart.com/v/2.0/pre_payment_webhook
6372

73+
# Configuration Reference
74+
75+
This section contains all **environment variables** you can use to configure the webhooks in this repository.
76+
77+
**You do not need to configure all these.** Check the documentation for the functions you are using for the configuration relevant to them.
78+
79+
This list is provided for your quick reference.
80+
81+
**Please, note** that the default values for these variables may be different for each function.
82+
We try to have sensible defaults for each third party service we are integrating with and for each task we are performing.
83+
84+
## How to set **environment variables** in Netlify
85+
86+
These are set with **environment variables** that you can set directly in Netlify dashboard.
87+
88+
To configure your **environment variables** follow these steps:
89+
- go to your site admin dashboard in Netlify
90+
- click "Site settings" button
91+
- click the "Build & deploy" tab in the left menu
92+
- under **Environment**, click "Edit variables"
93+
94+
Please, notice that the default values where chosen to match the default OrderDesk settings.
95+
You shouldn't need to change these values unless you are not using OrderDesk default fields.
96+
97+
This section contains all possible customizations you can do by setting environment variables.
98+
99+
After changing your environment variables you will want to redeploy your webhook.
100+
101+
| Environment Variable | Description |
102+
| ------- | --- |
103+
|`FOXY_API_CLIENT_ID`| [description in Foxy Webhook doc](src/foxy/README.md#environment-variables)|
104+
|`FOXY_API_CLIENT_SECRET`| [description in Foxy Webhook doc](src/foxy/README.md#environment-variables)|
105+
|`FOXY_API_REFRESH_TOKEN`| [description in Foxy Webhook doc](src/foxy/README.md#environment-variables)|
106+
|`FOXY_DEFAULT_AUTOSHIP_FREQUENCY`|[description in Cart doc](src/functions/cart#environment-variables)|
107+
|`FOXY_ERROR_INSUFFICIENT_INVENTORY`| [description in Foxy Webhook doc](src/foxy/README.md#environment-variables)|
108+
|`FOXY_ERROR_PRICE_MISMATCH`| [description in Foxy Webhook doc](src/foxy/README.md#environment-variables)|
109+
|`FOXY_FIELD_CODE`| [description in Foxy Webhook doc](src/foxy/README.md#environment-variables)|
110+
|`FOXY_FIELD_INVENTORY`| [description in Foxy Webhook doc](src/foxy/README.md#environment-variables)|
111+
|`FOXY_FIELD_PRICE`| [description in Foxy Webhook doc](src/foxy/README.md#environment-variables)|
112+
|`FOXY_IDEV_API_URL`|[description in Idev Affiliate doc](src/functions/idevaffiliate-marketplace/README.md#environment-variables)|
113+
|`FOXY_IDEV_SECRET_KEY`|[description in Idev Affiliate doc](src/functions/idevaffiliate-marketplace/README.md#environment-variables)|
114+
|`FOXY_ORDERDESK_API_KEY`|[description in Idev Affiliate doc](src/functions/datastore-integration-orderdesk/README.md#environment-variables)|
115+
|`FOXY_ORDERDESK_STORE_ID`|[description in Idev Affiliate doc](src/functions/datastore-integration-orderdesk/README.md#environment-variables)|
116+
|`FOXY_SKIP_INVENTORY_CODES`| [description in Foxy Webhook doc](src/foxy/README.md#environment-variables)|
117+
|`FOXY_SKIP_INVENTORY_UPDATE_CODES`| [description in Foxy Webhook doc](src/foxy/README.md#environment-variables)|
118+
|`FOXY_SKIP_PRICE_CODES`| [description in Foxy Webhook doc](src/foxy/README.md#environment-variables)|
119+
|`FOXY_WEBFLOW_TOKEN`|[description in Idev Affiliate doc](src/functions/datastore-integration-webflow/README.md#environment-variables)|
120+
|`FOXY_WEBHOOK_ENCRYPTION_KEY`| [description in Foxy Webhook doc](src/foxy/README.md#environment-variables)|
64121

65122
# Development
66123

124+
## Built to be approachable
125+
126+
This repository aims to used as a reference to users who wish to extend their use of Foxy by adding custom server-side features.
127+
128+
Here are some choices we've made:
129+
130+
- Javascript: the language is approachable in itself and widely known.
131+
- Netlify: provides easy deployment and continuous integration with near zero configuration.
132+
- Default behaviours: are set in according to the task, tunned to feel natural for users of the tools each function is integrating with
133+
- Specific instructions: each function has its own README file that should be the only required reading to implement that function. This does not mean we will repeat instructions everywhere, but simply that by reading the docs of the function user will find everything needed, either on that page or on clearly provided links.
134+
135+
These are some goals:
136+
137+
- users should be able to navigate the code with ease, without usage or knowledge of advanced tools
138+
- users should be able to copy and customize code with relative ease, without the need
139+
140+
67141
## Localdev Setup
68142

69143
1. `npm install -g netlify-cli`

Diff for: config.js

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ function env(envVar) {
1212

1313
const config = {
1414
datastore: {
15-
credentials: env('FOXY_DATASTORE_CREDENTIALS'),
1615
error: {
1716
insufficientInventory: env('FOXY_ERROR_INSUFFICIENT_INVENTORY') || env('FX_ERROR_INSUFFICIENT_INVENTORY'),
1817
priceMismatch: env('FOXY_ERROR_PRICE_MISMATCH') || env('FX_ERROR_PRICE_MISMATCH')

Diff for: netlify.toml

-10
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,3 @@
22
command = "npm i"
33
functions = "src/functions"
44

5-
[template.environment]
6-
FOXY_TIMEZONE = "Foxy store timezone, as in `America/Los_Angeles`. Full list: https://tinyurl.com/yxoaxbpz"
7-
FOXY_API_CLIENT_ID = "Enter your Foxy.io API Client ID here."
8-
FOXY_API_CLIENT_SECRET = "Enter your Foxy.io API Client Secret here."
9-
FOXY_API_REFRESH_TOKEN = "Enter your Foxy.io API Refresh Token here."
10-
FOXY_DEFAULT_AUTOSHIP_FREQUENCY = "Default autoship frequency (such as `1m`)"
11-
FOXY_IDEV_SECRET_KEY = "iDevAffiliate secret key (from the API settings)"
12-
FOXY_IDEV_API_URL = "iDevAffiliate API URL (from the API settings; should end in `sale.php`)"
13-
FOXY_WEBFLOW_TOKEN = "Get this from Webflow's project settings, at the 'Integrations' tab."
14-
FOXY_DATASTORE_CREDENTIALS = "Datastore credentials"

Diff for: src/foxy/FoxyWebhook.js

+4
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ function validSignature(payload, signature, key) {
149149
* @returns {boolean} the signature is valid
150150
*/
151151
function verifyWebhookSignature(req) {
152+
if (!config.foxy.webhook.encryptionKey) {
153+
console.log("Foxy Webhook Encryption key is not set. Security can be enhanced if you set it.");
154+
return true;
155+
}
152156
const foxyEvent = req.headers['foxy-webhook-event'];
153157
const signature = req.headers['foxy-webhook-signature'];
154158
if (foxyEvent === 'validation/payment') {

Diff for: src/foxy/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Foxy Webhook
2+
3+
This module aims to consolidate the interaction with Foxy Webhooks.
4+
5+
- use FoxySDK to validate the Foxy Webhook Signature
6+
- provide default responses
7+
8+
## Webhook validation
9+
10+
The webhook validation is optional and is performed if the
11+
`FOXY_WEBHOOK_ENCRYPTION_KEY` environment variable is set. If it is not set
12+
requests are presumed to be from Foxy.
13+
14+
## Environment variables
15+
16+
These environment variables are used to configure datastore functions.
17+
18+
| Variable | Default Value | Description|
19+
| -------------------------- | --------------- | ------------------- |
20+
| `FOXY_API_CLIENT_ID` | "" | **Required** Your Foxy Client Id. |
21+
| `FOXY_API_CLIENT_SECRET` | "" | **Required** Your Foxy Client Secret.|
22+
| `FOXY_API_REFRESH_TOKEN ` | "" | **Required** Your Foxy Client Refresh Token.|
23+
| `FOXY_WEBHOOK_ENCRYPTION_KEY` | "" | **Required** Your wehook encryption key. **This value must not be shared or made public.** |
24+
| `FOXY_ERROR_INSUFFICIENT_INVENTORY` | "Insufficient inventory for these items:" | Occurs when the quantity purchased is greater than the inventory available in Webflow. **A comma separated list of the names of the products out-of-stock will be appended to the end of the error message**. |
25+
| `FOXY_ERROR_PRICE_MISMATCH` | "Prices do not match." | Occurs when the price of any of the products does not match with the `price` field in Webflow |
26+
| `FOXY_FIELD_CODE` | "code"\* | The name of the field that stores the code in the webflow collection. |
27+
| `FOXY_FIELD_PRICE` | "price"\* | The name of the field that stores the price in the webflow collection. |
28+
| `FOXY_FIELD_INVENTORY` | "inventory"\* | The name of the field that stores the inventory in the webflow collection. Set this variable to "false" (without the quotes) to disable inventory verification for all items. |
29+
| `FOXY_SKIP_INVENTORY_CODES` | ""\* | A comma separated list of code values (this is the value set in your 'code' field in Webflow or in the field you set with `code_field` parameter. **The items with these codes will skip inventory verification**. |
30+
| `FOXY_SKIP_PRICE_CODES` | ""\* | A comma separated list of code values (this is the value set in your 'code' field in Webflow or in the field you set with `code_field` parameter. **The items with these codes will skip price verification**. |
31+
| `FOXY_SKIP_INVENTORY_CODES` | ""\* | A comma separated list of code values (this is the value set in your 'code' field in Webflow or in the field you set with `code_field` parameter. **The items with these codes will skip inventory verification**. |
32+
33+
\* These default values may be different for some functions.
34+
Please, check the specific documentation for the function you are using.
35+

Diff for: src/functions/cart/README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@ functions from this repository you don't need to do this step.
1717
1. Go to Netlify and click `New site from Git`.
1818
1. Choose the repository you've just cloned.
1919

20-
### Configuration
20+
### Environment variables
2121

2222
This webhook depends on FoxyAPI and requires setting the following environment variables:
2323

2424
| Variable | Default Value | Description|
2525
| -------------------------- | --------------- | ------------------- |
26-
| FOXY_API_CLIENT_ID | "" | **Required** Your Foxy Client Id. |
27-
| FOXY_API_CLIENT_SECRET | "" | **Required** Your Foxy Client Secret.|
28-
| FOXY_API_REFRESH_TOKEN | "" | **Required** Your Foxy Client Refresh Token.|
26+
| `FOXY_API_CLIENT_ID` | "" | **Required** Your Foxy Client Id. |
27+
| `FOXY_API_CLIENT_SECRET` | "" | **Required** Your Foxy Client Secret.|
28+
| `FOXY_API_REFRESH_TOKEN ` | "" | **Required** Your Foxy Client Refresh Token.|
29+
|`FOXY_DEFAULT_AUTOSHIP_FREQUENCY`| "1m" | The default subscription frequency to be used when converting a cart into subscriptions.|
2930

3031

3132
You can grab these values for your store going to your Foxy Store Admin page, click on the "integrations" link and, under "current integrations" click "Get Token".

Diff for: src/functions/datastore-integration-orderdesk/DataStore.js

+3-15
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,9 @@ class DataStore extends DataStoreBase {
6464
}
6565

6666
parseConfigCredentials(config) {
67-
const rawCredentials = config.datastore.credentials;
68-
let matched;
69-
if (rawCredentials) {
70-
matched = rawCredentials.match(/Store ID (\d{5}) API Key ([A-Za-z0-9]+)$/);
71-
}
72-
if (matched && matched.length === 3) {
73-
return {
74-
id: matched[1],
75-
key: matched[2]
76-
}
77-
} else {
78-
return {
79-
id: config.datastore.provider.orderDesk.storeId,
80-
key: config.datastore.provider.orderDesk.apiKey
81-
}
67+
return {
68+
id: config.datastore.provider.orderDesk.storeId,
69+
key: config.datastore.provider.orderDesk.apiKey
8270
}
8371
}
8472

Diff for: src/functions/datastore-integration-orderdesk/README.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Please, notice that **it ignores existing subscriptions**.
3232
- It does not handle discounts.
3333
- It does not handle item options, such as `price_mod`.
3434

35-
# Setup
35+
# Setup
3636

3737
## Setup Overview
3838

@@ -82,7 +82,7 @@ repository, there are some steps you may skip (those you've already done).
8282
- Save your settings clicking on the "Update Payment Gateway" button in the bottom of the page.
8383
1. Configure your **transaction/created** webhook if you want it to update your inventory in OrderDesk. You may check the docs here: https://wiki.foxycart.com/v/2.0/webhooks
8484
- Go to your [Foxy.io Admin page](https://admin.foxycart.com/admin.php)
85-
- Under "Account", click ["integrations]"(https://admin.foxycart.com/admin.php?ThisAction=AddIntegration)
85+
- Under "Account", click ["integrations]"(https://admin.foxycart.com/admin.php?ThisAction=AddIntegration)
8686
- Check the "JSON Webhook" box and paste the same Endpoint you copied from your Netlify function in the URL field.
8787
- Give it a title that makes sense to you.
8888
- Copy the value from the "Encryption Key" field.
@@ -92,7 +92,7 @@ repository, there are some steps you may skip (those you've already done).
9292

9393
# Configuration Reference
9494

95-
There are a some customization you can do to your webhook.
95+
There are some customization you can do to your webhook.
9696
These are set with **environment variables** that you can set directly in Netlify dashboard.
9797

9898
To configure your **environment variables** follow these steps:
@@ -108,17 +108,16 @@ This section contains all possible customizations you can do by setting environm
108108

109109
After changing your environment variables you may want to redeploy your webhook.
110110

111-
##### Configuration
111+
##### Environment variables
112112

113113
These environment variables are used to allow your webhook to authenticate authenticate with Foxy and OrderDesk.
114114

115115

116116
| Variable | Default Value | Description|
117117
| ------------------------------- | --------------- | -------------------------------------------------------------------------------- |
118-
| `FOXY_WEBHOOK_ENCRYPTION_KEY` | "" | **Required** Your wehook encryption key. **This value must not be shared or made public.** |
118+
| `FOXY_WEBHOOK_ENCRYPTION_KEY` | "" | **Optional** Your wehook encryption key. **This value must not be shared or made public.** |
119119
| `FOXY_ORDERDESK_API_KEY` | "" | **Required** Your OrderDesk API Key |
120120
| `FOXY_ORDERDESK_STORE_ID` | "" | **Required** Your OrderDesk Store id |
121-
| `FOXY_DATASTORE_CREDENTIALS` | "" | This variable is an alternative to `FOXY_ORDERDESK_API_KEY` and `FOXY_ORDERDESK_STORE_ID`. If you provide this variable you don't have to provide those. This is meant to receive a copy of the values provided in the API tab in your OrderDesk settings.|
122121

123122

124123
##### Price and Inventory verification
@@ -139,7 +138,7 @@ This is likely unnecessary and you shouldn't change these default values unless
139138
| Variable | Default Value | Example config |Description |
140139
| -------------------------------- | --------------- | ---------------------- |---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
141140
| `FOXY_FIELD_CODE` | "sku" | "code" |The name of the field that stores the code in OrderDesk. **There is no need to set this if you are using OrderDesk "code" as your "sku"**. |
142-
| `FOXY_FIELD_PRICE` | "price" | "value" |The name of the field that stores the price in the OrderDesk. **There is no need to set this if you are using OrderDesk "price" field**. |
141+
| `FOXY_FIELD_PRICE` | "price" | "value" |The name of the field that stores the price in the OrderDesk. **There is no need to set this if you are using OrderDesk "price" field**. |
143142
| `FOXY_FIELD_INVENTORY` | "stock" | "inventory" |The name of the field that stores the inventory in OrderDesk. **There is no need to set this if you are using OrderDesk "stock" field**. |
144143

145144
##### Error messages
@@ -173,9 +172,11 @@ Cloning the repository will create your own copy of this Webhook, allowing you t
173172
Go to your Netlify account and click the "New site from Git" button.
174173

175174
- Choose your repository.
176-
- Click the "Advanced" button and then "New Variable"
177-
- The key should be: `FOXY_DATASTORE_CREDENTIALS`
178-
- To get this token, go to OrderDesk settings, at the 'API' tab. Copy all the information provided.
175+
- Click the "Advanced" button add the following variables.
176+
- `FOXY_ORDERDESK_API_KEY`
177+
- `FOXY_ORDERDESK_STORE_ID`
178+
- To get these variables go to OrderDesk settings, at the 'API' tab.
179+
179180

180181
# Upgrade your webhook
181182

@@ -188,3 +189,4 @@ If you forked the repository, you can use the GitHub Action available in the "Ac
188189
- Click the SyncFork workflow and then "run workflow"
189190

190191
If you've made customizations, there may be conflicts. In this case you par pull the changes and resolve the conflicts manually.
192+

0 commit comments

Comments
 (0)