klarna.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* global Klarna */
  2. /**
  3. * This file is part of the Klarna KP module
  4. *
  5. * (c) Klarna Bank AB (publ)
  6. *
  7. * For the full copyright and license information, please view the NOTICE
  8. * and LICENSE files that were distributed with this source code.
  9. */
  10. define(
  11. [
  12. 'jquery',
  13. 'Magento_Checkout/js/model/quote',
  14. 'Magento_Customer/js/model/customer',
  15. 'Klarna_Kp/js/model/config',
  16. 'Klarna_Kp/js/model/debug',
  17. 'klarnapi'
  18. ],
  19. function ($, quote, customer, config, debug) {
  20. 'use strict';
  21. return {
  22. buildAddress: function (address, email) {
  23. var addr = {
  24. 'given_name': '',
  25. 'family_name': '',
  26. 'street_address': '',
  27. 'city': '',
  28. 'postal_code': '',
  29. 'country': '',
  30. 'phone': '',
  31. 'email': email
  32. };
  33. if (!address) { // Somehow we got a null passed in
  34. return addr;
  35. }
  36. if (address.prefix) {
  37. addr['title'] = address.prefix;
  38. }
  39. if (address.firstname) {
  40. addr['given_name'] = address.firstname;
  41. }
  42. if (address.lastname) {
  43. addr['family_name'] = address.lastname;
  44. }
  45. if (address.street) {
  46. if (address.street.length > 0) {
  47. addr['street_address'] = address.street[0];
  48. }
  49. if (address.street.length > 1) {
  50. addr['street_address2'] = address.street[1];
  51. }
  52. }
  53. if (address.city) {
  54. addr['city'] = address.city;
  55. }
  56. if (address.regionCode) {
  57. addr['region'] = address.regionCode;
  58. }
  59. if (address.postcode) {
  60. addr['postal_code'] = address.postcode;
  61. }
  62. if (address.countryId) {
  63. addr['country'] = address.countryId;
  64. }
  65. if (address.telephone) {
  66. addr['phone'] = address.telephone;
  67. }
  68. debug.log(addr);
  69. return addr;
  70. },
  71. getUpdateData: function () {
  72. var email = '',
  73. shippingAddress = quote.shippingAddress(),
  74. data = {
  75. 'billing_address': {},
  76. 'shipping_address': {}
  77. };
  78. if (customer.isLoggedIn()) {
  79. email = customer.customerData.email;
  80. } else {
  81. email = quote.guestEmail;
  82. }
  83. if (quote.isVirtual()) {
  84. shippingAddress = quote.billingAddress();
  85. }
  86. data.billing_address = this.buildAddress(quote.billingAddress(), email);
  87. data.shipping_address = this.buildAddress(shippingAddress, email);
  88. debug.log(data);
  89. return data;
  90. },
  91. load: function (payment_method, container_id, callback) {
  92. var data = null;
  93. debug.log('Loading container ' + container_id);
  94. if ($('#' + container_id).length) {
  95. debug.log('Loading method ' + payment_method);
  96. data = this.getUpdateData();
  97. Klarna.Payments.load(
  98. {
  99. payment_method_category: payment_method,
  100. container: "#" + container_id
  101. },
  102. data,
  103. function (res) {
  104. var errors = false;
  105. debug.log(res);
  106. if (res.errors) {
  107. errors = true;
  108. }
  109. config.hasErrors(errors);
  110. if (callback) {
  111. callback(res);
  112. }
  113. }
  114. );
  115. }
  116. },
  117. init: function () {
  118. Klarna.Payments.init({
  119. client_token: config.client_token
  120. });
  121. },
  122. authorize: function (payment_method, data, callback) {
  123. Klarna.Payments.authorize(
  124. {
  125. payment_method_category: payment_method
  126. },
  127. data,
  128. function (res) {
  129. var errors = false;
  130. debug.log(res);
  131. if (true === res.approved) {
  132. config.authorization_token(res.authorization_token);
  133. }
  134. if (res.errors) {
  135. errors = true;
  136. }
  137. config.hasErrors(errors);
  138. callback(res);
  139. }
  140. );
  141. },
  142. finalize: function (payment_method, data, callback) {
  143. Klarna.Payments.finalize(
  144. {
  145. payment_method_category: payment_method
  146. },
  147. data,
  148. function (res) {
  149. var errors = false;
  150. debug.log(res);
  151. if (true === res.approved) {
  152. config.authorization_token(res.authorization_token);
  153. }
  154. if (res.errors) {
  155. errors = true;
  156. }
  157. config.hasErrors(errors);
  158. callback(res);
  159. }
  160. );
  161. }
  162. };
  163. }
  164. );