This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bring braintree module nesting in line with go_cardless
- Loading branch information
Showing
52 changed files
with
1,702 additions
and
1,733 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
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
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,8 @@ | ||
class Payment::Braintree::Customer < ActiveRecord::Base | ||
belongs_to :member | ||
has_many :payment_methods, class_name: 'Payment::Braintree::PaymentMethod', foreign_key: 'customer_id' | ||
|
||
def default_payment_method | ||
payment_methods.order('created_at desc').first | ||
end | ||
end |
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,4 @@ | ||
class Payment::Braintree::PaymentMethod < ActiveRecord::Base | ||
belongs_to :customer, class_name: 'Payment::Braintree::Customer' | ||
has_many :transactions, class_name: 'Payment::Braintree::Transaction', foreign_key: 'payment_method_id' | ||
end |
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,5 @@ | ||
class Payment::Braintree::Subscription < ActiveRecord::Base | ||
belongs_to :page | ||
belongs_to :action | ||
has_many :transactions, class_name: 'Payment::Braintree::Transaction', foreign_key: :subscription_id | ||
end |
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,7 @@ | ||
class Payment::Braintree::Transaction < ActiveRecord::Base | ||
belongs_to :page | ||
belongs_to :payment_method, class_name: 'Payment::Braintree::PaymentMethod' | ||
belongs_to :customer, class_name: 'Payment::Braintree::Customer', primary_key: 'customer_id' | ||
belongs_to :subscription, class_name: 'Payment::Braintree::Subscription' | ||
enum status: [:success, :failure] | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
25 changes: 25 additions & 0 deletions
25
lib/payment_processor/braintree/acts_like_selector_with_currency.rb
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,25 @@ | ||
module PaymentProcessor | ||
module Braintree | ||
|
||
module ActsLikeSelectorWithCurrency | ||
def self.included(klass) | ||
klass.extend ClassMethods | ||
end | ||
|
||
module ClassMethods | ||
def for_currency(currency) | ||
new(currency).select_or_raise | ||
end | ||
end | ||
|
||
def initialize(currency) | ||
@currency = currency | ||
end | ||
|
||
def raise_error(message) | ||
raise PaymentProcessor::Exceptions::InvalidCurrency, message | ||
end | ||
end | ||
end | ||
end | ||
|
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,112 @@ | ||
module PaymentProcessor | ||
module Braintree | ||
class ErrorProcessing | ||
|
||
# Collection of error codes, generated by braintree for invalid | ||
# customer/credit card transaction data. | ||
# | ||
# See https://developers.braintreepayments.com/reference/general/validation-errors/all/ruby | ||
# for code definitions. | ||
# | ||
USER_ERROR_CODES = [ | ||
(81801..81813), | ||
(81501..81503), | ||
(91814..91826), | ||
(81604..81608), | ||
(81613..81616), | ||
(81709..81717), | ||
(91723..91726), | ||
81827, | ||
91803, | ||
81601, | ||
81723, | ||
81703, | ||
81706, | ||
81707, | ||
81736, | ||
81737, | ||
81528, | ||
81509].map{|code| code.is_a?(Range) ? code.to_a : code } | ||
.flatten | ||
.freeze | ||
|
||
FILTER = -> (error) { USER_ERROR_CODES.include?( error.code.to_i ) } | ||
|
||
# locale is unused, added to match API for GoCardless | ||
def initialize(response, locale: nil) | ||
@response = response | ||
end | ||
|
||
def process | ||
return user_errors if user_errors.any? | ||
return processor_errors if processor_errors.any? | ||
|
||
raise_system_errors if system_errors.any? | ||
end | ||
|
||
private | ||
|
||
def raise_system_errors | ||
raise ::Braintree::ValidationsFailed.new(error_messages) | ||
end | ||
|
||
def error_messages | ||
system_errors.map{|e| "#{e.message} (#{e.code} on #{e.attribute})"}.join(', ') | ||
end | ||
|
||
def user_errors | ||
@user_errors ||= errors.select(&FILTER) | ||
end | ||
|
||
def system_errors | ||
@system_errors ||= errors.reject(&FILTER) | ||
end | ||
|
||
def errors | ||
@response.errors | ||
end | ||
|
||
def processor_errors | ||
if @response.transaction | ||
case @response.transaction.status | ||
when 'processor_declined' | ||
processor_declined_error | ||
when 'gateway_rejected' | ||
gateway_rejected_error | ||
when 'settlement_declined' | ||
settlement_declined_error | ||
else | ||
[] | ||
end | ||
else | ||
[] | ||
end | ||
end | ||
|
||
def processor_declined_error | ||
[{ | ||
declined: true, | ||
code: @response.transaction.processor_response_code, | ||
message: @response.transaction.processor_response_text | ||
}] | ||
end | ||
|
||
def settlement_declined_error | ||
[{ | ||
declined: true, | ||
code: @response.transaction.processor_settlement_response_code, | ||
message: @response.transaction.processor_settlement_response_text | ||
}] | ||
end | ||
|
||
def gateway_rejected_error | ||
[{ | ||
declined: true, | ||
code: '', | ||
message: @response.transaction.gateway_rejection_reason | ||
}] | ||
end | ||
end | ||
end | ||
end | ||
|
17 changes: 17 additions & 0 deletions
17
lib/payment_processor/braintree/merchant_account_selector.rb
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,17 @@ | ||
module PaymentProcessor | ||
module Braintree | ||
class MerchantAccountSelector | ||
include ActsLikeSelectorWithCurrency | ||
|
||
MERCHANT_ACCOUNTS = Settings.braintree.merchants.freeze | ||
|
||
def select_or_raise | ||
raise_error if @currency.blank? | ||
id = MERCHANT_ACCOUNTS[@currency.upcase.to_sym] | ||
raise_error("No merchant account is associated with this currency: #{@currency}") unless id | ||
id | ||
end | ||
end | ||
end | ||
end | ||
|
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,51 @@ | ||
module PaymentProcessor | ||
module Braintree | ||
class Populator | ||
|
||
def customer_options | ||
@customer_options ||= { | ||
first_name: @user[:first_name] || namesplitter.first_name, | ||
last_name: @user[:last_name] || namesplitter.last_name, | ||
email: @user[:email] || '' | ||
} | ||
end | ||
|
||
def billing_options | ||
@billing_options ||= { | ||
first_name: customer_options[:first_name], | ||
last_name: customer_options[:last_name] | ||
}.tap do |options| | ||
populate( options, :region, [:province, :state, :region]) | ||
populate( options, :company, [:company]) | ||
populate( options, :locality, [:city, :locality]) | ||
populate( options, :postal_code, [:zip, :zip_code, :postal, :postal_code]) | ||
populate( options, :street_address, [:address, :address1, :street_address]) | ||
populate( options, :extended_address, [:apartment, :address2, :extended_address]) | ||
populate( options, :country_code_alpha2, [:country, :country_code, :country_code_alpha2]) | ||
end | ||
end | ||
|
||
def populate(options, field, pick_from) | ||
pick_from.each do |key| | ||
options[field] = @user[key] if @user[key].present? | ||
end | ||
end | ||
|
||
def namesplitter | ||
@splitter ||= NameSplitter.new(full_name: @user[:full_name] || @user[:name]) | ||
end | ||
|
||
def existing_customer | ||
@existing_customer ||= Payment::Braintree.customer(@user[:email]) | ||
end | ||
|
||
def success? | ||
@result.success? | ||
end | ||
|
||
def error_container | ||
@result | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.