Angular and stripe Cart Checkout - error TS1005: 'finally' expected

45 views
Skip to first unread message

Fofa

unread,
Dec 3, 2019, 5:00:32 AM12/3/19
to Angular and AngularJS discussion
import { ComponentOnInit } from '@angular/core';

import { environment } from '../../environments/environment';
import { DataService } from '../data.service';
import { RestApiService } from '../rest-api.service';
import { Router } from '@angular/router';


@Component({
  selector: 'app-cart',
  templateUrl: './cart.component.html',
  styleUrls: ['./cart.component.scss']
})
export class CartComponent implements OnInit {
  btnDisabled = false;
  handlerany;

  quantities: [];

  constructor(
    private dataDataService,
    private restRestApiService,
    private routerRouter
  ) { }

  trackByCartItems(indexnumberitemany) {
    return item._id;
  }

  get cartItems() {
    return this.data.getCart();
  }

  get cartTotal() {
    let total = 0;
    this.cartItems.forEach((dataindex=> {
      total += data['price'] * this.quantities[index];
    });
    return total;
  }

  removeProduct(indexproduct) {
    this.quantities.splice(index1);
    this.data.removeFromCart(product);
  }

  ngOnInit() {
    this.cartItems.forEach(data => {
      this.quantities.push[(1)];
    });
    this.handler = StripeCheckout.configure({
      key: environment.stripeKey,
      image: 'assets/img/favicon.png',
      locale: 'auto',
      token: async stripeToken => {
        let products;
        products = [];
        this.cartItems.forEach((dindex=> {
          products.push({
            product: d['_id'],
            quantity: this.quantities[index]
          });
        });

        try {
          const data = await this.rest.post(
            'http://localhost:5000/api/payment',
            {
              totalPrice: this.cartTotal,
              products,
              stripeToken
            }
          );
          data['success']
            ? (this.data.clearCart(), this.data.success('Purchase Successful'))
            : this.data.error(data['message']);
        } catch (error) {
          this.data.error(error['message']);
        }
      }
    });
  }


  validate() {
    if (!this.quantities.every(data => data > 0)) {
      this.data.warning('Quantity cannot be less than one');
    } else if (!localStorage.getItem('token')) {
      this.router.navigate(['/login'])
      .then(() => {
        this.data.warning('You need to login before making a purchase');
      });
    } else if (!this.data.user['address']) {
      this.router.navigate(['/profile/address'])
      .then(() => {
        this.data.warning('YOU need to login before making a purchase');
      });
    } else {
      this.data.message = '';
      return true;
    }
  }
  checkout() {
    this.btnDisabled = true;
    try {
      if (this.validate()) {
        this.handler.open({
          name: 'ProductUploads',
          description: 'Checkout Payment',
          amount: this.cartTotal * 100,
          closed: () => {
            this.btnDisabled = false;
          }
        });
      } else {
        this.btnDisabled = false;
        }
    }
  }

}


Reply all
Reply to author
Forward
0 new messages