だから私はこのBalanced Payments tutorialに従っていて、私のアプリケーションに合うように支払いモデルを微調整しようとしました.
オリジナル:
class Payment
def initialize(email, amount, credit_card_hash)
@email = email
@amount = (amount * 100).to_i
@credit_card_hash = credit_card_hash
@buyer = nil
@card = nil
@errors = []
end
end
私の簡易版(フルモデルのコードで更新):
require 'balanced'
class Transaction < ActiveRecord::Base
attr_reader :errors, :amount
def initialize(amount)
@amount = (amount * 100).to_i
@buyer = nil
@card = nil
@errors = []
end
def charge
begin
find_or_create_buyer
debit_buyer
credit_owner
return true
rescue
return false
end
end
private
def find_or_create_buyer
begin
@buyer = current_user.balanced_customer
rescue
@errors << 'Your account is invalid'
end
end
def debit_buyer
begin
payment = @buyer.debit(@amount, "Test transaction")
rescue
@errors << 'Your credit card could not be charged'
end
end
def credit_owner
begin
Balanced::Marketplace.my_marketplace.owner_account.credit(amount)
rescue
@errors << 'Your credit card payment did not go through.'
end
end
end
問題は、Rails コンソールからクラスをインスタンス化しようとするたびに、純粋な Ruby エラーのように見えることです。
> payment = Transaction.new(0.01)
output error: #<NoMethodError: undefined method `has_key?' for nil:NilClass>
私はグーグルで検索しましたが、良い答えを思いつくことができませんでした。
何か案は?