2

I am setting up a payments page for a site, which first completes a kid of simple shopping cart, then when the user clicks "Submit", I need to POST the transaction data to an external credit card processing site, and redirect the user to that site. Once they've entered their credit card details there, the site redirects back to me, either with approval or rejection.

Trouble is, I can't see anything in the MVC interface that allows me to redirect with a POST action. The Redirect() method uses a GET (which doesn't work anyway on the 3rd party site). I tried doing a programmatic post using WebClient.UploadValues(), and I get back from that a string which I can present to the user by returning Content() - but then the links inside that form, which are supposed to be relative to the CC processing site, are instead interpreted as relative to my site, which is no bleedin' use to me.

How do I do this?

3
  • Can you not just post to that site direct? Commented Apr 23, 2012 at 18:50
  • @TimBJames - No, I need to do some pre-processing to generate my own transaction ID etc. Commented Apr 23, 2012 at 18:51
  • @Shaul - the point would be that you generate these things before you hit your check out page. For example, your transaction id can be deterministically based on your cart id, customer id, etc. so that you can generated it before posting the transaction to the card processor. Commented Apr 23, 2012 at 19:32

2 Answers 2

5

You cannot redirect using a post.

You can however do this:

  • Receive the post and do stuff on it
  • Return the user a view containing a form filled with hidden field containing processed data
  • Have the form submitted automatically using javascript (having the post action set on your CC provider)
  • The user goes to the CC provider
  • The user returns to your site

EDIT - Using jQuery, form post with a form ID ccform, with the action set to the URL of your cc provider and the hidden fields :

$.ready(function() { $('#ccform').submit(); });
Sign up to request clarification or add additional context in comments.

4 Comments

LOL, that's the "evil" suggestion shunned by @tvanfosson. :-) Since I'm not at all at home with javascript, could you please include a code sample of how this might be done?
It's "evil" but it cannot be done any other way if you want to use POST. But I agree that your CC provider should provide you with better API.
Thanks for the edit! How do I know what string to put in for "#ccform"? And how do you use Html.BeginForm to post to a remote site?
@Shaul See this for an example on posting to external site. For the ID I guess you shouldn't need it. Simply use $('form').submit(); and it should be ok
0

Depending on your credit card processing provider, the mechanism may differ. Typically, you would either use a GET with URL parameters, which they in turn will use to populate a session and redirect to the form that collects the credit card info, or more likely, you use their URL as the action on your check out page so that it gets directly posted to their site. Check your provider's documentation and examples as the mechanism will depend on what they support.

3 Comments

Using a GET with URL parameters doesn't work. Posting does work - but I'm getting an uneasy feeling from your answer that MVC3 doesn't support programmatic posting. Is that correct? If so, why would they have designed it that way?
@Shaul unfortunately I believe that programmatic posting is not implemented in mvc.
Typically you would generate the view with the form pointed at the credit cart processor's site. Typically if you do a direct post to their site, as opposed to using some out of band API, you generate all the data they need in hidden (or otherwise) fields on your for and have it post directly to their site. There isn't, other than generating a page that autosubmits a form using javascript, any way from the server to force the browser to post elsewhere. I would consider the autosubmit evil and it shouldn't be necessary to work with the provider.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.