src/app/stripe-payment/stripe-payment.component.ts
OnInit
selector | app-stripe-payment |
styleUrls | ./stripe-payment.component.css |
templateUrl | ./stripe-payment.component.html |
Properties |
Methods |
Accessors |
constructor(http: HttpClient, formBuilder: FormBuilder, router: Router, authService: AuthService)
|
|||||||||||||||
Parameters :
|
completeOrder |
completeOrder()
|
Returns :
void
|
getOrderData |
getOrderData()
|
Returns :
Order
|
ngOnInit |
ngOnInit()
|
Returns :
void
|
onSubmit |
onSubmit()
|
Returns :
void
|
placeOrder |
placeOrder()
|
Returns :
void
|
address |
Type : string
|
error |
Type : string
|
orderId |
Type : string
|
paymentForm |
Type : FormGroup
|
phone |
Type : string
|
selectedPaymentMethod |
Type : "online" | "cash-on-delivery"
|
Default value : 'online'
|
userId |
Type : string | any
|
formControls |
getformControls()
|
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { AuthService } from '../auth.service';
export interface Cart {
_id: string;
user: string;
items: CartItem[];
}
export interface CartItem {
menuItem: {
name: string;
price: number;
};
quantity: number;
}
export interface Order {
_id: string;
user: string;
cart: string;
status: 'Pending' | 'Processing' | 'Shipped' | 'Delivered';
createdAt: Date;
paymentMethod: string;
address: string;
phone: string;
}
@Component({
selector: 'app-stripe-payment',
templateUrl: './stripe-payment.component.html',
styleUrls: ['./stripe-payment.component.css']
})
export class StripePaymentComponent implements OnInit {
paymentForm: FormGroup;
error: string;
orderId!: string;
userId!: string |any;
address: string;
phone: string;
selectedPaymentMethod: 'online'|'cash-on-delivery' = 'online';
constructor(private http: HttpClient, private formBuilder: FormBuilder,private router: Router,private authService: AuthService) { }
ngOnInit() {
this.userId = this.authService.getUserId();
this.paymentForm = this.formBuilder.group({
name: ['', Validators.required],
cardNumber: ['', Validators.required],
expirationMonth: ['', Validators.required],
expirationYear: ['', Validators.required],
cvc: ['', Validators.required],
address: ['', Validators.required],
phone: ['', Validators.required],
selectedPaymentMethod: ['', Validators.required],
});
}
get formControls() {
return this.paymentForm.controls;
}
onSubmit() {
if (this.paymentForm.invalid) {
return;
}
const paymentData = {
name: this.paymentForm.value.name,
cardNumber: this.paymentForm.value.cardNumber,
expirationMonth: this.paymentForm.value.expirationMonth,
expirationYear: this.paymentForm.value.expirationYear,
cvc: this.paymentForm.value.cvc,
address: this.paymentForm.value.address, // Updated line
phone: this.paymentForm.value.phone, // Updated line
selectedPaymentMethod: this.paymentForm.value.selectedPaymentMethod, // Get the selected value from the form
};
this.http.post('http://localhost:3000/payment', paymentData)
.subscribe(
(response) => {
// Handle success response
console.log(response);
alert('payment done');
this.placeOrder();
},
(error) => {
// Handle error response
this.error = error.message;
}
);
}
placeOrder() {
this.completeOrder();
}
completeOrder(): void {
const orderData = this.getOrderData();
this.http.post<Order>('http://localhost:3000/orders', orderData).subscribe(
(response) => {
console.log(response);
console.log('Order placed successfully');
alert('Order placed successfully');
localStorage.setItem('orderId', response._id);
this.orderId = response._id;
this.router.navigate(['/orders', this.userId, this.orderId]);
},
(error) => {
console.error('Error placing order:', error);
}
);
}
getOrderData(): Order {
return {
_id: '',
user: this.userId, // Assign the value of the user ID
cart: '', // Assign the value of the cart ID if necessary
status: 'Pending',
createdAt: new Date(),
paymentMethod: this.selectedPaymentMethod,
address: this.paymentForm.value.address, // Use the value from the form control
phone: this.paymentForm.value.phone, // Use the value from the form control
};
}
}
<h2>Stripe Payment</h2>
<form [formGroup]="paymentForm" (ngSubmit)="onSubmit()">
<div class="form-group">
<label for="name">Name on Card</label>
<input type="text" class="form-control" id="name" formControlName="name">
<div *ngIf="paymentForm.controls['name'].touched && paymentForm.controls['name'].invalid" class="error">
Name is required.
</div>
</div>
<div class="form-group">
<label for="cardNumber">Card Number</label>
<input type="text" class="form-control" id="cardNumber" formControlName="cardNumber">
<div *ngIf="paymentForm.controls['cardNumber'].touched && paymentForm.controls['cardNumber'].invalid" class="error">
Card number is required.
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="expirationMonth">Expiration Month</label>
<input type="text" class="form-control" id="expirationMonth" formControlName="expirationMonth">
<div *ngIf="paymentForm.controls['expirationMonth'].touched && paymentForm.controls['expirationMonth'].invalid" class="error">
Expiration month is required.
</div>
</div>
<div class="form-group col-md-6">
<label for="expirationYear">Expiration Year</label>
<input type="text" class="form-control" id="expirationYear" formControlName="expirationYear">
<div *ngIf="paymentForm.controls['expirationYear'].touched && paymentForm.controls['expirationYear'].invalid" class="error">
Expiration year is required.
</div>
</div>
</div>
<div class="form-group">
<label for="cvc">CVC</label>
<input type="text" class="form-control" id="cvc" formControlName="cvc">
<div *ngIf="paymentForm.controls['cvc'].touched && paymentForm.controls['cvc'].invalid" class="error">
CVC is required.
</div>
</div>
<div class="form-group">
<label for="address">Enter Delivery Address:</label>
<input type="text" id="address" formControlName="address" class="form-control">
</div>
<label for="phone">Phone Number:</label>
<input type="text" id="phone" formControlName="phone" name="phone" class="form-control">
<label for="paymentMethod">Payment Method:</label>
<select id="paymentMethod" formControlName="selectedPaymentMethod" name="paymentMethod" class="form-control">
<option value="online">Online Payment</option>
<option value="cash-on-delivery">Cash on Delivery</option>
</select>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<div *ngIf="error" class="error">
{{ error }}
</div>
./stripe-payment.component.css