samedi 9 mai 2015

Rails with Stripe Checkout: How can i seed my db with sample charges and use stripe checkout to generate tokens for each charge?

So I have a rails app that takes donations for a fund raising project using stripe checkout. I save the stripetokens to my db, and then go back and process them all at once if the project funding goal is met (sorta like kickstarter). I've figured out how to do this and have tested my code on a small number of charges (say like 10) without any problems. Here's my code

@project = Project.find(set_project)

# Create new stripe customer
    @customer = Stripe::Customer.create(
        :email => params[:stripeEmail],
        :card => params[:stripeToken]
        )

# Create new charge
    @charge = Charge.new(
        :email => params[:stripeEmail],
        :stripe_token => params[:stripeToken],
        :project_id => @project.id,
        :amount => params[:amount],
        :customer_id => @customer.id
        )
#Save charge to my db
@charge.save

Then in my project model

# Cycle through all charges for project and process using 
# stripe token and stripe customer id
def charge
    self.charges.each do |x|
        begin
            Stripe::Charge.create(
                :amount => x.amount,
                :currency => 'usd',
                :customer => x.customer_id
            )
        rescue Stripe::CardError => e
            x.error = e
        else
            x.processed = true
            x.save
        end
    end
end

Now here's my question. I want to be able to test this code at higher volumes of charges. How can I seed my db with 1,000 or more sample charges and use the stripe checkout script to create a customer and generate tokens for each one?

Aucun commentaire:

Enregistrer un commentaire