Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x | import {
AfterViewInit,
ChangeDetectionStrategy,
Component,
DestroyRef,
EventEmitter,
Input,
NgZone,
OnDestroy,
OnInit,
Output,
inject,
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { Router } from '@angular/router';
import {
BehaviorSubject,
Observable,
combineLatest,
filter,
firstValueFrom,
map,
race,
shareReplay,
switchMap,
take,
timer,
withLatestFrom,
} from 'rxjs';
import { CheckoutFacade } from 'ish-core/facades/checkout.facade';
import { Basket } from 'ish-core/models/basket/basket.model';
import { PaymentInstrument } from 'ish-core/models/payment-instrument/payment-instrument.model';
import { PaymentMethod } from 'ish-core/models/payment-method/payment-method.model';
import { whenTruthy } from 'ish-core/utils/operators';
import { PaypalButtonsPageType, PaypalConfigService } from 'ish-core/utils/paypal-config/paypal-config.service';
import { PAYPAL_BUTTON_STYLING } from './payment-paypal.component.styling';
/**
* PayPal Payment Component for handling PayPal button integration and checkout flow.
*
* This component manages the PayPal SDK integration for both standard checkout and express checkout flows.
*
* The component integrates with the checkout process by:
* 1. Loading appropriate PayPal scripts based on payment method configuration
* 2. Rendering PayPal buttons in designated containers
* 3. Handling payment flow through PayPal's hosted checkout
* 4. Managing basket updates and navigation upon completion
*
* @see {@link PaypalConfigService} - PayPal configuration and script loading
*/
@Component({
selector: 'ish-payment-paypal',
templateUrl: './payment-paypal.component.html',
styleUrls: ['./payment-paypal.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class PaymentPaypalComponent implements OnInit, AfterViewInit, OnDestroy {
/**
* The type of page where the component is displayed.
* Determines which PayPal message styling and configuration to use.
*/
@Input() pageType: PaypalButtonsPageType = 'cart';
/**
* Event emitted when a PayPal payment method is selected.
* Emits the payment instrument ID for the selected PayPal payment method.
*/
@Output() selectPaypalPaymentMethod = new EventEmitter<string>();
readonly paypalButtonsContainerId = 'paypal-buttons-container';
isPaypalPaymentMethodSelected$: Observable<boolean>;
scriptLoaded$ = new BehaviorSubject<boolean>(undefined);
private basket$ = this.checkoutFacade.basket$.pipe(whenTruthy(), shareReplay(1));
private paypalPaymentMethod$: Observable<PaymentMethod>;
/** References to PayPal component instances for proper cleanup */
private paypalButtonsComponent: { close?(): void } | undefined;
private destroyRef = inject(DestroyRef);
constructor(
private checkoutFacade: CheckoutFacade,
private ngZone: NgZone,
private router: Router,
private paypalConfigService: PaypalConfigService
) {}
ngOnInit(): void {
this.paypalPaymentMethod$ = this.checkoutFacade
.paypalPaymentMethod$(this.pageType === 'cart' ? 'FastCheckout' : 'RedirectBeforeCheckout')
.pipe(whenTruthy(), shareReplay(1));
this.isPaypalPaymentMethodSelected$ = combineLatest({
method: this.paypalPaymentMethod$,
basket: this.basket$,
}).pipe(map(({ method, basket }) => method?.paymentInstruments[0]?.id === basket?.payment?.paymentInstrument?.id));
}
ngAfterViewInit() {
this.loadScript();
}
/**
* Loads PayPal SDK script and initializes payment buttons.
*/
private loadScript() {
this.paypalPaymentMethod$
.pipe(
filter(paymentMethod => !!paymentMethod.hostedPaymentPageParameters?.length),
take(1),
switchMap(paypalPaymentMethod =>
this.paypalConfigService
.loadPayPalScript(this.getNameSpace(paypalPaymentMethod), {
paymentMethod: paypalPaymentMethod,
page: this.pageType,
type: 'button',
})
.pipe(
withLatestFrom(this.basket$),
map(([, basket]) => ({
basket,
paypalPaymentMethod,
}))
)
),
takeUntilDestroyed(this.destroyRef)
)
.subscribe({
next: ({ basket, paypalPaymentMethod }) => {
// ensure change detection is working properly
this.ngZone.run(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const paypalObject = (window as any)[this.getNameSpace(paypalPaymentMethod)];
Iif (paypalObject?.Buttons) {
const paypalButtonsConfig = paypalObject.Buttons(
this.pageType === 'checkout'
? this.initializePaypalCheckoutButton(basket, paypalPaymentMethod)
: this.initializePaypalExpressButton(paypalPaymentMethod)
);
this.paypalButtonsComponent = paypalButtonsConfig
.render(`#${this.paypalButtonsContainerId}`)
.catch((error: string) => {
console.error('PayPal Buttons render failed:', error);
});
this.scriptLoaded$.next(true);
}
});
},
error: () => {
this.scriptLoaded$.next(false);
},
});
}
private getNameSpace(paymentMethod: PaymentMethod): string {
return 'PayPal_iframe_'.concat(paymentMethod.id, '_button');
}
/**
* Initializes PayPal checkout button for standard checkout flow.
*
* Creates and renders PayPal payment buttons with proper event handlers for:
* - Order creation with basket totals
* - Payment approval and processing
* - Shipping address change handling
* - Error and cancellation scenarios
*
* @param basket - Current shopping basket
* @param paypalPaymentMethod - PayPal payment method configuration
*/
private initializePaypalCheckoutButton(basket: Basket, paypalPaymentMethod: PaymentMethod) {
let isShippingAddressChanged = false;
return {
style: PAYPAL_BUTTON_STYLING.checkout,
// Call your server to set up the transaction after the user has clicked the button
createOrder: () => {
// data: { paymentSource: string }
isShippingAddressChanged = false;
return this.createOrder(paypalPaymentMethod);
},
// after the user has submitted the payment in the paypal overlay
onApprove: (data: { payerID: string; orderID: string }) => {
this.onApprove(data, isShippingAddressChanged);
},
// in case the shipping address was changed in the paypal overlay
onShippingAddressChange: (data: {
shippingAddress: { city: string; countryCode: string; postalCode: string; state: string };
}) => {
const normalize = (val: string) => val?.trim()?.toLowerCase();
const basketAddress = basket?.commonShipToAddress;
const shippingAddress = data?.shippingAddress;
isShippingAddressChanged =
normalize(basketAddress?.country) !== normalize(shippingAddress?.countryCode) ||
normalize(basketAddress?.postalCode) !== normalize(shippingAddress?.postalCode) ||
normalize(basketAddress?.city) !== normalize(shippingAddress?.city);
// no state comparison, because it is not available in the basket and also not always provided by paypal
},
// after the user has cancelled the payment in the paypal overlay
onCancel: () => {
this.onCancel('/checkout/payment', paypalPaymentMethod.paymentInstruments[0]);
},
// show a generic error message in case of an error
onError: () => {
this.onError('/checkout/payment');
},
};
}
/**
* Initializes PayPal express checkout button for quick payment from product/basket pages.
*
* Creates and renders PayPal express payment buttons that allow customers to:
* - Make payments without going through full checkout process
* - Handle order creation and payment approval
* - Navigate directly to basket upon completion
* - Display appropriate error messages for failures
*
* @param paypalPaymentMethod - PayPal payment method configuration
*/
private initializePaypalExpressButton(paypalPaymentMethod: PaymentMethod) {
return {
style: PAYPAL_BUTTON_STYLING.cart,
// Call your server to set up the transaction after the user has clicked the button
createOrder: () => this.createOrder(paypalPaymentMethod),
// after the user has submitted the payment in the paypal overlay
onApprove: (data: { payerID: string; orderID: string }) => {
// shippingAddressChanged is always true, because shipping address is not known before
this.onApprove(data, true);
},
// after the user has cancelled the payment in the paypal overlay
onCancel: () => {
this.onCancel('/basket', paypalPaymentMethod.paymentInstruments[0]);
},
// show a generic error message in case of an error
onError: () => {
this.onError('/basket');
},
};
}
private createOrder(paypalPaymentMethod: PaymentMethod) {
this.selectPaypalPaymentMethod.emit(paypalPaymentMethod.paymentInstruments[0]?.id || paypalPaymentMethod.serviceId);
return firstValueFrom(this.getPaypalOrderId$().pipe(take(1)));
}
private onApprove(data: { payerID: string; orderID: string }, shippingAddressChanged: boolean) {
// ngZone is needed to navigate outside of the Angular zone in a callback function
this.ngZone.run(() => {
this.router.navigate(['/checkout/review'], {
queryParams: {
redirect: 'success',
token: data.orderID,
PayerID: data.payerID,
shippingAddressChanged,
},
});
});
}
private onCancel(navigationTarget: string, paymentInstrument: PaymentInstrument) {
Iif (paymentInstrument) {
this.checkoutFacade.deleteBasketPayment(paymentInstrument);
}
this.ngZone.run(() => {
this.router.navigate([navigationTarget], { queryParams: { redirect: 'cancel' } });
});
}
private onError(navigationTarget: string) {
this.ngZone.run(() => {
this.router.navigate([navigationTarget], { queryParams: { redirect: 'failure' } });
});
}
private getPaypalOrderId$(): Observable<string> {
return race(
this.checkoutFacade.basket$.pipe(
whenTruthy(),
filter(
basket =>
basket.payment?.capabilities?.includes('PaypalCheckout') && basket.payment.redirectUrl?.includes('token=')
),
map(basket => basket.payment.redirectUrl.split('token=')[1]),
take(1)
),
timer(4000).pipe(map(() => ''))
);
}
/**
* Component cleanup lifecycle hook.
*
* Properly cleans up PayPal components and removes the loaded script from the DOM
* when the component is destroyed. This prevents memory leaks, zoid errors, and
* ensures clean component lifecycle management.
*
* The cleanup process:
* 1. Closes active PayPal button components to prevent zoid destruction errors
* 2. Closes active PayPal message components to clean up rendering state
* 3. Removes the PayPal script from the DOM to free memory
*
* This is especially important for PayPal scripts which can be large and contain
* global state that should be cleaned up properly to avoid conflicts.
*/
ngOnDestroy(): void {
// Clean up PayPal components before removing the script to prevent zoid errors
try {
Iif (this.paypalButtonsComponent?.close) {
this.paypalButtonsComponent.close();
}
} catch (error) {
// Ignore cleanup errors - components may already be destroyed
console.warn('PayPal component cleanup warning:', error);
}
}
}
|