• Swapnil Dole
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hello Everyone,

I'm currently working on PayU payment gateway. I implemented it through rest api in apex, it is thorwing syntax error, as we have to send JSON body.
So, I tried to send request through jquery, bt I'm facing issue while redirecting. Please refer to the code below.
 
<script>
                function placeOrderPayU(){
                        
                        $.ajax('https://secure.snd.payu.com/api/v2_1/orders',{
                          type: 'POST',                                          
                          data: $.parseJSON('{"notifyUrl":"/apex/orderconfirm","customerIp":"203.122.41.218","extOrderId":'+'"'+Math.floor((Math.random()*1000)+1)+'"'+',"merchantposId":"300746","description":"des","currencyCode":"PLN","totalAmount":"4793.34","buyer":{"email":"test@wk.com","phone":"34454545","firstName":"test","lastName":"test","language":"en"},"settings":{"invoiceDisabled":"true"},"products":[{"name":"Camera","unitPrice":"1368.3400","quantity":"1"},{"name":"Jeans","unitPrice":"3425.0000","quantity":"1"}]}'),
                          crossdomain:'true',                      
                          dataType: 'jsonp',                          
                          success: function(response){
                              var res =  response["redirectUri"];
                              alert(res);
                          },
                          beforeSend: function (xhr,settings) {
                              
                              xhr.setRequestHeader('Content-Type','application/json');
                              xhr.setRequestHeader('Accept','*/*');
                              xhr.setRequestHeader('Authorization','Bearer <your token>');
                              xhr.setRequestHeader('Accept-Encoding','gzip');
                              xhr.setRequestHeader('Access-Control-Allow-Headers','*');
                              xhr.setRequestHeader('Access-Control-Allow-Origin','*');
                              xhr.setRequestHeader("Access-Control-Allow-Methods", "POST");
                              xhr.setRequestHeader('Access-Control-Request-Headers','origin,Authorization,Content-Type,Accept');
                          },
                        })
                }
            </script>

Code for generating access token is working fine.
<!--Controller-->
public with sharing class payuconfig {
    public string str{get;set;}
    public void click(){
        try{
            HttpRequest req = new HttpRequest();
            req.setEndpoint('https://secure.snd.payu.com/pl/standard/user/oauth/authorize');
            req.setMethod('POST');            
            req.setHeader('Content-Type','application/x-www-form-urlencoded');            
         
 req.setBody('grant_type=client_credentials&client_id=300746&client_secret=2ee86a66e5d97e3fadc400c9f19b065d');            
            
            Http http = new Http();
            HTTPresponse res= http.send(req);
            str = res.getBody();
            
            system.debug(str);
            
        }catch(exception e){
            system.debug(e);
        }
    }
}

<!-- Visualforce Page -->
<apex:page controller="payuconfig" standardstylesheets="false" applyHtmlTag="false" applybodytag="false" cache="false" showheader="false" doctype="html-5.0" title="Payu">
  <apex:form >
      <apex:outputPanel id="main">      
          {!str}
      </apex:outputPanel>      
      <apex:commandButton value="Click" action="{!click}" rerender="main"/>      
  </apex:form>
</apex:page>​

Please respond.
Thanx in advance
Regards
Aakanksha Singh