@xsolla/payment-client-core 0.2.53 → 0.2.55

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