Skip to content

Commit

Permalink
Fixes balanced#1631: Always credit from orders
Browse files Browse the repository at this point in the history
  • Loading branch information
kyungmin committed Jan 26, 2015
1 parent 2a72a27 commit 48842de
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 62 deletions.
21 changes: 10 additions & 11 deletions app/models/factories/credit-bank-account-transaction-factory.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
import Ember from "ember";
import ValidationHelpers from "balanced-dashboard/utils/validation-helpers";
import TransactionFactory from "./transaction-factory";
import CreditOrderFactory from "./credit-order-factory";
import BankAccount from "../bank-account";

/*
* This factory uses the api feature of creating a Credit without creating a
* BankAccount object.
*/
var CreditBankAccountTransactionFactory = TransactionFactory.extend({
var CreditBankAccountTransactionFactory = CreditOrderFactory.extend({
getDestinationAttributes: function() {
return this.getProperties("account_number", "name", "routing_number", "account_type");
},

getAttributes: function() {
var attributes = this.getProperties("amount", "appears_on_statement_as", "description");
attributes.destination = this.getDestinationAttributes();
return attributes;
},

save: function() {
var Credit = BalancedApp.__container__.lookupFactory("model:credit");
return Credit.create(this.getAttributes()).save();
getDestination: function(seller) {
return BankAccount
.create(this.getDestinationAttributes())
.tokenizeAndCreate(seller.get("uri"));
},

validations: {
Expand All @@ -30,6 +26,9 @@ var CreditBankAccountTransactionFactory = TransactionFactory.extend({
routing_number: ValidationHelpers.bankAccountRoutingNumber,
account_number: ValidationHelpers.bankAccountNumber,
account_type: ValidationHelpers.bankAccountType,
order: {
presence: true
}
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,23 @@
import Ember from "ember";
import ValidationHelpers from "balanced-dashboard/utils/validation-helpers";
import TransactionFactory from "./transaction-factory";
import CreditOrderFactory from "./credit-order-factory";

var CreditExistingFundingInstrumentTransactionFactory = TransactionFactory.extend({
var CreditExistingFundingInstrumentTransactionFactory = CreditOrderFactory.extend({
appears_on_statement_max_length: Ember.computed.oneWay("destination.appears_on_statement_max_length"),
destination_uri: Ember.computed.readOnly("destination.uri"),

getCreditAttributes: function() {
var properties = this.getProperties("amount", "appears_on_statement_as", "description", "destination_uri");
properties.uri = this.get("destination.credits_uri");

if (this.get("order.href")) {
properties.order_uri = this.get("order.href");
}
return properties;
},

save: function() {
var Credit = BalancedApp.__container__.lookupFactory("model:credit");

this.validate();
if (this.get("isValid")) {
return Credit.create(this.getCreditAttributes()).save();
} else {
return Ember.RSVP.reject();
}
getDestination: function() {
return Ember.RSVP.resolve(this.get("destination"));
},

validations: {
destination_uri: {
dollar_amount: ValidationHelpers.positiveDollarAmount,
appears_on_statement_as: ValidationHelpers.bankTransactionAppearsOnStatementAs,
destination: {
presence: true
},
order: {
presence: true
},
dollar_amount: ValidationHelpers.positiveDollarAmount,
appears_on_statement_as: ValidationHelpers.bankTransactionAppearsOnStatementAs
}
});

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

var CreditOrderFactory = TransactionFactory.extend({
save: function() {
var self = this;
var order = this.get("order");
console.log(order);
// TODO: check if order.seller == customer
var seller = this.get("order.seller");

var deferred = Ember.RSVP.defer();
this.validate();

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

if (this.get("isValid")) {
self.getDestination(seller)
.then(function(destination) {
return self.createCredit(destination, order);
})
.then(function(credit) {
deferred.resolve(credit);
})
.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;
},

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

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

return properties;
},

createCredit: function(destination, order) {
var destinationUri = destination.get("uri");
var creditsUri = destination.get("credits_uri");

var creditAttributes = _.extend({}, this.getCreditAttributes(), {
uri: creditsUri,
destination_uri: destinationUri,
order_uri: order.get("uri")
});

return Credit.create(creditAttributes).save();
}
});

export default CreditOrderFactory;
57 changes: 40 additions & 17 deletions app/templates/modals/credit-customer-modal.hbs
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
{{#view "form-fields/form-section" appearsOnStatementAsMaxLength=view.appearsOnStatementAsMaxLength appearsOnStatementAsLabelText=view.appearsOnStatementAsLabelText fundingInstruments=view.fundingInstruments customer=view.customer model=view.model sectionTitle="Payment information"}}
{{#view "form-fields/form-section" sectionTitle="Order information" model=view.model creditableOrders=view.creditableOrders}}
{{view "form-fields/select-form-field"
model=view.model
content=view.creditableOrders
value=view.model.order
labelText="Select one"
optionValuePath="content"
optionLabelPath="content.page_title"
name="order"
field="order"
prompt="Select an order"
}}
{{/view}}

{{#view "form-fields/form-section" fundingInstruments=view.fundingInstruments model=view.model sectionTitle="Payment information"}}

{{view "form-fields/static-text-form-field" labelText="Customer" value=view.customer.display_me_with_email}}

{{#if view.fundingInstruments}}
{{#if view.parentView.isDisplayExistingFundingInstruments}}
{{view "form-fields/select-form-field"
model=view.model
content=view.fundingInstruments
Expand All @@ -13,27 +27,36 @@
name="destination"
field="destination"
}}
{{else}}
<div class="form-group">
<p>This customer doesn't have any payment method to credit.</p>
</div>
{{/if}}

{{view "form-fields/static-text-form-field"
labelText="Name on account"
value=view.model.destination.name
}}
{{view "form-fields/static-text-form-field"
labelText="Name on account"
value=view.model.destination.name
}}

{{view "form-fields/static-text-form-field"
labelText="Bank"
value=view.model.destination.formatted_bank_name
}}
{{view "form-fields/static-text-form-field"
labelText="Bank"
value=view.model.destination.formatted_bank_name
}}
{{else}}
{{view "form-fields/text-form-field" model=view.model field="name" labelText="Card holder's name" inputClassNames="full"}}
{{view "form-fields/credit-card-number-form-field" model=view.model field="number" labelText="Card number"}}
{{view "form-fields/text-form-field" model=view.model field="cvv" labelText="Security code" tooltipTitle="Where is the security code?" tooltipContent='<img src="images/credit-card-instructions.png" alt="Credit Card Instructions" class="modal-informational-content"/>'}}
{{view "form-fields/credit-card-expiration-date-form-field"
model=view.model
field="expiration_date"
labelText="Expiration date"
name="expiration_date"
}}
{{view "form-fields/text-form-field" model=view.model field="postal_code" labelText="Billing zip code" explanationText="Required for American Express cards"}}
{{/if}}
{{/view}}

{{view "form-fields/amount-form-field" model=view.model field="dollar_amount" labelText="Amount"}}
{{#view "form-fields/form-section" appearsOnStatementAsMaxLength=view.appearsOnStatementAsMaxLength appearsOnStatementAsLabelText=view.appearsOnStatementAsLabelText model=view.model sectionTitle="Credit information"}}
{{view "form-fields/amount-form-field" model=view.model field="dollar_amount" labelText="Amount" explanationText=view.parentView.source.escrow_balance}}

{{view "form-fields/text-form-field" model=view.model field="appears_on_statement_as" labelText=view.appearsOnStatementAsLabelText maxLength=view.appearsOnStatementAsMaxLength inputClassNames="full" maxlength=view.appearsOnStatementAsMaxLength}}

{{view "form-fields/text-form-field" model=view.model field="description" labelText="Internal description" maxlength=Constants.MAXLENGTH.DESCRIPTION inputClassNames="full"}}
{{view "form-fields/text-form-field" model=view.model field="credit_description" labelText="Credit description" maxlength=Constants.MAXLENGTH.DESCRIPTION inputClassNames="full"}}
{{/view}}

{{#view "form-fields/form-section" sectionTitle="Note"}}
Expand Down
10 changes: 7 additions & 3 deletions app/views/modals/credit-customer-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ var CreditCustomerModalView = ModalBaseView.extend(Full, Form, Save, {
return "Appears on statement as (%@ characters max)".fmt(length);
}.property("appearsOnStatementAsMaxLength"),

creditableOrders: function() {
return this.get("customer").getOrdersLoader().get("results");
}.property("customer"),

fundingInstruments: Ember.computed.oneWay('customer.creditable_funding_instruments'),
isDisplayExistingFundingInstruments: Ember.computed.gt("fundingInstruments.length", 0),

actions: {
save: function() {
Expand All @@ -39,10 +44,9 @@ var CreditCustomerModalView = ModalBaseView.extend(Full, Form, Save, {
});

CreditCustomerModalView.reopenClass({
open: function(customer, order) {
open: function(customer) {
return this.create({
customer: customer,
order: order
customer: customer
});
},
});
Expand Down
9 changes: 3 additions & 6 deletions app/views/modals/debit-customer-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ var DebitCustomerModalView = ModalBaseView.extend(Full, Form, Save, {
}.property("isDisplayExistingFundingInstruments"),

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

return this.get("transactionFactoryClass").create({
customer: customer
customer: this.get("customer")
});
}.property("customer", "transactionFactoryClass"),

Expand All @@ -52,10 +50,9 @@ var DebitCustomerModalView = ModalBaseView.extend(Full, Form, Save, {
});

DebitCustomerModalView.reopenClass({
open: function(customer, order) {
open: function(customer) {
return this.create({
customer: customer,
order: order
customer: customer
});
},
});
Expand Down

0 comments on commit 48842de

Please sign in to comment.