@xsolla/payment-client-core 0.2.53 → 0.2.54

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8305,7 +8305,7 @@ class BraintreeHandler extends ApplePaySessionHandlerAbstract {
8305
8305
  setClient(client) {
8306
8306
  this.paymentClient = client;
8307
8307
  }
8308
- startSession(params, additionalParams) {
8308
+ startSession(params, additionalParams, applePayFinallyCallback) {
8309
8309
  if (!this.paymentClient) {
8310
8310
  console.error(ApplePayErrors.braintreeInitializeError);
8311
8311
  this.elkLoggerService.log(ApplePayErrors.braintreeInitializeError, [
@@ -8353,6 +8353,7 @@ class BraintreeHandler extends ApplePaySessionHandlerAbstract {
8353
8353
  .catch((error) => {
8354
8354
  console.error(ApplePayErrors.merchantValidationError, error);
8355
8355
  session.abort();
8356
+ applePayFinallyCallback();
8356
8357
  });
8357
8358
  };
8358
8359
  session.onpaymentauthorized = (event) => {
@@ -8366,15 +8367,18 @@ class BraintreeHandler extends ApplePaySessionHandlerAbstract {
8366
8367
  shippingEmail: event.payment.shippingContact?.emailAddress ?? null,
8367
8368
  }));
8368
8369
  session.completePayment(this.window.ApplePaySession.STATUS_SUCCESS);
8370
+ applePayFinallyCallback();
8369
8371
  })
8370
8372
  .catch((error) => {
8371
8373
  console.error('Apple Pay: Tokenization error', error);
8372
8374
  session.completePayment(this.window.ApplePaySession.STATUS_FAILURE);
8375
+ applePayFinallyCallback();
8373
8376
  });
8374
8377
  };
8375
8378
  session.oncancel = (event) => {
8376
8379
  console.error(ApplePayErrors.cancelled, event);
8377
8380
  this.emitError(ApplePayErrors.cancelled);
8381
+ applePayFinallyCallback();
8378
8382
  };
8379
8383
  session.begin();
8380
8384
  }
@@ -8395,7 +8399,7 @@ class CheckoutHandler extends ApplePaySessionHandlerAbstract {
8395
8399
  this.http = http;
8396
8400
  this.applePayCartService = applePayCartService;
8397
8401
  }
8398
- startSession(params, additionalParams) {
8402
+ startSession(params, additionalParams, applePayFinallyCallback) {
8399
8403
  const request = this.buildPaymentRequest(params);
8400
8404
  const session = new this.window.ApplePaySession(applePayVersion, request);
8401
8405
  session.onshippingcontactselected = (event) => {
@@ -8439,6 +8443,7 @@ class CheckoutHandler extends ApplePaySessionHandlerAbstract {
8439
8443
  error: (error) => {
8440
8444
  console.error(ApplePayErrors.merchantValidationError, error);
8441
8445
  session.abort();
8446
+ applePayFinallyCallback();
8442
8447
  },
8443
8448
  });
8444
8449
  };
@@ -8450,10 +8455,12 @@ class CheckoutHandler extends ApplePaySessionHandlerAbstract {
8450
8455
  shippingEmail: event.payment.shippingContact?.emailAddress ?? null,
8451
8456
  }));
8452
8457
  session.completePayment(this.window.ApplePaySession.STATUS_SUCCESS);
8458
+ applePayFinallyCallback();
8453
8459
  };
8454
8460
  session.oncancel = (event) => {
8455
8461
  console.error(ApplePayErrors.cancelled, event);
8456
8462
  this.emitError(ApplePayErrors.cancelled);
8463
+ applePayFinallyCallback();
8457
8464
  };
8458
8465
  session.begin();
8459
8466
  }
@@ -9146,6 +9153,95 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.3", ngImpor
9146
9153
  }]
9147
9154
  }] });
9148
9155
 
9156
+ class ApplePaySdkService {
9157
+ constructor(window, loggerService) {
9158
+ this.window = window;
9159
+ this.loggerService = loggerService;
9160
+ this.sdkLoaded = false;
9161
+ this.jsSdk = 'https://applepay.cdn-apple.com/jsapi/1.latest/apple-pay-sdk.js';
9162
+ this.sdkSuccessfulPayment = new Subject();
9163
+ this.windowClosing = new Subject();
9164
+ this.successPaymentType = 'successClose';
9165
+ this.windowClosingType = 'windowclosing';
9166
+ this.needToAddApplePayJs = !this.window.ApplePaySession;
9167
+ }
9168
+ get windowClosing$() {
9169
+ return this.windowClosing.asObservable();
9170
+ }
9171
+ emitWindowClosing() {
9172
+ return this.windowClosing.next();
9173
+ }
9174
+ addApplePaySdkJs() {
9175
+ const isApplePayJsAdded = this.isAppleSdkExist();
9176
+ if (!this.needToAddApplePayJs) {
9177
+ if (isApplePayJsAdded) {
9178
+ this.sdkLoaded = true;
9179
+ }
9180
+ return;
9181
+ }
9182
+ const listenApplePayPostMessages = () => {
9183
+ this.applePaySdkPostMessagesListener = (event) => {
9184
+ /* istanbul ignore if */
9185
+ if (!event.origin.includes('apple')) {
9186
+ return;
9187
+ }
9188
+ /* istanbul ignore if */
9189
+ if (event.data.messageType === this.windowClosingType) {
9190
+ this.emitWindowClosing();
9191
+ }
9192
+ /* istanbul ignore if */
9193
+ if (event.data.messageType === this.successPaymentType) {
9194
+ this.sdkSuccessfulPayment.next();
9195
+ }
9196
+ };
9197
+ this.window.addEventListener('message', this.applePaySdkPostMessagesListener);
9198
+ };
9199
+ if (isApplePayJsAdded && this.window.ApplePaySession) {
9200
+ this.sdkLoaded = true;
9201
+ listenApplePayPostMessages();
9202
+ return;
9203
+ }
9204
+ const firstScriptElement = this.window.document.getElementsByTagName('script')[0];
9205
+ const script = this.window.document.createElement('script');
9206
+ script.async = true;
9207
+ script.crossOrigin = 'anonymous';
9208
+ script.src = `${this.jsSdk}`;
9209
+ firstScriptElement?.parentNode?.insertBefore(script, firstScriptElement);
9210
+ script.onload = () => {
9211
+ this.sdkLoaded = true;
9212
+ listenApplePayPostMessages();
9213
+ };
9214
+ script.onerror = (error) => {
9215
+ this.loggerService.log('Apple pay: script sdk loading error', [ErrorTags.APPLEPAY],
9216
+ // @ts-expect-error error type
9217
+ { error: error?.message ? error.toString() : JSON.stringify(error) });
9218
+ console.error('Apple pay: script sdk loading error', error);
9219
+ };
9220
+ }
9221
+ /* istanbul ignore next */
9222
+ removeListeners() {
9223
+ if (!this.applePaySdkPostMessagesListener) {
9224
+ return;
9225
+ }
9226
+ this.window.removeEventListener('message', this.applePaySdkPostMessagesListener);
9227
+ }
9228
+ isAppleSdkExist() {
9229
+ const scriptsElements = this.window.document.querySelectorAll('script');
9230
+ return Array.from(scriptsElements).some((element) => element.src === this.jsSdk);
9231
+ }
9232
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.3", ngImport: i0, type: ApplePaySdkService, deps: [{ token: windowToken }, { token: ElkLoggerService }], target: i0.ɵɵFactoryTarget.Injectable }); }
9233
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.3.3", ngImport: i0, type: ApplePaySdkService, providedIn: 'root' }); }
9234
+ }
9235
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.3", ngImport: i0, type: ApplePaySdkService, decorators: [{
9236
+ type: Injectable,
9237
+ args: [{
9238
+ providedIn: 'root',
9239
+ }]
9240
+ }], ctorParameters: () => [{ type: undefined, decorators: [{
9241
+ type: Inject,
9242
+ args: [windowToken]
9243
+ }] }, { type: ElkLoggerService }] });
9244
+
9149
9245
  class ApplePayService {
9150
9246
  get paymentParameters() {
9151
9247
  return this.applePayParams;
@@ -9156,26 +9252,43 @@ class ApplePayService {
9156
9252
  get error$() {
9157
9253
  return this.error.asObservable();
9158
9254
  }
9255
+ /* istanbul ignore next */
9256
+ get applePayQrOpened$() {
9257
+ return this.applePayQrOpened.asObservable();
9258
+ }
9259
+ /* istanbul ignore next */
9260
+ get windowClosing$() {
9261
+ return this.applePaySdkService.windowClosing$;
9262
+ }
9159
9263
  get integrationType$() {
9160
9264
  return this.integrationType.asObservable();
9161
9265
  }
9162
- constructor(window, userAgentService, summaryFinanceDetailsAdapterService, braintreeHandler, checkoutHandler, appleParamsBuilder, elkLoggerService) {
9266
+ constructor(window, summaryFinanceDetailsAdapterService, braintreeHandler, checkoutHandler, appleParamsBuilder, elkLoggerService, applePaySdkService) {
9163
9267
  this.window = window;
9164
- this.userAgentService = userAgentService;
9165
9268
  this.summaryFinanceDetailsAdapterService = summaryFinanceDetailsAdapterService;
9166
9269
  this.braintreeHandler = braintreeHandler;
9167
9270
  this.checkoutHandler = checkoutHandler;
9168
9271
  this.appleParamsBuilder = appleParamsBuilder;
9169
9272
  this.elkLoggerService = elkLoggerService;
9273
+ this.applePaySdkService = applePaySdkService;
9170
9274
  this.payment = new Subject();
9171
9275
  this.error = new Subject();
9172
9276
  this.integrationType = new Subject();
9277
+ this.applePayQrOpened = new Subject();
9173
9278
  this.paymentClient = null;
9174
9279
  this.applePayParams = null;
9175
9280
  this.order = null;
9176
9281
  this.api = this.loadBraintree();
9177
9282
  this.subscribeToHandlers();
9178
9283
  }
9284
+ /* istanbul ignore next */
9285
+ addApplePaySdkJs() {
9286
+ this.applePaySdkService.addApplePaySdkJs();
9287
+ }
9288
+ /* istanbul ignore next */
9289
+ removeListeners() {
9290
+ this.applePaySdkService.removeListeners();
9291
+ }
9179
9292
  setApplePayParams(data) {
9180
9293
  const paymentForm = data?.paymentForm;
9181
9294
  const formFields = paymentForm?.fields;
@@ -9249,8 +9362,9 @@ class ApplePayService {
9249
9362
  }
9250
9363
  }
9251
9364
  get isDeviceCapable() {
9252
- if (!this.userAgentService.isSafari)
9253
- return false;
9365
+ if (this.applePaySdkService.needToAddApplePayJs) {
9366
+ return true;
9367
+ }
9254
9368
  try {
9255
9369
  return Boolean(this.window.ApplePaySession?.supportsVersion(applePayVersion) &&
9256
9370
  this.window.ApplePaySession?.canMakePayments());
@@ -9277,7 +9391,8 @@ class ApplePayService {
9277
9391
  console.error('Apple Pay: Unsupported acquirer:', parameters.acquirer);
9278
9392
  return;
9279
9393
  }
9280
- sessionHandler.startSession(parameters, additionalParams);
9394
+ sessionHandler.startSession(parameters, additionalParams, () => this.applePaySdkService.emitWindowClosing());
9395
+ this.emitApplePayOpened();
9281
9396
  }
9282
9397
  getSessionHandler(acquirer) {
9283
9398
  switch (acquirer) {
@@ -9297,10 +9412,7 @@ class ApplePayService {
9297
9412
  this.error.next(error);
9298
9413
  }
9299
9414
  emitIntegrationType() {
9300
- const integration = this.isDeviceCapable
9301
- ? ApplePayIntegrationType.native
9302
- : ApplePayIntegrationType.redirect;
9303
- this.integrationType.next(integration);
9415
+ this.integrationType.next(this.getIntegrationType());
9304
9416
  }
9305
9417
  subscribeToHandlers() {
9306
9418
  const handlers = [this.braintreeHandler, this.checkoutHandler];
@@ -9311,7 +9423,19 @@ class ApplePayService {
9311
9423
  });
9312
9424
  });
9313
9425
  }
9314
- static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.3", ngImport: i0, type: ApplePayService, deps: [{ token: windowToken }, { token: UserAgentService }, { token: SummaryFinanceDetailsAdapterService }, { token: BraintreeHandler }, { token: CheckoutHandler }, { token: ApplePayParamsBuilderService }, { token: ElkLoggerService }], target: i0.ɵɵFactoryTarget.Injectable }); }
9426
+ getIntegrationType() {
9427
+ /* istanbul ignore next */
9428
+ return this.isDeviceCapable
9429
+ ? ApplePayIntegrationType.native
9430
+ : ApplePayIntegrationType.redirect;
9431
+ }
9432
+ emitApplePayOpened() {
9433
+ if (this.getIntegrationType() === ApplePayIntegrationType.native &&
9434
+ this.applePaySdkService.needToAddApplePayJs) {
9435
+ this.applePayQrOpened.next();
9436
+ }
9437
+ }
9438
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.3", ngImport: i0, type: ApplePayService, deps: [{ token: windowToken }, { token: SummaryFinanceDetailsAdapterService }, { token: BraintreeHandler }, { token: CheckoutHandler }, { token: ApplePayParamsBuilderService }, { token: ElkLoggerService }, { token: ApplePaySdkService }], target: i0.ɵɵFactoryTarget.Injectable }); }
9315
9439
  static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.3.3", ngImport: i0, type: ApplePayService, providedIn: 'root' }); }
9316
9440
  }
9317
9441
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.3", ngImport: i0, type: ApplePayService, decorators: [{
@@ -9322,7 +9446,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.3", ngImpor
9322
9446
  }], ctorParameters: () => [{ type: undefined, decorators: [{
9323
9447
  type: Inject,
9324
9448
  args: [windowToken]
9325
- }] }, { type: UserAgentService }, { type: SummaryFinanceDetailsAdapterService }, { type: BraintreeHandler }, { type: CheckoutHandler }, { type: ApplePayParamsBuilderService }, { type: ElkLoggerService }] });
9449
+ }] }, { type: SummaryFinanceDetailsAdapterService }, { type: BraintreeHandler }, { type: CheckoutHandler }, { type: ApplePayParamsBuilderService }, { type: ElkLoggerService }, { type: ApplePaySdkService }] });
9326
9450
 
9327
9451
  /*
9328
9452
  * Public API Surface of core-library