0

We need to show a Stripe price table in my Angular app where the users should be able to subscribe. The subscription and payment's backend webhook works fine as it receives the events successfully from stripe. The problem we're experiencing is that Stripe has a field called client-reference-id to be able to utilize it to reconcile the payment in our end, and this field is not rendered when the component loads. The following code is the component after following the steps mentioned in this Stackoverflow post:

The HTML:

<mat-card class="price-table-card">
  <mat-card-header>
    <mat-card-title>Subscription</mat-card-title>
    <mat-card-subtitle>Manage your subscription</mat-card-subtitle>
  </mat-card-header>
  <mat-card-content>
    <stripe-pricing-table
    pricing-table-id="prctbl_HardCode"
    publishable-key="pk_test_HardCoded"
    client-reference-id={{clientReference}}>
    </stripe-pricing-table>
  </mat-card-content>
</mat-card>

The typescript:

import { Component, OnInit } from '@angular/core';
import { environment } from '@env/environment';

@Component({
  selector: 'app-manage-subscription',
  templateUrl: './manage-subscription.component.html',
  styleUrls: ['./manage-subscription.component.css']
})
export class ManageSubscriptionComponent implements OnInit {
  public priceTableId: string;
  public publishableKey: string;
  public clientReference?: string;

  constructor() {
    this.priceTableId = environment.stripePricingTableId;
    this.publishableKey = environment.stripePublishableKey;
  }

  ngOnInit(): void {
    this.clientReference = localStorage.getItem('myKey') as string;
  }
}

And, the following code snippet shows how this component is called in app.component.html within a mat-sidenav-containe along with other components in there:

<app-manage-subscription></app-manage-subscription>

Just to reiterate, the clientReference in the HTML is null or unpopulated despite this.clientReference = localStorage.getItem('myKey') as string returns a valid value.

If this works, we will be able to following the same pattern to use priceTableId and publishableKey instead of hard coding them in the HTML

I expect clientReference to be populated in the component when the page renders. Same expectation applies to both priceTableId and publishableKey to avoid hard coding them.

Arash
  • 3,628
  • 5
  • 46
  • 70
  • Have you tried in ngAfterViewInit? The ngOnInit is for initializing the given component defined by that class, but other components that belong to the parent component's view are loaded in an unpredictable speed, so that's why you have ngAfterViewInit hook. – Misha Mashina Apr 20 '23 at 16:05
  • @MishaMashina thanks for the reply but the solution you suggested did not work! – Arash Apr 20 '23 at 16:16
  • Just noticed now (although I'm not familiar with Stripe): would it be different if you put `client-reference-id="clientReference"` instead of `client-reference-id={{clientReference}}`? Or [] brackets for the property too? – Misha Mashina Apr 20 '23 at 16:20
  • It did not work when I used "clientReference" after removing the {{ and }}. The stripe sent "clientReference" string to the webhook and it proves that [] are not required for the property. This issue seems to be angular issue rather than stripe. @MishaMashina – Arash Apr 20 '23 at 16:49
  • Does it work properly if you hardcode the value in the HTML? Stripe doesn't let you change that value dynamically after rendering so you need to find a way to generate that HTML with the string in it, not update it after load. – koopajah Apr 20 '23 at 17:23
  • @koopajah yes it does work when it's hard coded. I am not trying to update the string after the load. All I want to provide clientReferenceId which varies for every user. – Arash Apr 20 '23 at 17:25
  • But now you know the problem is that when you rendered that HTML your variable/JS code hadn't run yet so that's what you need to change – koopajah Apr 20 '23 at 23:49
  • @koopajah please see my answer below. – Arash Apr 20 '23 at 23:52

1 Answers1

0

I used Angular attribute binding to resolve this issue:

<mat-card class="price-table-card">
  <mat-card-header>
    <mat-card-title>Subscription</mat-card-title>
    <mat-card-subtitle>Manage your subscription</mat-card-subtitle>
  </mat-card-header>
  <mat-card-content>
    <stripe-pricing-table
    pricing-table-id="prctbl_HardCode"
    publishable-key="pk_test_HardCoded"
    [attr.client-reference-id]="clientReference">
    </stripe-pricing-table>
  </mat-card-content>
</mat-card>
Arash
  • 3,628
  • 5
  • 46
  • 70