Skip to content

Commit

Permalink
split in more comprehensive methods + unit test coverage 100% for ver…
Browse files Browse the repository at this point in the history
…if cpnt & service
  • Loading branch information
FollowTheFlo committed Sep 20, 2023
1 parent f588dc3 commit 538a3b7
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 250 deletions.
104 changes: 49 additions & 55 deletions integration-libs/opf/base/core/facade/opf-global-functions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,39 @@ export class OpfGlobalFunctionsService implements OpfGlobalFunctionsFacade {
};
}

protected runSubmitComplete(
cartId: string,
additionalData: Array<KeyValuePair>,
callbackArray: [MerchantCallback, MerchantCallback, MerchantCallback],
paymentSessionId: string,
returnPath?: string | undefined,
vcr?: ViewContainerRef
) {
return this.ngZone.run(() => {
let overlayedSpinner: void | Observable<ComponentRef<any> | undefined>;
if (vcr) {
overlayedSpinner = this.startLoaderSpinner(vcr);
}

return this.opfPaymentFacade
.submitCompletePayment({
additionalData,
paymentSessionId,
cartId,
callbackArray,
returnPath,
})
.pipe(
finalize(() => {
if (overlayedSpinner) {
this.stopLoaderSpinner(overlayedSpinner);
}
})
)
.toPromise();
});
}

protected registerSubmitComplete(
paymentSessionId: string,
vcr?: ViewContainerRef
Expand All @@ -192,33 +225,14 @@ export class OpfGlobalFunctionsService implements OpfGlobalFunctionsFacade {
submitPending: MerchantCallback;
submitFailure: MerchantCallback;
}): Promise<boolean> => {
return this.ngZone.run(() => {
let overlayedSpinner: void | Observable<ComponentRef<any> | undefined>;
if (vcr) {
overlayedSpinner = this.startLoaderSpinner(vcr);
}
const callbackArray: [
MerchantCallback,
MerchantCallback,
MerchantCallback
] = [submitSuccess, submitPending, submitFailure];

return this.opfPaymentFacade
.submitCompletePayment({
additionalData,
paymentSessionId,
cartId,
callbackArray,
})
.pipe(
finalize(() => {
if (overlayedSpinner) {
this.stopLoaderSpinner(overlayedSpinner);
}
})
)
.toPromise();
});
return this.runSubmitComplete(
cartId,
additionalData,
[submitSuccess, submitPending, submitFailure],
paymentSessionId,
undefined,
vcr
);
};
}

Expand All @@ -245,34 +259,14 @@ export class OpfGlobalFunctionsService implements OpfGlobalFunctionsFacade {
submitPending: MerchantCallback;
submitFailure: MerchantCallback;
}): Promise<boolean> => {
return this.ngZone.run(() => {
let overlayedSpinner: void | Observable<ComponentRef<any> | undefined>;
if (vcr) {
overlayedSpinner = this.startLoaderSpinner(vcr);
}
const callbackArray: [
MerchantCallback,
MerchantCallback,
MerchantCallback
] = [submitSuccess, submitPending, submitFailure];

return this.opfPaymentFacade
.submitCompletePayment({
additionalData,
paymentSessionId,
cartId,
callbackArray,
returnPath: 'checkoutReviewOrder',
})
.pipe(
finalize(() => {
if (overlayedSpinner) {
this.stopLoaderSpinner(overlayedSpinner);
}
})
)
.toPromise();
});
return this.runSubmitComplete(
cartId,
additionalData,
[submitSuccess, submitPending, submitFailure],
paymentSessionId,
'checkoutReviewOrder',
vcr
);
};
}
}
14 changes: 11 additions & 3 deletions integration-libs/opf/base/occ/adapters/occ-opf.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,25 @@ export class OccOpfPaymentAdapter implements OpfPaymentAdapter {
protected config: OpfConfig
) {}

header: { [name: string]: string } = {
protected headerWithNoLanguage: { [name: string]: string } = {
accept: 'application/json',
'Content-Type': 'application/json',
};
protected header: { [name: string]: string } = {
...this.headerWithNoLanguage,
'Accept-Language': 'en-us',
};

protected headerWithContentLanguage: { [name: string]: string } = {
...this.headerWithNoLanguage,
'Content-Language': 'en-us',
};

verifyPayment(
paymentSessionId: string,
payload: OpfPaymentVerificationPayload
): Observable<OpfPaymentVerificationResponse> {
const headers = new HttpHeaders(this.header).set(
const headers = new HttpHeaders(this.headerWithNoLanguage).set(
OPF_CC_PUBLIC_KEY,
this.config.opf?.commerceCloudPublicKey || ''
);
Expand Down Expand Up @@ -110,7 +118,7 @@ export class OccOpfPaymentAdapter implements OpfPaymentAdapter {
otpKey: string,
paymentSessionId: string
): Observable<SubmitCompleteResponse> {
const headers = new HttpHeaders(this.header)
const headers = new HttpHeaders(this.headerWithContentLanguage)
.set(OPF_CC_PUBLIC_KEY, this.config.opf?.commerceCloudPublicKey || '')
.set(OPF_CC_OTP_KEY, otpKey || '');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Component } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ActivatedRoute } from '@angular/router';
import { HttpErrorModel } from '@spartacus/core';
import { Order } from '@spartacus/order/root';
import { of, throwError } from 'rxjs';
import { KeyValuePair } from '../../model';
import { OpfPaymentVerificationComponent } from './opf-payment-verification.component';
Expand All @@ -33,12 +32,11 @@ describe('OpfPaymentVerificationComponent', () => {
paymentServiceMock = jasmine.createSpyObj('OpfPaymentVerificationService', [
'checkIfProcessingCartIdExist',
'verifyResultUrl',
'verifyPayment',
'placeOrder',
'goToPage',
'displayError',
'removeResourcesAndGlobalFunctions',
'runHostedFieldsPattern',
'runHostedPagePattern',
]);

TestBed.configureTestingModule({
Expand Down Expand Up @@ -83,22 +81,20 @@ describe('OpfPaymentVerificationComponent', () => {
paramsMap: mockResponseMap,
afterRedirectScriptFlag: mockAfterRedirectScriptFlag,
};
const mockPlaceOrderResult: Order = { guid: 'placeOrderResult' };

paymentServiceMock.verifyResultUrl.and.returnValue(of(mockVerifyResult));
paymentServiceMock.verifyPayment.and.returnValue(of(true));
paymentServiceMock.placeOrder.and.returnValue(of(mockPlaceOrderResult));
paymentServiceMock.runHostedFieldsPattern.and.returnValue(of(true));
paymentServiceMock.runHostedPagePattern.and.returnValue(of(true));

component.ngOnInit();

expect(paymentServiceMock.verifyResultUrl).toHaveBeenCalledWith(
routeMock
);
expect(paymentServiceMock.verifyPayment).toHaveBeenCalledWith(
expect(paymentServiceMock.runHostedPagePattern).toHaveBeenCalledWith(
mockPaymentSessionId,
mockResponseMap
);
expect(paymentServiceMock.placeOrder).toHaveBeenCalled();
});

it('should handle error scenario', () => {
Expand All @@ -111,7 +107,9 @@ describe('OpfPaymentVerificationComponent', () => {
};

paymentServiceMock.verifyResultUrl.and.returnValue(of(mockVerifyResult));
paymentServiceMock.verifyPayment.and.returnValue(throwError(mockError));
paymentServiceMock.runHostedPagePattern.and.returnValue(
throwError(mockError)
);

spyOn(component, 'onError');

Expand All @@ -120,7 +118,24 @@ describe('OpfPaymentVerificationComponent', () => {
expect(component.onError).toHaveBeenCalledWith(mockError);
});

it('should handle HostedField pattern scenario', () => {
it('should call onError when payment fails', () => {
const mockVerifyResult = {
paymentSessionId: '1',
paramsMap: [],
afterRedirectScriptFlag: 'false',
};

paymentServiceMock.verifyResultUrl.and.returnValue(of(mockVerifyResult));
paymentServiceMock.runHostedPagePattern.and.returnValue(of(false));

spyOn(component, 'onError');

component.ngOnInit();

expect(component.onError).toHaveBeenCalledWith(undefined);
});

it('should handle HostedField pattern successful scenario', () => {
const mockVerifyResultWithFlag = {
paymentSessionId: '1',
paramsMap: [],
Expand All @@ -134,16 +149,7 @@ describe('OpfPaymentVerificationComponent', () => {
component.ngOnInit();

expect(paymentServiceMock.runHostedFieldsPattern).toHaveBeenCalled();
expect(paymentServiceMock.verifyPayment).not.toHaveBeenCalled();
});
});

describe('onSuccess', () => {
it('should call paymentService.goToPage with "orderConfirmation"', () => {
component.onSuccess();
expect(paymentServiceMock.goToPage).toHaveBeenCalledWith(
'orderConfirmation'
);
expect(paymentServiceMock.runHostedPagePattern).not.toHaveBeenCalled();
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { Component, OnDestroy, OnInit, ViewContainerRef } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { HttpErrorModel } from '@spartacus/core';

import { Subscription } from 'rxjs';
import { concatMap, map, tap } from 'rxjs/operators';
import { Observable, Subscription } from 'rxjs';
import { concatMap } from 'rxjs/operators';

import { TargetPage } from '../../model';
import { KeyValuePair, TargetPage } from '../../model';
import { OpfPaymentVerificationService } from './opf-payment-verification.service';

@Component({
Expand Down Expand Up @@ -39,31 +39,12 @@ export class OpfPaymentVerificationComponent implements OnInit, OnDestroy {
paymentSessionId,
paramsMap: paramsMap,
afterRedirectScriptFlag,
}) => {
if (afterRedirectScriptFlag === 'true') {
this.isHostedFieldPattern = true;
return this.paymentService.runHostedFieldsPattern(
TargetPage.RESULT,
paymentSessionId,
this.vcr,
paramsMap
);
} else {
return this.paymentService
.verifyPayment(paymentSessionId, paramsMap)
.pipe(
concatMap(() => {
return this.paymentService.placeOrder();
}),
map((order) => !!order),
tap((success: boolean) => {
if (success) {
this.onSuccess();
}
})
);
}
}
}) =>
this.runPaymentPattern({
paymentSessionId,
paramsMap,
afterRedirectScriptFlag,
})
)
)
.subscribe({
Expand All @@ -76,8 +57,29 @@ export class OpfPaymentVerificationComponent implements OnInit, OnDestroy {
});
}

onSuccess(): void {
this.paymentService.goToPage('orderConfirmation');
protected runPaymentPattern({
paymentSessionId,
paramsMap,
afterRedirectScriptFlag,
}: {
paymentSessionId: string;
paramsMap: KeyValuePair[];
afterRedirectScriptFlag?: string;
}): Observable<boolean> {
if (afterRedirectScriptFlag === 'true') {
this.isHostedFieldPattern = true;
return this.paymentService.runHostedFieldsPattern(
TargetPage.RESULT,
paymentSessionId,
this.vcr,
paramsMap
);
} else {
return this.paymentService.runHostedPagePattern(
paymentSessionId,
paramsMap
);
}
}

onError(error: HttpErrorModel | undefined): void {
Expand Down
Loading

0 comments on commit 538a3b7

Please sign in to comment.