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

Implement Default Trait for CreatePaymentIntent Struct #639

Open
matfndt opened this issue Nov 15, 2024 · 4 comments
Open

Implement Default Trait for CreatePaymentIntent Struct #639

matfndt opened this issue Nov 15, 2024 · 4 comments

Comments

@matfndt
Copy link

matfndt commented Nov 15, 2024

Is your feature request related to a problem? Please describe.

I am currently using this crate to implement a pre-authorization payment which requires some tweaking of the createPaymentIntent struct. I am following the instructions in the stripe docs on "Place a hold on a payment method". The issue is more about improving ergonomics rather than adding new functionality. As of my knowledge for my use case I need to initialize the struct with all fields which is quite tedious.

Describe the solution you'd like

I would implement the Default trait for CreatePaymentIntent to simplify the initialization process to allow only using subsets of the fields.

let params = CreatePaymentIntent {
        amount: 123, 
        currency: Currency::EUR,
        payment_method_types: Some(vec!["card".to_string()]),
        capture_method: Some(PaymentIntentCaptureMethod::Manual), // this is required for pre-auth
        metadata: Some(metadata),
        ..Default::default() // this doesn't work currently
    };

So some implementation such as this:

impl<'a> Default for CreatePaymentIntent<'a> {
    fn default() -> Self {
        CreatePaymentIntent {
            amount: 123, // some default value
            currency: Currency::USD, // some currency 
            // Add all other optional fields as None
        }
    }
}

Describe alternatives you've considered

No response

Additional context

No response

@dyc3
Copy link

dyc3 commented Nov 18, 2024

Have you considered just using CreatePaymentIntent::new and then modifying it?

let mut params = CreatePaymentIntent::new(amount, currency);
params.payment_method = ...
params.customer = ...
params.confirm = ...
params.off_session = ...

@matfndt
Copy link
Author

matfndt commented Nov 28, 2024

Have you considered just using CreatePaymentIntent::new and then modifying it?

let mut params = CreatePaymentIntent::new(amount, currency);
params.payment_method = ...
params.customer = ...
params.confirm = ...
params.off_session = ...

Yes I did. I am not a big fan of having to mutate the instance. I know this works but I want to make it very clear that there is nothing changed to the payment intent in the creation. So it is more ergonomically but in my eyes not cleanly written. Thanks for the help though :)

@dyc3
Copy link

dyc3 commented Dec 7, 2024

You could also try something like this to get the ergonomics you want:

let params  = {
  let mut params = CreatePaymentIntent::new(amount, currency);
  params.payment_method = ...
  params.customer = ...
  params.confirm = ...
  params.off_session = ...
  params
};

This way the mutability is limited only to the scope of your initialization.

@matfndt
Copy link
Author

matfndt commented Dec 7, 2024

You could also try something like this to get the ergonomics you want:

let params  = {

  let mut params = CreatePaymentIntent::new(amount, currency);

  params.payment_method = ...

  params.customer = ...

  params.confirm = ...

  params.off_session = ...

  params

};

This way the mutability is limited only to the scope of your initialization.

Thanks! I really like that idea of using scope and name aliasing. Never thought if that but really cool idea. Definitely going to use that in other code too.

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

No branches or pull requests

2 participants