Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

132 строки
4.6 KiB

  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'jquery',
  7. 'uiClass',
  8. 'Magento_CardinalCommerce/js/cardinal-factory',
  9. 'Magento_Checkout/js/model/quote',
  10. 'mage/translate'
  11. ], function ($, Class, cardinalFactory, quote, $t) {
  12. 'use strict';
  13. return {
  14. /**
  15. * Starts Cardinal Consumer Authentication
  16. *
  17. * @param {Object} cardData
  18. * @return {jQuery.Deferred}
  19. */
  20. startAuthentication: function (cardData) {
  21. var deferred = $.Deferred();
  22. if (this.cardinalClient) {
  23. this._startAuthentication(deferred, cardData);
  24. } else {
  25. cardinalFactory(this.getEnvironment())
  26. .done(function (client) {
  27. this.cardinalClient = client;
  28. this._startAuthentication(deferred, cardData);
  29. }.bind(this));
  30. }
  31. return deferred.promise();
  32. },
  33. /**
  34. * Cardinal Consumer Authentication
  35. *
  36. * @param {jQuery.Deferred} deferred
  37. * @param {Object} cardData
  38. */
  39. _startAuthentication: function (deferred, cardData) {
  40. //this.cardinalClient.configure({ logging: { level: 'verbose' } });
  41. this.cardinalClient.on('payments.validated', function (data, jwt) {
  42. if (data.ErrorNumber !== 0) {
  43. deferred.reject(data.ErrorDescription);
  44. } else if ($.inArray(data.ActionCode, ['FAILURE', 'ERROR']) !== -1) {
  45. deferred.reject($t('Authentication Failed. Please try again with another form of payment.'));
  46. } else {
  47. deferred.resolve(jwt);
  48. }
  49. this.cardinalClient.off('payments.validated');
  50. }.bind(this));
  51. this.cardinalClient.on('payments.setupComplete', function () {
  52. this.cardinalClient.start('cca', this.getRequestOrderObject(cardData));
  53. this.cardinalClient.off('payments.setupComplete');
  54. }.bind(this));
  55. this.cardinalClient.setup('init', {
  56. jwt: this.getRequestJWT()
  57. });
  58. },
  59. /**
  60. * Returns request order object.
  61. *
  62. * The request order object is structured object that is used to pass data
  63. * to Cardinal that describes an order you'd like to process.
  64. *
  65. * If you pass a request object in both the JWT and the browser,
  66. * Cardinal will merge the objects together where the browser overwrites
  67. * the JWT object as it is considered the most recently captured data.
  68. *
  69. * @param {Object} cardData
  70. * @returns {Object}
  71. */
  72. getRequestOrderObject: function (cardData) {
  73. var totalAmount = quote.totals()['base_grand_total'],
  74. currencyCode = quote.totals()['base_currency_code'],
  75. billingAddress = quote.billingAddress(),
  76. requestObject;
  77. requestObject = {
  78. OrderDetails: {
  79. Amount: totalAmount * 100,
  80. CurrencyCode: currencyCode
  81. },
  82. Consumer: {
  83. Account: {
  84. AccountNumber: cardData.accountNumber,
  85. ExpirationMonth: cardData.expMonth,
  86. ExpirationYear: cardData.expYear,
  87. CardCode: cardData.cardCode
  88. },
  89. BillingAddress: {
  90. FirstName: billingAddress.firstname,
  91. LastName: billingAddress.lastname,
  92. Address1: billingAddress.street[0],
  93. Address2: billingAddress.street[1],
  94. City: billingAddress.city,
  95. State: billingAddress.region,
  96. PostalCode: billingAddress.postcode,
  97. CountryCode: billingAddress.countryId,
  98. Phone1: billingAddress.telephone
  99. }
  100. }
  101. };
  102. return requestObject;
  103. },
  104. /**
  105. * Returns request JWT
  106. * @returns {String}
  107. */
  108. getRequestJWT: function () {
  109. return window.checkoutConfig.cardinal.requestJWT;
  110. },
  111. /**
  112. * Returns type of environment
  113. * @returns {String}
  114. */
  115. getEnvironment: function () {
  116. return window.checkoutConfig.cardinal.environment;
  117. }
  118. };
  119. });