Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checkout creation not matching docs #4

Open
BWStearns opened this issue Jul 28, 2024 · 2 comments · May be fixed by #5
Open

Checkout creation not matching docs #4

BWStearns opened this issue Jul 28, 2024 · 2 comments · May be fixed by #5
Assignees

Comments

@BWStearns
Copy link

According to the docs the following should work but the compiler is unhappy.

    let settings = settings::Settings::from_json(&ctx.config.settings.clone().unwrap())?;
    let user = users::Model::find_by_pid(&ctx.db, &auth.claims.pid).await?;
    let store_id = settings.lemon_squeezy.store_id;
    let customer_id = user.id.to_string();
    let lemon_squeezy = lemonsqueezy::LemonSqueezy::new(settings.lemon_squeezy.api_key);
    let checkout = Checkout::build(lemon_squeezy);
    let checkout = checkout.create(CreateCheckout {
            store_id: store_id,
            customer_id: customer_id,
            ..Default::default()
        }).await.unwrap();
    format::json(checkout)
image
error[E0560]: struct `lemonsqueezy::checkout::CreateCheckout` has no field named `store_id`
  --> src/controllers/subscriptions.rs:55:13
   |
55 |             store_id: store_id,
   |             ^^^^^^^^ `lemonsqueezy::checkout::CreateCheckout` does not have this field
   |
   = note: available fields are: `type`, `attributes`, `relationships`

Is there some intermediate object needed here? Should I wrap the store_id and customer_id in some attributes object?

@VarunPotti VarunPotti self-assigned this Aug 14, 2024
VarunPotti added a commit that referenced this issue Aug 14, 2024
@VarunPotti VarunPotti linked a pull request Aug 14, 2024 that will close this issue
@VarunPotti
Copy link
Owner

VarunPotti commented Aug 14, 2024

Hey @BWStearns

I sincerely apologize for the oversight from my end as well as the delayed response. I've been busy with my studies recently, and hence there was a delay in addressing this issue.

Below is the code snippet that I believe should work with your project:

// imports
use lemonsqueezy::checkout::Checkout;
use lemonsqueezy::types::checkout::*;

// usage
let checkout = checkout
    .create(CreateCheckout {
        r#type: String::from("checkouts"),
        attributes: CreateCheckoutAttributes {
            ..Default::default()
        },
        relationships: Some(CreateCheckoutRelationships {
            store: lemonsqueezy::types::Data {
                data: CreateCheckoutRelationShipData {
                    r#type: String::from("stores"),
                    id: String::from("//store id"),
                },
            },
            variant: lemonsqueezy::types::Data {
                data: CreateCheckoutRelationShipData {
                    r#type: String::from("variants"),
                    id: String::from("// variant id"),
                },
            },
        }),
    }).await;

Please let me know if it works, if it does I will merge #5

Also a pro tip the entire source code is designed to closely follow the documentation at lemonsqueezy. You can compare the above input with the official docs, which might help develop and debug any oversights from my end. However, if you encounter any more issues, please feel free to open another issue.

Thanks and happy hacking!

@BWStearns
Copy link
Author

Thanks for the help. That got me closer, but now I'm getting Failed to make request: error decoding response body: missing field dark at line 1 column 675. I suspect it's related to their dark mode option in the checkout?

This is what my code looks like now. I'm going to poke around the library code to see if I can debug it.

async fn checkout_handler(State(ctx): State<AppContext>, auth: middleware::auth::JWT) -> Result<Response> {
    println!("Checking out");
    let settings = settings::Settings::from_json(&ctx.config.settings.clone().unwrap())?;
    let user = users::Model::find_by_pid(&ctx.db, &auth.claims.pid).await?;
    let store_id = settings.lemon_squeezy.store_id;
    let customer_id = user.id.to_string();
    let lemon_squeezy = lemonsqueezy::LemonSqueezy::new(settings.lemon_squeezy.api_key);
    let checkout = Checkout::build(lemon_squeezy);
    println!("Making checkout");
    let checkout = checkout
        .create(CreateCheckout {
            r#type: String::from("checkouts"),
            attributes: CreateCheckoutAttributes {
                ..Default::default()
            },
            relationships: Some(CreateCheckoutRelationships {
                store: lemonsqueezy::types::Data {
                    data: CreateCheckoutRelationShipData {
                        r#type: String::from("stores"),
                        id: settings.lemon_squeezy.store_id.to_string(),
                    },
                },
                variant: lemonsqueezy::types::Data {
                    data: CreateCheckoutRelationShipData {
                        r#type: String::from("variants"),
                        id: settings.lemon_squeezy.monthly.id.to_string()
                    },
                },
            }),
        }).await;
    match checkout {
        Ok(checkout) => {
            format::json(checkout)
        }
        Err(err) => {
            println!("Error creating checkout: {}", err);
            Err(Error::InternalServerError)
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants