I’m implementing a checkout experience where the end user who is purchasing an item can choose to put in their credit card details and have the transaction handled by Stripe, or they can choose PayPal and use that instead.
So in my embedded_schema for checkout, I have an enum for the PaymentGateway like this:
defenum(PaymentGateway, stripe: 1, paypal: 2)
And then in the changeset I make sure the value is included with:
|> validate_inclusion(:payment_gateway, PaymentGateway.__valid_values__())
This is all working no problem.
But now for local testing I’d like to set up a new dummy payment gateway. I was thinking about going with something like this:
defenum(PaymentGateway, dummy: 0, stripe: 1, paypal: 2)
But of course I don’t want that dummy gateway being a valid option in production, however in development it would be really nice if I could pick that dummy gateway from my checkout form.
For the form itself, imagine a radio button where you can pick 1 of the 3 gateways.
Would it be common practice to only add this radio option to the template based on the value of
Mix.env not being production? And in a similar fashion, would I define the
defenum twice based on
Mix.env being production or not?
if Mix.env == :prod do defenum(PaymentGateway, stripe: 1, paypal: 2) else defenum(PaymentGateway, bogus: 0, stripe: 1, paypal: 2) end
Is there a better way to handle this? It feels dirty to me to check the mix env in a template because now suddenly it’s like I’m modifying the normal flow of my app just for dev and test, but I can’t think of a better way to do this.