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 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 14x 14x 14x 14x 14x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 2x 1x 1x 2x 2x 2x 2x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 1x 1x 2x 2x 2x 2x 2x 2x 1x 3x 1x 1x 1x 1x 3x 1x 1x | import { APP_BASE_HREF } from '@angular/common'; import { HttpHeaders, HttpParams } from '@angular/common/http'; import { Inject, Injectable } from '@angular/core'; import { Store, select } from '@ngrx/store'; import { Observable, of, throwError } from 'rxjs'; import { concatMap, first, map, switchMap, take, withLatestFrom } from 'rxjs/operators'; import { AppFacade } from 'ish-core/facades/app.facade'; import { Basket } from 'ish-core/models/basket/basket.model'; import { Customer } from 'ish-core/models/customer/customer.model'; import { Link } from 'ish-core/models/link/link.model'; import { PaymentInstrumentData } from 'ish-core/models/payment-instrument/payment-instrument.interface'; import { PaymentInstrument } from 'ish-core/models/payment-instrument/payment-instrument.model'; import { PaymentMethodBaseData, PaymentMethodOptionsDataType, } from 'ish-core/models/payment-method/payment-method.interface'; import { PaymentMethodMapper } from 'ish-core/models/payment-method/payment-method.mapper'; import { PaymentMethod } from 'ish-core/models/payment-method/payment-method.model'; import { Payment } from 'ish-core/models/payment/payment.model'; import { ApiService, unpackEnvelope } from 'ish-core/services/api/api.service'; import { getCurrentLocale } from 'ish-core/store/core/configuration'; import { whenTruthy } from 'ish-core/utils/operators'; /** * The Payment Service handles the interaction with the 'baskets' and 'users' REST API concerning payment functionality. */ @Injectable({ providedIn: 'root' }) export class PaymentService { constructor( private apiService: ApiService, private store: Store, private appFacade: AppFacade, @Inject(APP_BASE_HREF) private baseHref: string ) {} private basketHeaders = new HttpHeaders({ 'content-type': 'application/json', Accept: 'application/vnd.intershop.basket.v1+json', }); /** * Get eligible payment methods for selected basket. * * @returns The eligible payment methods. */ getBasketEligiblePaymentMethods(): Observable<PaymentMethod[]> { const params = new HttpParams().set('include', 'paymentInstruments'); return this.apiService .currentBasketEndpoint() .get('eligible-payment-methods', { headers: this.basketHeaders, params, }) .pipe(map(PaymentMethodMapper.fromData)); } /** * Adds a fast checkout payment at the selected basket and generate redirect url in one step/request. * * @param paymentInstrument The payment instrument id. * @returns The necessary url for redirecting to payment provider. */ setBasketFastCheckoutPayment(paymentInstrument: string): Observable<string> { Iif (!paymentInstrument) { return throwError(() => new Error('setBasketFastCheckoutPayment() called without paymentInstrument')); } const loc = `${location.origin}${this.baseHref}`; return this.store.pipe(select(getCurrentLocale)).pipe( whenTruthy(), take(1), switchMap(currentLocale => { const body = { paymentInstrument, redirect: { successUrl: `${loc}/checkout/review;lang=${currentLocale}?redirect=success`, cancelUrl: `${loc}/basket;lang=${currentLocale}?redirect=cancel`, failureUrl: `${loc}/basket;lang=${currentLocale}?redirect=cancel`, }, }; return this.apiService .currentBasketEndpoint() .put<{ data: { redirect: { redirectUrl: string; }; }; }>('payments/open-tender', body, { headers: this.basketHeaders, }) .pipe(map(payload => payload.data.redirect.redirectUrl)); }) ); } /** * Adds a payment at the selected basket. If redirect is required the redirect urls are saved at basket in dependence of the payment instrument capabilities (redirectBeforeCheckout/RedirectAfterCheckout). * * @param paymentInstrument The unique name of the payment method, e.g. ISH_INVOICE * @returns The payment instrument. */ setBasketPayment(paymentInstrument: string): Observable<string> { Iif (!paymentInstrument) { return throwError(() => new Error('setBasketPayment() called without paymentInstrument')); } const params = new HttpParams().set('include', 'paymentMethod'); return this.apiService .currentBasketEndpoint() .put<{ data: PaymentInstrument; included: { paymentMethod: { [id: string]: PaymentMethodBaseData } } }>( 'payments/open-tender', { paymentInstrument }, { headers: this.basketHeaders, params } ) .pipe( map(({ data, included }) => data?.paymentMethod && included ? included.paymentMethod[data.paymentMethod] : undefined ), withLatestFrom(this.store.pipe(select(getCurrentLocale))), concatMap(([pm, currentLocale]) => this.sendRedirectUrlsIfRequired(pm, paymentInstrument, currentLocale)) ); } /** * Checks, if RedirectUrls are requested by the server and sends them if it is necessary. * * @param pm The payment method to determine if redirect is required. * @param paymentInstrument The payment instrument id. * @param lang The language code of the current locale, e.g. en_US * @returns The payment instrument id. */ private sendRedirectUrlsIfRequired( pm: PaymentMethodBaseData, paymentInstrument: string, lang: string ): Observable<string> { if (!pm?.capabilities || !pm.capabilities.some(data => ['RedirectBeforeCheckout'].includes(data))) { return of(paymentInstrument); // send redirect urls if there is a redirect required } else E{ const loc = `${location.origin}${this.baseHref}`; const redirect = { successUrl: `${loc}/checkout/review;lang=${lang}?redirect=success`, cancelUrl: `${loc}/checkout/payment;lang=${lang}?redirect=cancel`, failureUrl: `${loc}/checkout/payment;lang=${lang}?redirect=failure`, }; Iif (pm.capabilities.some(data => ['RedirectAfterCheckout'].includes(data))) { // *OrderID* will be replaced by the ICM server redirect.successUrl = `${loc}/checkout/receipt;lang=${lang}?redirect=success&orderId=*orderID*`; redirect.cancelUrl = `${loc}/checkout/payment;lang=${lang}?redirect=cancel&orderId=*orderID*`; redirect.failureUrl = `${loc}/checkout/payment;lang=${lang}?redirect=failure&orderId=*orderID*`; } const body = { paymentInstrument, redirect }; return this.apiService .currentBasketEndpoint() .put('payments/open-tender', body, { headers: this.basketHeaders }) .pipe(map(() => paymentInstrument)); } } /** * Creates a payment instrument for the selected basket. * * @param paymentInstrument The payment instrument with parameters, id=undefined, paymentMethod= required. * @returns The created payment instrument. */ createBasketPayment(paymentInstrument: PaymentInstrument): Observable<PaymentInstrument> { Iif (!paymentInstrument) { return throwError(() => new Error('createBasketPayment() called without paymentInstrument')); } Iif (!paymentInstrument.paymentMethod) { return throwError(() => new Error('createBasketPayment() called without paymentMethodId')); } const params = new HttpParams().set('include', 'paymentMethod'); return this.apiService .currentBasketEndpoint() .post<{ data: PaymentInstrument }>('payment-instruments', paymentInstrument, { headers: this.basketHeaders, params, }) .pipe(map(({ data }) => data)); } /** * Updates a payment for the selected basket. Used to set redirect query parameters and status after redirect. * * @param redirect The payment redirect information (parameters and status). * @returns The updated payment. */ updateBasketPayment(params: { [key: string]: string }): Observable<Payment> { Iif (!params) { return throwError(() => new Error('updateBasketPayment() called without parameter data')); } Iif (!params.redirect) { return throwError(() => new Error('updateBasketPayment() called without redirect parameter data')); } const redirect = { status: params.redirect.toUpperCase(), parameters: Object.entries(params) .filter(([name]) => name !== 'redirect') .map(([name, value]) => ({ name, value })), }; return this.apiService .currentBasketEndpoint() .patch('payments/open-tender', { redirect }, { headers: this.basketHeaders }) .pipe(map(({ data }) => data)); } /** * Deletes a (basket/user) payment instrument. * If the payment instrument is used at basket the related payment is also deleted from the selected basket. * * @param basket The basket. * @param paymentInstrument The payment instrument, that is to be deleted */ deleteBasketPaymentInstrument(basket: Basket, paymentInstrument: PaymentInstrument): Observable<void> { Iif (!basket) { return throwError(() => new Error('deleteBasketPayment() called without basket')); } Iif (!paymentInstrument) { return throwError(() => new Error('deleteBasketPayment() called without paymentInstrument')); } const deletePayment = basket.payment?.paymentInstrument?.id === paymentInstrument.id; // user payment instrument if (paymentInstrument.urn?.includes('user')) { return this.deleteUserPaymentInstrument('-', paymentInstrument.id).pipe( concatMap(() => (deletePayment ? this.deleteBasketPayment(basket) : of(undefined))) ); } // basket payment instrument, payment will be deleted automatically, if necessary return this.apiService.delete( `baskets/${this.apiService.encodeResourceId(basket.id)}/payment-instruments/${this.apiService.encodeResourceId( paymentInstrument.id )}`, { headers: this.basketHeaders } ); } /** * Deletes the basket payment. * * @param basket The basket. */ deleteBasketPayment(basket: Basket): Observable<void> { Iif (!basket) { return throwError(() => new Error('deleteBasketPayment() called without basket')); } Iif (!basket.payment) { return of(); } return this.apiService.delete( `baskets/${this.apiService.encodeResourceId(basket.id)}/payments/${this.apiService.encodeResourceId( basket.payment.id )}`, { headers: this.basketHeaders } ); } /** * Gets the payment data of the customer. * * @param customer The customer data. * @returns The customer's payments. */ getUserPaymentMethods(customer: Customer): Observable<PaymentMethod[]> { Iif (!customer) { return throwError(() => new Error('getUserPaymentMethods called without required body data')); } return this.appFacade.customerRestResource$.pipe( first(), concatMap(restResource => this.apiService.get(`${restResource}/${this.apiService.encodeResourceId(customer.customerNo)}/payments`).pipe( unpackEnvelope<Link>(), this.apiService.resolveLinks<PaymentInstrumentData>(), concatMap(instruments => this.apiService .options(`${restResource}/${this.apiService.encodeResourceId(customer.customerNo)}/payments`) .pipe( unpackEnvelope<PaymentMethodOptionsDataType>('methods'), map(methods => PaymentMethodMapper.fromOptions({ methods, instruments })) ) ) ) ) ); } /** * Creates a payment instrument at the customer. * * @param customerNo The customer data. * @param paymentInstrument The payment instrument data. * @returns The created payment instrument. */ createUserPayment(customerNo: string, paymentInstrument: PaymentInstrument): Observable<PaymentInstrument> { Iif (!customerNo) { return throwError(() => new Error('createUserPayment called without required customer number')); } Iif (!paymentInstrument?.paymentMethod) { return throwError(() => new Error('createUserPayment called without required valid payment instrument')); } const body: { name: string; parameters?: { key: string; property: string; }[]; } = { name: paymentInstrument.paymentMethod, parameters: paymentInstrument.parameters?.length ? paymentInstrument.parameters.map(attr => ({ key: attr.name, property: attr.value })) : undefined, }; return this.appFacade.customerRestResource$.pipe( first(), concatMap(restResource => this.apiService .post(`${restResource}/${this.apiService.encodeResourceId(customerNo)}/payments`, body) .pipe(this.apiService.resolveLink<PaymentInstrument>()) ) ); } /** * Deletes a payment instrument and the related payment from the given user. * * @param customerNo The customer number. * @param paymentInstrumentId The (uu)id of the payment instrument. */ deleteUserPaymentInstrument(customerNo: string, paymentInstrumentId: string): Observable<void> { Iif (!customerNo) { return throwError(() => new Error('deleteUserPayment() called without customerNo')); } Iif (!paymentInstrumentId) { return throwError(() => new Error('deleteUserPayment() called without paymentInstrumentId')); } return this.appFacade.customerRestResource$.pipe( first(), concatMap(restResource => this.apiService.delete<void>( `${restResource}/${this.apiService.encodeResourceId(customerNo)}/payments/${this.apiService.encodeResourceId( paymentInstrumentId )}` ) ) ); } /** * Update CvcLastUpdated in concardis credit card (user/basket) payment instrument. * * @param paymentInstrument The payment instrument, that is to be updated */ updateConcardisCvcLastUpdated(paymentInstrument: PaymentInstrument): Observable<PaymentInstrument> { Iif (!paymentInstrument) { return throwError(() => new Error('updateConcardisCvcLastUpdated() called without paymentInstrument')); } if (paymentInstrument.urn?.includes('basket')) { // update basket payment instrument const body: { parameters?: { name: string; value: string; }[]; } = { parameters: paymentInstrument.parameters .filter(attr => attr.name === 'cvcLastUpdated') .map(attr => ({ name: attr.name, value: attr.value })), }; return this.apiService .currentBasketEndpoint() .patch<{ data: PaymentInstrument }>( `payment-instruments/${this.apiService.encodeResourceId(paymentInstrument.id)}`, body, { headers: this.basketHeaders } ) .pipe(map(({ data }) => data)); } else { // update user payment instrument const body: { name: string; parameters?: { key: string; property: string; }[]; } = { name: paymentInstrument.paymentMethod, parameters: paymentInstrument.parameters.map(attr => ({ key: attr.name, property: attr.value })), }; return this.apiService .put(`customers/-/payments/${this.apiService.encodeResourceId(paymentInstrument.id)}`, body) .pipe(map(() => paymentInstrument)); } } } |