# File lib/active_merchant/billing/gateways/braintree_blue.rb, line 18 18: def initialize(options = {}) 19: requires!(options, :merchant_id, :public_key, :private_key) 20: @options = options 21: @merchant_account_id = options[:merchant_account_id] 22: Braintree::Configuration.merchant_id = options[:merchant_id] 23: Braintree::Configuration.public_key = options[:public_key] 24: Braintree::Configuration.private_key = options[:private_key] 25: Braintree::Configuration.environment = (options[:environment] || (test? ? :sandbox : :production)).to_sym 26: Braintree::Configuration.logger.level = Logger::ERROR if Braintree::Configuration.logger 27: Braintree::Configuration.custom_user_agent = "ActiveMerchant #{ActiveMerchant::VERSION}" 28: super 29: end
# File lib/active_merchant/billing/gateways/braintree_blue.rb, line 35 35: def capture(money, authorization, options = {}) 36: commit do 37: result = Braintree::Transaction.submit_for_settlement(authorization, amount(money).to_s) 38: Response.new(result.success?, message_from_result(result)) 39: end 40: end
# File lib/active_merchant/billing/gateways/braintree_blue.rb, line 46 46: def credit(money, credit_card_or_vault_id, options = {}) 47: create_transaction(:credit, money, credit_card_or_vault_id, options) 48: end
# File lib/active_merchant/billing/gateways/braintree_blue.rb, line 42 42: def purchase(money, credit_card_or_vault_id, options = {}) 43: authorize(money, credit_card_or_vault_id, options.merge(:submit_for_settlement => true)) 44: end
# File lib/active_merchant/billing/gateways/braintree_blue.rb, line 50 50: def refund(*args) 51: # legacy signature: #refund(transaction_id, options = {}) 52: # new signature: #refund(money, transaction_id, options = {}) 53: money, transaction_id, options = extract_refund_args(args) 54: money = amount(money).to_s if money 55: 56: commit do 57: result = Braintree::Transaction.refund(transaction_id, money) 58: Response.new(result.success?, message_from_result(result), 59: {:braintree_transaction => (transaction_hash(result.transaction) if result.success?)}, 60: {:authorization => (result.transaction.id if result.success?)} 61: ) 62: end 63: end
# File lib/active_merchant/billing/gateways/braintree_blue.rb, line 75 75: def store(creditcard, options = {}) 76: commit do 77: result = Braintree::Customer.create( 78: :first_name => creditcard.first_name, 79: :last_name => creditcard.last_name, 80: :email => options[:email], 81: :credit_card => { 82: :number => creditcard.number, 83: :cvv => creditcard.verification_value, 84: :expiration_month => creditcard.month.to_s.rjust(2, "0"), 85: :expiration_year => creditcard.year.to_s 86: } 87: ) 88: Response.new(result.success?, message_from_result(result), 89: { 90: :braintree_customer => (customer_hash(result.customer) if result.success?), 91: :customer_vault_id => (result.customer.id if result.success?) 92: } 93: ) 94: end 95: end
# File lib/active_merchant/billing/gateways/braintree_blue.rb, line 124 124: def unstore(customer_vault_id) 125: commit do 126: Braintree::Customer.delete(customer_vault_id) 127: Response.new(true, "OK") 128: end 129: end
# File lib/active_merchant/billing/gateways/braintree_blue.rb, line 97 97: def update(vault_id, creditcard, options = {}) 98: braintree_credit_card = nil 99: customer_update_result = commit do 100: braintree_credit_card = Braintree::Customer.find(vault_id).credit_cards.detect { |cc| cc.default? } 101: return Response.new(false, 'Braintree::NotFoundError') if braintree_credit_card.nil? 102: result = Braintree::Customer.update(vault_id, 103: :first_name => creditcard.first_name, 104: :last_name => creditcard.last_name, 105: :email => options[:email] 106: ) 107: Response.new(result.success?, message_from_result(result), 108: :braintree_customer => (customer_hash(Braintree::Customer.find(vault_id)) if result.success?) 109: ) 110: end 111: return customer_update_result unless customer_update_result.success? 112: credit_card_update_result = commit do 113: result = Braintree::CreditCard.update(braintree_credit_card.token, 114: :number => creditcard.number, 115: :expiration_month => creditcard.month.to_s.rjust(2, "0"), 116: :expiration_year => creditcard.year.to_s 117: ) 118: Response.new(result.success?, message_from_result(result), 119: :braintree_customer => (customer_hash(Braintree::Customer.find(vault_id)) if result.success?) 120: ) 121: end 122: end
# File lib/active_merchant/billing/gateways/braintree_blue.rb, line 65 65: def void(authorization, options = {}) 66: commit do 67: result = Braintree::Transaction.void(authorization) 68: Response.new(result.success?, message_from_result(result), 69: {:braintree_transaction => (transaction_hash(result.transaction) if result.success?)}, 70: {:authorization => (result.transaction.id if result.success?)} 71: ) 72: end 73: end
# File lib/active_merchant/billing/gateways/braintree_blue.rb, line 147 147: def commit(&block) 148: yield 149: rescue Braintree::BraintreeError => ex 150: Response.new(false, ex.class.to_s) 151: end
# File lib/active_merchant/billing/gateways/braintree_blue.rb, line 161 161: def create_transaction(transaction_type, money, credit_card_or_vault_id, options) 162: transaction_params = create_transaction_parameters(money, credit_card_or_vault_id, options) 163: 164: commit do 165: result = Braintree::Transaction.send(transaction_type, transaction_params) 166: response_params, response_options, avs_result, cvv_result = {}, {}, {}, {} 167: if result.success? 168: response_params[:braintree_transaction] = transaction_hash(result.transaction) 169: response_params[:customer_vault_id] = result.transaction.customer_details.id 170: response_options[:authorization] = result.transaction.id 171: end 172: if result.transaction 173: response_options[:avs_result] = { 174: :code => nil, :message => nil, 175: :street_match => result.transaction.avs_street_address_response_code, 176: :postal_match => result.transaction.avs_postal_code_response_code 177: } 178: response_options[:cvv_result] = result.transaction.cvv_response_code 179: if result.transaction.status == "gateway_rejected" 180: message = "Transaction declined - gateway rejected" 181: else 182: message = "#{result.transaction.processor_response_code} #{result.transaction.processor_response_text}" 183: end 184: else 185: message = message_from_result(result) 186: end 187: response = Response.new(result.success?, message, response_params, response_options) 188: response.cvv_result['message'] = '' 189: response 190: end 191: end
# File lib/active_merchant/billing/gateways/braintree_blue.rb, line 271 271: def create_transaction_parameters(money, credit_card_or_vault_id, options) 272: parameters = { 273: :amount => amount(money).to_s, 274: :order_id => options[:order_id], 275: :customer => { 276: :id => options[:store] == true ? "" : options[:store], 277: :email => options[:email] 278: }, 279: :options => { 280: :store_in_vault => options[:store] ? true : false, 281: :submit_for_settlement => options[:submit_for_settlement] 282: } 283: } 284: if merchant_account_id = (options[:merchant_account_id] || @merchant_account_id) 285: parameters[:merchant_account_id] = merchant_account_id 286: end 287: if credit_card_or_vault_id.is_a?(String) || credit_card_or_vault_id.is_a?(Integer) 288: parameters[:customer_id] = credit_card_or_vault_id 289: else 290: parameters[:customer].merge!( 291: :first_name => credit_card_or_vault_id.first_name, 292: :last_name => credit_card_or_vault_id.last_name 293: ) 294: parameters[:credit_card] = { 295: :number => credit_card_or_vault_id.number, 296: :cvv => credit_card_or_vault_id.verification_value, 297: :expiration_month => credit_card_or_vault_id.month.to_s.rjust(2, "0"), 298: :expiration_year => credit_card_or_vault_id.year.to_s 299: } 300: end 301: parameters[:billing] = map_address(options[:billing_address]) if options[:billing_address] 302: parameters[:shipping] = map_address(options[:shipping_address]) if options[:shipping_address] 303: parameters 304: end
# File lib/active_merchant/billing/gateways/braintree_blue.rb, line 206 206: def customer_hash(customer) 207: credit_cards = customer.credit_cards.map do |cc| 208: { 209: "bin" => cc.bin, 210: "expiration_date" => cc.expiration_date 211: } 212: end 213: 214: { 215: "email" => customer.email, 216: "first_name" => customer.first_name, 217: "last_name" => customer.last_name, 218: "credit_cards" => credit_cards 219: } 220: end
# File lib/active_merchant/billing/gateways/braintree_blue.rb, line 193 193: def extract_refund_args(args) 194: options = args.extract_options! 195: 196: # money, transaction_id, options 197: if args.length == 1 # legacy signature 198: return nil, args[0], options 199: elsif args.length == 2 200: return args[0], args[1], options 201: else 202: raise ArgumentError, "wrong number of arguments (#{args.length} for 2)" 203: end 204: end
# File lib/active_merchant/billing/gateways/braintree_blue.rb, line 134 134: def map_address(address) 135: return {} if address.nil? 136: { 137: :street_address => address[:address1], 138: :extended_address => address[:address2], 139: :company => address[:company], 140: :locality => address[:city], 141: :region => address[:state], 142: :postal_code => address[:zip], 143: :country_name => address[:country] 144: } 145: end
# File lib/active_merchant/billing/gateways/braintree_blue.rb, line 153 153: def message_from_result(result) 154: if result.success? 155: "OK" 156: else 157: result.errors.map { |e| "#{e.message} (#{e.code})" }.join(" ") 158: end 159: end
# File lib/active_merchant/billing/gateways/braintree_blue.rb, line 222 222: def transaction_hash(transaction) 223: if transaction.vault_customer 224: vault_customer = { 225: } 226: vault_customer["credit_cards"] = transaction.vault_customer.credit_cards.map do |cc| 227: { 228: "bin" => cc.bin 229: } 230: end 231: else 232: vault_customer = nil 233: end 234: 235: customer_details = { 236: "id" => transaction.customer_details.id, 237: "email" => transaction.customer_details.email 238: } 239: 240: billing_details = { 241: "street_address" => transaction.billing_details.street_address, 242: "extended_address" => transaction.billing_details.extended_address, 243: "company" => transaction.billing_details.company, 244: "locality" => transaction.billing_details.locality, 245: "region" => transaction.billing_details.region, 246: "postal_code" => transaction.billing_details.postal_code, 247: "country_name" => transaction.billing_details.country_name, 248: } 249: 250: shipping_details = { 251: "street_address" => transaction.shipping_details.street_address, 252: "extended_address" => transaction.shipping_details.extended_address, 253: "company" => transaction.shipping_details.company, 254: "locality" => transaction.shipping_details.locality, 255: "region" => transaction.shipping_details.region, 256: "postal_code" => transaction.shipping_details.postal_code, 257: "country_name" => transaction.shipping_details.country_name, 258: } 259: 260: { 261: "order_id" => transaction.order_id, 262: "status" => transaction.status, 263: "customer_details" => customer_details, 264: "billing_details" => billing_details, 265: "shipping_details" => shipping_details, 266: "vault_customer" => vault_customer, 267: "merchant_account_id" => transaction.merchant_account_id 268: } 269: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.