Using Stripe with Angular 5

2019-05-07 15:12发布

I have an angular application that uses Stripe to save customer payment card info.

I include this script in my index.html

<script src="https://js.stripe.com/v3/"></script>

this script provides a "Stripe" object that I can use like this:

<script> var stripe = Stripe('pk_XXXXXXXXXXX') </script>

the question is: How can I access the Stripe object from my angular typescript code?

2条回答
做自己的国王
2楼-- · 2019-05-07 15:40

Try to use

npm i stripe-payment-component

for angular typescript version.

查看更多
仙女界的扛把子
3楼-- · 2019-05-07 15:54

Register the type in typings.d.ts

declare var Stripe: any;

Instantiate a Stripe object from a Service

import { Injectable } from '@angular/core';

@Injectable()
export class PaymentService {

  stripe = Stripe('pk_test_XXXXX');

}

Now you can inject this service into your components and interact with elements, but make sure to use AfterViewInit.

export class SomeComponent implements AfterViewInit {

  elements: any;

  constructor(private pmt: PaymentService) 

  ngAfterViewInit() {
    // initalize elements
    this.elements = this.pmt.stripe.elements()
  }

}
查看更多
登录 后发表回答