Brian Gilbank

Joined

7,810 Experience
50 Lessons Completed
3 Questions Solved

Activity

Ah I am dumb, I got it working. Forgot that I needed to add the admin module in my form.

<%= form_for ["admin", @user] do |f| %>

Thank you for the quick reply.

Hey Chris - Do you have a best practices guide for server maintenance?
Brian

Posted in Nested Comment Threads in Rails - Part 1 Discussion

Woohoo, thanks for doing this!

Posted in Comments With Polymorphic Associations Discussion

That would be awesome. Thanks!

Posted in Comments With Polymorphic Associations Discussion

It would great if you could expand this to include comment replies. There are no great tutorials on how to accomplish this task.

Posted in Paypal & Stripe

I guess I am just not understanding how to change it from a subscription to a plan.

I am also currently using a developement url not a production one for my webhooks.

irb(main):001:0> e = Stripe::Event.retrieve("evt_1D3oX72OobZT8tZuFcNxXcpk")
=> #<Stripe::Event:0x2b1cb2657800 id=evt_1D3oX72OobZT8tZuFcNxXcpk> JSON: {
  "id": "evt_1D3oX72OobZT8tZuFcNxXcpk",
  "object": "event",  "api_version": "2018-05-21",
  "created": 1535390985,  "data": {"object":{"id":"sub_DUo9TmZzClFG1N","object":"subscription","application_fee_percent":null,"billing":"charge_automatically","billing_cycle_anchor":1535390983,"cancel_at_period_end":false,"canceled_at":null,"created":1535390983,"current_period_end":1538069383,"current_period_start":1535390983,"customer":"cus_DT7O4lor9yOHge","days_until_due":null,"discount":null,"ended_at":null,"items":{"object":"list","data":[{"id":"si_DUo92aCIKDATBh","object":"subscription_item","created":1535390984,"metadata":{},"plan":{"id":"monthly","object":"plan","active":true,"aggregate_usage":null,"amount":1900,"billing_scheme":"per_unit","created":1534660009,"currency":"cad","interval":"month","interval_count":1,"livemode":false,"metadata":{},"nickname":"Monthly","product":"prod_DRdeH2Q4y35faL","tiers":null,"tiers_mode":null,"transform_usage":null,"trial_period_days":null,"usage_type":"licensed"},"quantity":1,"subscription":"sub_DUo9TmZzClFG1N"}],"has_more":false,"total_count":1,"url":"/v1/subscription_items?subscription=sub_DUo9TmZzClFG1N"},"livemode":false,"metadata":{},"plan":{"id":"monthly","object":"plan","active":true,"aggregate_usage":null,"amount":1900,"billing_scheme":"per_unit","created":1534660009,"currency":"cad","interval":"month","interval_count":1,"livemode":false,"metadata":{},"nickname":"Monthly","product":"prod_DRdeH2Q4y35faL","tiers":null,"tiers_mode":null,"transform_usage":null,"trial_period_days":null,"usage_type":"licensed"},"quantity":1,"start":1535390983,"status":"active","tax_percent":null,"trial_end":null,"trial_start":null}},
  "livemode": false,
  "pending_webhooks": 1,
  "request": {"id":"req_jreFxqxCqmXk5P","idempotency_key":null},
  "type": "customer.subscription.created"
}
irb(main):002:0> Webhooks::ChargeSucceeded.new.call(e)
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."stripe_id" = $1 LIMIT $2  [["stripe_id", "cus_DT7O4lor9yOHge"], ["LIMIT", 1]]
  Charge Load (0.2ms)  SELECT  "charges".* FROM "charges" WHERE "charges"."user_id" = $1 AND "charges"."stripe_id" = $2 ORDER BY "charges"."id" ASC LIMIT $3  [["user_id", 9], ["stripe_id", "sub_DUo9TmZzClFG1N"], ["LIMIT", 1]]
   (0.1ms)  BEGIN
  Charge Create (33.9ms)  INSERT INTO "charges" ("user_id", "stripe_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["user_id", 9], ["stripe_id", "sub_DUo9TmZzClFG1N"], ["created_at", "2018-08-27 22:52:08.728957"], ["updated_at", "2018-08-27 22:52:08.728957"]]
   (7.2ms)  COMMIT
Traceback (most recent call last):
        2: from (irb):2
        1: from app/models/webhooks/charge_succeeded.rb:10:in `call'
NoMethodError (undefined method `amount' for #<Stripe::Subscription:0x00005639687c59c8>)

Inside of Stripe

Webhooks
Pending
http://localhost:3000/webhooks/stripe
Status
503 Pending (5 tries)
Retry history
[2018/08/27 11:29 to http://localhost:3000]: (503) ERR
[2018/08/27 12:49 to http://localhost:3000]: (503) ERR
[2018/08/27 14:02 to http://localhost:3000]: (503) ERR
[2018/08/27 15:07 to http://localhost:3000/webhooks/stripe]: (503) ERR
[2018/08/27 15:47 to http://localhost:3000/webhooks/stripe]: (503) ERR
Request
{
  "id": "evt_1D3oX72OobZT8tZuFcNxXcpk",
  "object": "event",
  "api_version": "2018-05-21",
  "created": 1535390985,
  "data": {
    "object": {
      "id": "sub_DUo9TmZzClFG1N",
      "object": "subscription",
      "application_fee_percent": null,
      "billing": "charge_automatically",
      "billing_cycle_anchor": 1535390983,
      "cancel_at_period_end": false,
      "canceled_at": null,
      "created": 1535390983,
      "current_period_end": 1538069383,
      "current_period_start": 1535390983,
      "customer": "cus_DT7O4lor9yOHge",
      "days_until_due": null,
      "discount": null,
      "ended_at": null,
      "items": {
        "object": "list",
        "data": [
          {
            "id": "si_DUo92aCIKDATBh",
            "object": "subscription_item",
            "created": 1535390984,
            "metadata": {
            },
            "plan": {
              "id": "monthly",
              "object": "plan",
              "active": true,
              "aggregate_usage": null,
              "amount": 1900,
              "billing_scheme": "per_unit",
              "created": 1534660009,
              "currency": "cad",
              "interval": "month",
              "interval_count": 1,
              "livemode": false,
              "metadata": {
              },
              "nickname": "Monthly",
              "product": "prod_DRdeH2Q4y35faL",
              "tiers": null,
              "tiers_mode": null,
              "transform_usage": null,
              "trial_period_days": null,
              "usage_type": "licensed"
            },
            "quantity": 1,
            "subscription": "sub_DUo9TmZzClFG1N"
          }
        ],
        "has_more": false,
        "total_count": 1,
        "url": "/v1/subscription_items?subscription=sub_DUo9TmZzClFG1N"
      },
      "livemode": false,
      "metadata": {
      },
      "plan": {
        "id": "monthly",
        "object": "plan",
        "active": true,
        "aggregate_usage": null,
        "amount": 1900,
        "billing_scheme": "per_unit",
        "created": 1534660009,
        "currency": "cad",
        "interval": "month",
        "interval_count": 1,
        "livemode": false,
        "metadata": {
        },
        "nickname": "Monthly",
        "product": "prod_DRdeH2Q4y35faL",
        "tiers": null,
        "tiers_mode": null,
        "transform_usage": null,
        "trial_period_days": null,
        "usage_type": "licensed"
      },
      "quantity": 1,
      "start": 1535390983,
      "status": "active",
      "tax_percent": null,
      "trial_end": null,
      "trial_start": null
    }
  },
  "livemode": false,
  "pending_webhooks": 1,
  "request": {
    "id": "req_jreFxqxCqmXk5P",
    "idempotency_key": null
  },
  "type": "customer.subscription.created"
}
Response
host localhost:3000 resolves to illegal IP 127.0.0.1

Posted in Paypal & Stripe

I am a bit confused. So I would have to ask for the plan first before I can retrieve the amount?
Brian

Posted in Paypal & Stripe

Hi Chris - Can I bug you for one last question? I am getting a no method error for amount in the rails console after I run:
e = Stripe::Event.retrieve("evt_1D29rN2OobZT8tZuHzzw9Zbp")
Webhooks::ChargeSucceeded.new.call(e)

NoMethodError (undefined method `amount' for #Stripe::Subscription:0x00005630b03da5b8)

Amount is set in my database so I am not sure what's going on.

module Webhooks
  class ChargeSucceeded
    def call(event)
      charge = event.data.object

      user = User.find_by(stripe_id: charge.customer)

      c = user.charges.where(stripe_id: charge.id).first_or_create
      c.update(
        amount: charge.amount,
        card_brand: charge.source.brand,
        card_last4: charge.source.last4,
        card_exp_month: charge.source.exp_month,
        card_exp_year: charge.source.exp_year
      )
    end
  end
end
class CreateCharges < ActiveRecord::Migration[5.2]
  def change
    create_table :charges do |t|
      t.references :user, foreign_key: true
      t.string :stripe_id
      t.integer :amount
      t.string :card_brand
      t.string :card_last4
      t.string :card_exp_month
      t.string :card_exp_year

      t.timestamps
    end
  end
end

Here is a dropbox link to my application: https://www.dropbox.com/s/xj9ul4lbj118mh5/stripe.zip?dl=0

Posted in Paypal & Stripe

After some testing and digging around, I figured it out. I think it was due to the fact that I was using form_with instead of form_tag.

Maybe form_with doesn't accept a style tag anymore? My fix was just to change style to class, and then add the none class to my css.

<%= form_with url: subscription_path, id: "payment-form", class: (current_user.card_last4? ? "none" : nil) do |form| %>

Thanks for you help with this.

Posted in Paypal & Stripe

Thanks Chris - I just subscribed, and I will watch that eposide.

One last question if you don't mind. I am on this eposide of your stripe course "Resubscribing using an existing card" but I am having a slight problem.

My form is still displaying for users with an existing credit card on file with this set up:

<%= form_with url: subscription_path, id: "payment-form", style: (current_user.card_last4? ? "display:none" : nil) do |form| %>

Do you know what I am doing wrong here?

Complete file: (Subscriptions.new.html.erb)

<h1>Subscribe</h1>

<% if current_user.card_last4? %>
  <%= form_with url: subscription_path, id: "existing-card" do |form| %>
    <p>Pay with existing card</p> 
    <div><strong>Card on file:</strong> <%= current_user.card_brand %> (**** **** **** <%= current_user.card_last4 %>)</div>
    <div><strong>Expiration:</strong> <%= current_user.card_exp_month %> / <%= current_user.card_exp_year %></div>
    <p>or <%= link_to "Add a new card", "#", class: "show-card-form" %></p>

    <%= hidden_field_tag :plan, params[:plan] %>
    <button class="btn btn-primary">Submit Payment</button>
 <% end %>
<% end %>

<%= form_with url: subscription_path, id: "payment-form", style: (current_user.card_last4? ? "display:none" : nil) do |form| %>

  <div class="form-row">
    <label for="card-element">
      Credit or debit card
    </label>
    <div id="card-element" class="form-control">
      <!-- A Stripe Element will be inserted here. -->
    </div>

    <!-- Used to display Element errors. -->
    <div id="card-errors" role="alert"></div>
  </div>

  <%= hidden_field_tag :plan, params[:plan] %>

  <button class="btn btn-primary">Submit Payment</button>


<% end %>

Posted in Paypal & Stripe

Hi Chris - I just finished your Stripe course, which works great, but my client also wants the PayPal option. I am just wondering if you could share some insight on how you did it on your site?

Also, it would be great if you could update your Stripe course to include the pay now buttton integration. https://stripe-payments-demo.appspot.com/

Thank you,
Brian

Posted in Stripe Elements Javascript Discussion

Thanks René, I should of figured that out.

Posted in Stripe Elements Javascript Discussion

Hi Chris - The Bootstrap 4 Gem, specifically @import "bootstrap/forms"; blocks the Stripe card element from dispalying.

Do you know of a work around?

Thanks,
Brian

Posted in Payments with Stripe Master Class

Hi Chris - Quick update, I am just getting back to this course and I've updated my Rails app to 5.2.

I am on the "New Stripe Elements Video" and trying to call my credentials from the application file.

Therefore, I am just wondering how to correctly call my credentials from config/credentials.yml.enc. Currently I have:

<%= tag :meta, name: "stripe_key", content: Rails.application.credentials.development[:stripe_publishable_key] %>

This is working but should I do an if else statement for production vs development?

This is the content of the my credentials file:

development:
stripe_publishible_key:
stripe_secret_key:

production:
stripe_publishible_key:
stripe_secret_key:

Posted in Payments with Stripe Master Class

Thanks for the quick response Chris. I will defintely let you know if I have have any follow up questions.

On an unrelated note, I am using the Windows Subsystem for Linux with PostgreSQL installed on the Windows side. This means I can't access the Heroku database by running heroku psql in bash. Do you know of any work around for this?

The local psql command could not be located. For help installing psql, see https://devcenter.heroku.com/articles/heroku-postgresql#local-setup

Thanks,
Brian

Posted in Payments with Stripe Master Class

Hey Chris - I signed up for your payments with Stripe course and "Creating The Checkout Form With Stripe.js" section is really confusing. Basically video three on the New Stripe JS element does everything the followng three videos do, but the codiing is a little different?

Can I just use the content from video three?

Brian

Posted in How do I add Ransack in my posts controller?

Good suggestion on the gem. It worked well, but it is highlighting both my home and about page at the same time: https://mirrorcommunications.herokuapp.com/

Home and about are both show pages from my pages controller. I think this might be the reason, but I am not sure.

Any help would be awesome! This what I have:

<%= active_link_to "Home", root_path, :active => :exclusive %>
<%= active_link_to "About", pages_path, :action => "show", :id => 'about', :active => :exact %>

Posted in How do I add Ransack in my posts controller?

Yes, I have two different header styles and I can get menu highlighting on one but not both. There seems to be a lot of different options out there: http://stackoverflow.com/questions/3705898/best-way-to-add-current-class-to-nav-in-rails-3

Brian

Posted in How do I add Ransack in my posts controller?

Hi Chris - I tried that and nothing happens for me unfortunately? It just stays on the same post, even if I search for a different post. In addition, I am currently using ransack search in my header, and I am receiving errors on my other controllers, such as: undefined method `title_or_body_cont' for #Ransack::Search:0x007f9da497a2b8.

Just wondering how I get around this. Should I add ransack to my application controller?

Also on a secondary note, if you could do a video on setting up active menu classes that would be awesome, I can't find a good solution!

Brian

Posted in How do I add Ransack in my posts controller?

Just a quick follow up. How would I add searching for my posts show action?

I have a search form in my side bar on individual posts.

def show
end

Thanks,
Brian

logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2021 GoRails, LLC. All rights reserved.