代码之家  ›  专栏  ›  技术社区  ›  ymatt

在Rails 4上创建条带帐户失败

  •  0
  • ymatt  · 技术社区  · 7 年前

    我编写了一个函数,让电子商务网站成员创建自己的Stripe帐户,让他们能够通过Stripe接收付款。我学习了一个在线教程,但到目前为止还不起作用(结果是 flash[:danger] = "Unable to create Stripe account!"

    应用程序/控制器/条带控制器。rb型

    class StripeController < ApplicationController
      # Create a manage Stripe account for yourself.
      # Only works on the currently logged in user.
      # See app/services/stripe_managed.rb for details.
      def managed
        connector = StripeManaged.new( current_org_person )
        account = connector.create_account!(params[:country], params[:tos] == '1', request.remote_ip)
    
        if account
          flash[:success] = "Managed Stripe account created!"
        else
          flash[:danger] = "Unable to create Stripe account!"
        end
        redirect_to org_people_stripe_settings_path
      end
    end
    

    应用程序/服务/条带管理。rb型

    class StripeManaged < Struct.new( :org_person )
      ALLOWED = [ 'US', 'CA', 'TW' ] # public beta
      COUNTRIES = [
        { name: 'United States', code: 'US' },
        { name: 'Canada', code: 'CA' },
        { name: 'Taiwan', code: 'TW' }
        # { name: 'Australia', code: 'AU' },
        # { name: 'United Kingdom', code: 'GB' },
        # { name: 'Ireland', code: 'IE' }
      ]
    
      def create_account!( country, tos_accepted, ip )
        return nil unless tos_accepted
        return nil unless country.in?( COUNTRIES.map { |c| c[:code] } )
    
        begin
          @account = Stripe::Account.create(
            managed: true,
            country: country,
            email: org_person.email,
            tos_acceptance: {
              ip: ip,
              date: Time.now.to_i
            },
            legal_entity: {
              type: 'individual'
            }
          )
        rescue
          nil # TODO: improve
        end
    
        if @account
          org_person.update_attributes(
            stripe_currency: @account.default_currency,
            stripe_account_type: 'managed',
            stripe_user_id: @account.id,
            stripe_secret_key: @account.keys.secret,
            stripe_publishable_key: @account.keys.publishable,
            stripe_account_status: account_status
          )
        end
    
        @account
        binding.pry
      end
    
      def update_account!( params: nil )
        if params
          if params[:bank_account_token]
            account.bank_account = params[:bank_account_token]
            account.save
          end
    
          if params[:legal_entity]
            # clean up dob fields
            params[:legal_entity][:dob] = {
              year: params[:legal_entity].delete('dob(1i)'),
              month: params[:legal_entity].delete('dob(2i)'),
              day: params[:legal_entity].delete('dob(3i)')
            }
    
            # update legal_entity hash from the params
            params[:legal_entity].entries.each do |key, value|
              if [ :address, :dob ].include? key.to_sym
                value.entries.each do |akey, avalue|
                  next if avalue.blank?
                  # Rails.logger.error "#{akey} - #{avalue.inspect}"
                  account.legal_entity[key] ||= {}
                  account.legal_entity[key][akey] = avalue
                end
              else
                next if value.blank?
                # Rails.logger.error "#{key} - #{value.inspect}"
                account.legal_entity[key] = value
              end
            end
    
            # copy 'address' as 'personal_address'
            pa = account.legal_entity['address'].dup.to_h
            account.legal_entity['personal_address'] = pa
    
            account.save
          end
        end
    
        org_person.update_attributes(
          stripe_account_status: account_status
        )
      end
    
      def legal_entity
        account.legal_entity
      end
    
      def needs?( field )
        org_person.stripe_account_status['fields_needed'].grep( Regexp.new( /#{field}/i ) ).any?
      end
    
      def supported_bank_account_countries
        country_codes = case account.country
                        when 'US' then %w{ US }
                        when 'CA' then %w{ US CA }
                        when 'IE', 'UK' then %w{ IE UK US }
                        when 'AU' then %w{ AU }
                        when 'TW' then %w{ TW }
                        end
        COUNTRIES.select do |country|
          country[:code].in? country_codes
        end
      end
    
      protected
    
      def account_status
        {
          details_submitted: account.details_submitted,
          charges_enabled: account.charges_enabled,
          transfers_enabled: account.transfers_enabled,
          fields_needed: account.verification.fields_needed,
          due_by: account.verification.due_by
        }
      end
    
      def account
        @account ||= Stripe::Account.retrieve( org_person.stripe_user_id )
      end
    end
    

    问题应该是 def create_account! 在stripe_管理中。rb返回 @account nil . 所以我怀疑 Stripe::Account.create

    1. 我目前住在台湾,哪个Stripe尚不支持,并使用Stripe Japan的帐户来管理平台。我提到的教程来自北美,所以它最初只关注美国和加拿大,然后我在散列中添加了台湾 COUNTRIES 以“美国”为所在国。

    2. 由于问题1,我怀疑ip地址与所选国家(美国)不符。然后它回来了 [7] pry(#<StripeManaged>)> ip => "::1" . 我在谷歌上搜索了一下,但看不出“1”在我的语境中意味着什么。

    3. 在控制台上,我执行 [9] pry(#<StripeManaged>)> Stripe::Account.create(managed: true, country: country, email: org_person.email, tos_acceptance: {ip: ip, date: Time.now.to_i}, legal_entity: {type: 'individual'}) 得到了 Stripe::InvalidRequestError: Missing required param: type. 我在网上找不到可参考的例子,但这似乎是我最怀疑的问题。

    1 回复  |  直到 7 年前
        1
  •  0
  •   koopajah MKumar    7 年前

    这里的问题是,您试图通过API创建自定义帐户,但发送了错误的参数。当你以这种方式创建帐户时,你应该通过 type: "custom" managed 2017-05-25 并且在较新的帐户上不受支持。

    您的代码应该是:

    Stripe::Account.create(
        type: "custom",
        country: country,
        email: org_person.email,
        tos_acceptance: {
          ip: ip,
          date: Time.now.to_i
        },
        legal_entity: {
          type: 'individual'
        }
      )
    

    所有用于条带化的API请求要么返回有效资源,要么在发生错误时引发异常。在调试时,您的代码至少需要记录这些错误,以便您可以轻松跟踪条带问题。您还可以在中的日志中直接看到所有API请求 dashboard . 这可以让您轻松查看发送的参数和API返回的内容。