Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
#1 handle cancel of paypal token
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Jul 2, 2019
1 parent dff7175 commit 14ce387
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 53 deletions.
9 changes: 5 additions & 4 deletions src/app/reservation/overview/overview.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ <h2 translate="reservation-page.title"></h2>
<div *ngIf="!reservationInfo.orderSummary.free">
<div class="page-header wMarginTop"><h2 translate="reservation-page.payment"></h2></div>

<div *ngIf="paymentMethodsCount(event) > 1">
<div *ngIf="paymentMethodsCount() > 1">
<div class="btn-group btn-group-toggle payment-method-selector wMarginBottom">
<label class="btn btn-outline-dark" [ngClass]="{'active': overviewForm.value.selectedPaymentMethod === 'CREDIT_CARD'}" *ngIf="event.activePaymentMethods['CREDIT_CARD']">
<input type="radio" formControlName="selectedPaymentMethod" [value]="'CREDIT_CARD'"><fa-icon [icon]="['fas', 'credit-card']"></fa-icon> {{'reservation-page.credit-card'|translate}}
Expand All @@ -32,8 +32,8 @@ <h2 translate="reservation-page.title"></h2>
</label>
</div>
</div>
<div *ngIf="paymentMethodsCount(event) === 1">
<h4 class="wMarginTop" [ngSwitch]="getSinglePaymentMethod(event)">
<div *ngIf="paymentMethodsCount() === 1">
<h4 class="wMarginTop" [ngSwitch]="getSinglePaymentMethod()">
<span *ngSwitchCase="'CREDIT_CARD'">
<fa-icon [icon]="['fas', 'credit-card']"></fa-icon> {{'reservation-page.credit-card'|translate}}
</span>
Expand Down Expand Up @@ -110,7 +110,8 @@ <h4 class="wMarginTop" [ngSwitch]="getSinglePaymentMethod(event)">
</button>
</div>
<div class="col-md-5 order-0 col-12 ">
<button type="button" class="block-button btn btn-outline-dark" (click)="back()" translate="common.back"></button>
<button type="button" class="block-button btn btn-outline-dark" (click)="back()" translate="common.back" *ngIf="!reservationInfo.tokenAcquired"></button>
<button type="button" class="block-button btn btn-outline-dark" (click)="clearToken()" translate="reservation-page-complete.cancel" *ngIf="reservationInfo.tokenAcquired"></button>
</div>
</div>
</form>
Expand Down
112 changes: 64 additions & 48 deletions src/app/reservation/overview/overview.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { ReservationService } from '../../shared/reservation.service';
import { FormBuilder, FormGroup } from '@angular/forms';
import { Event, PaymentMethod, PaymentProxy } from 'src/app/model/event';
import { Event, PaymentMethod, PaymentProxy, PaymentProxyWithParameters } from 'src/app/model/event';
import { EventService } from 'src/app/shared/event.service';
import { ReservationInfo } from 'src/app/model/reservation-info';
import { PaymentProvider, SimplePaymentProvider } from 'src/app/payment/payment-provider';
Expand Down Expand Up @@ -30,6 +30,8 @@ export class OverviewComponent implements OnInit {

selectedPaymentProvider: PaymentProvider;

activePaymentMethods: {[key in PaymentMethod]?: PaymentProxyWithParameters};

constructor(
private route: ActivatedRoute,
private router: Router,
Expand All @@ -49,73 +51,81 @@ export class OverviewComponent implements OnInit {
this.eventService.getEvent(this.eventShortName).subscribe(ev => {
this.event = ev;

this.activePaymentMethods = this.event.activePaymentMethods;

this.i18nService.setPageTitle('reservation-page.header.title', ev.displayName);

this.reservationService.getReservationInfo(this.eventShortName, this.reservationId).subscribe(resInfo => {
this.reservationInfo = resInfo;
this.loadReservation(ev);

let paymentProxy : PaymentProxy = null;
let selectedPaymentMethod: PaymentMethod = null;
this.analytics.pageView(ev.analyticsConfiguration);
});
});
}

if (!resInfo.orderSummary.free && this.paymentMethodsCount(ev) === 1) {
selectedPaymentMethod = this.getSinglePaymentMethod(ev);
paymentProxy = ev.activePaymentMethods[selectedPaymentMethod].paymentProxy;
}

if (resInfo.orderSummary.free) {
selectedPaymentMethod = 'NONE';
this.selectedPaymentProvider = new SimplePaymentProvider();
}
loadReservation(ev: Event) {
this.reservationService.getReservationInfo(this.eventShortName, this.reservationId).subscribe(resInfo => {
this.reservationInfo = resInfo;

//
if (this.reservationInfo.tokenAcquired) {
paymentProxy = this.reservationInfo.paymentProxy;
selectedPaymentMethod = this.getPaymentMethodMatchingProxy(ev, paymentProxy);

// we override and keep only the one selected
//TODO: kinda ugly, but it works
let paymentProxyAndParam = this.event.activePaymentMethods[selectedPaymentMethod];
this.event.activePaymentMethods = {};
this.event.activePaymentMethods[selectedPaymentMethod] = paymentProxyAndParam
//
}
//

this.overviewForm = this.formBuilder.group({
termAndConditionsAccepted: null,
privacyPolicyAccepted: null,
selectedPaymentMethod: selectedPaymentMethod, //<- note: not used by the backend
paymentMethod: paymentProxy, //<- name mismatch for legacy reasons
gatewayToken: null
});

// we synchronize the selectedPaymentMethod with the corresponding paymentMethod (which is a payment proxy)
this.overviewForm.get('selectedPaymentMethod').valueChanges.subscribe(v => {
this.overviewForm.get('paymentMethod').setValue(ev.activePaymentMethods[v as PaymentMethod].paymentProxy);
});
});
let paymentProxy : PaymentProxy = null;
let selectedPaymentMethod: PaymentMethod = null;

this.analytics.pageView(ev.analyticsConfiguration);
if (!resInfo.orderSummary.free && this.paymentMethodsCount() === 1) {
selectedPaymentMethod = this.getSinglePaymentMethod();
paymentProxy = ev.activePaymentMethods[selectedPaymentMethod].paymentProxy;
}

if (resInfo.orderSummary.free) {
selectedPaymentMethod = 'NONE';
this.selectedPaymentProvider = new SimplePaymentProvider();
}

//
if (this.reservationInfo.tokenAcquired) {
paymentProxy = this.reservationInfo.paymentProxy;
selectedPaymentMethod = this.getPaymentMethodMatchingProxy(paymentProxy);

// we override and keep only the one selected
let paymentProxyAndParam = this.event.activePaymentMethods[selectedPaymentMethod];
this.activePaymentMethods = {};
this.activePaymentMethods[selectedPaymentMethod] = paymentProxyAndParam
//
} else {
this.activePaymentMethods = this.event.activePaymentMethods;
}
//

this.overviewForm = this.formBuilder.group({
termAndConditionsAccepted: null,
privacyPolicyAccepted: null,
selectedPaymentMethod: selectedPaymentMethod, //<- note: not used by the backend
paymentMethod: paymentProxy, //<- name mismatch for legacy reasons
gatewayToken: null
});

// we synchronize the selectedPaymentMethod with the corresponding paymentMethod (which is a payment proxy)
this.overviewForm.get('selectedPaymentMethod').valueChanges.subscribe(v => {
this.overviewForm.get('paymentMethod').setValue(ev.activePaymentMethods[v as PaymentMethod].paymentProxy);
});
});
}

paymentMethodsCount(event: Event) : number {
return Object.keys(event.activePaymentMethods).length;
paymentMethodsCount() : number {
return Object.keys(this.activePaymentMethods).length;
}

getPaymentMethodMatchingProxy(event: Event, paymentProxy: PaymentProxy) : PaymentMethod | null {
let keys: PaymentMethod[] = Object.keys(event.activePaymentMethods) as PaymentMethod[];
private getPaymentMethodMatchingProxy(paymentProxy: PaymentProxy) : PaymentMethod | null {
let keys: PaymentMethod[] = Object.keys(this.activePaymentMethods) as PaymentMethod[];
for(let idx in keys) {
if(event.activePaymentMethods[keys[idx]].paymentProxy === paymentProxy) {
if(this.activePaymentMethods[keys[idx]].paymentProxy === paymentProxy) {
return keys[idx];
}
}
return null;
}

getSinglePaymentMethod(event: Event) : PaymentMethod {
return (Object.keys(event.activePaymentMethods) as PaymentMethod[])[0];
getSinglePaymentMethod() : PaymentMethod {
return (Object.keys(this.activePaymentMethods) as PaymentMethod[])[0];
}

back() {
Expand Down Expand Up @@ -178,4 +188,10 @@ export class OverviewComponent implements OnInit {
registerCurrentPaymentProvider(paymentProvider: PaymentProvider) {
this.selectedPaymentProvider = paymentProvider;
}

clearToken(): void {
this.reservationService.removePaymentToken(this.eventShortName, this.reservationId).subscribe(r => {
this.loadReservation(this.event);
});
}
}
6 changes: 5 additions & 1 deletion src/app/shared/reservation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,9 @@ export class ReservationService {
getPaymentStatus(eventShortName: string, reservationId: string): Observable<ReservationPaymentResult> {
return this.http.get<ReservationPaymentResult>(`/api/v2/public/event/${eventShortName}/reservation/${reservationId}/payment/CREDIT_CARD/status`);
}


removePaymentToken(eventShortName: string, reservationId: string): Observable<boolean> {
return this.http.delete<boolean>(`/api/v2/public/event/${eventShortName}/reservation/${reservationId}/payment/token`)
}

}

0 comments on commit 14ce387

Please sign in to comment.