Ive implemented the Rails book example Catalog/Cart/Checkout in my project. I''m now taking it a step further and putting my credit card processing in place. To do so, Ive added 4 fields to the orders table that will store the results from the cc response. Obviously, I only want to store them if the order is approved. So, here is my dilemma: I need to validate the form (@order.name exists, @order.address exists, etc). If it validates, I will try to process the credit card. Ill take the 4 responses from that and put them in @order (IE. @order.result = cc_response[:result]). Here''s the catch.. I now want to validate the whole @order, requiring that @order.result == ''APPROVED''. If I put this as a validation requirement, the first validation check will always fail and Ill never get to the cc processing part. So, I guess I need 2 separate variances of validation, but I cant figure out how to do that. One idea I''m debating is putting the cc responses in a separate table/model, and linking them with an order_id. That should allow me to validate exactly what I want independently. My only concern is I "think" it would make more sense to have the whole order in one table, but I''m open to views on that. The other idea I''m just coming up with as I write is to do the APPROVED validation in the controller instead of the model. Its almost seeming apparent that would be easy to do now. My only concern here is that I feel like I''m not following good MVC/Rails practice. If anyone has insight on this, please let me know. I know theres an elegant solution, I just cant seem to find it as of now. Joe Noon
Joe Noon wrote:> Ive implemented the Rails book example Catalog/Cart/Checkout in my > project. I''m now taking it a step further and putting my credit card > processing in place. To do so, Ive added 4 fields to the orders table > that will store the results from the cc response. Obviously, I only > want to store them if the order is approved. So, here is my dilemma: > > I need to validate the form (@order.name exists, @order.address exists, etc). > If it validates, I will try to process the credit card. > Ill take the 4 responses from that and put them in @order (IE. > @order.result = cc_response[:result]). > Here''s the catch.. I now want to validate the whole @order, requiring > that @order.result == ''APPROVED''. > If I put this as a validation requirement, the first validation check > will always fail and Ill never get to the cc processing part. So, I > guess I need 2 separate variances of validation, but I cant figure out > how to do that. > > One idea I''m debating is putting the cc responses in a separate > table/model, and linking them with an order_id. That should allow me > to validate exactly what I want independently. My only concern is I > "think" it would make more sense to have the whole order in one table, > but I''m open to views on that. > > The other idea I''m just coming up with as I write is to do the > APPROVED validation in the controller instead of the model. Its > almost seeming apparent that would be easy to do now. My only concern > here is that I feel like I''m not following good MVC/Rails practice.I think that you have been tripping yourself up by thinking of credit card authorisation as "validation". Validation is about checking that the user has filled in the form correctly. If it fails, you re-display the form with errors highlighted. But the credit card authorisation may fail because of the card having been reported stolen, or because the account is over its credit limit... in which case you may want to display an entirely different page to explain the problem. So it seems good to regard validation and authorisation as two distinct steps, which means that the controller is the right place to manage them. You may still want to use more than one table, or even to not save the credit card information in the database at all. regards Justin
On 9/24/05, Joe Noon <joenoon-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Ive implemented the Rails book example Catalog/Cart/Checkout in my > project. I''m now taking it a step further and putting my credit card > processing in place. To do so, Ive added 4 fields to the orders table > that will store the results from the cc response. Obviously, I only > want to store them if the order is approved. So, here is my dilemma: > > I need to validate the form (@order.name exists, @order.address exists, etc). > If it validates, I will try to process the credit card. > Ill take the 4 responses from that and put them in @order (IE. > @order.result = cc_response[:result]). > Here''s the catch.. I now want to validate the whole @order, requiring > that @order.result == ''APPROVED''. > If I put this as a validation requirement, the first validation check > will always fail and Ill never get to the cc processing part. So, I > guess I need 2 separate variances of validation, but I cant figure out > how to do that. > > One idea I''m debating is putting the cc responses in a separate > table/model, and linking them with an order_id. That should allow me > to validate exactly what I want independently. My only concern is I > "think" it would make more sense to have the whole order in one table, > but I''m open to views on that. > > The other idea I''m just coming up with as I write is to do the > APPROVED validation in the controller instead of the model. Its > almost seeming apparent that would be easy to do now. My only concern > here is that I feel like I''m not following good MVC/Rails practice. > > If anyone has insight on this, please let me know. I know theres an > elegant solution, I just cant seem to find it as of now.Keep in mind that (I think) you shouldn''t charge customers until the order ships.
On 9/24/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 9/24/05, Joe Noon <joenoon-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Ive implemented the Rails book example Catalog/Cart/Checkout in my > > project. I''m now taking it a step further and putting my credit card > > processing in place. To do so, Ive added 4 fields to the orders table > > that will store the results from the cc response. Obviously, I only > > want to store them if the order is approved. So, here is my dilemma: > > > > I need to validate the form (@order.name exists, @order.address exists, etc). > > If it validates, I will try to process the credit card. > > Ill take the 4 responses from that and put them in @order (IE. > > @order.result = cc_response[:result]). > > Here''s the catch.. I now want to validate the whole @order, requiring > > that @order.result == ''APPROVED''. > > If I put this as a validation requirement, the first validation check > > will always fail and Ill never get to the cc processing part. So, I > > guess I need 2 separate variances of validation, but I cant figure out > > how to do that. > > > > One idea I''m debating is putting the cc responses in a separate > > table/model, and linking them with an order_id. That should allow me > > to validate exactly what I want independently. My only concern is I > > "think" it would make more sense to have the whole order in one table, > > but I''m open to views on that. > > > > The other idea I''m just coming up with as I write is to do the > > APPROVED validation in the controller instead of the model. Its > > almost seeming apparent that would be easy to do now. My only concern > > here is that I feel like I''m not following good MVC/Rails practice. > > > > If anyone has insight on this, please let me know. I know theres an > > elegant solution, I just cant seem to find it as of now. > > Keep in mind that (I think) you shouldn''t charge customers until the > order ships.I''m also working on a similar problem -- how to properly do the authorization, reserving funds, and charging on shipping the product. I''d really appreciate any thoughts or ideas or code that people have on this matter! (I''m writing a replacement for http://www.gamefest.com and all the code for the store will be released. So if you give me a good idea, it''ll be released back to the community shortly in the form of working code)
Don''t most commerce apps: 1. Authorize when the customer makes the purchase. 2. Capture (isn''t that the term) when the fulfillment folks mark the order as shipped? As far as how to do those two things, it depends on the API provided by the CC processor.> From: Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > Reply-To: Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>, <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> > Date: Sat, 24 Sep 2005 12:08:53 -0700 > To: Joe Noon <joenoon-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>, <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> > Subject: Re: [Rails] the Right Way to approach my orders model > > I''m also working on a similar problem -- how to properly do the > authorization, reserving funds, and charging on shipping the product. > > I''d really appreciate any thoughts or ideas or code that people have > on this matter!