Working cart

This commit is contained in:
Zakary Timson 2018-07-22 19:52:58 -04:00
parent b0019c0c4b
commit 88dd66188e
4 changed files with 17 additions and 8 deletions

View File

@ -33,7 +33,6 @@ export const checkout = functions.https.onRequest((request, response) => {
// Fill in information from DB // Fill in information from DB
let promises = []; let promises = [];
console.log(request.body);
let cart = request.body.cart.filter(row => row.quantity > 0); let cart = request.body.cart.filter(row => row.quantity > 0);
cart.forEach(async row => cart.forEach(async row =>
promises.push( promises.push(
@ -47,19 +46,15 @@ export const checkout = functions.https.onRequest((request, response) => {
let products = await Promise.all(promises); let products = await Promise.all(promises);
req.transactions[0].item_list.items = products.map((row, i) => { req.transactions[0].item_list.items = products.map((row, i) => {
const data = row.data(); const data = row.data();
console.log(data);
return {name: data.name, sku: data.name, price: data.price, currency: 'CAD', quantity: cart[i].quantity}; return {name: data.name, sku: data.name, price: data.price, currency: 'CAD', quantity: cart[i].quantity};
}); });
req.transactions[0].amount.total = req.transactions[0].item_list.items.reduce((acc, row, i) => { req.transactions[0].amount.total = req.transactions[0].item_list.items.reduce((acc, row, i) => {
return acc + row.price * row.quantity; return acc + row.price * row.quantity;
}, 0); }, 0);
console.log(req);
// Send request to PayPal // Send request to PayPal
let create = new Promise((res, rej) => { let create = new Promise((res, rej) => {
paypal.payment.create(req, (error, payment) => { paypal.payment.create(req, (error, payment) => {
console.log(error, payment);
if (error) rej(error); if (error) rej(error);
let link = payment.links.filter(row => row.rel == 'approval_url').map(row => row.href)[0]; let link = payment.links.filter(row => row.rel == 'approval_url').map(row => row.href)[0];
@ -73,7 +68,7 @@ export const checkout = functions.https.onRequest((request, response) => {
}); });
try { try {
response.send(await create); response.json({url: await create});
} catch (err) { } catch (err) {
console.error(err); console.error(err);
response.status(500); response.status(500);

View File

@ -26,6 +26,7 @@ import {ProductsComponent} from './store/products/products.component';
import {CartComponent} from './store/cart/cart.component'; import {CartComponent} from './store/cart/cart.component';
import {ViewComponents} from './formulaManager/viewComponents/viewComponents.component'; import {ViewComponents} from './formulaManager/viewComponents/viewComponents.component';
import {NewComponentComponent} from './formulaManager/newComponent/newComponent.component'; import {NewComponentComponent} from './formulaManager/newComponent/newComponent.component';
import {HttpModule} from '@angular/http';
@NgModule({ @NgModule({
declarations: [ declarations: [
@ -55,6 +56,7 @@ import {NewComponentComponent} from './formulaManager/newComponent/newComponent.
BrowserAnimationsModule, BrowserAnimationsModule,
BrowserModule, BrowserModule,
FormsModule, FormsModule,
HttpModule,
NgxElectronModule, NgxElectronModule,
ReactiveFormsModule, ReactiveFormsModule,
RouterModule.forRoot([ RouterModule.forRoot([

View File

@ -33,7 +33,7 @@
<td class="pl-3">{{total() | currency}}</td> <td class="pl-3">{{total() | currency}}</td>
</tr> </tr>
</table> </table>
<button mat-raised-button class="float-right mt-3" color="primary">Checkout</button> <button mat-raised-button class="float-right mt-3" color="primary" (click)="checkout()">Checkout</button>
</div> </div>
</mat-card> </mat-card>
</div> </div>

View File

@ -1,6 +1,8 @@
import {Component} from '@angular/core'; import {Component} from '@angular/core';
import {LocalStorage} from 'webstorage-decorators'; import {LocalStorage} from 'webstorage-decorators';
import {access} from 'fs'; import {access} from 'fs';
import {Http} from '../../../../node_modules/@angular/http';
import {Router} from '../../../../node_modules/@angular/router';
@Component({ @Component({
selector: 'cart', selector: 'cart',
@ -16,7 +18,17 @@ export class CartComponent {
province: string; province: string;
postal: string; postal: string;
constructor() {} constructor(private http: Http, private router: Router) {}
async checkout() {
let cart = this.cart.map(row => {
return {id: row.id, quantity: row.quantity};
});
let link = await this.http
.post('https://us-central1-fhsons-7e90b.cloudfunctions.net/checkout', {cart: cart})
.toPromise();
window.location.href = link.url;
}
remove(i: number) { remove(i: number) {
let c = this.cart; let c = this.cart;