This repository has been archived by the owner on Feb 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheckout.js
91 lines (78 loc) · 3.82 KB
/
checkout.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
'use strict';
module.exports = function (RED) {
var uglycart = require('./lib/UGLYCART.js');
var YaaS = require('yaas.js');
function YaasCheckoutNode(config) {
RED.nodes.createNode(this, config);
var yaas = new YaaS();
this.yaasCustomerCredentials = RED.nodes.getNode(config.yaasCustomerCredentials);
this.yaasCredentials = RED.nodes.getNode(config.yaasCredentials);
this.stripeCredentials = RED.nodes.getNode(config.stripeCredentials);
this.tenant_id = this.yaasCredentials.application_id.split('.')[0];
this.currency = config.currency;
this.siteCode = config.siteCode;
this.status({ fill: 'yellow', shape: 'dot', text: 'idle' });
this.on('input', msg => {
var email = this.yaasCustomerCredentials.email;
var inputEmail = '' + msg.payload;
if (inputEmail.indexOf('@') > 0) {
// the input seems to be an email (and not a twitter account)
email = inputEmail;
}
var customer;
var addresses;
yaas.init(this.yaasCredentials.client_id,
this.yaasCredentials.client_secret,
'hybris.checkout_manage hybris.customer_read hybris.cart_manage',
this.tenant_id)
.then(() => yaas.customer.getCustomers({ q: 'contactEmail:"' + email + '"', expand: 'addresses' }))
.then(response => {
customer = response.body[0];
customer.email = email; // TODO: uuh wow nice inconsistency here
addresses = [
Object.assign({},
customer.addresses[0]),
customer.addresses.length > 1 ?
Object.assign({}, customer.addresses[1]) :
Object.assign({}, customer.addresses[0])
];
delete customer.addresses;
addresses[0].type = 'BILLING';
addresses[1].type = 'SHIPPING';
this.status({ fill: 'yellow', shape: 'dot', text: 'got customer with id ' + customer.customerNumber });
return uglycart.getCartByCustomerId(yaas, customer.customerNumber, this.siteCode, this.currency);
})
.then(cart => {
this.status({ fill: 'yellow', shape: 'dot', text: 'got cart id ' + cart.cartId });
var obj = {
paymentMethods: [{
provider: 'stripe',
method: 'invoice',
paymentMethodYrn: 'urn:yaas:hybris:payments:payment-method:' + this.tenant_id + ';invoice',
customAttributes:
{
token: ""
}
}],
cartId: cart.cartId,
customer: customer,
addresses: addresses,
currency: this.currency,
siteCode: this.siteCode
};
return yaas.checkout.checkout(obj);
})
.then(response => {
this.send({ payload: response.body.orderId });
this.status({ fill: 'green', shape: 'dot', text: 'order created: ' + response.body.orderId });
})
.catch(error => {
console.log('error');
error = error.body ? error.body.details[0].message : error;
this.status({ fill: 'red', shape: 'dot', text: 'error: ' + error });
console.error(error);
});
});
}
RED.nodes.registerType('checkout', YaasCheckoutNode);
};