adamthedeveloper / wepay-rails

Collect payments from wepay in your rails application.
MIT License
32 stars 24 forks source link

PreApprovals #25

Closed lvxn0va closed 11 years ago

lvxn0va commented 11 years ago

I'm working on setting up Preapprovals and delayed Charges with Wepay-Rails as a plugin for the open source Catarse Crowdfunding platform.

By copying the Checkout methods am able to get some test preapprovals flowing properly, and now need to call the :preapproval_id and :preapproval_uri to make a payment/capture.

Sorry for the silly question, but I am looking for the code that saves the :preapproval_id and :preapproval_uri in the database in the same manner as the checkout_id. In my tests, It seems they are not saving to the database on the callback. Is that something I need to setup in the lib/wepay_checkout_methods model through the gem or do I need to code it in one of my models in my app?

The :preapproval_id and :preapproval_uri columns have been added to the wepay_checkout_records table, but do not show when I call the records in the console after a successful callback from Wepay.

I should add I'm using this sort of code in my preapproval_controller which admittedly is a very ugly stab in the dark:

def success response = WepayCheckoutRecord.find_by_preapproval_id(params[:preapproval_id]) logger.info response.inspect if response[:state] == 'approved' response.update_attributes :preapproval_id => :preapproval_id response.save!

@user.build_payment_detail

 #@user.confirm!
 #flash[:notice] =  "Successful Approval"
 render :success

end end

My fork of the gem is here: https://github.com/lvxn0va/wepay-rails

adamthedeveloper commented 11 years ago

The IPN's purpose is to record everything that comes back in the callback. Let me take a quick look and get back to you.

Adam

adamthedeveloper commented 11 years ago

Just real quick while I am looking through your code to get you your real answer, I noticed this in the Wepay::CheckoutController

conds = {
      :security_token  => params[:security_token],
      :checkout_id     => params[:checkout_id],
      :preapproval_id  => params[:preapproval_id],
    }.delete_if {|k,v| v.nil?}

    record = WepayCheckoutRecord.where(conds).first

The delete_if will actually make the security token optional. I think we want it there to make sure people can't just guess checkout id's. The additional security token makes it extremely difficult or darn near impossible to guess the combination.

Was that something you changed or was that already there? If it was already there, we need to fix that.

adamthedeveloper commented 11 years ago

Did you change your WepayCheckoutRecord model in anyway other than what was copied over on install? Can you paste your model so I can see what you have? I am trying to determine if you added attr_accessible or something that would stop certain fields getting written to.

lvxn0va commented 11 years ago

Adam thx for the heads up on the delete_if...I had copied from another fork and let it go without understanding that statement..Here's my lib/WepayCheckoutRecord :

class WepayCheckoutRecord < ActiveRecord::Base belongs_to :checkout belongs_to :preapproval attr_accessible :amount, :short_description, :access_token, :checkout_id, :security_token, :checkout_uri, :account_id, :currency, :fee_payer, :state, :redirect_uri, :auto_capture, :app_fee, :gross, :fee, :callback_uri, :tax, :payer_email, :payer_name, :mode, :preapproval_id, :preapproval_uri end

I basically threw the kitchen sink at the attr_accessible because I was getting mass assignment errors

adamthedeveloper commented 11 years ago

My goodness. It all looks like it should work with what you did with the checkout controller.

      if record.checkout_id.present?
        checkout = wepay_gateway.lookup_checkout(record.checkout_id)
      else
        checkout = wepay_gateway.lookup_preapproval(record.preapproval_id)
      end
      checkout.delete_if {|k,v| !record.attributes.include? k.to_s}
      record.update_attributes(checkout)

What is inside the checkout hash before you do the update_attributes? And then if you reload record (record.reload), what is saved in there? Also, print what checkout is before the delete_if too. I think this is where everything should be happening.

lvxn0va commented 11 years ago

Thx so much, checking it...will update

adamthedeveloper commented 11 years ago

Sounds good. If you can get this working, I'd like you to give me a pull request and I'll include your change in the project.

lvxn0va commented 11 years ago

Thanks, I think I may have gotten it..You were right, I was passing the redirect_uri in the params hash from the Wepay.com ruby sample code and it was bypassing the preapproval/index and going straight to my success.erb page! doh!

Tonight, I'll work on getting the capture portion working, clean up the controllers and push the code..Thanks for your help. I'll close this now..