Create a gist now

Instantly share code, notes, and snippets.

@fteem /BasePayment.rb Secret
Last active Aug 29, 2015

class BasePayment
def initialize card, amount
@card = card
@amount = amount
end
def process_payment!
authenticate_merchant && make_payment
end
def authenticate_merchant
raise NotImplementedError.new "authenticate_merchant"
end
def make_payment
raise NotImplementedError.new "make_payment"
end
end
class StripePayment < BasePayment
def authenticate_merchant
begin
return true if Stripe::Merchant.authenticate ENV['STRIPE_API_KEY']
rescue Stripe::MerchantError => e
Rails.logger.error "Cannot establish connection between Merchant and Provider."
return false
rescue Stripe::ProviderUnreachable => e
Rails.logger.error "Provider unreachable."
return false
end
end
def make_payment
begin
return true if Stripe::Payment.process! ENV['STRIPE_API_KEY'], @card, @amount
rescue Stripe::PaymentUnprocessable => e
Rails.logger.error "Payment unprocessable, try again."
return false
end
end
end
@serg-kovalev

Hello, thx for the great article!
Just my two cents about raising exceptions. I believe that better to use this one built-in exception class:

raise NotImplementedError.new 'bla_bla_bla'

intead of:

raise "Implement 'bla_bla_bla'."
@fteem
Owner
fteem commented Apr 13, 2015

Yes! I just got the same suggestion in the comments on the blog - I updated the examples. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment