Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iloveitaly/8dc375a571bc3a0267b5722b3f4c954f to your computer and use it in GitHub Desktop.
Save iloveitaly/8dc375a571bc3a0267b5722b3f4c954f to your computer and use it in GitHub Desktop.
# Author: <mike@suitesync.io>
# Description: Create a "fresh" charge when a uncaptured Stripe charge expires (i.e. is 7 days past the original authorization date)
# Resources:
# - https://support.stripe.com/questions/does-stripe-support-authorize-and-capture
# - https://stripe.com/blog/auth-capture
require 'stripe'
@token = 'sk_test_'
def create_fresh_charge(stripe_charge)
if !stripe_charge.customer
puts 'capture failed, and standalone charge detected'
return false
end
stripe_customer = Stripe::Customer.retrieve(stripe_charge.customer, @token)
original_payment_source = stripe_charge.source
payment_source = stripe_customer.sources.detect { |s| s.id == original_payment_source.id }
if !payment_source
fail "original payment source cannot be found"
end
fresh_stripe_charge = Stripe::Charge.create({
amount: stripe_charge.amount,
currency: stripe_charge.currency,
description: stripe_charge.description,
# customer ID is required when specifying a source!
customer: stripe_customer.id,
source: payment_source.id,
metadata: {
# note original charge ID on the charge
original_charge_id: stripe_charge.id
}
}, @token)
end
# pull an uncaptured and expired charge
charge = Stripe::Charge.retrieve('ch_123')
create_fresh_charge(charge)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment