-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from Adyen/feature/AD-334
Feature/ad 334
- Loading branch information
Showing
17 changed files
with
407 additions
and
47 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 |
---|---|---|
|
@@ -86,6 +86,7 @@ | |
}, | ||
"configurations": { | ||
"production": { | ||
"sourceMap": true, | ||
"budgets": [ | ||
{ | ||
"type": "initial", | ||
|
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
4 changes: 2 additions & 2 deletions
4
projects/adyen/adyen-spartacus/src/adyen-redirect/adyen-redirect-success.component.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
1 change: 1 addition & 0 deletions
1
...-spartacus/src/cart/components/express-checkout-cart/express-checkout-cart.component.html
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
<cx-google-express-payment></cx-google-express-payment> | ||
<cx-apple-express-payment></cx-apple-express-payment> | ||
<p></p> |
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
5 changes: 5 additions & 0 deletions
5
projects/adyen/adyen-spartacus/src/core/models/occ.order.models.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
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
Empty file.
2 changes: 2 additions & 0 deletions
2
...en/adyen-spartacus/src/express/apple-express-payment/apple-express-payment.component.html
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,2 @@ | ||
<div id="apple-pay-button"></div> | ||
|
73 changes: 73 additions & 0 deletions
73
...adyen-spartacus/src/express/apple-express-payment/apple-express-payment.component.spec.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,73 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { of } from 'rxjs'; | ||
import { AppleExpressPaymentComponent } from './apple-express-payment.component'; | ||
import { CheckoutAdyenConfigurationService } from '../../service/checkout-adyen-configuration.service'; | ||
import { AdyenExpressOrderService } from '../../service/adyen-express-order.service'; | ||
import { RoutingService, QueryState } from '@spartacus/core'; | ||
import { AdyenConfigData } from '../../core/models/occ.config.models'; | ||
|
||
describe('AppleExpressPaymentComponent', () => { | ||
let component: AppleExpressPaymentComponent; | ||
let fixture: ComponentFixture<AppleExpressPaymentComponent>; | ||
let mockCheckoutAdyenConfigurationService: jasmine.SpyObj<CheckoutAdyenConfigurationService>; | ||
let mockAdyenExpressOrderService: jasmine.SpyObj<AdyenExpressOrderService>; | ||
let mockRoutingService: jasmine.SpyObj<RoutingService>; | ||
|
||
const mockAdyenConfigData: AdyenConfigData = { | ||
amountDecimal: 12.99, | ||
paymentMethods: [], | ||
connectedTerminalList: [], | ||
storedPaymentMethodList: [], | ||
issuerLists: new Map<string, string>(), | ||
creditCardLabel: 'Credit Card', | ||
allowedCards: [], | ||
amount: { value: 1000, currency: 'USD' }, | ||
adyenClientKey: 'mockClientKey', | ||
adyenPaypalMerchantId: 'mockPaypalMerchantId', | ||
deviceFingerPrintUrl: 'mockDeviceFingerPrintUrl', | ||
sessionData: { id: 'mockSessionId', sessionData: 'mockSessionData' }, | ||
selectedPaymentMethod: 'mockPaymentMethod', | ||
showRememberTheseDetails: true, | ||
checkoutShopperHost: 'mockShopperHost', | ||
environmentMode: 'test', | ||
shopperLocale: 'en-US', | ||
openInvoiceMethods: [], | ||
showSocialSecurityNumber: false, | ||
showBoleto: false, | ||
showComboCard: false, | ||
showPos: false, | ||
immediateCapture: false, | ||
countryCode: 'US', | ||
cardHolderNameRequired: true, | ||
sepaDirectDebit: false | ||
}; | ||
|
||
beforeEach(async () => { | ||
mockCheckoutAdyenConfigurationService = jasmine.createSpyObj('CheckoutAdyenConfigurationService', ['getCheckoutConfigurationState']); | ||
mockAdyenExpressOrderService = jasmine.createSpyObj('AdyenExpressOrderService', ['adyenPlaceOrder']); | ||
mockRoutingService = jasmine.createSpyObj('RoutingService', ['go']); | ||
|
||
mockCheckoutAdyenConfigurationService = jasmine.createSpyObj('CheckoutAdyenConfigurationService', ['getCheckoutConfigurationState']); | ||
mockCheckoutAdyenConfigurationService.getCheckoutConfigurationState.and.returnValue( | ||
of({ loading: false, error: false, data: mockAdyenConfigData } as QueryState<AdyenConfigData>) | ||
); | ||
|
||
await TestBed.configureTestingModule({ | ||
imports: [AppleExpressPaymentComponent], | ||
providers: [ | ||
{ provide: CheckoutAdyenConfigurationService, useValue: mockCheckoutAdyenConfigurationService }, | ||
{ provide: AdyenExpressOrderService, useValue: mockAdyenExpressOrderService }, | ||
{ provide: RoutingService, useValue: mockRoutingService } | ||
] | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(AppleExpressPaymentComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
}); |
175 changes: 175 additions & 0 deletions
175
...dyen/adyen-spartacus/src/express/apple-express-payment/apple-express-payment.component.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,175 @@ | ||
import {Component, Input, OnDestroy, OnInit} from '@angular/core'; | ||
import {filter, map, switchMap, take} from 'rxjs/operators'; | ||
import {CommonModule} from '@angular/common'; | ||
import { ApplePay, CoreConfiguration, SubmitData, UIElement} from "@adyen/adyen-web"; | ||
import {AdyenCheckout, AdyenCheckoutError} from '@adyen/adyen-web/auto'; | ||
import {CheckoutAdyenConfigurationService} from "../../service/checkout-adyen-configuration.service"; | ||
import {AdyenConfigData} from "../../core/models/occ.config.models"; | ||
import {AdyenExpressOrderService} from "../../service/adyen-express-order.service"; | ||
import {EventService, Product, RoutingService, UserIdService,} from '@spartacus/core'; | ||
import {of, Subscription} from 'rxjs'; | ||
import {ActiveCartFacade} from '@spartacus/cart/base/root'; | ||
import {CheckoutAdyenConfigurationReloadEvent} from "../../events/checkout-adyen.events"; | ||
|
||
@Component({ | ||
selector: 'cx-apple-express-payment', | ||
standalone: true, | ||
imports: [CommonModule], | ||
templateUrl: './apple-express-payment.component.html', | ||
styleUrls: ['./apple-express-payment.component.css'] | ||
}) | ||
export class AppleExpressPaymentComponent implements OnInit, OnDestroy{ | ||
|
||
protected subscriptions = new Subscription(); | ||
|
||
@Input() | ||
product: Product; | ||
|
||
applePay: ApplePay; | ||
|
||
private authorizedPaymentData: any; | ||
|
||
constructor( | ||
protected checkoutAdyenConfigurationService: CheckoutAdyenConfigurationService, | ||
protected adyenOrderService: AdyenExpressOrderService, | ||
protected routingService: RoutingService, | ||
protected eventService: EventService, | ||
protected activeCartFacade: ActiveCartFacade, | ||
private userIdService: UserIdService, | ||
) {} | ||
|
||
ngOnInit(): void { | ||
|
||
this.eventService.dispatch( | ||
new CheckoutAdyenConfigurationReloadEvent() | ||
); | ||
|
||
this.subscriptions.add( | ||
this.eventService.get(CheckoutAdyenConfigurationReloadEvent).subscribe(event => { | ||
this.handleConfigurationReload(event); | ||
}) | ||
); | ||
|
||
this.initializeApplePay(); | ||
} | ||
|
||
ngOnDestroy(): void { | ||
this.subscriptions.unsubscribe(); | ||
if(this.applePay) this.applePay.unmount(); | ||
} | ||
|
||
private initializeApplePay() { | ||
this.checkoutAdyenConfigurationService.getCheckoutConfigurationState() | ||
.pipe( | ||
filter((state) => !state.loading), | ||
take(1), | ||
map((state) => state.data), | ||
switchMap((config) => config ? this.setupAdyenCheckout(config) : of(null)) | ||
) | ||
.subscribe({ | ||
error: (error) => console.error('Error initializing Apple Pay:', error) | ||
}); | ||
} | ||
|
||
private async setupAdyenCheckout(config: AdyenConfigData) { | ||
const adyenCheckout = await AdyenCheckout(this.getAdyenCheckoutConfig(config)); | ||
|
||
this.applePay = new ApplePay(adyenCheckout, { | ||
amount: { | ||
currency: config.amount.currency, | ||
value: config.amount.value | ||
}, | ||
// Button config | ||
buttonType: "check-out", | ||
buttonColor: "black", | ||
requiredShippingContactFields: [ | ||
"postalAddress", | ||
"name", | ||
"email" | ||
], | ||
onSubmit: (state, element: UIElement, actions) => this.handleOnSubmit(state, actions), | ||
onAuthorized: (paymentData, actions) => { | ||
this.authorizedPaymentData = paymentData; | ||
actions.resolve(); | ||
}, | ||
onError: (error) => this.handleError(error) | ||
}) | ||
|
||
this.applePay.isAvailable() | ||
.then(() => this.applePay.mount("#apple-pay-button")) | ||
} | ||
|
||
private handleOnSubmit(state: SubmitData, actions: any) { | ||
this.adyenOrderService.adyenPlaceAppleExpressOrder(state.data, this.authorizedPaymentData, this.product).subscribe( | ||
result => { | ||
if (result?.success) { | ||
if (result.executeAction && result.paymentsAction !== undefined) { | ||
this.applePay.handleAction(result.paymentsAction); | ||
} else { | ||
this.onSuccess(); | ||
} | ||
} else { | ||
console.error(result?.error); | ||
actions.reject(); | ||
} | ||
actions.resolve({ resultCode: 'Authorised' }); | ||
}, | ||
error => { | ||
console.error(error); | ||
actions.reject(); | ||
} | ||
); | ||
} | ||
|
||
protected handleConfigurationReload(event: CheckoutAdyenConfigurationReloadEvent): void { | ||
this.applePay.unmount(); | ||
this.activeCartFacade.getActiveCartId().pipe( | ||
filter(cartId => !!cartId), | ||
switchMap(cartId => this.userIdService.takeUserId().pipe( | ||
switchMap(userId => this.checkoutAdyenConfigurationService.fetchCheckoutConfiguration(userId, cartId)) | ||
)) | ||
).subscribe(async config => { | ||
if (config) { | ||
await this.setupAdyenCheckout(config) | ||
} | ||
}); | ||
} | ||
|
||
protected getAdyenCheckoutConfig(adyenConfig: AdyenConfigData): CoreConfiguration { | ||
return { | ||
paymentMethodsResponse: { | ||
paymentMethods: adyenConfig.paymentMethods, | ||
storedPaymentMethods: adyenConfig.storedPaymentMethodList | ||
}, | ||
locale: adyenConfig.shopperLocale, | ||
environment: this.castToEnvironment(adyenConfig.environmentMode), | ||
clientKey: adyenConfig.adyenClientKey, | ||
session: { | ||
id: adyenConfig.sessionData.id, | ||
sessionData: adyenConfig.sessionData.sessionData | ||
}, | ||
countryCode: adyenConfig.countryCode ? adyenConfig.countryCode : 'US', | ||
analytics: { | ||
enabled: false | ||
}, | ||
//@ts-ignore | ||
risk: { | ||
enabled: true | ||
} | ||
}; | ||
} | ||
|
||
protected castToEnvironment(env: string): CoreConfiguration['environment'] { | ||
const validEnvironments: CoreConfiguration['environment'][] = ['test', 'live', 'live-us', 'live-au', 'live-apse', 'live-in']; | ||
if (validEnvironments.includes(env as CoreConfiguration['environment'])) { | ||
return env as CoreConfiguration['environment']; | ||
} | ||
throw new Error(`Invalid environment: ${env}`); | ||
} | ||
|
||
handleError(error: AdyenCheckoutError) {} | ||
|
||
onSuccess(): void { | ||
this.routingService.go({ cxRoute: 'orderConfirmation' }); | ||
} | ||
} |
Oops, something went wrong.