Skip to content

Commit 0abab52

Browse files
committed
add subscription component
1 parent 501f2ca commit 0abab52

File tree

6 files changed

+102
-1
lines changed

6 files changed

+102
-1
lines changed

projects/fusio-sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-fusio-sdk",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "SDK to integrate Fusio into an Angular app",
55
"keywords": [
66
"Fusio",

projects/fusio-sdk/src/lib/component/subscription/subscription.component.css

Whitespace-only changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
<fusio-message [response]="response"></fusio-message>
3+
4+
<button class="btn btn-primary" (click)="doBillingPortal()">Billing portal</button>
5+
6+
<hr>
7+
8+
<div class="card float-start" style="width:14rem;margin-right:1rem;margin-bottom:1rem" *ngFor="let plan of plans">
9+
<h5 class="card-header">{{plan.name}}</h5>
10+
<div class="card-body">
11+
<dl>
12+
<dt>Price</dt>
13+
<dd>{{ plan.price | currency : currencyCode }}</dd>
14+
<dt>Points</dt>
15+
<dd>{{ plan.points }}</dd>
16+
</dl>
17+
<p>{{ plan.description }}</p>
18+
<div class="btn-group" role="group">
19+
<a (click)="doPurchase(plan)" class="btn btn-primary">Purchase</a>
20+
</div>
21+
</div>
22+
</div>
23+
24+
<div style="clear:both"></div>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import {Component, Input, OnInit} from '@angular/core';
2+
import {Plan} from "fusio-sdk/dist/src/generated/consumer/Plan";
3+
import {Message} from "fusio-sdk/dist/src/generated/consumer/Message";
4+
import {ConsumerService} from "../../service/consumer.service";
5+
import {LocationStrategy} from "@angular/common";
6+
import {ErrorConverter} from "../../util/error-converter";
7+
8+
@Component({
9+
selector: 'fusio-subscription',
10+
templateUrl: './subscription.component.html',
11+
styleUrls: ['./subscription.component.css']
12+
})
13+
export class SubscriptionComponent implements OnInit {
14+
15+
@Input()
16+
provider: string = 'stripe';
17+
18+
@Input()
19+
currencyCode: string = 'USD';
20+
21+
@Input()
22+
redirectPath: string = '/account';
23+
24+
plans?: Array<Plan>
25+
response?: Message;
26+
27+
constructor(private consumer: ConsumerService, private location: LocationStrategy) { }
28+
29+
async ngOnInit(): Promise<void> {
30+
const plan = await this.consumer.getClient().getConsumerPlan();
31+
const response = await plan.consumerActionPlanGetAll({count: 1024});
32+
this.plans = response.data.entry;
33+
}
34+
35+
async doBillingPortal() {
36+
try {
37+
const path = this.location.prepareExternalUrl(this.redirectPath);
38+
const redirectUrl = location.origin + path;
39+
40+
const portal = await this.consumer.getClient().getConsumerPaymentByProviderPortal(this.provider);
41+
const response = await portal.consumerActionPaymentPortal({
42+
returnUrl: redirectUrl
43+
});
44+
45+
if (response.data.redirectUrl) {
46+
location.href = response.data.redirectUrl;
47+
} else {
48+
throw new Error('You can only visit the billing portal once you have successfully purchased a subscription');
49+
}
50+
} catch (error) {
51+
this.response = ErrorConverter.convert(error);
52+
}
53+
}
54+
55+
async doPurchase(plan: Plan) {
56+
try {
57+
const path = this.location.prepareExternalUrl(this.redirectPath);
58+
const redirectUrl = location.origin + path;
59+
60+
const checkout = await this.consumer.getClient().getConsumerPaymentByProviderCheckout(this.provider);
61+
const response = await checkout.consumerActionPaymentCheckout({
62+
planId: plan.id,
63+
returnUrl: redirectUrl,
64+
});
65+
66+
if (response.data.approvalUrl) {
67+
location.href = response.data.approvalUrl;
68+
}
69+
} catch (error) {
70+
this.response = ErrorConverter.convert(error);
71+
}
72+
}
73+
74+
}

projects/fusio-sdk/src/lib/fusio-sdk.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {SecurityComponent} from './component/security/security.component';
2222
import {Config, FUSIO_CONFIG} from "./config/config";
2323
import {NgxCaptchaModule} from "ngx-captcha";
2424
import {GravatarModule} from "ngx-gravatar";
25+
import {SubscriptionComponent} from './component/subscription/subscription.component';
2526

2627
@NgModule({
2728
declarations: [
@@ -40,6 +41,7 @@ import {GravatarModule} from "ngx-gravatar";
4041
SidebarComponent,
4142
AccountComponent,
4243
SecurityComponent,
44+
SubscriptionComponent,
4345
],
4446
imports: [
4547
BrowserModule,

projects/fusio-sdk/src/public-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export * from './lib/component/scopes/scopes.component';
2121
export * from './lib/component/search/search.component';
2222
export * from './lib/component/security/security.component';
2323
export * from './lib/component/sidebar/sidebar.component';
24+
export * from './lib/component/subscription/subscription.component';
2425

2526
export * from './lib/guard/authentication.guard';
2627

0 commit comments

Comments
 (0)