Skip to content

Commit

Permalink
fix: fixed few minor bugs related to ngOnchanges and some ts return t…
Browse files Browse the repository at this point in the history
…ypes
  • Loading branch information
shiv-source committed Mar 31, 2024
1 parent 12394a6 commit a6546d8
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions projects/ngx-razorpay/src/lib/ngx-razorpay.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, EventEmitter, Inject, Input, OnDestroy, OnInit, Output, PLATFORM_ID, Renderer2, TemplateRef } from '@angular/core'
import { Component, EventEmitter, Inject, Input, OnChanges, OnDestroy, OnInit, Output, PLATFORM_ID, Renderer2, SimpleChanges, TemplateRef } from '@angular/core'
import { isPlatformBrowser } from '@angular/common'
import { PaymentOptions } from './razorpay.interface'
import { PaymentMethod } from './ngx-razorpay.types'
Expand Down Expand Up @@ -39,7 +39,7 @@ declare var Razorpay: any
`
],
})
export class NgxRazorpayComponent implements OnInit, OnDestroy {
export class NgxRazorpayComponent implements OnInit, OnChanges, OnDestroy {
@Input({ required: true }) key: string = ''
@Input({ required: true }) amount: number = 100
@Input({ required: true }) businessName: string = ''
Expand Down Expand Up @@ -163,13 +163,19 @@ export class NgxRazorpayComponent implements OnInit, OnDestroy {
}
}

initializeRazorpay() {
ngOnChanges(changes: SimpleChanges): void {
if (this.rzp) {
this.initializeRazorpay()
}
}

initializeRazorpay(): void {
this.rzp = new Razorpay(this.options)
this.rzp.on('payment.success', (response: any) => this.paymentSuccessEvent.emit(response))
this.rzp.on('payment.failed', (response: any) => this.paymentFailedEvent.emit(response))
}

loadScript(src: string) {
loadScript(src: string): void {
const script: HTMLScriptElement = this.renderer.createElement('script');
script.type = 'text/javascript'
script.src = src
Expand All @@ -181,11 +187,11 @@ export class NgxRazorpayComponent implements OnInit, OnDestroy {
isScriptAlreadyLoaded(src: string): boolean {
const scripts: HTMLCollectionOf<HTMLScriptElement> = document.getElementsByTagName('script');
const scriptArray: HTMLScriptElement[] = Array.from(scripts);
const sourceUrls = scriptArray.map((script) => script.src)
const sourceUrls: string[] = scriptArray.map((script) => script.src)
return sourceUrls.includes(src)
}

onPay() {
onPay(): void {
if (this.rzp) {
this.rzp.open()
}
Expand Down

0 comments on commit a6546d8

Please sign in to comment.