Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,21 @@
*ngIf="paymentLink?.destination?.form?.length"
class="cx-payment-link"
>
<iframe
*ngIf="paymentLink.renderType === RENDER_PATTERN.IFRAME"
class="cx-payment-iframe"
name="cx-payment-iframe"
></iframe>
<form
[action]="paymentLink.destination?.url"
[method]="paymentLink.destination?.method"
[enctype]="paymentLink.destination?.contentType"
[target]="
paymentLink.renderType === RENDER_PATTERN.FULL_PAGE
? '_self'
: this.PAYMENT_IFRAME_NAME
"
#paymentForm
>
<fieldset>
<input
Expand All @@ -60,6 +71,7 @@
/>
</fieldset>
<input
*ngIf="paymentLink.renderType === RENDER_PATTERN.FULL_PAGE"
class="btn btn-primary"
type="submit"
[value]="'opfCheckout.proceedPayment' | cxTranslate"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ViewContainerRef } from '@angular/core';
import { ViewContainerRef, ElementRef } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { DomSanitizer } from '@angular/platform-browser';
import { CurrencyService, LanguageService } from '@spartacus/core';
Expand Down Expand Up @@ -344,4 +344,33 @@ describe('OpfCheckoutPaymentWrapperComponent', () => {
mockGlobalFunctionsService.registerGlobalFunctions
).not.toHaveBeenCalled();
});

describe('submitFormToIframe', () => {
let mockFormElement: jasmine.SpyObj<HTMLFormElement>;

beforeEach(() => {
mockFormElement = jasmine.createSpyObj('HTMLFormElement', ['submit']);
component.formElement = {
nativeElement: mockFormElement,
} as ElementRef<HTMLFormElement>;
});

it('should submit form when payment data is ready and form targets iframe', () => {
component['isPaymentDataReady'] = true;
mockFormElement.target = 'cx-payment-iframe';

component['submitFormToIframe']();

expect(mockFormElement.submit).toHaveBeenCalled();
});

it('should not submit form when form element is not available', () => {
component['isPaymentDataReady'] = true;
component.formElement = null as any;

component['submitFormToIframe']();

expect(mockFormElement.submit).not.toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@

import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
Input,
OnDestroy,
OnInit,
ViewChild,
ViewContainerRef,
inject,
} from '@angular/core';
Expand Down Expand Up @@ -54,8 +57,12 @@ export class OpfCheckoutPaymentWrapperComponent implements OnInit, OnDestroy {
protected currencyService = inject(CurrencyService);
protected activeCartService = inject(ActiveCartFacade);
protected vcr = inject(ViewContainerRef);
protected cdr = inject(ChangeDetectorRef);
protected isPaymentDataReady = false;
protected readonly PAYMENT_IFRAME_NAME = 'cx-payment-iframe';

@Input() selectedPaymentId: number;
@ViewChild('paymentForm') formElement!: ElementRef<HTMLFormElement>;

renderPaymentMethodEvent$ = this.service.getRenderPaymentMethodEvent();

Expand Down Expand Up @@ -88,6 +95,15 @@ export class OpfCheckoutPaymentWrapperComponent implements OnInit, OnDestroy {
this.service.reloadPaymentMode();
}

protected submitFormToIframe(): void {
if (this.isPaymentDataReady && this.formElement?.nativeElement) {
const form = this.formElement.nativeElement;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we move const form inside the 'if' block so won't be created if not IFRAME?

if (this.formElement.nativeElement?.target === this.PAYMENT_IFRAME_NAME) {
form.submit();
}
}
}

protected listenForReinitiatePaymentEvent(): void {
this.sub.add(
this.opfPaymentEventsService?.reinitiatePaymentEvent$.subscribe(
Expand Down Expand Up @@ -136,6 +152,7 @@ export class OpfCheckoutPaymentWrapperComponent implements OnInit, OnDestroy {

protected initiatePaymentMode(paymentOptionId?: number): void {
const idToUse = paymentOptionId || this.selectedPaymentId;
this.isPaymentDataReady = false;
this.sub.add(
this.service.initiatePayment(idToUse).subscribe({
next: (paymentSessionData) => {
Expand All @@ -151,6 +168,13 @@ export class OpfCheckoutPaymentWrapperComponent implements OnInit, OnDestroy {
OpfGlobalFunctionsDomain.CHECKOUT
);
}

this.isPaymentDataReady = true;
this.cdr.detectChanges();
this.submitFormToIframe();
},
error: () => {
this.isPaymentDataReady = false;
},
})
);
Expand Down
Loading