app/models/spree/gateway/stripe_gateway.rb in spree_gateway-2.2.1 vs app/models/spree/gateway/stripe_gateway.rb in spree_gateway-3.0.0

- old
+ new

@@ -1,10 +1,16 @@ module Spree class Gateway::StripeGateway < Gateway preference :secret_key, :string preference :publishable_key, :string + CARD_TYPE_MAPPING = { + 'American Express' => 'american_express', + 'Diners Club' => 'diners_club', + 'Visa' => 'visa' + } + def method_type 'stripe' end def provider_class @@ -21,35 +27,48 @@ def authorize(money, creditcard, gateway_options) provider.authorize(*options_for_purchase_or_auth(money, creditcard, gateway_options)) end - def capture(payment, creditcard, gateway_options) - provider.capture((payment.amount * 100).round, payment.response_code, gateway_options) + def capture(money, response_code, gateway_options) + provider.capture(money, response_code, gateway_options) end def credit(money, creditcard, response_code, gateway_options) provider.refund(money, response_code, {}) end def void(response_code, creditcard, gateway_options) provider.void(response_code, {}) end + def cancel(response_code) + provider.void(response_code, {}) + end + def create_profile(payment) return unless payment.source.gateway_customer_profile_id.nil? options = { email: payment.order.email, login: preferred_secret_key, }.merge! address_for(payment) - response = provider.store(payment.source, options) + source = update_source!(payment.source) + if source.number.blank? && source.gateway_payment_profile_id.present? + creditcard = source.gateway_payment_profile_id + else + creditcard = source + end + + response = provider.store(creditcard, options) if response.success? payment.source.update_attributes!({ - :gateway_customer_profile_id => response.params['id'], - :gateway_payment_profile_id => response.params['default_card'] + cc_type: payment.source.cc_type, # side-effect of update_source! + gateway_customer_profile_id: response.params['id'], + gateway_payment_profile_id: response.params['default_source'] || response.params['default_card'] }) + else payment.send(:gateway_error, response.message) end end @@ -59,11 +78,10 @@ def options options = super options.merge(:login => preferred_secret_key) end - def options_for_purchase_or_auth(money, creditcard, gateway_options) options = {} options[:description] = "Spree Order ID: #{gateway_options[:order_id]}" options[:currency] = gateway_options[:currency] @@ -96,8 +114,13 @@ if state = address.state options[:address].merge!(state: state.name) end end end + end + + def update_source!(source) + source.cc_type = CARD_TYPE_MAPPING[source.cc_type] if CARD_TYPE_MAPPING.include?(source.cc_type) + source end end end