Snipcart Shipping
@ammein
code:
data:privatelast updated:2 years ago
today
Build integrations remarkably fast!
You're viewing a public workflow template.
Sign up to customize, add steps, modify code and more.
Join 800,000+ developers using the Pipedream platform
steps.
trigger
HTTP API
Deploy to generate unique URL
This workflow runs on Pipedream's servers and is triggered by HTTP / Webhook requests.
steps.
snipcart
auth
to use OAuth tokens and API keys in code via theauths object
(auths.snipcart)
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps, auths) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
}
81
const base64 = require('base-64');
const utf8 = require('utf8');
 
var bytes = utf8.encode(auths.snipcart.api_key);
var api64 = base64.encode(bytes);

const validateToken = await require("@pipedreamhq/platform").axios(this, {
  url: `https://app.snipcart.com/api/requestvalidation/${steps.trigger.event.headers["x-snipcart-requesttoken"]}`,
  headers: {
    contentType: "application/json",
    authorization: "Basic " + api64
  }
});

if(!validateToken){
  return await $response({
    status: 400,
    body: {
      errors: [{
        "key": "bad_request",
        "message": "Not from snipcart API"
      }]
    }
  })
}

console.log(event.body.content.shippingAddressPostalCode);
this.shippingPostalCode = event.body.content.shippingAddressPostalCode;

const shipping_methods = await require("@pipedreamhq/platform").axios(this, {
  url: `https://app.snipcart.com/api/shipping_methods`,
  headers:{
    accept: "application/json"
  },
  auth: {
    username: `${auths.snipcart.api_key}`,
    password: ``,
  },
});

this.shipping_methods = shipping_methods;

for(var i = 0; i < shipping_methods.length; i++){
  if(shipping_methods[i].postalCodeRegex){
    var regexPostalCode = new RegExp(shipping_methods[i].postalCodeRegex, "g");
    console.log("Found Regex", regexPostalCode);
    if((steps.trigger.event.body.content.shippingAddressPostalCode.toString()).match(regexPostalCode) === null){
      console.log("Send Error Response", shipping_methods[i].postalCodeRegex);
      return await $respond({
          status:200,
          headers: {
            contentType: "application/json",
            authorization: "Basic " + api64
          },
          body: {
              "errors": [
                  {
                    "key": "shipping-failed",
                    "message": "Shipping is not available on your location. Sorry..."
                  }
                ]
            }
        });
    }
  }else{
    console.log("Skip", shipping_methods[i].postalCodeRegex);
    continue;
  }
}

$respond({
  status: 200,
  headers: {
    contentType: "application/json"
  },
  body: steps.trigger.event.body.content
})

return $end("Nothing is return");