payu integration patch to download

PayU Controler

 

An example of jPALIO™ Groove PayU service controller object $*api.payU.PayUController

 


package api.payU;

import palio.*
import palio.modules.*
import palio.PalioException;

import api.payU.config.Configuration

import groovy.json.JsonSlurper

import org.codehaus.groovy.control.*

import org.apache.http.client.*
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class PayUController {

private static final Util util = (Util) Groovy.module("util");
private static final Net net = (Net) Groovy.module("net");
private static final Text text = (Text) Groovy.module("text");
private static final JSON json = (JSON) Groovy.module("json");
private static final Error error = (Error) Groovy.module("error");
private static final Page page = (Page) Groovy.module("page");

public static byte[] httpPostPayU(String url, RequestEntity request, Map<String, String> headerParams) throws HttpException, IOException, PalioException {
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);
for (Map.Entry<String, String> e : headerParams.entrySet()) {
method.addRequestHeader(e.getKey(), e.getValue());
}
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
try {
method.setRequestEntity(request);
int statusCode = client.executeMethod(method);
if ( statusCode == 200 || statusCode == 201 || statusCode == 302){
return method.getResponseBody();
}else {
throw new PalioException(statusCode, method.getStatusLine().toString());
}
} finally {
method.releaseConnection();
}
}

public static api.payU.ResponseClass getAccessToken(){
// Client id i client secret znajdziemy w "klucze konfiguracji" sklepu.
// Przekazywane parametry headers. Trafaia do POSTA.
Map emptyHeaders = [:]
emptyHeaders["Content-Type"]="application/x-www-form-urlencoded"
// Pobiera dane z pliku konfiguracyjnego (getAccessTokenBodyParam()) i tworzy obiekt RequestEntity wymagany do POST.
RequestEntity strReqEntity = new StringRequestEntity(api.payU.config.Configuration.getAccessTokenBodyParam(),"application/json","UTF-8")
// Wysyla POST do PayU z Mapa danych. Pamietaj o zmienie adresu po testach.
byte[] byteResponse
try{
byteResponse = httpPostPayU(api.payU.config.Configuration.getHostUrl()+"/pl/standard/user/oauth/authorize", (org.apache.commons.httpclient.methods.RequestEntity)strReqEntity, emptyHeaders )
}catch(ex){
print("<script>alert('Błąd podczas próby pobrania tokenu!');</script>"+ex);
throw ex
}
// Konwersja odpowiedzi na String.
String response = Palio.toString(byteResponse)
// Do utworzonej klasy przez moduł JSON pobierane są dane z odpowiedzi (String) i przekazywane do Mapy.
api.payU.ResponseClass myResponse = Groovy.module("json").fromJSON((String)response, (java.lang.Class)api.payU.ResponseClass, (Map)null)
return myResponse
}

public static void createTokeAndOrder() throws PalioException {
api.payU.ResponseClass token = getAccessToken()
String access_token = token.getAccess_token();
String token_type = token.getToken_type();
try{
createOrder(token_type,access_token)
}catch(ex){
print("<script>alert('ups... Coś poszło nie tak!');</script>"+ex);
throw ex
}
}

// Przekaz String w formacie JSON zamowienia do bodyParam.
// Tworzy zamowienie. Argument token_type i access_token zostają przekazane z metody getAccessToken.
public static void createOrder(String token_type, String access_token) throws PalioException {
Map emptyHeaders = [:]
emptyHeaders["Content-Type"]="application/json"
emptyHeaders["Authorization"]= token_type+" "+access_token

byte[] byteResponse
RequestEntity strReqEntity = new StringRequestEntity(api.payU.config.Configuration.getOrderBodyParam(),"application/json","UTF-8")
try{
byteResponse = httpPostPayU(api.payU.config.Configuration.getHostUrl()+"/api/v2_1/orders/", (org.apache.commons.httpclient.methods.RequestEntity)strReqEntity, emptyHeaders )
}catch(ex){
print("<script>alert('Błąd podczas próby tworzenia zamowienia!');</script>"+ex);
throw ex
}
String response = Palio.toString(byteResponse)
api.payU.ResponseClass myResponse = Groovy.module("json").fromJSON((String)response, (java.lang.Class)api.payU.ResponseClass, (Map)null)
// Opis kodu statusu zwracanego przez PayU.
println util.get(myResponse.getStatus(), "statusCode")
// Zwraca przekierowanie do i od PayU.
page.redirectObject(myResponse.getRedirectUri())
}

}


Return