| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- /* global Klarna */
- /**
- * This file is part of the Klarna KP module
- *
- * (c) Klarna Bank AB (publ)
- *
- * For the full copyright and license information, please view the NOTICE
- * and LICENSE files that were distributed with this source code.
- */
- define(
- [
- 'jquery',
- 'Magento_Checkout/js/model/quote',
- 'Magento_Customer/js/model/customer',
- 'Klarna_Kp/js/model/config',
- 'Klarna_Kp/js/model/debug',
- 'klarnapi'
- ],
- function ($, quote, customer, config, debug) {
- 'use strict';
- return {
- buildAddress: function (address, email) {
- var addr = {
- 'given_name': '',
- 'family_name': '',
- 'street_address': '',
- 'city': '',
- 'postal_code': '',
- 'country': '',
- 'phone': '',
- 'email': email
- };
- if (!address) { // Somehow we got a null passed in
- return addr;
- }
- if (address.prefix) {
- addr['title'] = address.prefix;
- }
- if (address.firstname) {
- addr['given_name'] = address.firstname;
- }
- if (address.lastname) {
- addr['family_name'] = address.lastname;
- }
- if (address.street) {
- if (address.street.length > 0) {
- addr['street_address'] = address.street[0];
- }
- if (address.street.length > 1) {
- addr['street_address2'] = address.street[1];
- }
- }
- if (address.city) {
- addr['city'] = address.city;
- }
- if (address.regionCode) {
- addr['region'] = address.regionCode;
- }
- if (address.postcode) {
- addr['postal_code'] = address.postcode;
- }
- if (address.countryId) {
- addr['country'] = address.countryId;
- }
- if (address.telephone) {
- addr['phone'] = address.telephone;
- }
- debug.log(addr);
- return addr;
- },
- getUpdateData: function () {
- var email = '',
- shippingAddress = quote.shippingAddress(),
- data = {
- 'billing_address': {},
- 'shipping_address': {}
- };
- if (customer.isLoggedIn()) {
- email = customer.customerData.email;
- } else {
- email = quote.guestEmail;
- }
- if (quote.isVirtual()) {
- shippingAddress = quote.billingAddress();
- }
- data.billing_address = this.buildAddress(quote.billingAddress(), email);
- data.shipping_address = this.buildAddress(shippingAddress, email);
- debug.log(data);
- return data;
- },
- load: function (payment_method, container_id, callback) {
- var data = null;
- debug.log('Loading container ' + container_id);
- if ($('#' + container_id).length) {
- debug.log('Loading method ' + payment_method);
- data = this.getUpdateData();
- Klarna.Payments.load(
- {
- payment_method_category: payment_method,
- container: "#" + container_id
- },
- data,
- function (res) {
- var errors = false;
- debug.log(res);
- if (res.errors) {
- errors = true;
- }
- config.hasErrors(errors);
- if (callback) {
- callback(res);
- }
- }
- );
- }
- },
- init: function () {
- Klarna.Payments.init({
- client_token: config.client_token
- });
- },
- authorize: function (payment_method, data, callback) {
- Klarna.Payments.authorize(
- {
- payment_method_category: payment_method
- },
- data,
- function (res) {
- var errors = false;
- debug.log(res);
- if (true === res.approved) {
- config.authorization_token(res.authorization_token);
- }
- if (res.errors) {
- errors = true;
- }
- config.hasErrors(errors);
- callback(res);
- }
- );
- },
- finalize: function (payment_method, data, callback) {
- Klarna.Payments.finalize(
- {
- payment_method_category: payment_method
- },
- data,
- function (res) {
- var errors = false;
- debug.log(res);
- if (true === res.approved) {
- config.authorization_token(res.authorization_token);
- }
- if (res.errors) {
- errors = true;
- }
- config.hasErrors(errors);
- callback(res);
- }
- );
- }
- };
- }
- );
|