Payment Element
The Payment Element is an embeddable component for securely collecting payment details. The Payment Element supports dozens of payment methods with a single integration. Whether you’re just collecting card payments or dozens of payment methods, the Payment Element is the easiest way to build an embedded and customized payments experience.
If you want more information about what the Payment Element and how to use it, you can visit official Stripe docs.
In this guide we are going to focus on how to use the Payment Element with Angular using ngx-stripe As we any other Stripe Element, ngx-stripe tries to provide a similar API. Here as an example on how to collection card payments using the Payment Element
payment-element.component.ts
payment-element.component.html
server.js
1
import { Component, OnInit, ViewChild } from '@angular/core';
2
import { FormBuilder, Validators } from '@angular/forms';
3
import { HttpClient } from '@angular/common/http';
4
import { Observable } from 'rxjs';
5
6
import { StripeService, StripePaymentElementComponent } from 'ngx-stripe';
7
import {
8
StripeElementsOptions,
9
PaymentIntent
10
} from '@stripe/stripe-js';
11
12
@Component({
13
selector: 'app-test-06',
14
templateUrl: './payment-element.component.html'
15
})
16
export class Test06Component implements OnInit {
17
@ViewChild(StripePaymentElementComponent)
18
paymentElement: StripePaymentElementComponent;
19
20
stripeTest = this.fb.group({
21
name: ['Angular v12', [Validators.required]],
22
amount: [1109, [Validators.required, Validators.pattern(/\d+/)]]
23
});
24
25
elementsOptions: StripeElementsOptions = {
26
locale: 'en'
27
};
28
29
paying = false;
30
31
constructor(
32
private http: HttpClient,
33
private fb: FormBuilder,
34
private stripeService: StripeService
35
) {}
36
37
ngOnInit() {
38
this.createPaymentIntent(this.stripeTest.get('amount').value)
39
.subscribe(pi => {
40
this.elementsOptions.clientSecret = pi.client_secret;
41
});
42
}
43
44
pay() {
45
if (this.stripeTest.valid) {
46
this.paying = true;
47
this.stripeService.confirmPayment({
48
elements: this.paymentElement.elements,
49
confirmParams: {
50
payment_method_data: {
51
billing_details: {
52
name: this.stripeTest.get('name').value
53
}
54
}
55
},
56
redirect: 'if_required'
57
}).subscribe(result => {
58
this.paying = false;
59
console.log('Result', result);
60
if (result.error) {
61
// Show error to your customer (e.g., insufficient funds)
62
alert({ success: false, error: result.error.message });
63
} else {
64
// The payment has been processed!
65
if (result.paymentIntent.status === 'succeeded') {
66
// Show a success message to your customer
67
alert({ success: true });
68
}
69
}
70
});
71
} else {
72
console.log(this.stripeTest);
73
}
74
}
75
76
private createPaymentIntent(amount: number): Observable<PaymentIntent> {
77
return this.http.post<PaymentIntent>(
78
`${env.apiUrl}/create-payment-intent`,
79
{ amount }
80
);
81
}
82
}
83
Copied!
1
<div [formGroup]="stripeTest">
2
<mat-form-field class="example-full-width" appearance="fill">
3
<input matInput placeholder="name" formControlName="name" />
4
</mat-form-field>
5
<mat-form-field class="example-full-width" appearance="fill">
6
<input matInput placeholder="amount" type="number" formControlName="amount" />
7
</mat-form-field>
8
<ng-container *ngIf="elementsOptions?.clientSecret as clientSecret">
9
<ngx-stripe-payment [clientSecret]="clientSecret">
10
</ngx-stripe-payment>
11
</ng-container>
12
<button (click)="pay()">PAY</button>
13
</div>
Copied!
1
const express = require("express");
2
const bodyParser = require("body-parser");
3
4
const PORT = 3000;
5
6
const Stripe = require("stripe");
7
const stripe = Stripe("***your-stripe-private-key***");
8
9
const app = express();
10
app.use(bodyParser.json());
11
app.post("/create-payment-intent", (req, res) => {
12
stripe.paymentIntents.create(
13
{
14
amount: parseInt(req.body.amount),
15
currency: "usd",
16
payment_method_types: ["card"],
17
},
18
function (err, paymentIntent) {
19
if (err) {
20
res.status(500).json(err.message);
21
} else {
22
res.status(201).json(paymentIntent);
23
}
24
}
25
);
26
});
27
28
app.listen(PORT, () => console.log(`Server ready on port ${PORT}`));
29
Copied!
You can also pass the elementsOptions directly to the component
Last modified 18d ago
Copy link