Skip to content

Commit

Permalink
0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
braintreeps committed Aug 24, 2011
1 parent b0aa020 commit fa8d8fb
Show file tree
Hide file tree
Showing 40 changed files with 546 additions and 476 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,4 @@
/node_modules/.bin
/node_modules/coffee-script /node_modules/coffee-script
/node_modules/o3-xml /node_modules/o3-xml
/node_modules/underscore /node_modules/underscore
Expand Down
4 changes: 3 additions & 1 deletion Rakefile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ task :default => :spec


desc "run the specs" desc "run the specs"
task :spec do task :spec do
sh "./node_modules/.bin/vows " + Dir.glob("spec/**/*_spec.js").join(" ") local_vows = "./node_modules/.bin/vows"
vows = File.exist?(local_vows) ? local_vows : "vows"
sh "#{vows} " + Dir.glob("spec/**/*_spec.js").join(" ")
end end
13 changes: 13 additions & 0 deletions lib/braintree.coffee
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,13 @@
sys = require "sys"
{Config} = require("./braintree/config")
{Environment} = require("./braintree/environment")
{BraintreeGateway} = require("./braintree/braintree_gateway")
errorTypes = require("./braintree/error_types")

connect = (config) ->
new BraintreeGateway(new Config(config))

exports.connect = connect
exports.version = '0.3.0'
exports.Environment = Environment
exports.errorTypes = errorTypes
34 changes: 5 additions & 29 deletions lib/braintree.js
Original file line number Original file line Diff line number Diff line change
@@ -1,31 +1,7 @@
require('coffee-script'); require('coffee-script');
braintree = require('./braintree.coffee')


var sys = require("sys"), exports.connect = braintree.connect;
Config = require("./braintree/config").Config, exports.version = braintree.version;
Environment = require("./braintree/environment").Environment, exports.Environment = braintree.Environment;
Gateway = require("./braintree/gateway").Gateway, exports.errorTypes = braintree.errorTypes;
AddressGateway = require("./braintree/address_gateway").AddressGateway,
CreditCardGateway = require("./braintree/credit_card_gateway").CreditCardGateway,
CustomerGateway = require("./braintree/customer_gateway").CustomerGateway,
SubscriptionGateway = require("./braintree/subscription_gateway").SubscriptionGateway,
TransactionGateway = require("./braintree/transaction_gateway").TransactionGateway,
TransparentRedirectGateway = require("./braintree/transparent_redirect_gateway").TransparentRedirectGateway,
errorTypes = require("./braintree/error_types");

var connect = function(config) {
var gateway = Gateway(Config(config));
return {
_gateway: gateway,
address: AddressGateway(gateway),
creditCard: CreditCardGateway(gateway),
customer: CustomerGateway(gateway),
subscription: SubscriptionGateway(gateway),
transaction: TransactionGateway(gateway),
transparentRedirect: TransparentRedirectGateway(gateway)
};
};

exports.connect = connect;
exports.version = '0.2.0';
exports.Environment = Environment;
exports.errorTypes = errorTypes;
5 changes: 5 additions & 0 deletions lib/braintree/address.coffee
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,5 @@
{AttributeSetter} = require('./attribute_setter')

class Address extends AttributeSetter

exports.Address = Address
51 changes: 18 additions & 33 deletions lib/braintree/address_gateway.coffee
Original file line number Original file line Diff line number Diff line change
@@ -1,43 +1,28 @@
ErrorResponse = require('./error_response').ErrorResponse {Gateway} = require('./gateway')
{Address} = require('./address')


AddressGateway = (gateway) -> class AddressGateway extends Gateway
my = { gateway: gateway } constructor: (@gateway) ->


create = (attributes, callback) -> create: (attributes, callback) ->
customerId = attributes.customerId customerId = attributes.customerId
delete(attributes.customerId) delete(attributes.customerId)
my.gateway.http.post("/customers/#{customerId}/addresses", {address: attributes}, responseHandler(callback)) @gateway.http.post("/customers/#{customerId}/addresses", {address: attributes}, @responseHandler(callback))


destroy = (customerId, id, callback) -> delete: (customerId, id, callback) ->
my.gateway.http.delete("/customers/#{customerId}/addresses/#{id}", callback) @gateway.http.delete("/customers/#{customerId}/addresses/#{id}", callback)


find = (customerId, id, callback) -> find: (customerId, id, callback) ->
callback = callback @gateway.http.get "/customers/#{customerId}/addresses/#{id}", (err, response) ->
my.gateway.http.get("/customers/#{customerId}/addresses/#{id}" , (err, response) -> if err
return callback(err, null) if err callback(err, null)
callback(null, response.address) else
) callback(null, response.address)


update: (customerId, id, attributes, callback) ->
@gateway.http.put("/customers/#{customerId}/addresses/#{id}", {address: attributes}, @responseHandler(callback))


update = (customerId, id, attributes, callback) -> responseHandler: (callback) ->
my.gateway.http.put("/customers/#{customerId}/addresses/#{id}", {address: attributes}, responseHandler(callback)) @createResponseHandler("address", Address, callback)

responseHandler = (callback) ->
return (err, response) ->
return callback(err, response) if err

if (response.address)
response.success = true
callback(null, response)
else if (response.apiErrorResponse)
callback(null, ErrorResponse(response.apiErrorResponse))

{
create: create,
delete: destroy,
find: find,
update: update
}


exports.AddressGateway = AddressGateway exports.AddressGateway = AddressGateway

6 changes: 6 additions & 0 deletions lib/braintree/attribute_setter.coffee
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,6 @@
class AttributeSetter
constructor: (attributes) ->
for key, value of attributes
@[key] = value

exports.AttributeSetter = AttributeSetter
21 changes: 21 additions & 0 deletions lib/braintree/braintree_gateway.coffee
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,21 @@
{Http} = require('./http')
{AddressGateway} = require("./address_gateway")
{CreditCardGateway} = require("./credit_card_gateway")
{CustomerGateway} = require("./customer_gateway")
{SettlementBatchSummaryGateway} = require("./settlement_batch_summary_gateway")
{SubscriptionGateway} = require("./subscription_gateway")
{TransactionGateway} = require("./transaction_gateway")
{TransparentRedirectGateway} = require("./transparent_redirect_gateway")

class BraintreeGateway
constructor: (@config) ->
@http = new Http(@config)
@address = new AddressGateway(this)
@creditCard = new CreditCardGateway(this)
@customer = new CustomerGateway(this)
@settlementBatchSummary = new SettlementBatchSummaryGateway(this)
@subscription = new SubscriptionGateway(this)
@transaction = new TransactionGateway(this)
@transparentRedirect = new TransparentRedirectGateway(this)

exports.BraintreeGateway = BraintreeGateway
17 changes: 8 additions & 9 deletions lib/braintree/config.coffee
Original file line number Original file line Diff line number Diff line change
@@ -1,11 +1,10 @@
Config = (rawConfig) -> class Config
{ constructor: (rawConfig) ->
apiVersion: '2', @apiVersion = '2'
environment: rawConfig.environment, @environment = rawConfig.environment
merchantId: rawConfig.merchantId, @merchantId = rawConfig.merchantId
publicKey: rawConfig.publicKey, @publicKey = rawConfig.publicKey
privateKey: rawConfig.privateKey, @privateKey = rawConfig.privateKey
baseMerchantPath: '/merchants/' + rawConfig.merchantId @baseMerchantPath = "/merchants/#{rawConfig.merchantId}"
}


exports.Config = Config exports.Config = Config
14 changes: 7 additions & 7 deletions lib/braintree/credit_card.coffee
Original file line number Original file line Diff line number Diff line change
@@ -1,9 +1,9 @@
CreditCard = (attributes) -> {AttributeSetter} = require('./attribute_setter')
that = {}
for key, value of attributes class CreditCard extends AttributeSetter
that[key] = value constructor: (attributes) ->
that.maskedNumber = that.bin + '******' + that.last4 super attributes
that.expirationDate = that.expirationMonth + '/' + that.expirationYear @maskedNumber = "#{@bin}******#{@last4}"
that @expirationDate = "#{@expirationMonth}/#{@expirationYear}"


exports.CreditCard = CreditCard exports.CreditCard = CreditCard
57 changes: 18 additions & 39 deletions lib/braintree/credit_card_gateway.coffee
Original file line number Original file line Diff line number Diff line change
@@ -1,47 +1,26 @@
CreditCard = require('./credit_card').CreditCard {Gateway} = require('./gateway')
ErrorResponse = require('./error_response').ErrorResponse {CreditCard} = require('./credit_card')


CreditCardGateway = (gateway) -> class CreditCardGateway extends Gateway
my = { gateway: gateway } constructor: (@gateway) ->


create = (attributes, callback) -> create: (attributes, callback) ->
my.gateway.http.post('/payment_methods', {creditCard: attributes}, responseHandler(callback)) @gateway.http.post('/payment_methods', {creditCard: attributes}, @responseHandler(callback))


destroy = (token, callback) -> delete: (token, callback) ->
my.gateway.http.delete('/payment_methods/' + token, callback) @gateway.http.delete("/payment_methods/#{token}", callback)


find = (token, callback) -> find: (token, callback) ->
callback = callback @gateway.http.get "/payment_methods/#{token}", (err, response) ->
my.gateway.http.get('/payment_methods/' + token, (err, response) -> if err
return callback(err, null) if err callback(err, null)
callback(null, CreditCard(response.creditCard)) else
) callback(null, new CreditCard(response.creditCard))


update: (token, attributes, callback) ->
@gateway.http.put("/payment_methods/#{token}", {creditCard: attributes}, @responseHandler(callback))


update = (token, attributes, callback) -> responseHandler: (callback) ->
my.gateway.http.put( @createResponseHandler("creditCard", CreditCard, callback)
'/payment_methods/' + token,
{creditCard: attributes},
responseHandler(callback)
)

responseHandler = (callback) ->
return (err, response) ->
return callback(err, response) if err

if (response.creditCard)
response.success = true
response.creditCard = CreditCard(response.creditCard)
callback(null, response)
else if (response.apiErrorResponse)
callback(null, ErrorResponse(response.apiErrorResponse))

{
create: create,
delete: destroy,
find: find,
responseHandler: responseHandler,
update: update
}


exports.CreditCardGateway = CreditCardGateway exports.CreditCardGateway = CreditCardGateway
13 changes: 6 additions & 7 deletions lib/braintree/customer.coffee
Original file line number Original file line Diff line number Diff line change
@@ -1,10 +1,9 @@
CreditCard = require('./credit_card').CreditCard {AttributeSetter} = require('./attribute_setter')
{CreditCard} = require('./credit_card')


Customer = (attributes) -> class Customer extends AttributeSetter
that = {} constructor: (attributes) ->
for key, value of attributes super attributes
that[key] = value @creditCards = (new CreditCard(cardAttributes) for cardAttributes in attributes.creditCards)
that.creditCards = (CreditCard(cardAttributes) for cardAttributes in attributes.creditCards)
that


exports.Customer = Customer exports.Customer = Customer
57 changes: 18 additions & 39 deletions lib/braintree/customer_gateway.coffee
Original file line number Original file line Diff line number Diff line change
@@ -1,47 +1,26 @@
Customer = require('./customer').Customer {Gateway} = require('./gateway')
ErrorResponse = require('./error_response').ErrorResponse {Customer} = require('./customer')


CustomerGateway = (gateway) -> class CustomerGateway extends Gateway
my = { gateway: gateway } constructor: (@gateway) ->


create = (attributes, callback) -> create: (attributes, callback) ->
my.gateway.http.post('/customers', {customer: attributes}, responseHandler(callback)) @gateway.http.post('/customers', {customer: attributes}, @responseHandler(callback))


destroy = (customer_id, callback) -> delete: (customerId, callback) ->
my.gateway.http.delete('/customers/' + customer_id, callback) @gateway.http.delete("/customers/#{customerId}", callback)


find = (customer_id, callback) -> find: (customerId, callback) ->
callback = callback @gateway.http.get "/customers/#{customerId}", (err, response) ->
my.gateway.http.get('/customers/' + customer_id, (err, response) -> if err
return callback(err, null) if err callback(err, null)
callback(null, Customer(response.customer)) else
) callback(null, new Customer(response.customer))


update = (customer_id, attributes, callback) -> update: (customerId, attributes, callback) ->
my.gateway.http.put( @gateway.http.put("/customers/#{customerId}", {customer: attributes}, @responseHandler(callback))
'/customers/' + customer_id,
{ customer: attributes },
responseHandler(callback)
)


responseHandler = (callback) -> responseHandler: (callback) ->
(err, response) -> @createResponseHandler("customer", Customer, callback)
return callback(err, response) if err

if (response.customer)
response.success = true
response.customer = Customer(response.customer)
callback(null, response)
else if (response.apiErrorResponse)
callback(null, ErrorResponse(response.apiErrorResponse))

{
create: create,
delete: destroy,
find: find,
responseHandler: responseHandler,
update: update
}


exports.CustomerGateway = CustomerGateway exports.CustomerGateway = CustomerGateway

20 changes: 9 additions & 11 deletions lib/braintree/digest.coffee
Original file line number Original file line Diff line number Diff line change
@@ -1,20 +1,18 @@
sys = require('sys') sys = require('sys')
crypto = require('crypto') crypto = require('crypto')


Digest = -> class Digest
sha1 = (data) -> @hexdigest: (privateKey, string) ->
hash = crypto.createHash('sha1') new Digest().hmacSha1(privateKey, string)
hash.update(data)
hash.digest()


hmacSha1 = (key, data) -> hmacSha1: (key, data) ->
hmac = crypto.createHmac('sha1', sha1(key)) hmac = crypto.createHmac('sha1', @sha1(key))
hmac.update(data) hmac.update(data)
hmac.digest('hex') hmac.digest('hex')


{ hmacSha1: hmacSha1 } sha1: (data) ->

hash = crypto.createHash('sha1')
Digest.hexdigest = (privateKey, string) -> hash.update(data)
Digest().hmacSha1(privateKey, string) hash.digest()


exports.Digest = Digest exports.Digest = Digest
18 changes: 6 additions & 12 deletions lib/braintree/environment.coffee
Original file line number Original file line Diff line number Diff line change
@@ -1,15 +1,9 @@
Environment = (server, port, ssl) -> class Environment
{ DEVELOPMENT_PORT = process.env['GATEWAY_PORT'] || '3000'
server: server, @Development = new Environment('localhost', DEVELOPMENT_PORT, false)
port: port, @Sandbox = new Environment('sandbox.braintreegateway.com', '443', true)
ssl: ssl @Production = new Environment('www.braintreegateway.com', '443', true)
}



constructor: (@server, @port, @ssl) ->
developmentPort = process.env['GATEWAY_PORT'] || '3000'

Environment.Development = Environment('localhost', developmentPort, false)
Environment.Sandbox = Environment('sandbox.braintreegateway.com', '443', true)
Environment.Production = Environment('www.braintreegateway.com', '443', true)


exports.Environment = Environment exports.Environment = Environment
Loading

0 comments on commit fa8d8fb

Please sign in to comment.