0

I need to pull in my Stripe keys from my Laravel .env file and pass it into my Vue component. I read some similar questions on SO and the Laravel docs that mention doing it by simply adding the MIX prefix, and I can call process.env.MIX_STRIPE_KEY anywhere in my JS.

.env

STRIPE_KEY=pk_test_xxxxxxxxxxxxxxxxxx
STRIPE_SECRET=sk_test_xxxxxxxxxxxxxxxxxxx

MIX_STRIPE_KEY="${STRIPE_KEY}"
MIX_STRIPE_SECRET="${STRIPE_SECRET}"

In my Vue Component:

<template>
{{ stripe }} // Renders process.env.MIX_STRIPE_KEY (Not the actual key?)
...code
</template>
<script>
export default {
    data: function() {
        return {
            stripe: Stripe('process.env.MIX_STRIPE_KEY'),
...

After I did this I recompiled by npm run dev, production, watch tried all of them... Still not getting the Stripe key to show in the app.js file.

Speaking of the app.js file. I tried adding it in there as well.

const app = new Vue({
    el: '#app',
 data:{
        stripeKey: 'process.env.MIX_STRIPE_KEY',
 },

Then tried calling {{ stripKey }} in the Vue component, but that didn't render correctly either.

Any help on this would be appreciated.

4
  • 1
    If the component is in blade file, I would try to set the value to a "prop" defined in the element. As follow: <my-element :data="{{ env('MIX_STRIPE_KEY') }}"></my-element> Commented Sep 20, 2019 at 4:11
  • The component is in a blade file. Doing as you mentioned, how would you use the data in the Vue file? Commented Sep 20, 2019 at 4:23
  • 1
    Just above the "data" function, define an array called "props" with the "prop" as needed as follows: props: ['stripe'], then in Blade: <my-element :stripe="{{ env('MIX_STRIPE_KEY') }}"></my-element> Commented Sep 20, 2019 at 4:50
  • @AlexandreBarbosa This solution works great as well and might be best for my specific case at the moment. Appreciate your time and help! Commented Sep 20, 2019 at 4:59

2 Answers 2

1

You need to remove the quotes. Webpack will interpolate the value for you:

<template>
{{ stripe }} // Renders process.env.MIX_STRIPE_KEY (Not the actual key?)
...code
</template>
<script>
export default {
    data: function() {
        return {
            stripe: Stripe(process.env.MIX_STRIPE_KEY),
...

Will compile as:

<template>
{{ stripe }} // Renders process.env.MIX_STRIPE_KEY (Not the actual key?)
...code
</template>
<script>
export default {
    data: function() {
        return {
            stripe: Stripe("pk_test_xxxxxxxxxxxxxxxxxx"),
...
Sign up to request clarification or add additional context in comments.

5 Comments

🤦‍♂️ Oh Jeez, thank you, that worked! If you wouldn't mind me asking one more... How/Where would I use the if else statement to use test keys in dev and production keys in production? Similar to this if ( process.env.Mix_APP_ENV == 'production' ) { use production keys } else { use test keys }
hehe, no worries, happy to help! Webpack will similarly swap out process.env.NODE_ENV with production when building in production mode. So Stripe(process.env.NODE_ENV === 'production' ? process.env.MIX_PROD_KEY : process.env.MIX_DEV_KEY) should work.
Thanks for the follow up. Trying to wrap my head around that. Where would you put that statement and should I add both Dev and Production keys to my .env file since Webpack has to compile for it to work?
It really depends on your use case. Behind the scenes, mix uses the webpack define plugin (webpack.js.org/plugins/environment-plugin) and filters out everything from .env that isn't prefixed with MIX_. Where you place that is up to you. There are some good suggestions here: stackoverflow.com/questions/30030031/… I really like the alias config so you can actually separate those in different files to be included based on NODE_ENV
Thank you for all the help. This is good info. Appreciate it!
0

Note for future readers: do not place your STRIPE_SECRET into Mix as this may make it exposed to the browser and any user that decides to snoop in it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.