Skip to content

Commit

Permalink
Limit payment method updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ivank committed Oct 6, 2017
1 parent a6954a8 commit a2d0ae3
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongoose-subscriptions-braintree",
"version": "1.5.0",
"version": "1.5.1",
"description": "Braintree processor for mongoose-subscriptions",
"main": "src/index.js",
"repository": "[email protected]:enhancv/mongoose-subscriptions-braintree.git",
Expand Down
18 changes: 17 additions & 1 deletion src/paymentMethodProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ function fields(customer, paymentMethod) {
return pickBy(identity, response);
}

function isPaymentMethodChanged(customer, paymentMethod) {
const original = paymentMethod.original || {};
const originalCustomer = customer.original || {};
const nonceChanged = paymentMethod.nonce !== original.nonce && paymentMethod.nonce;
const billingAddressIdChanged = paymentMethod.billingAddressId !== original.billingAddressId;
const defaultPaymentMethodChanged =
customer.defaultPaymentMethodId !== originalCustomer.defaultPaymentMethodId &&
customer.defaultPaymentMethodId === paymentMethod.id;

return Boolean(nonceChanged || billingAddressIdChanged || defaultPaymentMethodChanged);
}

function save(processor, customer, paymentMethod, index) {
const data = processorFields(customer, paymentMethod);

Expand All @@ -103,7 +115,10 @@ function save(processor, customer, paymentMethod, index) {
return customer;
}

if (paymentMethod.processor.state === ProcessorItem.CHANGED) {
if (
paymentMethod.processor.state === ProcessorItem.CHANGED &&
isPaymentMethodChanged(customer, paymentMethod)
) {
processor.emit("event", new Event(Event.PAYMENT_METHOD, Event.UPDATING, data));
return processor.gateway.paymentMethod
.update(paymentMethod.processor.id, data)
Expand All @@ -124,5 +139,6 @@ function save(processor, customer, paymentMethod, index) {
module.exports = {
fields: curry(fields),
processorFields: curry(processorFields),
isPaymentMethodChanged: curry(isPaymentMethodChanged),
save: curry(save),
};
56 changes: 56 additions & 0 deletions test/paymentMethodProcessorTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,62 @@ describe("paymentMethodProcessor", () => {
});
});

const isPaymentMethodChanged = [
{
name: "No change",
change: {},
expected: { three: false, four: false },
},
{
name: "Change if default changed",
customer: {
defaultPaymentMethodId: "four",
},
expected: { three: false, four: true },
},
{
name: "Change if nonce changed",
paymentMehtod1: { nonce: "some nonce" },
expected: { three: false, four: true },
},
{
name: "No change if billingAddressId same value",
paymentMehtod1: { billingAddressId: "one" },
expected: { three: false, four: false },
},
{
name: "Change if billingAddressId changed",
paymentMehtod1: { billingAddressId: "123" },
expected: { three: false, four: true },
},
{
name: "Nonce change if nonce same value",
paymentMehtod1: { nonce: null },
expected: { three: false, four: false },
},
];

isPaymentMethodChanged.forEach(test => {
it(`isPaymentMethodChanged should return correct value for ${test.name}`, function() {
this.customer.initOriginals();
this.customer.paymentMethods[0].initOriginals();
this.customer.paymentMethods[1].initOriginals();

this.customer.set(test.customer || {});
this.customer.paymentMethods[0].set(test.paymentMehtod0 || {});
this.customer.paymentMethods[1].set(test.paymentMehtod1 || {});

Object.keys(test.expected).forEach(id => {
const result = paymentMethodProcessor.isPaymentMethodChanged(
this.customer,
this.customer.paymentMethods.id(id)
);

assert.equal(result, test.expected[id]);
});
});
});

it("save should send a rejection on api error", function() {
const apiError = new Error("error");

Expand Down

0 comments on commit a2d0ae3

Please sign in to comment.