forked from balanced/balanced-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes balanced#1631: Make debit this customer create orders automatic…
…ally
- Loading branch information
Showing
7 changed files
with
196 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 0 additions & 17 deletions
17
app/models/factories/debit-existing-card-transaction-factory.js
This file was deleted.
Oops, something went wrong.
37 changes: 11 additions & 26 deletions
37
app/models/factories/debit-existing-funding-instrument-transaction-factory.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.