Shopify / js-buy-sdk

The JS Buy SDK is a lightweight library that allows you to build ecommerce into any website. It is based on Shopify's API and provides the ability to retrieve products and collections from your shop, add products to a cart, and checkout.
https://shopify.github.io/js-buy-sdk
MIT License
989 stars 261 forks source link

Do I need a paid Shopify store to use js-buy-sdk #628

Closed AlexanderProd closed 5 years ago

AlexanderProd commented 5 years ago

Describe the bug I'm developing a shop that uses the js-buy-sdk, while developing I've been using the graphql store credentials that can be found in the storefront-api-examples repository.

Now I've encountered this error though, which lead me to wonder if a paid Shopify store is required to use some functions of js-buy-sdk. image

Environment (please complete the following information):

Bug Report Checklist

AlexanderProd commented 5 years ago

This happens with a paid Shopify store as well, therefore the error seems to be of different origin. The use of status code 402 is missleading though.

AlexanderProd commented 5 years ago

Re opening because I really have difficulties creating a checkout. According to #379 this can happen when you don't use the right token. I've tested tokens for 3 different stores though and the error still persists.

What are other problems that can lead to a checkout not being created?

I'm using it like this and initializeCheckout is being called inside a react componentDidMount() method.

const createNewCheckout = () => this.state.store.client.checkout.create()
    const fetchCheckout = id => this.state.store.client.checkout.fetch(id)

    if (existingCheckoutID) {
      try {
        const checkout = await fetchCheckout(existingCheckoutID);

        // Make sure this cart hasn’t already been purchased.
        if (!checkout.completedAt) {
          setCheckoutInState(checkout)
          return
        }
      } catch (e) {
        localStorage.setItem('shopify_checkout_id', null)
      }
    }

    const newCheckout = await createNewCheckout()
    setCheckoutInState(newCheckout)

image

jamiemtdwyer commented 5 years ago

Hey @AlexanderProd !

The error is because undefined.myshopify.com is not a real store (or at least, not one that is operational).

It's worth noting that the credential set must be valid for the store you are executing API calls against – access tokens cannot be used globally across Shopify.

If you want to create a free store for testing purposes, I recommend creating a free account with our partner program. https://www.shopify.ca/partners

Once you've created a shop, you can follow the Getting Started guide to generate storefront credentials. Follow the steps for Via a private app.

Cheers!

AlexanderProd commented 5 years ago

@jamiemtdwyer I dont even understand why it says undefined.myshopify.com. I've used the credentials for a real paid shopify store (storefront access token and the store name) as well as the credentials for the graphql shopify store found inside the js-buy-sdk examples and also a test store created with the partner programm, In all cases everything works but I cant create a checkout.

jamiemtdwyer commented 5 years ago

To me, this points to the buy client not being initialized properly.

https://github.com/Shopify/js-buy-sdk#initializing-the-client

Perhaps the shop domain has not been passed in correctly, which is why the client is attempting to perform a request to undefined.myshopify.com

I'm not sure why you're having success with the other operations except checkout. There's nothing special about the checkout mutations. My advice would be to try to replicate this in the simplest environment possible, by simply initializing the client and creating a checkout, and see if that works.

It might be some kind of scoping issue, where your environment does not have access to the client you've initialized inside of the componentDidMount() callback, i.e. this.state.store.client is not initialized properly or does not point to your actual client.