0

I need to connect to the payment gateway from my JSF application. I need to achieve the following:

(A) Need to send a POST request with required parameters. The Java controller code is as below is being called from the xhtml page

A payment object is already created when user hits "Proceed for Payment" button.

public void paymentGateway() {
    ExternalContext externalContext=FacesContext.getCurrentInstance().getExternalContext();

    try {
        externalContext.redirect("https://test.payu.in/_payment?key="
                + this.key
                + "&txnid=1232132&amount="
                + payment.getTotalAmount()
                + "&productinfo="
                + payment.getItem().getName());
    } catch (IOException e) {
        e.printStackTrace();
    }
}
<div class="form-group">
    <h:commandButton value="Proceed for Payment"
                     action="#{myController.paymentGateway}" 
                     styleClass="btn btn-submit" />
 </div>

The above is INCORRECT, I need to send parameters to EXTERNAL SERVER but the above will show all parameters on the browser URL bar.

(B) Also when the Payment Gateway server responds (successful payment or error), I need capture that in the "response" and show it on the XHTML page.

I know for this, Servlet would be ideal to redirect it to Payment Gateway with requirement parameters & store the response in response object, but how to accomplish this part, I do not know.

Requesting any help, please.

Thank you!

Tiny
  • 24,933
  • 92
  • 299
  • 571
DIM
  • 73
  • 9
  • How is this explicitly 'payment-gateway' related? Isn't that you 'just' want to send the user to an external site after executing a commandButton? And how is this 'servlet' related? If you think in terms of the generic problem, you'll most likely find duplicates – Kukeltje Dec 22 '15 at 21:17
  • Tackling problem is payment gateway integration. Wanted to see if anyone has done something similar. But I understand what you are trying to say. Sorry about misinterpreting the tags here. If you could guide me to a solution, it will really helpful. – DIM Dec 23 '15 at 01:34
  • It's not different in a JSF bean than in a servlet. It's just the same Java code after all. Your real question is answered here: http://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests/ – BalusC Dec 23 '15 at 08:49
  • @BalusC Thank you, but I am not sure still how this can be accomplished in a simpler way. I had seen one of your post where you talked about passing post parameters in JSF using Javascript and hidden form fields. I have somehow lost that link. With the code what I posted above, the user gets "redirected" to the payment-gateway (the redirection requires me to pass params, which currently are "seen" in the URL on the browser. This is what I want to avoid.), and once he makes a payment, result is given as a "response" by the gateway. I have no idea how to capture all this. Requesting help. – DIM Dec 23 '15 at 10:37
  • The question in its current form gives the impression that you wanted to execute it programmatically (you yelled that you don't want to see URL in address bar and wanted to capture the entire response all by yourself). The link in my previous comment answers that. The other way is to let the webbrowser do all the hard work by itself by simply using a HTML form or link to that URL, which is apparently what you found in the other post. – BalusC Dec 23 '15 at 10:38
  • @BalusC Sorry about the impression. Can you help me with that link where you let the browser do the hardwork through hidden form fields, HTML and Javascript on a JSF/XHTML page. I remember yours was the first question in the thread. Link is lost from me. I want this task to be done asap (i.e. without params getting seen in the address bar and later capture what has come back from the gateway), maybe I can take a cleaner approach later on. You are gem on this platform, requesting assistance, please. – DIM Dec 23 '15 at 10:52
  • Just use a POST form instead of a GET form if you want to hide form data from URL. The payment gateway should offer the possibility to take a callback URL as parameter (where it should be redirecting back on complete of payment), you're supposed to set it to the URL of the desired landing page on your side. I'm not seeing it anywhere in your example URL. – BalusC Dec 23 '15 at 11:04
  • Since I am doing a "externalContext.redirect()" - is there a way to have a POST request? By default it uses GET. This is the link I was talking about: http://stackoverflow.com/questions/17232209/facescontext-redirect-with-post-parameters – DIM Dec 23 '15 at 11:17
  • @BalusC Payment Gateway takes in request parameter as follows - key, txnid, firstname, amount, productinfo, email, phone, surl (success-url), furl (failure-url). – DIM Dec 23 '15 at 11:49
  • Just use a POST form. It's clear that you can provide surl and furl as callback URLs. – BalusC Dec 23 '15 at 11:57
  • Thanks a lot. How do I capture the "response & its parameters" if I am sending the callback URLs as my application URLs (an X HTML page)? Like, I want to display, "Payment Successful" on the same page from where I am redirecting user to the payment gateway. Same in case of failure. Thanks a ton for your support. – DIM Dec 23 '15 at 12:08

0 Answers0