Skip to content

Commit

Permalink
Fixes balanced#1631: Creating a debit automatically creates customers…
Browse files Browse the repository at this point in the history
… and order
  • Loading branch information
kyungmin committed Jan 23, 2015
1 parent 6e2ad54 commit d7e4105
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 20 deletions.
9 changes: 2 additions & 7 deletions app/models/credit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Ember from "ember";
import Computed from "balanced-dashboard/utils/computed";
import Transaction from "./transaction";
import Rev1Serializer from "../serializers/rev1";
import TransactionSerializer from "../serializers/transaction";
import Model from "./core/model";
import Utils from "balanced-dashboard/lib/utils";

Expand Down Expand Up @@ -70,7 +70,7 @@ var Credit = Transaction.extend({
});

Credit.reopenClass({
serializer: Rev1Serializer.extend({
serializer: TransactionSerializer.extend({
serialize: function(record) {
var json = this._super(record);

Expand All @@ -83,11 +83,6 @@ Credit.reopenClass({
}
}

if (!Ember.isBlank(json.order_uri)) {
json.order = json.order_uri;
delete json.order_uri;
}

return json;
}
}).create(),
Expand Down
9 changes: 9 additions & 0 deletions app/models/customer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CountryCodesToNames } from "balanced-dashboard/lib/country-codes";
import Model from "./core/model";
import Order from "./order";
import Computed from "balanced-dashboard/utils/computed";
import FundingInstrumentsResultsLoader from "./results-loaders/funding-instruments";
import TransactionsResultsLoader from "./results-loaders/transactions";
Expand Down Expand Up @@ -83,6 +84,14 @@ var Customer = Model.extend({
return AccountsResultsLoader.create(attributes);
},

createOrder: function(description) {
var order = Order.create({
description: description
});
order.set("uri", this.get("orders_uri"));
return order.save();
},

type: function() {
return (this.get('ein') || this.get('business_name')) ? CUSTOMER_TYPES.BUSINESS : CUSTOMER_TYPES.PERSON;
}.property('ein', 'business_name'),
Expand Down
5 changes: 5 additions & 0 deletions app/models/debit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Computed from "balanced-dashboard/utils/computed";
import Transaction from "./transaction";
import Model from "./core/model";
import Utils from "balanced-dashboard/lib/utils";
import TransactionSerializer from "../serializers/transaction";

var Debit = Transaction.extend({

Expand Down Expand Up @@ -59,4 +60,8 @@ var Debit = Transaction.extend({
}.property('amount', 'refund_amount', 'is_succeeded', 'dispute')
});

Debit.reopenClass({
serializer: TransactionSerializer.create()
});

export default Debit;
46 changes: 38 additions & 8 deletions app/models/factories/debit-card-transaction-factory.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Debit from "../debit";
import Card from "../card";
import Customer from "../customer";
import TransactionFactory from "./transaction-factory";
import ValidationHelpers from "balanced-dashboard/utils/validation-helpers";

Expand All @@ -13,7 +14,7 @@ var DebitCardTransactionFactory = TransactionFactory.extend({
},

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

validations: {
Expand All @@ -32,22 +33,51 @@ var DebitCardTransactionFactory = TransactionFactory.extend({
var baseDebitAttributes = this.getDebitAttributes();
var self = this;
this.validate();

if (this.get("isValid")) {
Card.create(this.getDestinationAttributes())
.tokenizeAndCreate()
.then(function(card) {
var buyer = Customer.create({
name: self.get("buyer_name"),
email: self.get("buyer_email_address")
});
var card;

buyer.save()
.then(function() {
return Card
.create(self.getDestinationAttributes())
.tokenizeAndCreate(buyer.get("uri"));
})
.then(function(c) {
card = c;
var seller = Customer.create({
name: self.get("seller_name"),
email: self.get("seller_email_address")
});
return seller.save();
})
.then(function(seller) {
var description = self.get("order_description");
return seller.createOrder(description);
})
.then(function(order) {
var debitAttributes = _.extend({}, baseDebitAttributes, {
customer_uri: buyer.get("uri"),
uri: card.get('debits_uri'),
source_uri: card.get('uri')
source_uri: card.get('uri'),
order_uri: order.get("uri")
});
return Debit.create(debitAttributes).save();
})
.then(function(model) {
deferred.resolve(model);
}, function(response) {
response.errors.forEach(function(error) {
self.get("validationErrors").add(undefined, "server", null, error.description);
});
if (response.message) {
self.get("validationErrors").add(undefined, "server", null, response.message);
} else if (response.errors) {
response.errors.forEach(function(error) {
self.get("validationErrors").add(undefined, "server", null, error.description);
});
}
deferred.reject(self);
});
} else {
Expand Down
18 changes: 18 additions & 0 deletions app/serializers/transaction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Rev1Serializer from "./rev1";

var TransactionSerializer = Rev1Serializer.extend({
_propertiesMap: function(record) {
var json = this._super(record);

if (!Ember.isBlank(json.order_uri)) {
json.order = json.order_uri;
delete json.order_uri;
}

console.log(json);
return json;
}
})


export default TransactionSerializer;
16 changes: 13 additions & 3 deletions app/templates/modals/card-debit-create-modal.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
{{#view "form-fields/form-section" appearsOnStatementAsLabelText=view.appearsOnStatementAsLabelText appearsOnStatementAsMaxLength=view.appearsOnStatementAsMaxLength model=view.model sectionTitle="Payment information"}}
{{#view "form-fields/form-section" sectionTitle="Order information" appearsOnStatementAsLabelText=view.appearsOnStatementAsLabelText appearsOnStatementAsMaxLength=view.appearsOnStatementAsMaxLength model=view.model}}
{{view "form-fields/text-form-field" model=view.model field="order_description" labelText="Order description" inputClassNames="full"}}

{{view "form-fields/text-form-field" model=view.model field="seller_name" labelText="Merchant's name" inputClassNames="full"}}
{{view "form-fields/email-form-field" model=view.model field="seller_email_address" labelText="Merchant's email address" inputClassNames="full"}}
{{/view}}

{{#view "form-fields/form-section" sectionTitle="Buyer information" model=view.model}}
{{view "form-fields/text-form-field" model=view.model field="buyer_name" labelText="Name" inputClassNames="full"}}
{{view "form-fields/email-form-field" model=view.model field="buyer_email_address" labelText="Email address" inputClassNames="full"}}
{{view "form-fields/text-form-field" model=view.model field="name" labelText="Card holder's name" inputClassNames="full"}}
{{view "form-fields/text-form-field" model=view.model field="number" labelText="Card number" inputClassNames="full"}}
{{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"/>'}}
Expand All @@ -9,12 +18,13 @@
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"}}
{{/view}}

{{#view "form-fields/form-section" sectionTitle="Debit information" appearsOnStatementAsLabelText=view.appearsOnStatementAsLabelText appearsOnStatementAsMaxLength=view.appearsOnStatementAsMaxLength model=view.model}}
{{view "form-fields/amount-form-field" model=view.model field="dollar_amount" labelText="Amount"}}

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

{{view "form-fields/text-form-field" model=view.model field="description" labelText="Internal description" inputClassNames="full"}}
{{view "form-fields/text-form-field" model=view.model field="debit_description" labelText="Debit description" inputClassNames="full"}}
{{/view}}
8 changes: 8 additions & 0 deletions app/views/form-fields/email-form-field.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import BaseFormFieldView from "./base-form-field";

var EmailFormFieldView = BaseFormFieldView.extend({
inputType: "email",
inputClassNames: "full"
});

export default EmailFormFieldView;
4 changes: 2 additions & 2 deletions app/views/modals/card-debit-create-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import Save from "balanced-dashboard/views/modals/mixins/object-action-mixin";
var CardDebitCreateModalView = ModalBaseView.extend(Save, Full, Form, {
templateName: "modals/card-debit-create-modal",
elementId: "charge-card",
title: "Debit a card",
title: "Create an order",
cancelButtonText: "Cancel",
submitButtonText: "Debit",
submitButtonText: "Create",

model: function() {
var DebitCardTransactionFactory = require("balanced-dashboard/models/factories/debit-card-transaction-factory")['default'];
Expand Down

0 comments on commit d7e4105

Please sign in to comment.