Topic: too much rescue not enough begin

I'm testing using the authorizenet test site, and I can't seem to ever make the  transaction.submit ever go to the "is_ok.rhtml"...regardless of whether the test card is authorized or not...

Can you tell me what I'm missing?

  def process_payment
    @customer = Customer.find(params[:customer][:id])
    require 'payment/authorize_net'
    transaction = Payment::AuthorizeNet.new(
     :login => 'login,
    :transaction_key => 'pass',
    :amount      => @cart.final_total,
    :url => 'https://test.authorize.net/gateway/transact.dll',
    :expiration  => @customer.credit_card_exp,
    :first_name  => @customer.first_name,
    :last_name   => @customer.last_name,
    :address =>  @customer.address,
    :city =>  @customer.city,
    :state =>  @customer.state,
    :zip =>  @customer.zip,
    :card_number => @customer.credit_card,
    :card_code => @customer.card_code,
        )

    begin
      transaction.submit
      flash[:notice] = "Card processed successfully: #{transaction.error_message}
      Response codes:
      authorization: #{transaction.authorization}
           redirect_to :action => 'is_ok'
    end
  rescue
    flash[:notice] = "Card was rejected: #{transaction.error_message}
    Response codes:
    authorization: #{transaction.authorization}
    redirect_to :action => 'not_ok'
    return
  end

Last edited by chasC (2006-11-14 16:16:58)

2

Re: too much rescue not enough begin

A missing close quote on "login"?

Re: too much rescue not enough begin

good eye..but I just substituted the values for the forum. The request totally works...I just can't get mine to respond conditionally like the Payment example:

 require 'payment/authorize_net'

    transaction = Payment::AuthorizeNet.new (
                      :login       => 'username',
                      :password    => 'password',
                      :amount      => '49.95',
                      :card_number => '4012888818888',
                      :expiration  => '03/10',
                      :first_name  => 'John',
                      :last_name   => 'Doe'
                     )
    begin
      transaction.submit
      puts "Card processed successfully: #{transaction.authorization}"
    rescue
      puts "Card was rejected: #{transaction.error_message}"
    end


BTW  here is s typical response from a request apprehended with a flash[:notice]
Card was rejected: 'This transaction has been approved.'
Response codes:
authorization: 'ZpHnqb'
result code: 0
avs code: 'Y'
transaction id: '507077825'
md5: '5028B0D27FC645A5D24AFE5F181CCF8C'
cvv2 response: 'P'
cavv response: '2'

Last edited by chasC (2006-11-14 16:56:27)

Re: too much rescue not enough begin

Are you sure the transaction will raise an exception when it fails? It might just return the response code or something. I'm not familiar with that plugin so I don't know how it works.

Railscasts - Free Ruby on Rails Screencasts

Re: too much rescue not enough begin

yeah...I'm not sure...I'm just going from the examples from the docs:
http://payment.rubyforge.org/

and I'd try a conditional if I could evaluate the transaction object which looks like the place to start.
Its appears to be a hash from the flash[:notice], but i haven't been successful in getting the transaction into another variable like @transaction to evaluate it.

Last edited by chasC (2006-11-14 19:03:34)

Re: too much rescue not enough begin

Oh, if it's in the docs like that then it is probably correct (it raises an exception upon an invalid credit card). Not sure what the problem is then.

BTW, it should be required outside of the method, I suggest the environment.rb file:

# in environment.rb
require 'payment/authorize_net'

Also, have you taken a look at Active Merchant? That handles credit card processing as well.

Railscasts - Free Ruby on Rails Screencasts

Re: too much rescue not enough begin

Thanks Ryan! I'll try it.