1
- import { Component , Input , OnInit } from '@angular/core' ;
1
+ import { Component , Inject , Input , OnInit } from '@angular/core' ;
2
2
import { Plan } from "fusio-sdk/dist/src/generated/consumer/Plan" ;
3
3
import { Message } from "fusio-sdk/dist/src/generated/consumer/Message" ;
4
4
import { ConsumerService } from "../../service/consumer.service" ;
5
5
import { LocationStrategy } from "@angular/common" ;
6
6
import { ErrorConverter } from "../../util/error-converter" ;
7
+ import { Config , FUSIO_CONFIG } from "../../config/config" ;
7
8
8
9
@Component ( {
9
10
selector : 'fusio-subscription' ,
@@ -12,32 +13,28 @@ import {ErrorConverter} from "../../util/error-converter";
12
13
} )
13
14
export class SubscriptionComponent implements OnInit {
14
15
15
- @Input ( )
16
- provider : string = 'stripe' ;
17
-
18
- @Input ( )
19
- currencyCode : string = 'USD' ;
20
-
21
- @Input ( )
22
- redirectPath : string = '/account' ;
23
-
16
+ currencyCode : string = 'EUR' ;
24
17
plans ?: Array < Plan >
25
18
response ?: Message ;
26
19
27
- constructor ( private consumer : ConsumerService , private location : LocationStrategy ) { }
20
+ constructor ( private consumer : ConsumerService , private location : LocationStrategy , @ Inject ( FUSIO_CONFIG ) private config : Config ) { }
28
21
29
22
async ngOnInit ( ) : Promise < void > {
30
23
const plan = await this . consumer . getClient ( ) . getConsumerPlan ( ) ;
31
24
const response = await plan . consumerActionPlanGetAll ( { count : 1024 } ) ;
32
25
this . plans = response . data . entry ;
26
+
27
+ if ( this . config . paymentCurrency ) {
28
+ this . currencyCode = this . config . paymentCurrency ;
29
+ }
33
30
}
34
31
35
32
async doBillingPortal ( ) {
36
33
try {
37
- const path = this . location . prepareExternalUrl ( this . redirectPath ) ;
34
+ const path = this . location . prepareExternalUrl ( this . getHomePath ( ) ) ;
38
35
const redirectUrl = location . origin + path ;
39
36
40
- const portal = await this . consumer . getClient ( ) . getConsumerPaymentByProviderPortal ( this . provider ) ;
37
+ const portal = await this . consumer . getClient ( ) . getConsumerPaymentByProviderPortal ( this . getPaymentProvider ( ) ) ;
41
38
const response = await portal . consumerActionPaymentPortal ( {
42
39
returnUrl : redirectUrl
43
40
} ) ;
@@ -54,10 +51,10 @@ export class SubscriptionComponent implements OnInit {
54
51
55
52
async doPurchase ( plan : Plan ) {
56
53
try {
57
- const path = this . location . prepareExternalUrl ( this . redirectPath ) ;
54
+ const path = this . location . prepareExternalUrl ( this . getHomePath ( ) ) ;
58
55
const redirectUrl = location . origin + path ;
59
56
60
- const checkout = await this . consumer . getClient ( ) . getConsumerPaymentByProviderCheckout ( this . provider ) ;
57
+ const checkout = await this . consumer . getClient ( ) . getConsumerPaymentByProviderCheckout ( this . getPaymentProvider ( ) ) ;
61
58
const response = await checkout . consumerActionPaymentCheckout ( {
62
59
planId : plan . id ,
63
60
returnUrl : redirectUrl ,
@@ -71,4 +68,20 @@ export class SubscriptionComponent implements OnInit {
71
68
}
72
69
}
73
70
71
+ private getHomePath ( ) : string {
72
+ if ( this . config . homePath ) {
73
+ return this . config . homePath ;
74
+ } else {
75
+ return '/account' ;
76
+ }
77
+ }
78
+
79
+ private getPaymentProvider ( ) : string {
80
+ if ( this . config . paymentProvider ) {
81
+ return this . config . paymentProvider ;
82
+ } else {
83
+ return 'stripe' ;
84
+ }
85
+ }
86
+
74
87
}
0 commit comments