title: Stripe Subscription - the server responded with status 400
body: <p>Why my stripe subscription is not working? I get the below error but i am unable to resolve the issue.</p>
  
  <p>when i created a payment in the stripe admin panel i was given and ID ref. I currently use the ID within my table to match the ID in strip. I am unsure how to correct this.</p>
  
  <pre><code>payment = customer.subscriptions.create(
          source: params[:stripeToken],
          plan: @subscription
          # the subscription.id is the database id
          # the plan.id in stripe uses the same id as that of the subscription.id in the database in order to select the right subsciption in stripe
        )
  </code></pre>
  
  <p>error in terminal:</p>
  
  <blockquote>
    <p>Stripe::InvalidRequestError (This customer has no attached payment source):
    the server responded with status 400</p>
  </blockquote>
  
  <pre><code>2018-06-09T16:27:10.457567+00:00 app[web.1]: Completed 500 Internal Server Error in 987ms
  2018-06-09T16:27:10.459118+00:00 app[web.1]: 
  2018-06-09T16:27:10.459122+00:00 app[web.1]: Stripe::InvalidRequestError (This customer has no attached payment source):
  2018-06-09T16:27:10.459124+00:00 app[web.1]: app/controllers/payments_controller.rb:58:in `create'
  2018-06-09T16:27:10.459125+00:00 app[web.1]: 
  2018-06-09T16:27:10.459127+00:00 app[web.1]: 
  2018-06-09T16:27:10.774887+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=www.spefz.com request_id=81e62211-2807-4dd3-a9bb-ea260afe0998 fwd="37.152.39.155" dyno=web.1 connect=0ms service=2ms status=200 bytes=202 protocol=https
  2018-06-09T16:27:50.921236+00:00 app[api]: Starting process with command `bin/rails console` by user richill
  2018-06-09T16:28:01.326416+00:00 heroku[run.7880]: Awaiting client
  2018-06-09T16:28:01.346795+00:00 heroku[run.7880]: Starting process with command `bin/rails console`
  2018-06-09T16:28:01.714175+00:00 heroku[run.7880]: State changed from starting to up
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/R4qbf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/R4qbf.png" alt="enter image description here"></a></p>
  
  <p>in my stripe account i have this:</p>
  
  <p><a href="https://i.stack.imgur.com/CrRQQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CrRQQ.png" alt="enter image description here"></a></p>
  
  <p>payments_controller.rb [create action in the payments controller]</p>
  
  <pre><code>def create
      @payment = Payment.new(payment_params)
      @subscription_id = @payment.subscription_id
      @event_id = @payment.event_id
  
      # ------- PAYMENT_SUBSCRIPTION -------
      if @subscription_id.present?
        @user = current_user
        @payment = Payment.new(payment_params)
        @subscription = @payment.subscription_id
        @payment.user_id = current_user.id
  
        if current_user.stripe_id?
          customer = Stripe::Customer.retrieve(current_user.stripe_id)
        else
          customer = Stripe::Customer.create(email: current_user.email)
        end
  
        payment = customer.subscriptions.create(
          source: params[:stripeToken],
          plan: @subscription
          # the subscription.id is the database id
          # the plan.id in stripe uses the same id as that of the subscription.id in the database in order to select the right subsciption in stripe
        )
  
        current_user.update(
          stripe_id: customer.id,
          stripe_subscription_pymt_id: payment.id,
          card_last4: params[:card_last4],
          card_exp_month: params[:card_exp_month],
          card_exp_year: params[:card_exp_year],
          card_type: params[:card_brand],
          recent_subscription_pymt_date: DateTime.now,
          recent_subscription_cancel_date: nil
        )
  
  
        # if payment is true/successful save the below params under payments table
        if payment.present?
          @payment.update(
            stripe_customer_id: customer.id,
            stripe_subscription_id: payment.id,
            stripe_payment_id: "none",
            subscription_payment_date: DateTime.now,
            event_payment_date: "none",
            event_payment_date_status: "none",
            user_card_type: params[:card_brand],
            user_card_last4: params[:card_last4],
            user_card_exp_month: params[:card_exp_month],
            user_card_exp_year:params[:card_exp_year],
            status: "success"
          )
        else
          @payment.update(
            stripe_customer_id: customer.id,
            stripe_subscription_id: payment.id,
            stripe_payment_id: "none",
            subscription_payment_date: DateTime.now,
            event_payment_date: "none",
            user_card_type: params[:card_brand],
            user_card_last4: params[:card_last4],
            user_card_exp_month: params[:card_exp_month],
            user_card_exp_year:params[:card_exp_year],
            status: "fail"
          )
        end
  
        respond_to do |format|
          if @payment.save
            MailerPaymentuserreceipt.paymentreceipt(@payment).deliver
            format.html { redirect_to account_user_path(current_user), notice: 'Your Subscription Payment was successful.' }
            format.json { render :show, status: :created, location: @payment }
          else
            format.html { redirect_to new_payment_path(subscription_id: @subscription.id), alert: 'Ensure all fields are completed'}
            format.json { render json: @payment.errors, status: :unprocessable_entity }
          end
        end
  
      end
    end
  </code></pre>
  
  <p>schema [payment table in the schema]</p>
  
  <pre><code>create_table "payments", force: :cascade do |t|
      t.string   "email"
      t.integer  "user_id"
      t.integer  "subscription_id"
      t.string   "reference"
      t.datetime "created_at",                       null: false
      t.datetime "updated_at",                       null: false
      t.integer  "event_id"
      t.string   "stripe_customer_id"
      t.string   "stripe_subscription_id"
      t.string   "stripe_payment_id"
      t.datetime "subscription_payment_date"
      t.datetime "event_payment_date"
      t.string   "user_card_type"
      t.string   "user_card_last4"
      t.integer  "user_card_exp_month"
      t.integer  "user_card_exp_year"
      t.string   "status"
      t.string   "event_payment_date_status"
      t.string   "subscription_payment_date_status"
    end
  </code></pre>