Lee Terng Gio

Joined

2,360 Experience
3 Lessons Completed
1 Question Solved

Activity

Posted in Braintree Error In Production Mode

I have already solved it. Thanks!

Posted in Braintree Error In Production Mode

I have already set it
def new
@plans = Braintree::Plan.all
end

But right now the issue is
018-03-28T00:36:21.534429+00:00 app[web.1]: NoMethodError (undefined method `payment_methods' for nil:NilClass

2018-03-28T00:36:21.534430+00:00 app[web.1]: Did you mean? private_methods):

2018-03-28T00:36:21.534432+00:00 app[web.1]: app/controllers/subscriptions_controller.rb:27:in `create'

Posted in Braintree Error In Production Mode

I tried to put validation in create customer, which is
        if current_user.braintree_id?
          customer = Braintree::Customer.find(current_user.braintree_id)
        else
          result = Braintree::Customer.create(
          email: current_user.company_email,
          company: current_user.company_name,
          payment_method_nonce: params[:payment_method_nonce]
          )
          if result.success?
            customer = result.customer
            current_user.update(braintree_id: customer.id)
          else
            flash[:info] = "Opps wrong"
            render 'new'
          end
        end

Now the error shows
2018-03-28T00:36:21.534429+00:00 app[web.1]: NoMethodError (undefined method `payment_methods' for nil:NilClass

2018-03-28T00:36:21.534430+00:00 app[web.1]: Did you mean? private_methods):
2018-03-28T00:36:21.534432+00:00 app[web.1]: app/controllers/subscriptions_controller.rb:27:in `create'


I really have no idea what causes this issue because everything works fine in development mode.

Posted in Braintree Error In Production Mode

Hi, I have implemented Braintree subscription payment in rails app which followed your tutorial. Everything works well in development, however when I switched to production (I have registered with Braintree and got a real account, and I change all the key in environment)

I have tried to submit an invalid card information to test the app, the page keeps showing error.

I look at the application logs and it said 
NoMethodError (undefined method `customer' for #<Braintree::ErrorResult:0x007f6ed80f1d80>):

Here's my create method, I follow your tutorial and it works fine in development
def create
        if current_user.braintree_id?
          customer = Braintree::Customer.find(current_user.braintree_id)
        else
          result = Braintree::Customer.create(
          email: current_user.company_email,
          company: current_user.company_name,
          payment_method_nonce: params[:payment_method_nonce]
          )
      
          customer = result.customer
          current_user.update(braintree_id: customer.id)

        end

    result = Braintree::Subscription.create(
      payment_method_token: customer.payment_methods.find{ |pm| pm.default? }.token,
      plan_id: params[:plan_id]
    )
    if result.success?
    result.subscription.transactions.each do |transaction|
      current_user.transactions.create(braintree_transaction_id: transaction.id,
        plan_name: params[:plan_name],
        price: transaction.amount.to_f,
        start_date: transaction.subscription_details.billing_period_start_date,
        end_date: transaction.subscription_details.billing_period_end_date,
        subscription_id: result.subscription.id
        )
        
          
    end
  
    
    current_user.update(braintree_subscription_id: result.subscription.id, next_billing_date: result.subscription.next_billing_date,
    billing_period_start_date: result.subscription.billing_period_start_date,
    billing_period_end_date: result.subscription.billing_period_end_date,
    status: result.subscription.status,
    next_billing_period_amount: result.subscription.next_billing_period_amount,
    paid_through_date: result.subscription.paid_through_date,
    plan_id: params[:plan_id],
    plan_name: params[:plan_name])
    
    flash[:info] = "You've been subscribed successfully"
    redirect_to @current_user
    else
          flash[:warning] = "Invalid card information"
          render 'new'
    end
    end

The weird thing is it doesn't render the flash warning of unsuccessful result and redirect to the original new_subscription_path, instead the website url redirect to this
https://herokuappname.herokuapp.com/subscription.1
and the page error shows
This page isn’t working herokuappname.herokuapp.com is currently unable to handle this request.
HTTP ERROR 500

So, I want to know whether it is the customer method error (which I don't think so because it doesn't have any problem in development mode) or any other problem such as why the page url so weird?

I looked at the Braintree control panel, and the reason that the subscription failed was because the bank declined the transactions due to incorrect card information, which I entered incorrect card in order to test it, if it is invalid card info, why didn't it display the flash notice and redirect back to the new_subscription_path, instead it redirects to the subscription.1 url which I have mentioned above?
I have created a message template based on the video that you've created and everything works well and I can select template and the message will show on the text area. But I have integrate SimpleMDE as the editor, once I select the template, the message won't show the text editor, the message will only show on normal text area. How do I fix this?

Posted in Message Templates In Rich Text Editor SimpleMDE

I have created a message template based on the video that you've created and everything works well and I can select template and the message will show on the text area. But I have integrate SimpleMDE as the editor, once I select the template, the message won't show the text editor, the message will only show on normal text area. How do I fix this?

Posted in Message Templates In Rich Text Editor SimpleMDE

I have created a message template based on the video that you've created and everything works well and I can select template and the message will show on the text area. But I have integrate SimpleMDE as the editor, once I select the template, the message won't show the text editor, the message will only show on normal text area. How do I fix this?

Posted in Message Templates Discussion

It works well with text area, but if I use rich text editor, the text wont show on the text editor.

Posted in Rails Braintree couldn't update

Below is the User model:

    validates :company_name, presence: true, length: {maximum: 50}
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
        validates :company_email, presence: true, length: {maximum: 255}, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
    validates :company_contact, presence: true, length: {maximum: 15}
    validates :term, presence: true
    has_secure_password
    validates :password, presence: true, length: { minimum: 6 }
    validates_confirmation_of :password

Below is my db/seed

User.create!(company_name:  "Example User",
             company_email: "[email protected]",
             company_contact: "011999484",
             password:              "foobar",
             password_confirmation: "foobar",
             term:     true,
             activated: true,
             activated_at: Time.zone.now)

99.times do |n|
  company_name  = "Example user"
  company_email = "example-#{n+1}@railstutorial.org"
  company_contact = "0119999494"
  password = "password"
  User.create!(company_name:  company_name,
              company_email: company_email,
              company_contact: company_contact,
              password:              password,
              password_confirmation: password,
              term: true,
              activated: true,
              activated_at: Time.zone.now)
end

When I run rake db:seed, everything works well, and I can login using the password as "password", but when run user.valid?, it gives false and the errors messages show {:password=>["can't be blank", "is too short (minimum is 6 characters)"]}

Posted in Rails Braintree couldn't update

I'm implementing Braintree Subscription Payment into my rails app. I've encounter some issue when trying to update subscription information in my database. When I make a payment, it's successfully created and saved into Braintree. The transaction was also created and saved into my database. The only problem is it couldn't update the payment information such as braintree_id, braintree_subscription_id etc in the User.

Below is my Subscriptions Controller:

class SubscriptionsController < ApplicationController
  before_action :logged_in_user, only: [:new, :create]

  def new
    @plans = Braintree::Plan.all
  end

  def create
    if current_user.braintree_id?
      customer = Braintree::Customer.find(current_user.braintree_id)
    else
      result = Braintree::Customer.create(
        email: current_user.company_email,
        payment_method_nonce: params[:payment_method_nonce]
      )
      customer = result.customer
      current_user.update(braintree_id: customer.id)

    end

    result = Braintree::Subscription.create(
      payment_method_token: customer.payment_methods.find{ |pm| pm.default? }.token,
      plan_id: params[:plan_id],
    )

    result.subscription.transactions.each do |transaction|
      current_user.transactions.create(braintree_transaction_id: transaction.id,
        plan_name: params[:plan_name],
        price: transaction.amount.to_f,
        start_date: transaction.subscription_details.billing_period_start_date,
        end_date: transaction.subscription_details.billing_period_end_date,
        subscription_id: result.subscription.id,
        )
    end

    current_user.update(braintree_subscription_id: result.subscription.id, next_billing_date: result.subscription.next_billing_date,
    billing_period_start_date: result.subscription.billing_period_start_date,
    billing_period_end_date: result.subscription.billing_period_end_date,
    status: result.subscription.status,
    next_billing_period_amount: result.subscription.next_billing_period_amount,
    paid_through_date: result.subscription.paid_through_date,
    plan_id: params[:plan_id],
    plan_name: params[:plan_name])

    redirect_to @current_user, notice: "You have been subscribed"
  end
end

I've tried to manually update the information in the console, and everything works well. Below are the output that when making a new payment, it rollback and couldn't update the User. While the Transaction was created and saved into database.

Started POST "/subscription.213" for 61.6.143.87 at 2017-12-14 07:57:55 +0000
Processing by SubscriptionsController#create as 
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"FHwbQSfiinjfskBG3Iec8C0gKuGIeOQDmBnjJu4fpEQwsrmsewHYvgAFIpKVguehm+TZAjHrfd8Ya6CWYPX1Rw==", "plan_id"=>"monthly", "plan_name"=>"monthly", "payment_method_nonce"=>"0986895d-aab4-01b3-5829-778101fa0ebb"}
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 213]]
I, [2017-12-14T07:57:56.724260 #10346]  INFO -- : [Braintree] [14/Dec/2017 07:57:56 UTC] POST /merchants/wt92v8tk2hn8j7kg/customers 201
   (0.1ms)  begin transaction
  User Exists (0.3ms)  SELECT  1 AS one FROM "users" WHERE (LOWER("users"."company_email") = LOWER('[email protected]') AND "users"."id" != 213) LIMIT 1
   (0.1ms)  rollback transaction
I, [2017-12-14T07:57:57.747920 #10346]  INFO -- : [Braintree] [14/Dec/2017 07:57:57 UTC] POST /merchants/wt92v8tk2hn8j7kg/subscriptions 201
   (0.0ms)  begin transaction
  SQL (0.3ms)  INSERT INTO "transactions" ("braintree_transaction_id", "plan_name", "price", "start_date", "end_date", "subscription_id", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)  [["braintree_transaction_id", "38273ws4"], ["plan_name", "monthly"], ["price", 9.0], ["start_date", "2017-12-14"], ["end_date", "2018-01-13"], ["subscription_id", "7882v6"], ["user_id", 213], ["created_at", "2017-12-14 07:57:57.780968"], ["updated_at", "2017-12-14 07:57:57.780968"]]
   (8.8ms)  commit transaction
   (0.1ms)  begin transaction
  User Exists (0.3ms)  SELECT  1 AS one FROM "users" WHERE (LOWER("users"."company_email") = LOWER('[email protected]') AND "users"."id" != 213) LIMIT 1
   (0.1ms)  rollback transaction
Redirected to https://kiplr-ternggio.c9users.io/users/213
Completed 302 Found in 2257ms (ActiveRecord: 10.4ms)

This thing works perfectly in my other app which using Devise. I didn't use Devise in this app and I'm not sure what really cause this issue. It really appreciated if somebody point me some direction.

Posted in Rails Full_Calendar Implementation

There was no errors. The modal just doesn't show up.

Posted in Rails Full_Calendar Implementation

I'm implementing full_calendar for my Rails app. I follow the tutorial which can be found on Youtube, https://www.youtube.com/watch?v=NiTLuPqvt58. I've changed some javascript code as below:

$(document).ready(function() {
    $('.calendar').fullCalendar({
   //doing something here
   });
});

The calendar shows up and everything is fine, then I add whatever the remaining code inside and here it is:

$(document).ready(function() {
    $('.calendar').fullCalendar({
     header: {
        left: 'prev,next today',
        center: 'title',
        right: 'month,agendaWeek,agendaDay'
      },
      selectable: true,
      selectHelper: true,
      editable: true,
      eventLimit: true,
      events: '/events.json',

      select: function(start, end) {
        $.getScript('/events/new', function() {
          $('#event_date_range').val(moment(start).format("MM/DD/YYYY HH:mm") + ' - ' + moment(end).format("MM/DD/YYYY HH:mm"))
          date_range_picker();
          $('.start_hidden').val(moment(start).format('YYYY-MM-DD HH:mm'));
          $('.end_hidden').val(moment(end).format('YYYY-MM-DD HH:mm'));
        });
        $('.calendar').fullCalendar('unselect');
      },
      eventDrop: function(event, delta, revertFunc) {
        event_data = { 
          event: {
            id: event.id,
            start: event.start.format(),
            end: event.end.format()
          }
        };
        $.ajax({
            url: event.update_url,
            data: event_data,
            type: 'PATCH'
        });
      },

      eventClick: function(event, jsEvent, view) {
        $.getScript(event.edit_url, function() {
          $('#event_date_range').val(moment(event.start).format("MM/DD/YYYY HH:mm") + ' - ' + moment(event.end).format("MM/DD/YYYY HH:mm"))
          date_range_picker();
          $('.start_hidden').val(moment(event.start).format('YYYY-MM-DD HH:mm'));
          $('.end_hidden').val(moment(event.end).format('YYYY-MM-DD HH:mm'));
        });
      }
   });
});

The modal form doesn't show up. What am I missing?

Hi, I want to display a list of check boxes, below is my code,

<%= f.collection_check_boxes :job_ids, @current_user.jobs.all, :id, :job_name  do |b| %>    
        <div class="collection-check-box">
          <%= b.check_box %>
          <%= b.label %>
        </div>
<% end %>

It works well which display a list of Job_name which can be selected by the user. However, I want to display more information about the job, not just the job_name only, but also the job_type as well. How would I achieve that? I want it to be display in the check_box like this -> System Engineer, Full Time

Posted in Rails associations

I'm create a Ruby on Rails web application. I have 4 models, which are User, Job, Candidate, ApplyingJob. User is the one who will be using the web application and the user can create Job and Candidate, while the from the Candidate, the candidate will be able to add an ApplyingJob and ApplyingJob will carry the same information from the Job. Below are my associations,

User
has_many :candidates
has_many :jobs

Job
belongs_to :user
has_many :applyingjobs

Candidate
belongs_to :user
has_one :applyingjob

ApplyingJob
belongs_to :candidate
belongs_to :job

Here's my problem, I want to let the User be able add an ApplyingJob in the Candidate's profile, and that particular ApplyingJob belongs_to that Candidate and also the Job. ApplyingJob carries 2 foreign keys, which are candidate_id and job_id. If the User creates a new ApplyingJob from the Candidate's profile, the ApplyingJob is associated to the Candidate since there is a candidate_id, but how does it associate to the Job because the User creates a new ApplyingJob in the Candidate's profile?

Posted in Using Webhooks with Stripe Discussion

Hi, can you make a video of this for Braintree subscription because it's lack of documentation and demo for Braintree rails app.

Posted in Integrating Braintree (and PayPal) Discussion

I'd like to see the subscription cancel and update card functionality and also display billing/subscription info to the user. Hope you'll make a new complete course for Rails Braintree just like the the course you did for the Rails Stripe.

Posted in Update next_billing_date in Rails App with Braintree

Hi, I recently developed a Rails app using Braintree Recurring, when the user subscribes, the information such as plan_name, next_billing_date ware saved into the database. However, I found the next_billing_date was not update if the current billing period was finished and the latest next_billing_date was changed. How do I retrieve the next_billing_date from Braintree and update in my database automatically?

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.