Last active
February 16, 2016 20:26
-
-
Save SeanRoberts/3396a1f1795fedea79b1 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
# somewhere else, maybe in your controller or your checkout service class or something | |
def process_order | |
if @item.stripe_card_token.present? | |
status = StripeProcessor.process_new_card(@order, @item.stripe_card_token) | |
else | |
status = StripeProcessor.process_customer(@order, @item.stripe_customer_token) | |
end | |
if status == :success | |
handle_payment_success | |
else | |
handle_payment_failure | |
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
# Stateless (for now), run either: | |
# | |
# StripeProcessor.process_new_card(order, card_token) | |
# | |
# or | |
# | |
# StripeProcessor.process_customer(order, customer_token) | |
# | |
# to process an order. | |
# | |
# card_token and customer_token are fields that should come from your | |
# payment form. | |
# | |
# Order must be an instance that responds to: | |
# .stripe_customer_id=, .charge_id=, .charge!, .total_price_in_cents, | |
# .user.email, .id, .valid?, .stripe_error, .fail!, .failed? | |
# | |
# Returns either :success, :invalid, or :failure. | |
class StripeProcessor | |
class << self | |
def process_new_card(order, card_token) | |
return :invalid unless order.valid? | |
customer = create_customer(order, card_token) | |
process_with_stripe(order, customer) | |
rescue Stripe::CardError => e | |
handle_card_error(order, e) | |
end | |
def process_customer(order, customer_token) | |
return :invalid unless order.valid? | |
customer = Stripe::Customer.retrieve(customer_token) | |
process_with_stripe(order, customer) | |
rescue Stripe::CardError => e | |
handle_card_error(order, e) | |
end | |
private | |
def process_with_stripe(order, customer) | |
charge = create_charge(order, customer) | |
order.stripe_customer_id = customer.id | |
order.charge_id = charge.id | |
order.charge! | |
:success | |
end | |
def create_customer(order, card_token) | |
Stripe::Customer.create( | |
email: order.user.email, | |
card: card_token | |
) | |
end | |
def create_charge(order, customer) | |
Stripe::Charge.create( | |
customer: customer.id, | |
amount: order.total_price_in_cents, | |
description: "Tradyo Order ##{order.id}", | |
currency: 'cad', | |
capture: true | |
) | |
end | |
def handle_card_error(order, e) | |
order.stripe_error = e.message | |
order.fail! unless order.failed? | |
:failure | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment