Skip to content

Commit

Permalink
Fixes balanced#1631: Make debit this customer create orders automatic…
Browse files Browse the repository at this point in the history
…ally
  • Loading branch information
kyungmin committed Jan 26, 2015
1 parent 8b4a1c9 commit 2a72a27
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 166 deletions.
134 changes: 33 additions & 101 deletions app/models/factories/debit-card-transaction-factory.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
import Debit from "../debit";
import Card from "../card";
import Customer from "../customer";
import TransactionFactory from "./transaction-factory";
import DebitOrderFactory from "./debit-order-factory";
import ValidationHelpers from "balanced-dashboard/utils/validation-helpers";
import Customer from "../customer";
import Card from "../card";

var EXPIRATION_DATE_FORMAT = /^(\d\d) [\/-] (\d\d\d\d)$/;

var DebitCardTransactionFactory = TransactionFactory.extend({
getDestinationAttributes: function() {
var DebitCardTransactionFactory = DebitOrderFactory.extend({
getSourceAttributes: function() {
var attributes = this.getProperties("name", "number", "cvv", "expiration_month", "expiration_year");
attributes.address = {
postal_code: this.get("postal_code")
};
return attributes;
},

getDebitAttributes: function() {
var attributes = this.getProperties("amount", "appears_on_statement_as");
attributes.description = this.get("debit_description");
return attributes;
},

validations: {
dollar_amount: ValidationHelpers.positiveDollarAmount,
appears_on_statement_as: ValidationHelpers.cardTransactionAppearsOnStatementAs,
Expand All @@ -45,6 +38,33 @@ var DebitCardTransactionFactory = TransactionFactory.extend({
}
},

getSource: function(buyer) {
return Card
.create(this.getSourceAttributes())
.tokenizeAndCreate(buyer.get("uri"));
},

getBuyerCustomerAttributes: function() {
var email = this.get("buyer_email_address");
if (Ember.isBlank(email)) {
email = undefined;
}
return {
name: this.get("buyer_name"),
email: email
};
},

getBuyer: function() {
var customer = this.get("customer");

if (customer) {
return customer;
} else {
return Customer.create(this.getBuyerCustomerAttributes()).save();
}
},

getExpirationDate: function() {
var match = this.getExpirationDateMatch();
if (match) {
Expand Down Expand Up @@ -74,95 +94,7 @@ var DebitCardTransactionFactory = TransactionFactory.extend({
if (match) {
return match[2];
}
}.property("expiration_date"),

saveCard: function(buyerUri) {
return Card
.create(this.getDestinationAttributes())
.tokenizeAndCreate(buyerUri);
},

getBuyerCustomerAttributes: function() {
var email = this.get("buyer_email_address");
if (Ember.isBlank(email)) {
email = undefined;
}
return {
name: this.get("buyer_name"),
email: email
};
},

getSellerCustomerAttributes: function() {
var email = this.get("seller_email_address");
if (Ember.isBlank(email)) {
email = undefined;
}
return {
name: this.get("seller_name"),
email: email
};
},

save: function() {
var deferred = Ember.RSVP.defer();
var self = this;
var order = undefined;
var card = undefined;
this.validate();

var getErrorMessage = function(error) {
return Ember.isBlank(error.additional) ?
error.description :
error.additional;
};

if (this.get("isValid")) {
var buyer = Customer.create(self.getBuyerCustomerAttributes());


buyer.save()
.then(function() {
return self.saveCard(buyer.get("uri"));
})
.then(function(c) {
card = c;
var seller = Customer.create(self.getSellerCustomerAttributes());
return seller.save();
})
.then(function(seller) {
var description = self.get("order_description");
return seller.createOrder(description);
})
.then(function(order) {
var debitAttributes = _.extend({}, self.getDebitAttributes(), {
customer_uri: buyer.get("uri"),
uri: card.get('debits_uri'),
source_uri: card.get('uri'),
order_uri: order.get("uri")
});
return Debit.create(debitAttributes).save();
})
.then(function(debit) {
deferred.resolve(debit);
})
.catch(function(response) {
response.errors.forEach(function(error) {
if (error.extras) {
_.each(error.extras, function(value, key) {
self.get("validationErrors").add(key, "server", null, value);
});
}
self.get("validationErrors").add(undefined, "server", null, getErrorMessage(error));
});
deferred.reject(self);
});
} else {
deferred.reject();
}

return deferred.promise;
}
}.property("expiration_date")
});

export default DebitCardTransactionFactory;
17 changes: 0 additions & 17 deletions app/models/factories/debit-existing-card-transaction-factory.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,38 +1,23 @@
import TransactionFactory from "./transaction-factory";
import DebitOrderFactory from "./debit-order-factory";
import ValidationHelpers from "balanced-dashboard/utils/validation-helpers";

var DebitExistingFundingInstrumentTransactionFactory = TransactionFactory.extend({
var DebitExistingFundingInstrumentTransactionFactory = DebitOrderFactory.extend({
appears_on_statement_max_length: Ember.computed.oneWay("source.appears_on_statement_max_length"),
source_uri: Ember.computed.readOnly("source.uri"),
getDebitAttributes: function() {
var properties = this.getProperties("amount", "appears_on_statement_as", "description", "source_uri");
properties.uri = this.get("source.debits_uri");
return properties;

getBuyer: function() {
return Ember.RSVP.resolve(this.get("source.customer"));
},

getSource: function() {
return Ember.RSVP.resolve(this.get("source"));
},

validations: {
dollar_amount: ValidationHelpers.positiveDollarAmount,
appears_on_statement_as: ValidationHelpers.cardTransactionAppearsOnStatementAs,
},

save: function() {
var Debit = BalancedApp.__container__.lookupFactory("model:debit");
var deferred = Ember.RSVP.defer();

this.validate();
if (this.get("isValid")) {
Debit.create(this.getDebitAttributes())
.save()
.then(function(model) {
deferred.resolve(model);
}, function() {
deferred.reject();
});
} else {
deferred.reject();
source: {
presence: true
}

return deferred.promise;
}
});

Expand Down
105 changes: 105 additions & 0 deletions app/models/factories/debit-order-factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import TransactionFactory from "./transaction-factory";
import Debit from "../debit";
import Customer from "../customer";

var DebitOrderFactory = TransactionFactory.extend({
save: function() {
var self = this;
var deferred = Ember.RSVP.defer();
this.validate();

var getErrorMessage = function(error) {
return Ember.isBlank(error.additional) ?
error.description :
error.additional;
};

if (this.get("isValid")) {
var order;

this.createOrderWithSeller()
.then(function(o) {
order = o;
return self.getBuyer();
})
.then(function(buyer) {
return self.getSource(buyer);
})
.then(function(source) {
return self.createDebit(source, order);
})
.then(function(debit) {
deferred.resolve(debit);
})
.catch(function(response) {
response.errors.forEach(function(error) {
if (error.extras) {
_.each(error.extras, function(value, key) {
self.get("validationErrors").add(key, "server", null, value);
});
}
self.get("validationErrors").add(undefined, "server", null, getErrorMessage(error));
});
deferred.reject(self);
});
} else {
deferred.reject();
}

return deferred.promise;
},

getSource: function(/* buyer */) {
Ember.assert("Implement #getSource and make it return a promise with the source", false);
},

getBuyer: function() {
Ember.assert("Implement #getbuyer and make it return a promise with the buyer", false);
},

getDebitAttributes: function() {
var properties = this.getProperties("amount", "appears_on_statement_as");
properties.description = this.get("debit_description");

return properties;
},

createDebit: function(source, order) {
var sourceUri = source.get("uri");
var debitsUri = source.get("debits_uri");
var buyerUri = source.get("customer_uri");

var debitAttributes = _.extend({}, this.getDebitAttributes(), {
customer_uri: buyerUri,
uri: debitsUri,
source_uri: sourceUri,
order_uri: order.get("uri")
});

return Debit.create(debitAttributes).save();
},

getSellerCustomerAttributes: function() {
var email = this.get("seller_email_address");
if (Ember.isBlank(email)) {
email = undefined;
}
return {
name: this.get("seller_name"),
email: email
};
},

createOrderWithSeller: function() {
var orderDescription = this.get("order_description");
var seller = Customer.create(this.getSellerCustomerAttributes());

return seller.save()
.then(function(seller) {
var description = orderDescription;
return seller.createOrder(description);
});
},
});

export default DebitOrderFactory;
Loading

0 comments on commit 2a72a27

Please sign in to comment.