-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add hosted-fields payment logic Pattern1 (#17584)
CXSPA-3717
- Loading branch information
1 parent
aa6f36b
commit 20a3f6d
Showing
51 changed files
with
835 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,12 @@ | |
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/* | ||
* SPDX-FileCopyrightText: 2023 SAP Spartacus team <[email protected]> | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export const opf = { | ||
opf: { | ||
payment: { | ||
|
@@ -17,6 +23,7 @@ export const opf = { | |
invalidCreditCard: 'Invalid credit card. Please review card details.', | ||
unknown: | ||
'Unknown error occurred while fetching payment. Please contact support', | ||
cardReportedLost: 'Card is reported lost.', | ||
}, | ||
}, | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2023 SAP Spartacus team <[email protected]> | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { InjectionToken } from '@angular/core'; | ||
import { Converter } from '@spartacus/core'; | ||
|
||
export const OTP_NORMALIZER = new InjectionToken< | ||
Converter<any, string | undefined> | ||
>('OtpNormalizer'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
integration-libs/opf/base/core/facade/opf-global-functions.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2023 SAP Spartacus team <[email protected]> | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { | ||
ComponentRef, | ||
Injectable, | ||
NgZone, | ||
ViewContainerRef, | ||
} from '@angular/core'; | ||
import { WindowRef } from '@spartacus/core'; | ||
import { | ||
GlobalOpfPaymentMethods, | ||
KeyValuePair, | ||
MerchantCallback, | ||
OpfGlobalFunctionsFacade, | ||
OpfPaymentFacade, | ||
PaymentMethod, | ||
} from '@spartacus/opf/base/root'; | ||
import { LAUNCH_CALLER, LaunchDialogService } from '@spartacus/storefront'; | ||
import { Observable } from 'rxjs'; | ||
import { finalize } from 'rxjs/operators'; | ||
|
||
@Injectable() | ||
export class OpfGlobalFunctionsService implements OpfGlobalFunctionsFacade { | ||
protected _isGlobalServiceInit = false; | ||
|
||
constructor( | ||
protected winRef: WindowRef, | ||
private ngZone: NgZone, | ||
protected opfPaymentFacade: OpfPaymentFacade, | ||
protected launchDialogService: LaunchDialogService | ||
) {} | ||
|
||
registerGlobalFunctions( | ||
paymentSessionId: string, | ||
vcr?: ViewContainerRef | ||
): void { | ||
this.registerSubmit(paymentSessionId, vcr); | ||
this._isGlobalServiceInit = true; | ||
} | ||
|
||
removeGlobalFunctions(): void { | ||
if (!this._isGlobalServiceInit) { | ||
return; | ||
} | ||
const window = this.winRef.nativeWindow as any; | ||
if (window?.Opf) { | ||
window.Opf = undefined; | ||
} | ||
this._isGlobalServiceInit = false; | ||
} | ||
|
||
protected getGlobalFunctionContainer(): GlobalOpfPaymentMethods { | ||
const window = this.winRef.nativeWindow as any; | ||
if (!window.Opf?.payments) { | ||
window.Opf = window?.Opf ?? {}; | ||
window.Opf.payments = {}; | ||
} | ||
return window.Opf.payments; | ||
} | ||
|
||
protected registerSubmit( | ||
paymentSessionId: string, | ||
vcr?: ViewContainerRef | ||
): void { | ||
this.getGlobalFunctionContainer().submit = ({ | ||
cartId, | ||
additionalData, | ||
submitSuccess = (): void => { | ||
// this is intentional | ||
}, | ||
submitPending = (): void => { | ||
// this is intentional | ||
}, | ||
submitFailure = (): void => { | ||
// this is intentional | ||
}, | ||
paymentMethod, | ||
}: { | ||
cartId: string; | ||
additionalData: Array<KeyValuePair>; | ||
submitSuccess: MerchantCallback; | ||
submitPending: MerchantCallback; | ||
submitFailure: MerchantCallback; | ||
paymentMethod: PaymentMethod; | ||
}): Promise<boolean> => { | ||
return this.ngZone.run(() => { | ||
let overlayedSpinner: void | Observable<ComponentRef<any> | undefined>; | ||
if (vcr) { | ||
overlayedSpinner = this.launchDialogService.launch( | ||
LAUNCH_CALLER.PLACE_ORDER_SPINNER, | ||
vcr | ||
); | ||
} | ||
const callbackArray: [ | ||
MerchantCallback, | ||
MerchantCallback, | ||
MerchantCallback | ||
] = [submitSuccess, submitPending, submitFailure]; | ||
|
||
return this.opfPaymentFacade | ||
.submitPayment({ | ||
additionalData, | ||
paymentSessionId, | ||
cartId, | ||
callbackArray, | ||
paymentMethod, | ||
returnPath: undefined, | ||
}) | ||
.pipe( | ||
finalize(() => { | ||
if (overlayedSpinner) { | ||
overlayedSpinner | ||
.subscribe((component) => { | ||
this.launchDialogService.clear( | ||
LAUNCH_CALLER.PLACE_ORDER_SPINNER | ||
); | ||
if (component) { | ||
component.destroy(); | ||
} | ||
}) | ||
.unsubscribe(); | ||
} | ||
}) | ||
) | ||
.toPromise(); | ||
}); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.