【问题标题】:Get Stripe charge information rails 5 create order获取 Stripe 费用信息 rails 5 创建订单
【发布时间】:2019-01-27 14:36:39
【问题描述】:

所以我正在实施 Stripe,用户可以成功购买,但是,我想获取费用信息、最后 4 个卡号、卡类型等,以便我可以使用 https://github.com/excid3/receipts 生成收据。

这是我目前所拥有的:

支付控制器

class PaymentsController < ApplicationController
  before_action :authenticate_user!

  def create
    token = params[:stripeToken]
    @course = Course.find(params[:course_id])
    @user = current_user

    begin
      charge = Stripe::Charge.create(
        amount: (@course.price*100).to_i,
        currency: "gbp",
        source: token,
        description: params[:stripeEmail],
        receipt_email: params[:stripeEmail]
      )

      if charge.paid
        Order.create(
          course_id: @course.id,
          user_id: @user.id,
          Amount: @course.price
        )
      end

    flash[:success] = "Your payment was processed successfully"

    rescue Stripe::CardError => e
      body = e.json_body
      err = body[:error]
      flash[:error] = "Unfortunately, there was an error processing your payment: #{err[:message]}"
    end

    redirect_to course_path(@course)
  end
end

订单控制器

class OrdersController < ApplicationController
  layout proc { user_signed_in? ? "dashboard" : "application" }

  before_action :authenticate_user!

  def index
    @orders = Order.includes(:course).all
  end

  def show
    @order = Order.find(params[:id])

    respond_to do |format|
      format.pdf {
        send_data @order.receipt.render,
        filename: "#{@order.created_at.strftime("%Y-%m-%d")}-aurameir-courses-receipt.pdf",
        type: "application/pdf",
        disposition: :inline
      }
    end
  end

  def create
  end

  def destroy
  end
end

订单.rb

class Order < ApplicationRecord
  belongs_to :course
  belongs_to :user

  validates :stripe_id, uniqueness: true

  def receipt
    Receipts::Receipt.new(
      id: id,
      subheading: "RECEIPT FOR CHARGE #%{id}",
      product: "####",
      company: {
        name: "####",
        address: "####",
        email: "####",
        logo: "####"
      },
      line_items: [
        ["Date",           created_at.to_s],
        ["Account Billed", "#{user.full_name} (#{user.email})"],
        ["Product",        "####"],
        ["Amount",         "£#{amount / 100}.00"],
        ["Charged to",     "#{card_type} (**** **** **** #{card_last4})"],
        ["Transaction ID", uuid]
      ],
      font: {
        normal: Rails.root.join('app/assets/fonts-converted/font-files/AvenirBook.ttf')
      }
    )
  end
end

schema.rb

create_table "orders", force: :cascade do |t|
    t.integer "user_id"
    t.integer "course_id"
    t.integer "stripe_id"
    t.integer "amount"
    t.string "card_last4"
    t.string "card_type"
    t.integer "card_exp_month"
    t.integer "card_exp_year"
    t.string "uuid"
    t.index ["course_id"], name: "index_orders_on_course_id"
    t.index ["user_id"], name: "index_orders_on_user_id"
  end

如何获取收费信息?

【问题讨论】:

    标签: ruby ruby-on-rails-5 stripe-payments


    【解决方案1】:

    您需要的所有信息都在您创建的charge 对象中。

      if charge.paid
        Order.create(
          course_id: @course.id,
          user_id: @user.id,
          amount: @course.price, 
          card_last4: charge.source.last4,
          card_type: charge.source.brand,
          card_exp_month: charge.source.exp_month,
          card_exp_year: charge.source.exp_year
        )
      end
    

    请参阅https://stripe.com/docs/api/ruby#charge_object,了解有关收费的所有可用信息。

    【讨论】:

    【解决方案2】:

    我猜你在成功付款时没有将 Stripe Charge ID 存储在任何地方。

    我的建议,将费用 ID 存储在您的订单记录中

    if charge.paid
      Order.create(
        course_id: @course.id,
        user_id: @user.id,
        amount: @course.price,
        stripe_charge_id: charge.id
      )
    end
    

    在您的收据方法中,您可以按如下方式获取条带费用:

    def receipt
      stripe_charge = Stripe::Charge.retrieve(stripe_charge_id)
      ...
    
    end
    

    stripe_charge 对象包含所有信息,您可以使用所需的任何数据。

    希望这会有所帮助。

    【讨论】:

    • 费用 ID 正在保存,但没有关于订单 gyazo.com/2fb070b7aab949a1f2b1a1b072d05e21 所以最后 4 位等
    • 如答案中所述,所有信息都在 stripe_charge 对象中。例如,您可以通过调用stripe_charge.source.last4获取最后4位数字
    猜你喜欢
    • 2013-08-22
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 2017-08-28
    • 2018-09-17
    • 2018-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多