基本的なeコマースアクティビティを備えたアプリがあります。テストauthorize.netアカウントで試してみましたが、正常に動作します。ただし、本番モードのAPIを入力しましたが、何かを購入しようとすると、失敗画面にリダイレクトされ続けます。エラーが発生せず、herokuのログにも何も表示されず、デバッグを開始する場所すらわかりません。Authorize.netに正常に接続しています。トランザクションは開発モードで成功しています。これは、Ryan BateのRailsCastエピソード145(http://railscasts.com/episodes/145-integrating-active-merchant)に大きく基づいていますが、ここにあります。私のコードのいくつかのハイライト(私はtiをテストしているので、注文したものに関係なく1セントのトランザクションを実行するように強制しています)
enviroments/production.rbで
config.after_initialize do
ActiveMerchant::Billing::Base.mode = :production
::GATEWAY = ActiveMerchant::Billing::AuthorizeNetGateway.new(
:login => "scrubbed",
:password => "scrubbed",
:test => false
)
end
orders_controller.rb
def create
@order = Order.new(params[:order])
@order.cart = current_cart
if @order.save
if @order.purchase
@order.state = 'paid'
@order.save
render :action => "success"
end
else
render :action => "failure"
end
else
redirect_to home_page_path, notice: "The order failed to save"
end
end
def purchase
response = GATEWAY.purchase(1, credit_card, purchase_options)
transactions.create!(:action => "purchase", :amount => price_in_cents, :response => response)
#cart.update_attribute(:purchased_at, Time.now) if response.success?
response.success?
end
order.rb
def purchase
response = GATEWAY.purchase(1, credit_card, purchase_options)
transactions.create!(:action => "purchase", :amount => price_in_cents, :response => response)
#cart.update_attribute(:purchased_at, Time.now) if response.success?
response.success?
end
private
def purchase_options
{
:ip => ip_address,
:billing_address => {
:first_name => first_name,
:last_name => last_name,
:address1 => address_line_1,
:address2 => address_line_2,
:city => city,
:state => billing_state,
:country => "US",
:zip => zip_code,
:phone => phone_number,
:company => company
},
:shipping_address => {
:first_name => sfirst_name,
:last_name => slast_name,
:address1 => saddress_line_1,
:address2 => saddress_line_2,
:city => scity,
:state => sbilling_state,
:country => "US",
:zip => szip_code,
:phone => sphone_number,
:company => scompany
}
}
end
def validate_card
unless credit_card.valid?
credit_card.errors.full_messages.each do |message|
errors.add :base, message
end
end
end
def credit_card
@credit_card ||= ActiveMerchant::Billing::CreditCard.new(
:brand => card_type,
:number => card_number,
:verification_value => card_verification,
:month => card_expires_on.month,
:year => card_expires_on.year,
:first_name => first_name,
:last_name => last_name
)
end