Save 36% for Black Friday! Learn more

Charles Smith

Joined

850 Experience
0 Lessons Completed
1 Question Solved

Activity

Posted in Passing name objects to Stripe

Hi Chris

A few months ago I purchased the Stripe master class https://courses.gorails.com/payments-with-stripe-and-rails-master-class - awsome - learned a lot!! I could ask this question on Stack Overflow, but since the methods are specific to the course, I thought it be best asked here.

In the user.rb file, there is the method listed below that creates a Stripe customer is one doesn't exist.

def stripe_customer
    if stripe_id?
      Stripe::Customer.retrieve(stripe_id)
    else
      stripe_customer = Stripe::Customer.create(email: email)
      update(stripe_id: stripe_customer.id)
      stripe_customer
    end
  end

Stripe has a customer name field, how can I pass the user_first and user_last along with the email when creating a new customer? I looked on the Stripe api and it doesn't make reference at all to a name object.

Thank you.

I cannot for the life of me figure out the best model association to choose and I've thought myself into a corner. I'm building a site for a client that offers party services for four(4) locations in Southern California.

For this, I have three(3) models - Locations, Categories, Products.

Here are some URL examples:

http://somewebsite.com/san-diego/birthday-parties/boy-themed
http://somewebsite.com/orange-county/birthday-parties/boy-themed
http://somewebsite.com/los-angeles/birthday-parties/boy-themed
http://somewebsite.com/riverside/birthday-parties/boy-themed

The products model will have 4 price columns and the price on the page will be determined by the /location-url/ and will be displayed with jQuery. Other than that all the models are shared with the only caveate is that a Product has to belong to a category and location, but I want to be able to share the Product and Category models with many locations so I don't have to create a duplicate for each location. I hope I am making some sense. Thank you.

Posted in assign_attributes not working

In my addFieldToForm function, I had hiddenInput.setAttribute('name', "user[card_" + field + "]"); but after changing it to hiddenInput.setAttribute('name', "card_" + field); it works as expected.

Posted in assign_attributes not working

I just signed up for Payments with Stripe and Rails Master Class and am loving it...can't walk away from it; learning a ton!

Having an issue though and was hoping someone could lend a hand. I am following the videos exactly and the only thing I can see that is different is the videos are using the Stripejs form example from Stripe.com whereas I am using Elements. There is an Elements section in the videos which is why I implemented it, but the videos continue without it. Either way, things seem to be working ok, well - until this snag. Oh, even with the attributes not being assigned, Stripe is still creating/updating users properly.

I am trying to assign_attributes if params[:card_last4] but the attributes are not saving so I am not able to display card_brand, card_last4 etc.. Below is my code, thank you.

subscriptions_controller

def create
    customer = current_user.stripe_customer
    begin
      subscription = customer.subscriptions.create(
          source: params[:stripeToken],
          plan: params[:plan],
      )

      current_user.assign_attributes(
          card_brand: params[:card_brand],
          card_last4: params[:card_last4],
          card_exp_month: params[:card_exp_month],
          card_exp_year: params[:card_exp_year]
      ) if params[:card_last4]
      current_user.assign_attributes(stripe_subscription_id: subscription.id)

      current_user.save

      flash.notice = 'Thank you for subscribing'
      redirect_to root_path
    rescue Stripe::CardError => e
      flash.alert = e.message
      render action: :new
    end
  end

views/subscriptions/new.html.erb

<h1>Subscribe</h1>
<%= form_tag subscription_path, id: 'payment-form' do |form| %>
  <div class="form-row">
    <label for="card-element">
      Credit or debit card
    </label>
    <div id="card-element">
      <!-- a Stripe Element will be inserted here. -->
    </div>
    <%= hidden_field_tag :plan, params[:plan] %>
    <!-- Used to display Element errors -->
    <div id="card-errors" role="alert"></div>
  </div>

  <button>Submit Payment</button>

<% end %>