Added currency and paypal stuff

This commit is contained in:
2018-07-23 19:08:29 -04:00
parent 6b86486fe3
commit 0c34ce3502
5 changed files with 39 additions and 29 deletions

View File

@ -14,26 +14,18 @@
<tr *ngFor="let item of cart; let i = index">
<td>{{i + 1}}</td>
<td>
<a [routerLink]="['/products', item.item]">{{item.item}}</a>
<a [routerL ink]="['/products', item.item]">{{item.item}}</a>
</td>
<td>{{item.quantity}}</td>
<td>{{item.price | currency}}</td>
<td>
<mat-icon class="curs-pointer" (click)="remove(i)">delete</mat-icon>
<td>{{item.currency}} {{item.price | currency}}</td>
<td (click)="remove(i)" style="width: 50px">
<button mat-icon-button>
<mat-icon>delete</mat-icon>
</button>
</td>
</tr>
</tbody>
</table>
<div class="float-right">
<table>
<tr>
<td>
<strong>Sub Total:</strong>
</td>
<td class="pl-3">{{total() | currency}}</td>
</tr>
</table>
<button mat-raised-button class="float-right mt-3" color="primary" (click)="checkout()" [disabled]="total() <= 0">Checkout</button>
</div>
<div id="paypal-button" class="float-right mt-3"></div>
</mat-card>
</div>

View File

@ -10,7 +10,7 @@ import {Router} from '../../../../node_modules/@angular/router';
})
export class CartComponent {
@LocalStorage({defaultValue: []})
cart: {id: string; item: string; price: number; quantity: number}[];
cart: {id: string; item: string; price: number; curency: 'CAD' | 'USD'; quantity: number}[];
address1: string;
address2: string;
@ -20,17 +20,32 @@ export class CartComponent {
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;
ngOnInit() {
if (this.cart.length > 0) {
window['paypal'].Button.render(
{
env: 'sandbox',
client: {
sandbox: 'AejQ8-4hWWWhg1gYKbcuimT8Nf6-wutpEfYBHDDXiEXdujwJzHt6szwtmXBe2d3zW9d3khb3TgQBZUUJ',
live: 'AUhKVWkqvpzRBg0n_IFPMNi9QAl4JCXuWzc04BERDpBdG5ixFH1SimU85I9YSaksqKNCFjp_fOd4OAdd'
},
style: {size: 'medium', color: 'blue', shape: 'pill'},
payment: function(data, actions) {
return actions.payment.create({transactions: [{amount: {total: 0.01, currency: 'CAD'}}]});
},
onAuthorize: function(data, actions) {
return actions.payment.execute().then(function() {
window.alert('Thank you for your purchase!');
});
}
},
'#paypal-button'
);
}
}
remove(i: number) {
console.log('fire');
let c = this.cart;
c.splice(i, 1);
this.cart = c;