Unable to set shipping rates through Carrier Service API

Highlighted
Shopify Partner
18 0 8

I am developing an application to give customers a discount on shipping if they have previously purchased a product and the record exists in my database (adding the customer in database is handled on order/create hook). I have added an endpoint for shipping rate calculation in my app. Upon receiving a request I fetch abandoned checkouts and see if the address matches the one received in calculate shipping request. Once customer is found, I query my database to check if discount code is still valid. However, even if my query returns the customer the shipping rate does not get set. Is there some response time restriction here that I am missing? All this calculation seems to happen in a split second at my end.

I am able to set shipping rate when I set response body outside my request to fetch abandoned checkouts but not from within the request. Is there something obvious here that I am missing? Here is my code:

 

  router.post("/get-shipping-rate", async ctx => {
    // Get abandoned checkouts
    const reqShipAddr = ctx.request.body.rate.destination;
    var options = {
      method: "GET",
      uri: checkouts_url,
      qs: {
        access_token: access_token
      },
      headers: {
        "User-Agent": "Request-Promise"
      },
      json: true
    };
    let customerID = null;
    request(options)
      .then(async function(parsedBody) {
        const checkouts = parsedBody.checkouts;
        for (let i = 0; i < checkouts.length; i++) {
          const shippingAddress = checkouts[i].shipping_address;
          if (
            shippingAddress.address1 === reqShipAddr.address1 &&
            shippingAddress.address2 === reqShipAddr.address2 &&
            shippingAddress.city === reqShipAddr.city
          ) {
            customerID = checkouts[i].customer.id;
            console.log("customer found: ", customerID);
            try {
              await db.start();
              const text = "select * from members WHERE customer_id = $1;";
              const values = [customerID];
              await db.query(text, values).then(c => {
                if (c.rows.length !== 0) {
                  console.log("setting shipping rate");
                  ctx.body = {
                    rates: [
                      {
                        service_name: "Special Discounted Shipping",
                        service_code: "ON",
                        total_price: "0",
                        description: "This is the fastest option by far",
                        currency: "USD",
                        min_delivery_date: "2020-04-12 14:48:45 -0400",
                        max_delivery_date: "2020-04-12 14:48:45 -0400"
                      }
                    ]
                  };
                }
              });
            } catch (error) {
              console.log(error);
            } finally {
              console.log("database closed");
              await db.close();
            }
          }
        }
      })
      .catch(function(err) {
        // API call failed...
        console.log("error", err);
      });
  });

 

0 Likes