条纹连接 - Stripe::AuthenticationError

Stripe Connect - Stripe::AuthenticationError

我已经使用 Stripe Connect 建立了一个点对点市场,通过 stripe 结账处理信用卡付款,并将该费用转移到关联的 stripe 账户及其银行账户信息,我的账户将收取佣金。

我的代码之前在开发模式下工作,但是一旦我将它推送到 heroku,我在通过条带结帐发送费用后收到错误。

这是我从 运行 heroku 日志中捕获的当前错误...

Stripe::AuthenticationError (The provided key 'sk_live_********************3yOZ' does not have access to account 'ca_*******************1LR1' (or that account does not exist). Application access may have been revoked.):

这是我的意大利面条代码...(注意:我只是 Rails 的周末勇士...令人惊奇的是,我没有任何编程经验就能走到这一步。)

订单控制器

class OrdersController < ApplicationController
  before_action :set_order, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!


  def sales
    @orders = Order.all.where(seller: current_user).order("created_at DESC")
  end

  def purchases
    @orders = Order.all.where(buyer: current_user).order("created_at DESC")
  end

  # GET /orders/new
  def new
    @order = Order.new
    @item = Item.find(params[:item_id])
  end

  # POST /orders
  # POST /orders.json
  def create
    @order = Order.new(order_params)
    @item = Item.find(params[:item_id])
    @seller = @item.user

    @order.item_id = @item.id
    @order.buyer_id = current_user.id
    @order.seller_id = @seller.id

    token = params[:stripeToken]

    begin

    customer = Stripe::Customer.create(
        :email => params[:stripeEmail],
        :source  => token
    )

    require 'json'

      charge = Stripe::Charge.create({
        :customer => customer.id,
        :amount => (@item.price * 91.1).floor - 30,
        :currency => "usd",
        :description => @item.title,
        :application_fee => ((@item.price * 100) * 0.089).floor + 30
      },
      {:stripe_account => ENV["STRIPE_CONNECT_CLIENT_ID"] }
    )
      @order.name = params[:stripeShippingName]
      @order.address = params[:stripeShippingAddressLine1]
      @order.city = params[:stripeShippingAddressCity]
      @order.state = params[:stripeShippingAddressState]
      @order.zip = params[:stripeShippingAddressZip]
      @order.country = params[:stripeShippingAddressCountry]

      flash[:notice] = "Thanks for ordering!"
    rescue Stripe::CardError => e
      flash[:danger] = e.message
      redirect_to new_order_path
    end

    respond_to do |format|
      if @order.save
        format.html { redirect_to root_url }
        format.json { render :show, status: :created, location: @order }
      else
        flash[:alert] = "Something went wrong :("
        # gon.client_token = generate_client_token
        format.html { render :new }
        format.json { render json: @order.errors, status: :unprocessable_entity }
      end
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def order_params
      if params[:orders] && params[:orders][:stripe_card_token].present?
        params.require(:orders).permit(:stripe_card_token)
      end
    end

end

OmniAuth 回调控制器

class OmniauthCallbacksController < Devise::OmniauthCallbacksController

  def stripe_connect
    @user = current_user
    if @user.update_attributes({
      provider: request.env["omniauth.auth"].provider,
      uid: request.env["omniauth.auth"].uid,
      access_code: request.env["omniauth.auth"].credentials.token,
      publishable_key: request.env["omniauth.auth"].info.stripe_publishable_key
    })
      # anything else you need to do in response..
      sign_in_and_redirect @user, :event => :authentication
      set_flash_message(:notice, :success, :kind => "Stripe") if is_navigational_format?
    else
      session["devise.stripe_connect_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end

end

Items Coffee Script(用户必须在上市前将银行账户信息与 Stripe Connect 关联)

jQuery -> 
    Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
    item.setupForm()

item =
  setupForm: ->
    $('#new_item').submit ->
      $('input[type=submit]').attr('disabled', true)
      Stripe.bankAccount.createToken($('#new_item'), item.handleStripeResponse)
      false

  handleStripeResponse: (status, response) ->
    if status == 200
      $('#new_item').append($('<input type="hidden" name="stripeToken" />').val(response.id))
      $('#new_item')[0].submit()
    else
      $('#stripe_error').text(response.error.message).show()
      $('input[type=submit]').attr('disabled', false)

订单咖啡脚本(Stripe 将在结账时处理卡信息)

jQuery -> 
    Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
    payment.setupForm()

payment =
  setupForm: ->
    $('#new_order').submit ->
      $('input[type=submit]').attr('disabled', true)
      Stripe.card.createToken($('#new_order'), payment.handleStripeResponse)
      false

  handleStripeResponse: (status, response) ->
    if status == 200
      $('#new_order').append($('<input type="hidden" name="stripeToken" />').val(response.id))
      $('#new_order')[0].submit()
    else
      $('#stripe_error').text(response.error.message).show()
      $('input[type=submit]').attr('disabled', false)

devise.rb 初始化器

  config.omniauth :stripe_connect,
    ENV['STRIPE_CONNECT_CLIENT_ID'],
    ENV['STRIPE_SECRET_KEY'],
    :scope => 'read_write',
    :stripe_landing => 'register'

stripe.rb 初始化程序

Rails.configuration.stripe = {
  :publishable_key => ENV['STRIPE_PUBLISHABLE_KEY'],
  :secret_key      => ENV['STRIPE_SECRET_KEY']
}

Stripe.api_key = Rails.configuration.stripe[:secret_key]

application.yml (figaro) (键删减)

production:
  STRIPE_SECRET_KEY: "sk_live_****************3yOZ"
  STRIPE_PUBLISHABLE_KEY: "pk_live_******************HhWi"
  STRIPE_CONNECT_CLIENT_ID: "ca_**********************1LR1"
  CONNECTED_STRIPE_ACCOUNT_ID: "acct_***********crNm"

订单_form.html.erb(只是条带脚本)

<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
          data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
          data-description="<%= @item.title %>"
          data-amount="<%= (@item.price * 100).floor %>"
          data-email="<%= current_user.email %>"
          data-shipping-address="true"
          data-locale="auto"></script>

这里的问题是您在混合常量。每当您代表关联帐户发出 API 请求时,您希望在 Stripe-Account header.

中传递关联帐户的 ID acct_XXXYYYZZZ

这里的问题是您将平台的客户端 ID ca_XXXX 传递到那里。 Stripe 然后试图找到 ID 为 ca_XXXX 的帐户连接到您的平台,但它不存在。

您需要修正您的费用代码以传递正确的常量:

charge = Stripe::Charge.create({
    customer: customer.id,
    amount: (@item.price * 91.1).floor - 30,
    currency: 'usd',
    description: @item.title,
    application_fee: ((@item.price * 100) * 0.089).floor + 30
  },
  {
    stripe_account: ENV["CONNECTED_STRIPE_ACCOUNT_ID"]
  }
)