Adamski
Adamski

Reputation: 1

Chicken and Egg: save model before proceed to payment gateway? (Rails)

I am in the process of integrating Barclays ePDQ CPI payment into our app.

The way I have coded so far is:

1.User enters details 2.Model is saved to session (not db) 3. User is forwarded to payment page. 4. User enters payment details. 5. eDPQ server performs post back, picked up by a controller. This should save order details and save the user model. 6. User is redirected to results page by CPI.

The reason I do not want to save user details before sending to payment page is so that the user can press back on the browser if they have entered wrong details, and resubmit the form.

However - I now realise that since the eDPQ server calls the post back directly, there will be no session. It is possible to send an order_id to the payment page which gets sent back as a reference. So now I am thinking to go back and save the user before sending them to the payment page, so I send a reference in the order_id. This can then be picked up by the post_back method, which will find the user and complete the registration process.

But, if they click back and try and resubmit there will be errors as the user will already exist.

One way round this, is to make sure the "new" page for the user is never cached. So when they click back, they are actually shown an "edit" page, which will look like a "new" page. However this looks like it will add some coding complications.

I'm hoping someone has come up against something similar and may be able to give some advice here.

Thanks for reading!

Upvotes: 0

Views: 305

Answers (1)

Pan Thomakos
Pan Thomakos

Reputation: 34350

In general I think it's best to go ahead a completely save your model. If you want to keep track of progress you can do so with a progress column. For example, in your order you could do the following:

class Order
  def processed?
    status == 'processed'
  end

  def entered_details?
    status == 'details entered'
  end

  def can_process?
    entered_details?
  end
end

And then you could simply update your status as you progress. For example, after the user enters their payment details you could update the status to 'details entered' and after you process with the payment gateway you can update the status to 'processed'. This would allow you to enforce a state in your model without having to store it in the session temporarily.

As for the back button, you can't control what a user sees when they click the back button. They are taken to the page they were previously on, and your server is not notified of this. That's the curse of the back button. The only way to get around this is to ensure that the second form submit causes a validation error, or that if it comes from the same session it performs an update, but in general this is not a case you should be trying to accomodate since it doesn't follow the flow of your application. That is why many payment sites often write 'do not click the back button' when they are processing payments. That said, you should provide your own back button on the page, but you can adjust that to redirect to an edit page instead of a new page.

Upvotes: 1

Related Questions