1
votes

How do I charge an account's external account in the Stripe Connected Accounts API?

I create invoices on my system then I want to let the users pay them.

On the first screen I list their payment accounts that are saved on the system. This will be bank accounts, credit cards, debit cards.

When they select the account to pay with this is where my trouble starts.

Here is my code

def pay_invoice invoice_id, source_id
    invoice = Invoice.find(invoice_id)
    account = Stripe::Account.retrieve(@account)
    charge_source = account.external_accounts.retrieve(source_id)

    c = Stripe::Charge.create(
        :amount => invoice.cents,
        :currency => "usd",
        #:customer => @account,
        :source => charge_source,
        :description => "Charge for invoice ##{invoice_id}"
    )

end

the source_id passed in to the method is the id of the external account I got earlier.

My first attempt was to pass that as the source and I got the error "No such token"

2
I'm thinking I need to add each account as a customer as well :(David Silva Smith

2 Answers

1
votes

From what I can tell there is no good way to charge accounts through the connected accounts API.

I'm going to make a Stripe account for each user and also a Stripe contact for each user.

Then the code ends up looking like this:

def pay_invoice invoice_id, source_id
    invoice = Invoice.find(invoice_id)
    c = Stripe::Customer.retrieve(@customer_id)
    paying_source = nil
    c.sources.each do | source|
        if source.id == source_id 
            paying_source = source
        end
    end

    c = Stripe::Charge.create(
        :amount => invoice.cents,
        :currency => "usd",
        :customer => c,
        :source => paying_source,
        :description => "Charge for invoice ##{invoice_id}"
    )

    invoice.status = "paid"
    invoice.save

end
0
votes

When you take Credit/debit card from user Stripe returns a Token and you can do two things 1. You can charge the customer for one time and the token will expire. 2. Create customer using that token and save for future payments So using this way you can make further charges

Here you are using that token for charge and it is getting expire so next same token is giving error.