-
-
Save fteem/8a7073359b7cbcb2b86a to your computer and use it in GitHub Desktop.
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
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 |
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
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 |
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
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:
intead of: