Angular 快速入门
在应用启动时初始化该插件一次即可。独立的 Angular 应用可以使用provideAppInitializer功能,这样初始化过程可以在 Stripe 用户界面显示之前完成。
src/app/app.config.ts
import { ApplicationConfig, provideAppInitializer } from '@angular/core';
import { provideHttpClient } from '@angular/common/http';
import { Stripe } from '@capacitor-community/stripe';
export const appConfig: ApplicationConfig = {
providers: [
provideHttpClient(),
provideAppInitializer(() =>
Stripe.initialize({
publishableKey: 'Your Publishable Key',
}),
),
],
};
将监听器的注册结果保存在相同的启动路径中。详情请参阅事件监听器。
Web
请安装stripe-pwa-elements@^5.3.0模块,然后在 Angular 启动后调用defineCustomElements()函数。
npm install stripe-pwa-elements@^5.3.0
src/main.ts
import { bootstrapApplication } from '@angular/platform-browser';
import { defineCustomElements } from 'stripe-pwa-elements/loader';
import { AppComponent } from './app/app.component';
import { appConfig } from './app/app.config';
bootstrapApplication(AppComponent, appConfig)
.then(() => defineCustomElements(window))
.catch((err) => console.log(err));
将 HttpClient 以 http 的名称注入服务或组件;上面的 provideHttpClient() 会注册它。通过 Angular 的 HttpClient 获取 PaymentIntent 或 SetupIntent 客户端密钥时,请使用 firstValueFrom。通过拦截器或请求选项携带应用的身份凭据。下方 environment.api URL 仅用于演示响应格式;生产环境应使用自己的、要求身份验证的后端。请勿使用已移除的 toPromise() 辅助方法。
import { firstValueFrom } from 'rxjs';
const { paymentIntent, ephemeralKey, customer } = await firstValueFrom(
this.http.post<{
paymentIntent: string;
ephemeralKey: string;
customer: string;
}>(environment.api + 'intent', {}),
);