изменение суммы затрат на метод начисления в зависимости от выбора пользователя в форме - PullRequest
0 голосов
/ 25 мая 2019

Итак, у меня есть модель поста, где пользователи могут загружать посты, которые относятся к категории и подкатегории.Так же, как Craigslist.Я хочу, чтобы пользователи имели возможность продвигать продукты.Например, если пользователи платят $ 2,99, это будет популярное сообщение за день.Если вы заплатите 8 долларов, он будет выставлен в течение 7 дней.

, например, введите описание изображения здесь

В данный момент я создал полосу в своем действии создания, когда пользователь создаетпост, который должен заплатить установленную цену.У пользователей должны быть варианты оплаты [бесплатно (без оплаты), 2,99 долл. США (1 день), 10,00 долл. США (7 дней)].Я вроде застрял там

class PostsController < ApplicationController
  before_action :authenticate_user!, only: [:new, :create, :update, :edit]
  before_action :set_post, only: [:show, :edit, :update, :destroy]
  before_action :restriction, only: [:edit, :update, :destroy]

  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
  end

  # GET /posts/new
  def new
    @post = Post.new
  end

  # GET /posts/1/edit
  def edit
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = current_user.posts.build(post_params)

    token = params[:stripeToken]
    post_title = params[:title]
    card_brand = params[:user][:card_brand]
    card_exp_month = params[:user][:card_brand]
    card_exp_year = params[:user][:card_exp_year]
    card_last4 = params[:user][:card_last4]

    charge = Stripe::Charge.create(
      :amount => 30000,
      :currency => "usd",
      :description => post_title,
      :statement_descriptor => post_title,
      :source => token
    )

    current_user.stripe_id = charge.id
    current_user.card_brand = card_brand
    current_user.card_exp_month = card_exp_month
    current_user.card_exp_year = card_exp_year
    current_user.card_last4 = card_last4
    current_user.save!

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render :show, status: :created, location: @post }
      else
        format.html { render :new }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end

  rescue Stripe::CardError => e 
    flash.alert = e.message
    render action: :new
  end

  # PATCH/PUT /posts/1
  # PATCH/PUT /posts/1.json
  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { render :show, status: :ok, location: @post }
      else
        format.html { render :edit }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post.destroy
    respond_to do |format|
      format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_post
      @post = Post.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def post_params
      params.require(:post).permit(:title, :description, :condition, :trades, :vin, :phone_number, :country, :state, :city, :price, :shipping, :category_id, :subcategory_id, :user_id)
    end

    def restriction
     unless @post.user_id == current_user.id
      flash[:notice] = 'You are not the owner of this Ad listing'
      redirect_to root_path
     end
    end
end
...