user1924165
user1924165

Reputation: 109

Single Charge with Stripe in Rails

I'm trying to figure out how to accept credit card using stripe in a single charge that is entered within the forms input field.

Example: user1 logged in and creates a post, entered $20 in a field and clicks submit. The post gets created and $20 gets charged to his card. User2 does the same but puts $45 and enters his credit card and submits the form.

I've got the following done; - devise setup - roles - stripe gem installed and account setup

I haven't found an example that would explain a single charge to stripe, everything is subscription based.

I'm new to this, so I'm trying to mimic the subscription examples but without success.

Upvotes: 2

Views: 1647

Answers (2)

Abel Pc Ag
Abel Pc Ag

Reputation: 96

You need to use Stripe.js in order to get a token, then pass that token string instead of credit card object.

For testing purposes, you can go to https://stripe.com/docs/stripe-js and obtain a token from the form like in this image: enter image description here

How to use it in rails console:

gateway = ActiveMerchant::Billing::StripeGateway.new(:login => STRIPE_SECRET_KEY)
gateway.purchase(1200, "tok_1E0CzPDdSOg3FLH0IFmWo1T2")

Upvotes: 0

Jeff Steil
Jeff Steil

Reputation: 1770

Here is how I am executing a single charge using Stripe and the ActiveMerchant gem.

transaction = ActiveMerchant::Billing::StripeGateway.new(:login => STRIPE_SECRET_KEY)

paymentInfo = ActiveMerchant::Billing::CreditCard.new(
            :number             => "4242424242424242",
            :month              => "12",
            :year               => "2020",
            :verification_value => "411")

purchaseOptions = {:billing_address => {
    :name     => "Customer Name",
    :address1 => "Customer Address Line 1",
    :city     => "Customer City",
    :state    => "Customer State",
    :zip      => "Customer Zip Code"
}}

response = transaction.purchase((17.50 * 100).to_i, paymentInfo, purchaseOptions)

if response.success? then
  logger.debug "charge successful"
end

Upvotes: 7

Related Questions