lmsqueezy / laravel

A package to easily integrate your Laravel application with Lemon Squeezy.
https://lemonsqueezy.com
MIT License
521 stars 51 forks source link

The related resource does not exist. #76

Closed devhoussam1998 closed 8 months ago

devhoussam1998 commented 8 months ago

Lemon Squeezy for Laravel Version

1.5

Laravel Version

11.0

PHP Version

8.2

Description

Screenshot 2024-03-17 at 19 36 35

Steps To Reproduce

Web.php

Route::get('/buy', function (Request $request) {
    $checkout = $request->user()->checkout('218148');

    return view('billing', ['checkout' => $checkout]);
});

billing.blade.php

<x-app-layout>
    <x-slot name="header">
        <h2 class="text-xl font-semibold leading-tight text-gray-800 dark:text-gray-200">
            {{ __('Billing') }}
        </h2>
    </x-slot>

    <div class="py-12">
        <x-lemon-button :href="$checkout" class="px-8 py-4" dark>
            Buy Product
        </x-lemon-button>
    </div>
</x-app-layout>
sahapranta commented 8 months ago

Not just from the button, even I am getting the same error in command too.

image

- Here is the log

[2024-03-18 10:02:56] local.ERROR: HTTP request returned status code 404:
{"jsonapi":{"version":"1.0"},"errors":[{"detail":"The related resource does not exist.","source":{"pointer":"\/data\/rel (truncated...)
 {"exception":"[object] (Illuminate\\Http\\Client\\RequestException(code: 404): HTTP request returned status code 404:
{\"jsonapi\":{\"version\":\"1.0\"},\"errors\":[{\"detail\":\"The related resource does not exist.\",\"source\":{\"pointer\":\"\\/data\\/rel (truncated...)
devhoussam1998 commented 8 months ago

Not just from the button, even I am getting the same error in command too.

image

Here is the log

[2024-03-18 10:02:56] local.ERROR: HTTP request returned status code 404:
{"jsonapi":{"version":"1.0"},"errors":[{"detail":"The related resource does not exist.","source":{"pointer":"\/data\/rel (truncated...)
 {"exception":"[object] (Illuminate\\Http\\Client\\RequestException(code: 404): HTTP request returned status code 404:
{\"jsonapi\":{\"version\":\"1.0\"},\"errors\":[{\"detail\":\"The related resource does not exist.\",\"source\":{\"pointer\":\"\\/data\\/rel (truncated...)

I hope the core team will fix this soon as possible

heyjorgedev commented 8 months ago

Did you set the Store Identifier correctly by using the LEMON_SQUEEZY_STORE env variable? Example: LEMON_SQUEEZY_STORE=your-lemon-squeezy-store-id

https://github.com/lmsqueezy/laravel/tree/main?tab=readme-ov-file#store-identifier

devhoussam1998 commented 8 months ago

your-lemon-squeezy-store-id

of course i setup everything

heyjorgedev commented 8 months ago

I was almost sure that the response you are getting comes from the related resources on the lemonsqueezy api, to be more specific here: https://github.com/lmsqueezy/laravel/blob/main/src/Checkout.php#L243

Let's try to debug it together, do you have more details on the exception that you could share?

Also can you double check that the value of the LEMON_SQUEEZY_STORE env variable is the ID of your store, the id is a number and can be found here: https://app.lemonsqueezy.com/settings/stores

devhoussam1998 commented 8 months ago

I was almost sure that the response you are getting comes from the related resources on the lemonsqueezy api, to be more specific here: https://github.com/lmsqueezy/laravel/blob/main/src/Checkout.php#L243

Let's try to debug it together, do you have more details on the exception that you could share?

Also can you double check that the value of the LEMON_SQUEEZY_STORE env variable is the ID of your store, the id is a number and can be found here: https://app.lemonsqueezy.com/settings/stores

@heyjorgedev I follow the docs and this video https://www.youtube.com/watch?v=Zx6VhRzOMkQ&ab_channel=CodeWithTony

I share the code and also here is the error: https://flareapp.io/share/w5BZdwX5

devhoussam1998 commented 8 months ago

my .env contains:

LEMON_SQUEEZY_API_KEY=xxxxxxxxxxxxxx
LEMON_SQUEEZY_STORE=628xxx
LEMON_SQUEEZY_SIGNING_SECRET=xxx123
heyjorgedev commented 8 months ago

Sorry I couldn't get back to you sooner, thank you for the stack trace on flare! I still think one of those ids is wrong, so let's try to abstract from the package and dig into curl requests:

With curl we can at least check if the store and the variant are correct and exclude that from debug:

Pay attention to the values in {}, you need to replace them with your env variables or variant id.

To retrieve a store you can execute this:

curl "https://api.lemonsqueezy.com/v1/stores/{LEMON_SQUEEZY_STORE}" \
     -H 'Accept: application/vnd.api+json' \
     -H 'Content-Type: application/vnd.api+json' \
     -H 'Authorization: Bearer {LEMON_SQUEEZY_API_KEY}'

To retrieve a variant you can execute this:

curl "https://api.lemonsqueezy.com/v1/variants/{variant-id}" \
     -H 'Accept: application/vnd.api+json' \
     -H 'Content-Type: application/vnd.api+json' \
     -H 'Authorization: Bearer {LEMON_SQUEEZY_API_KEY}'

What I want to see is if you are not getting an answer like this on both of the requests:

{"jsonapi":{"version":"1.0"},"errors":[{"detail":"The route v1\/stores\/dasdasd could not be found.","status":"404","title":"Not Found"}]}%

devhoussam1998 commented 8 months ago

Thank for replay @heyjorgedev

Here is what I get:

Screenshot 2024-03-22 at 13 54 08 Screenshot 2024-03-22 at 14 03 24

Route/Web.php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::get('/buy', function (Request $request) {
    $checkout = $request->user()->checkout('220882');

    return view('billing', ['checkout' => $checkout]);
});

For:

curl "https://api.lemonsqueezy.com/v1/stores/{LEMON_SQUEEZY_STORE}" \
     -H 'Accept: application/vnd.api+json' \
     -H 'Content-Type: application/vnd.api+json' \
     -H 'Authorization: Bearer {LEMON_SQUEEZY_API_KEY}'

Here is what I get:

{"jsonapi":{"version":"1.0"},"links":{"self":"https:\/\/api.lemonsqueezy.com\/v1\/stores\/62843"},"data":{"type":"stores","id":"62843","attributes":{"name":"Test","slug":"test956555","domain":"test956555.lemonsqueezy.com","url":"https:\/\/test956555.lemonsqueezy.com","avatar_url":"https:\/\/eu.ui-avatars.com\/api\/?name=Test&length=1&background=2DD272&color=fff&size=64&rounded=true","plan":"free","country":"US","country_nicename":"United States","currency":"USD","total_sales":0,"total_revenue":0,"thirty_day_sales":0,"thirty_day_revenue":0,"created_at":"2024-01-04T18:11:34.000000Z","updated_at":"2024-01-04T18:11:34.000000Z"},"relationships":{"products":{"links":{"related":"https:\/\/api.lemonsqueezy.com\/v1\/stores\/62843\/products","self":"https:\/\/api.lemonsqueezy.com\/v1\/stores\/62843\/relationships\/products"}},"orders":{"links":{"related":"https:\/\/api.lemonsqueezy.com\/v1\/stores\/62843\/orders","self":"https:\/\/api.lemonsqueezy.com\/v1\/stores\/62843\/relationships\/orders"}},"subscriptions":{"links":{"related":"https:\/\/api.lemonsqueezy.com\/v1\/stores\/62843\/subscriptions","self":"https:\/\/api.lemonsqueezy.com\/v1\/stores\/62843\/relationships\/subscriptions"}},"discounts":{"links":{"related":"https:\/\/api.lemonsqueezy.com\/v1\/stores\/62843\/discounts","self":"https:\/\/api.lemonsqueezy.com\/v1\/stores\/62843\/relationships\/discounts"}},"license-keys":{"links":{"related":"https:\/\/api.lemonsqueezy.com\/v1\/stores\/62843\/license-keys","self":"https:\/\/api.lemonsqueezy.com\/v1\/stores\/62843\/relationships\/license-keys"}},"webhooks":{"links":{"related":"https:\/\/api.lemonsqueezy.com\/v1\/stores\/62843\/webhooks","self":"https:\/\/api.lemonsqueezy.com\/v1\/stores\/62843\/relationships\/webhooks"}}},"links":{"self":"https:\/\/api.lemonsqueezy.com\/v1\/stores\/62843"}}}%

For:

curl "https://api.lemonsqueezy.com/v1/variants/220882" \
     -H 'Accept: application/vnd.api+json' \
     -H 'Content-Type: application/vnd.api+json' \
     -H 'Authorization: Bearer {LEMON_SQUEEZY_API_KEY}'

Here is what I get:

{"jsonapi":{"version":"1.0"},"errors":[{"detail":"Unauthenticated.","status":"401","title":"Unauthorized"}]}%

heyjorgedev commented 8 months ago

@devhoussam1998 there you go, that product you are showing me in the screenshot is a product that has variants, from the curl request you made to the variants endpoint I can see that the id you have for the variant is wrong, probably you have the product id there instead.

I'm not on the computer so I can't provide you with nice step by step screenshots on how to get the variant id from the lemonsqueezy dashboard but I'll try to give you a text step by step guide for the time being to see if you get there:

  1. Click on the product on the lemon Squeezy dashboard
  2. A slide hover should appear
  3. Scroll the slide over until you see "Variants"
  4. You should have a "..." icon on the right side of each variant
  5. Click on it and you should have an option "Copy Id" or "Copy Variant ID"
  6. Click on it, that's the id you should use to call the checkout method

When I get to the computer I can help more but I hope this is enough to get you going, now I am sure that what you have wrong is the variant id, and that's easy to fix

devhoussam1998 commented 8 months ago

@devhoussam1998 there you go, that product you are showing me in the screenshot is a product that has variants, from the curl request you made to the variants endpoint I can see that the id you have for the variant is wrong, probably you have the product id there instead.

I'm not on the computer so I can't provide you with nice step by step screenshots on how to get the variant id from the lemonsqueezy dashboard but I'll try to give you a text step by step guide for the time being to see if you get there:

  1. Click on the product on the lemon Squeezy dashboard
  2. A slide hover should appear
  3. Scroll the slide over until you see "Variants"
  4. You should have a "..." icon on the right side of each variant
  5. Click on it and you should have an option "Copy Id" or "Copy Variant ID"
  6. Click on it, that's the id you should use to call the checkout method

When I get to the computer I can help more but I hope this is enough to get you going, now I am sure that what you have wrong is the variant id, and that's easy to fix

thank you so mush @heyjorgedev now I understand the problem is wrong variant id

devhoussam1998 commented 8 months ago

it works thank you @heyjorgedev