Skip to content

Instantly share code, notes, and snippets.

@fteem
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fteem/8a7073359b7cbcb2b86a to your computer and use it in GitHub Desktop.
Save fteem/8a7073359b7cbcb2b86a to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link
Author

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