-
Notifications
You must be signed in to change notification settings - Fork 0
/
basketPaymentHooks.js
58 lines (56 loc) · 2.21 KB
/
basketPaymentHooks.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
var Transaction = require("dw/system/Transaction");
var service = require("~/cartridge/scripts/stripe/service");
// eslint-disable-next-line no-unused-vars
exports.beforePOST = function (basket, paymentInstrumentRequest) {
const instruments = basket.getPaymentInstruments("STRIPE").iterator();
if (instruments.hasNext()) {
const instrument = instruments.next();
basket.removePaymentInstrument(instrument);
}
if (paymentInstrumentRequest.paymentMethodId === "STRIPE") {
Transaction.wrap(function () {
const profile = request.session.customer.getProfile();
if (!profile.custom.stripeCustomerId) {
const customer = service.createCustomer(
`${profile.lastName} ${profile.firstName}`,
profile.email,
profile.customerNo
);
profile.custom.stripeCustomerId = customer.id;
}
let intent;
if (!basket.custom.stripeIntentId) {
intent = service.createPaymentIntent(
basket.getTotalGrossPrice().getValue(),
basket.getCurrencyCode().toLowerCase(),
paymentInstrumentRequest.c_stripePyamentMethodId,
profile.custom.stripeCustomerId
);
} else {
intent = service.updatePaymentIntent(
basket.custom.stripeIntentId,
basket.getTotalGrossPrice().getValue(),
basket.getCurrencyCode().toLowerCase(),
paymentInstrumentRequest.c_stripePyamentMethodId,
profile.custom.stripeCustomerId
);
}
basket.custom.stripeIntentId = intent.id;
basket.custom.stripeClientSecret = intent.client_secret;
});
}
};
// eslint-disable-next-line no-unused-vars
exports.afterPOST = function (basket, paymentInstrumentRequest) {
Transaction.wrap(function () {
const instruments = basket.getPaymentInstruments("STRIPE").iterator();
if (instruments.hasNext()) {
const instrument = instruments.next();
instrument.custom.stripePaymentMethodId =
paymentInstrumentRequest.c_stripePyamentMethodId;
instrument.custom.willSaved = paymentInstrumentRequest.c_willSaved;
instrument.custom.stripeIntentId = basket.custom.stripeIntentId;
instrument.custom.stripeClientSecret = basket.custom.stripeClientSecret;
}
});
};