2018-06-28 23:17:14 +02:00
|
|
|
import {
|
|
|
|
Component,
|
2018-07-03 15:27:59 +02:00
|
|
|
OnInit,
|
2018-06-29 04:27:32 +02:00
|
|
|
ViewChild,
|
2018-06-28 23:17:14 +02:00
|
|
|
} from '@angular/core';
|
2018-07-03 15:27:59 +02:00
|
|
|
import { Router } from '@angular/router';
|
2018-06-28 23:17:14 +02:00
|
|
|
|
2018-06-29 05:05:49 +02:00
|
|
|
import { ToasterService } from 'angular2-toaster';
|
|
|
|
import { Angulartics2 } from 'angulartics2';
|
|
|
|
|
|
|
|
import { ApiService } from 'jslib/abstractions/api.service';
|
2018-06-28 23:17:14 +02:00
|
|
|
import { I18nService } from 'jslib/abstractions/i18n.service';
|
2018-07-03 15:27:59 +02:00
|
|
|
import { MessagingService } from 'jslib/abstractions/messaging.service';
|
2018-07-02 16:30:51 +02:00
|
|
|
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
|
2018-07-05 14:39:22 +02:00
|
|
|
import { SyncService } from 'jslib/abstractions/sync.service';
|
2018-07-03 15:27:59 +02:00
|
|
|
import { TokenService } from 'jslib/abstractions/token.service';
|
2018-08-31 23:42:19 +02:00
|
|
|
import { UserService } from 'jslib/abstractions/user.service';
|
2018-06-28 23:17:14 +02:00
|
|
|
|
2018-06-29 04:27:32 +02:00
|
|
|
import { PaymentComponent } from './payment.component';
|
|
|
|
|
2018-06-28 23:17:14 +02:00
|
|
|
@Component({
|
|
|
|
selector: 'app-premium',
|
|
|
|
templateUrl: 'premium.component.html',
|
|
|
|
})
|
2018-07-03 15:27:59 +02:00
|
|
|
export class PremiumComponent implements OnInit {
|
2018-06-29 04:27:32 +02:00
|
|
|
@ViewChild(PaymentComponent) paymentComponent: PaymentComponent;
|
|
|
|
|
2018-08-31 23:42:19 +02:00
|
|
|
canAccessPremium = false;
|
2018-07-02 16:30:51 +02:00
|
|
|
selfHosted = false;
|
2018-06-28 23:17:14 +02:00
|
|
|
premiumPrice = 10;
|
|
|
|
storageGbPrice = 4;
|
|
|
|
additionalStorage = 0;
|
|
|
|
|
2018-06-29 05:05:49 +02:00
|
|
|
formPromise: Promise<any>;
|
|
|
|
|
|
|
|
constructor(private apiService: ApiService, private i18nService: I18nService,
|
2018-07-02 16:30:51 +02:00
|
|
|
private analytics: Angulartics2, private toasterService: ToasterService,
|
2018-07-03 15:27:59 +02:00
|
|
|
platformUtilsService: PlatformUtilsService, private tokenService: TokenService,
|
2018-07-05 14:39:22 +02:00
|
|
|
private router: Router, private messagingService: MessagingService,
|
2018-08-31 23:42:19 +02:00
|
|
|
private syncService: SyncService, private userService: UserService) {
|
2018-07-02 16:30:51 +02:00
|
|
|
this.selfHosted = platformUtilsService.isSelfHost();
|
|
|
|
}
|
2018-06-28 23:17:14 +02:00
|
|
|
|
2018-07-03 15:27:59 +02:00
|
|
|
async ngOnInit() {
|
2018-08-31 23:42:19 +02:00
|
|
|
this.canAccessPremium = await this.userService.canAccessPremium();
|
2018-07-03 15:27:59 +02:00
|
|
|
const premium = await this.tokenService.getPremium();
|
|
|
|
if (premium) {
|
2019-02-18 21:28:23 +01:00
|
|
|
this.router.navigate(['/settings/subscription']);
|
2018-07-03 15:27:59 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-28 23:17:14 +02:00
|
|
|
async submit() {
|
2018-07-03 15:55:59 +02:00
|
|
|
let files: FileList = null;
|
|
|
|
if (this.selfHosted) {
|
|
|
|
const fileEl = document.getElementById('file') as HTMLInputElement;
|
|
|
|
files = fileEl.files;
|
|
|
|
if (files == null || files.length === 0) {
|
|
|
|
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
|
|
|
|
this.i18nService.t('selectFile'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-28 23:17:14 +02:00
|
|
|
try {
|
2018-07-03 15:55:59 +02:00
|
|
|
if (this.selfHosted) {
|
2018-07-20 04:04:16 +02:00
|
|
|
if (!this.tokenService.getEmailVerified()) {
|
|
|
|
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
|
|
|
|
this.i18nService.t('verifyEmailFirst'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-29 05:05:49 +02:00
|
|
|
const fd = new FormData();
|
2018-07-03 15:55:59 +02:00
|
|
|
fd.append('license', files[0]);
|
|
|
|
this.formPromise = this.apiService.postAccountLicense(fd).then(() => {
|
|
|
|
return this.finalizePremium();
|
|
|
|
});
|
|
|
|
} else {
|
2019-02-19 23:06:01 +01:00
|
|
|
this.formPromise = this.paymentComponent.createPaymentToken().then((result) => {
|
2018-07-03 15:55:59 +02:00
|
|
|
const fd = new FormData();
|
2019-02-19 23:06:01 +01:00
|
|
|
fd.append('paymentMethodType', result[1].toString());
|
2019-02-21 06:03:40 +01:00
|
|
|
if (result[0] != null) {
|
|
|
|
fd.append('paymentToken', result[0]);
|
|
|
|
}
|
2018-07-03 15:55:59 +02:00
|
|
|
fd.append('additionalStorageGb', (this.additionalStorage || 0).toString());
|
|
|
|
return this.apiService.postPremium(fd);
|
2019-08-10 05:57:30 +02:00
|
|
|
}).then((paymentResponse) => {
|
|
|
|
if (!paymentResponse.success && paymentResponse.paymentIntentClientSecret != null) {
|
|
|
|
return this.paymentComponent.handleStripeCardPayment(paymentResponse.paymentIntentClientSecret,
|
|
|
|
() => this.finalizePremium());
|
|
|
|
} else {
|
|
|
|
return this.finalizePremium();
|
|
|
|
}
|
2018-07-03 15:55:59 +02:00
|
|
|
});
|
|
|
|
}
|
2018-06-29 05:05:49 +02:00
|
|
|
await this.formPromise;
|
|
|
|
} catch { }
|
2018-06-28 23:17:14 +02:00
|
|
|
}
|
|
|
|
|
2018-07-02 16:30:51 +02:00
|
|
|
async finalizePremium() {
|
2018-08-08 19:44:01 +02:00
|
|
|
await this.apiService.refreshIdentityToken();
|
|
|
|
await this.syncService.fullSync(true);
|
2018-07-02 16:30:51 +02:00
|
|
|
this.analytics.eventTrack.next({ action: 'Signed Up Premium' });
|
|
|
|
this.toasterService.popAsync('success', null, this.i18nService.t('premiumUpdated'));
|
2018-07-03 15:27:59 +02:00
|
|
|
this.messagingService.send('purchasedPremium');
|
2019-02-18 21:28:23 +01:00
|
|
|
this.router.navigate(['/settings/subscription']);
|
2018-07-02 16:30:51 +02:00
|
|
|
}
|
|
|
|
|
2018-06-28 23:17:14 +02:00
|
|
|
get additionalStorageTotal(): number {
|
2019-03-21 18:11:40 +01:00
|
|
|
return this.storageGbPrice * Math.abs(this.additionalStorage || 0);
|
2018-06-28 23:17:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get total(): number {
|
|
|
|
return this.additionalStorageTotal + this.premiumPrice;
|
|
|
|
}
|
|
|
|
}
|