-
Notifications
You must be signed in to change notification settings - Fork 6
/
paymentMethodProcessor.js
106 lines (94 loc) · 4.12 KB
/
paymentMethodProcessor.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const ProcessorItem = require('../').Schema.ProcessorItem;
const braintree = require('braintree');
const Event = require('./Event');
const name = require('./name');
const { get, pickBy, identity, curry } = require('lodash/fp');
function processorFields (customer, paymentMethod) {
const response = {
billingAddressId: paymentMethod.billingAddressId ? get('processor.id', customer.addresses.id(paymentMethod.billingAddressId)) : null,
paymentMethodNonce: paymentMethod.nonce,
options: customer.defaultPaymentMethodId === paymentMethod.id ? { makeDefault: true } : null,
}
return pickBy(identity, response);
}
function fields (customer, paymentMethod) {
const response = {};
if (paymentMethod instanceof braintree.CreditCard) {
Object.assign(response, {
__t: 'CreditCard',
maskedNumber: paymentMethod.maskedNumber,
countryOfIssuance: paymentMethod.countryOfIssuance,
issuingBank: paymentMethod.issuingBank,
cardType: paymentMethod.cardType,
cardholderName: paymentMethod.cardholderName,
expirationMonth: paymentMethod.expirationMonth,
expirationYear: paymentMethod.expirationYear,
});
} else if (paymentMethod instanceof braintree.PayPalAccount) {
Object.assign(response, {
__t: 'PayPalAccount',
name: name.full(paymentMethod.payerInfo.firstName, paymentMethod.payerInfo.lastName),
payerId: paymentMethod.payerInfo.payerId,
email: paymentMethod.email,
});
} else if (paymentMethod instanceof braintree.ApplePayCard) {
Object.assign(response, {
__t: 'ApplePayCard',
cardType: paymentMethod.cardType,
paymentInstrumentName: paymentMethod.paymentInstrumentName,
expirationMonth: paymentMethod.expirationMonth,
expirationYear: paymentMethod.expirationYear,
});
} else if (paymentMethod instanceof braintree.AndroidPayCard) {
Object.assign(response, {
__t: 'AndroidPayCard',
sourceCardLast4: paymentMethod.sourceCardLast4,
virtualCardLast4: paymentMethod.virtualCardLast4,
sourceCardType: paymentMethod.sourceCardType,
virtualCardType: paymentMethod.virtualCardType,
expirationMonth: paymentMethod.expirationMonth,
expirationYear: paymentMethod.expirationYear,
});
}
Object.assign(response, {
processor: {
id: paymentMethod.token,
state: ProcessorItem.SAVED,
},
billingAddressId: ProcessorItem.getId(get('billingAddress.id', paymentMethod), customer.addresses),
createdAt: paymentMethod.createdAt,
updatedAt: paymentMethod.updatedAt,
});
return pickBy(identity, response);
}
function save (processor, customer, paymentMethod) {
const data = processorFields(customer, paymentMethod);
return new Promise((resolve, reject) => {
function callback (err, result) {
if (err) {
reject(err);
} else if (result.success) {
processor.emit('event', new Event(Event.PAYMENT_METHOD, Event.SAVED, result));
Object.assign(paymentMethod, fields(customer, result.paymentMethod), { nonce: null });
resolve(customer);
} else {
reject(new Error(result.message));
}
}
if (paymentMethod.processor.state === ProcessorItem.CHANGED) {
processor.emit('event', new Event(Event.PAYMENT_METHOD, Event.UPDATING, data));
processor.gateway.paymentMethod.update(paymentMethod.processor.id, data, callback);
} else if (paymentMethod.processor.state === ProcessorItem.INITIAL) {
data.customerId = customer.processor.id;
processor.emit('event', new Event(Event.PAYMENT_METHOD, Event.CREATING, data));
processor.gateway.paymentMethod.create(data, callback);
} else {
resolve(paymentMethod);
}
});
}
module.exports = {
fields: curry(fields),
processorFields: curry(processorFields),
save: save,
}