Devbreaker29
Devbreaker29

Reputation: 23

Stripe Test API for payments not working on Ruby on Rails App?

I am developing an app using Ruby on Rails for my study course. For payments I installed Stripe on the app using a test API and it worked fine, but then I installed it on another app and used the same test API. When I click on Buy Now it doesn't go to the Stripe payment page and I get a message "Waiting for q.stripe.com". Can I use my same Test API for two apps or is it a different problem?

My code is follows: In the places_controller.rb

def show
    session = Stripe::Checkout::Session.create(
      payment_method_types: ['card'],
      customer_email: current_user.email,
      line_items:  [{
        name: @place.name,
        description: @place.description,
        amount: @place.price,
        currency: 'aud',
        quantity: 1
      }],
      payment_intent_data: {
        metadata: {
          user_id: current_user.id,
          place_id: @place.id
        }
      },
      success_url: "#{root_url}payments/success?placeId=#{@place.id}",
      # todo - check if we can use places_path
      cancel_url: "#{root_url}places"
    )

    @sessions_id = session.id
  end

and in the show.html.erb for Places View

<button data-stripe="payment">
Buy Now!
</button>

<script src="https://js.stripe.com/v3/"></script>
<script>
  document
    .querySelector("[data-stripe='payment']")
    .addEventListener("click", () => {
      const stripe = Stripe(
        "<%= Rails.application.credentials.dig(:stripe, :public_key) %>"
      )

      stripe.redirectToCheckout({
        sessionId: "<%= @session_id %>"
      })
    })
</script>

stripe.rb in config/initializers

Stripe.api_key = Rails.application.credentials.dig(:stripe, :secret_key)

Upvotes: 0

Views: 247

Answers (1)

Zhi Kai
Zhi Kai

Reputation: 1587

Yes, you can use your test / production keys on multiple applications, if that is what you're trying to ask. It would help to check the console for any error messages.

Upvotes: 1

Related Questions