Created
May 6, 2015 12:29
-
-
Save jsuchal/5a86f34dcd198a0326de 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 PaymentGatewayCallbackService | |
# New custom exception | |
TransactionFailed = Class.new(StandardError) | |
def callback(order_id, gateway_transaction_attributes) | |
order = Order.find(order_id) | |
transaction = order.order_transactions.create(callback: gateway_transaction_attributes) | |
# raise the exception when things went wrong | |
transaction.successful? or raise TransactionFailed | |
order.paid! | |
OrderMailer.order_paid(order.id).deliver | |
rescue ActiveRecord::RecordNotFound, TransactionFailed => e | |
raise | |
rescue => e | |
Honeybadger.notify(e) | |
AdminOrderMailer.order_problem(order.id).deliver | |
raise | |
end | |
end | |
class PaymentGatewayController < ApplicationController | |
ALLOWED_IPS = ["127.0.0.1"] | |
before_filter :whitelist_ip | |
def callback | |
PaymentGatewayCallbackService.new.callback(params[:order_id], gateway_transaction_attributes) | |
redirect_to successful_order_path(params[:order_id]) | |
# Rescue and redirect | |
rescue PaymentGatewayCallbackService::TransactionFailed => f | |
redirect_to retry_order_path(params[:order_id]) | |
rescue ActiveRecord::RecordNotFound => e | |
redirect_to missing_order_path(params[:order_id]) | |
rescue | |
redirect_to failed_order_path(params[:order_id]), alert: t("order.problems") | |
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 PaymentGatewayService < Struct.new(:listener) | |
def callback(order_id, gateway_transaction_attributes) | |
order = Order.find_by_id(order_id) | |
return listener.order_missing unless order | |
transaction = order.order_transactions.create(callback: gateway_transaction_attributes) | |
if transaction.successful? | |
order.paid! | |
OrderMailer.order_paid(order.id).deliver | |
listener.order_successful(order) | |
else | |
listener.transaction_unsuccessful(order) | |
end | |
rescue => e | |
listener.order_error(order, e) | |
end | |
end | |
class PaymentGatewayController < ApplicationController | |
ALLOWED_IPS = ["127.0.0.1"] | |
before_filter :whitelist_ip | |
def callback | |
PaymentGatewayService.new(self).callback(params[:order_id], gateway_transaction_attributes) | |
end | |
# listener callbacks | |
def order_successful(order) | |
redirect_to successful_order_path(order.id) | |
end | |
def transaction_unsuccessful(order) | |
redirect_to retry_order_path(order.id) | |
end | |
def order_missing | |
redirect_to missing_order_path(params[:order_id]) | |
end | |
def order_error(order, exception) | |
Honeybadger.notify(exception) | |
AdminOrderMailer.order_problem(order.id).deliver | |
redirect_to failed_order_path(order.id), alert: t("order.problems") | |
end | |
private | |
def whitelist_ip | |
raise UnauthorizedIpAccess unless ALLOWED_IPS.include?(request.remote_ip) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment