-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2001 from tvdeyen/rename-gateway
Rename Gateway into PaymentMethod::CreditCard
- Loading branch information
Showing
32 changed files
with
572 additions
and
366 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
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 |
---|---|---|
@@ -1,62 +1,12 @@ | ||
module Spree | ||
# A concrete implementation of `Spree::PaymentMethod` intended to provide a | ||
# base for extension. See https://github.com/solidusio/solidus_gateway/ for | ||
# offically supported payment gateway implementations. | ||
# | ||
class Gateway < PaymentMethod | ||
delegate :authorize, :purchase, :capture, :void, :credit, to: :provider | ||
|
||
validates :name, :type, presence: true | ||
|
||
preference :server, :string, default: 'test' | ||
preference :test_mode, :boolean, default: true | ||
|
||
def payment_source_class | ||
CreditCard | ||
end | ||
|
||
def provider | ||
gateway_options = options | ||
gateway_options.delete :login if gateway_options.key?(:login) && gateway_options[:login].nil? | ||
if gateway_options[:server] | ||
ActiveMerchant::Billing::Base.mode = gateway_options[:server].to_sym | ||
end | ||
@provider ||= provider_class.new(gateway_options) | ||
end | ||
|
||
def options | ||
preferences.to_hash | ||
end | ||
|
||
def payment_profiles_supported? | ||
false | ||
end | ||
|
||
def method_type | ||
'gateway' | ||
end | ||
|
||
def supports?(source) | ||
return true unless provider_class.respond_to? :supports? | ||
return true if source.brand && provider_class.supports?(source.brand) | ||
source.has_payment_profile? | ||
end | ||
|
||
def reusable_sources_by_order(order) | ||
source_ids = order.payments.where(payment_method_id: id).pluck(:source_id).uniq | ||
payment_source_class.where(id: source_ids).select(&:reusable?) | ||
end | ||
alias_method :sources_by_order, :reusable_sources_by_order | ||
deprecate sources_by_order: :reusable_sources_by_order, deprecator: Spree::Deprecation | ||
|
||
def reusable_sources(order) | ||
if order.completed? | ||
reusable_sources_by_order(order) | ||
elsif order.user_id | ||
order.user.wallet.wallet_payment_sources.map(&:payment_source).select(&:reusable?) | ||
else | ||
[] | ||
end | ||
# @deprecated Use Spree::PaymentMethod::CreditCard or Spree::PaymentMethod instead | ||
class Gateway < PaymentMethod::CreditCard | ||
def initialize(*args) | ||
Spree::Deprecation.warn \ | ||
"Using Spree::Gateway as parent class of payment methods is deprecated. " \ | ||
"Please use Spree::PaymentMethod::CreditCard for credit card based payment methods " \ | ||
"or Spree::PaymentMethod for non credit card payment methods instead." | ||
super | ||
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 |
---|---|---|
@@ -1,87 +1,11 @@ | ||
module Spree | ||
class Gateway::Bogus < Gateway | ||
TEST_VISA = ['4111111111111111', '4012888888881881', '4222222222222'] | ||
TEST_MC = ['5500000000000004', '5555555555554444', '5105105105105100'] | ||
TEST_AMEX = ['378282246310005', '371449635398431', '378734493671000', '340000000000009'] | ||
TEST_DISC = ['6011000000000004', '6011111111111117', '6011000990139424'] | ||
|
||
VALID_CCS = ['1', TEST_VISA, TEST_MC, TEST_AMEX, TEST_DISC].flatten | ||
|
||
attr_accessor :test | ||
|
||
def provider_class | ||
self.class | ||
end | ||
|
||
def create_profile(payment) | ||
return if payment.source.has_payment_profile? | ||
# simulate the storage of credit card profile using remote service | ||
if success = VALID_CCS.include?(payment.source.number) | ||
payment.source.update_attributes(gateway_customer_profile_id: generate_profile_id(success)) | ||
end | ||
end | ||
|
||
def authorize(_money, credit_card, _options = {}) | ||
profile_id = credit_card.gateway_customer_profile_id | ||
if VALID_CCS.include?(credit_card.number) || (profile_id && profile_id.starts_with?('BGS-')) | ||
ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345', avs_result: { code: 'D' }) | ||
else | ||
ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', { message: 'Bogus Gateway: Forced failure' }, test: true) | ||
end | ||
end | ||
|
||
def purchase(_money, credit_card, _options = {}) | ||
profile_id = credit_card.gateway_customer_profile_id | ||
if VALID_CCS.include?(credit_card.number) || (profile_id && profile_id.starts_with?('BGS-')) | ||
ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345', avs_result: { code: 'M' }) | ||
else | ||
ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', message: 'Bogus Gateway: Forced failure', test: true) | ||
end | ||
end | ||
|
||
def credit(_money, _credit_card, _response_code, _options = {}) | ||
ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345') | ||
end | ||
|
||
def capture(_money, authorization, _gateway_options) | ||
if authorization == '12345' | ||
ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true) | ||
else | ||
ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', error: 'Bogus Gateway: Forced failure', test: true) | ||
end | ||
end | ||
|
||
def void(_response_code, _credit_card, _options = {}) | ||
ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345') | ||
end | ||
|
||
def cancel(_response_code) | ||
ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345') | ||
end | ||
|
||
def test? | ||
# Test mode is not really relevant with bogus gateway (no such thing as live server) | ||
true | ||
end | ||
|
||
def payment_profiles_supported? | ||
true | ||
end | ||
|
||
def actions | ||
%w(capture void credit) | ||
end | ||
|
||
private | ||
|
||
def generate_profile_id(success) | ||
record = true | ||
prefix = success ? 'BGS' : 'FAIL' | ||
while record | ||
random = "#{prefix}-#{Array.new(6){ rand(6) }.join}" | ||
record = Spree::CreditCard.where(gateway_customer_profile_id: random).first | ||
end | ||
random | ||
# @deprecated Use Spree::PaymentMethod::BogusCreditCard instead | ||
class Gateway::Bogus < PaymentMethod::BogusCreditCard | ||
def initialize(*args) | ||
Spree::Deprecation.warn \ | ||
'Spree::Gateway::Bogus is deprecated. ' \ | ||
'Please use Spree::PaymentMethod::BogusCreditCard instead' | ||
super | ||
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 |
---|---|---|
@@ -1,24 +1,11 @@ | ||
module Spree | ||
# Bogus Gateway that doesn't support payment profiles. | ||
class Gateway::BogusSimple < Gateway::Bogus | ||
def payment_profiles_supported? | ||
false | ||
end | ||
|
||
def authorize(_money, credit_card, _options = {}) | ||
if VALID_CCS.include? credit_card.number | ||
ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345', avs_result: { code: 'A' }) | ||
else | ||
ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', { message: 'Bogus Gateway: Forced failure' }, test: true) | ||
end | ||
end | ||
|
||
def purchase(_money, credit_card, _options = {}) | ||
if VALID_CCS.include? credit_card.number | ||
ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345', avs_result: { code: 'A' }) | ||
else | ||
ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', message: 'Bogus Gateway: Forced failure', test: true) | ||
end | ||
# @deprecated Use Spree::PaymentMethod::SimpleBogusCreditCard instead | ||
class Gateway::BogusSimple < Spree::PaymentMethod::SimpleBogusCreditCard | ||
def initialize(*args) | ||
Spree::Deprecation.warn \ | ||
'Spree::Gateway::BogusSimple is deprecated. ' \ | ||
'Please use Spree::PaymentMethod::SimpleBogusCreditCard instead' | ||
super | ||
end | ||
end | ||
end |
Oops, something went wrong.