7

In my angular app there is this module called Ngx-Stripe

I have defined it as documentation like following:

NgxStripeModule.forRoot('***your-stripe-publishable key***');

But the problem is I don't get this key on app bootstrap, I am not supposed to hardcode it in angular app.module or global like in index.html when I am using stripe withput any angular library.

I am getting this key on the api call after user proceeds to payment page. How can I define this key in this scenario ?

4
  • Was trying to achieve this some time ago... Unfortunately I was forced to set the data in a webpack or <script> in index.html before Angular's sources Commented Jan 29, 2018 at 14:03
  • HI @DanielKucal, I also tried within index.html, for that matter I dont even need ngx-stripe, I am able to do things just like that but is this not possible right or what is the scene like ? Commented Jan 29, 2018 at 14:08
  • I will try to explain it in an answer Commented Jan 29, 2018 at 14:21
  • @Rakeschand if you're interested in a better way of doing this as Samber suggested in another comment, take a look at this answer Commented Aug 21, 2022 at 7:28

3 Answers 3

2

I wish it'd be straightforward, but the only way I was able to achieve it was something like:

index.html (alternatively webpack-injected script), has to be placed before Angular's sources:

<script>
  var STRIPE_KEY = 'paste it here';
</script>

app.module.ts:

declare var STRIPE_KEY;

// ...
NgxStripeModule.forRoot(STRIPE_KEY);

The problem here is .forRoot() has to be statically-analyzed by Angular AoT compiler, so it can't accept what you want it to accept... How about setting the key after you got it via StripeService.changeKey(key: string) method?

Sign up to request clarification or add additional context in comments.

3 Comments

I understood that, but my requirement is something else. Let’s see if I can do that.
How about passing empty key at the beginning and then setting it by StripeService.changeKey() method in this case?
FYI, stripeService.changeKey() is only available in @nomadreservations/ngx-stripe fork.
1

Simply import StripeService from ngx-stripe and call changeKey on that service once you have your key.

Comments

0

For lazy loading your Stripe config key you can create your own instance of StripeService using StripeFactory that is available from ngx-stripe.

Check the documentation here for a full explanation:

https://richnologies.gitbook.io/ngx-stripe/core-concepts/service-factory

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.