Dear Folks,
i build for my application a function that make the basic api request of chargedesk a powerfull tool for handling payment ( like braintree) . I build the code for my purpose but give an idea exact to how to make the calls (POST/GET) and how to proper parser the JSON response , i'll attach the response from my test
Please ignore all reference to my application and focus on how to parse JSON
few thing to remind:
1) in the basic auth chargdesk release one secret key for api, put the same value in the password
2)json parsing as mountain of test are:
- every array use .toJSONList(); at end
- every segment of the array use the getJson
ex: array charge
charge = responseText.getJSON("charge").toJSONList();
status_text part of array charge
idchk.STATO_CHK=charge.getJSON("status_text");
3) thisapp.Mie_funzioni.debug is a simple function that write a row in a debug form with a specify sintax , if you want i can share the code.
From this base to create the complete code integration is not hard.. need lots of time to parse every attribute from response , that's why i prefer to describe the basic usage and who need adapt for their scope.
I'll be happy to find someone that complete the code :)
int business.payment_gw(int iddg, int mode, string reason, float preso, float reso)
{
//debug
space = " ### ";
//fine debug
// CR - KAM-I42
/* modes:
0=charge
1=request
2=retreive
3=refund
4=cancel
5=capture
6=notify
reason:
extend=estensione
p_dep=prende il deposito
r_dep=rilascia il deposito
*/
//chargedesk credential
password = "copy here the username value";
username = "YOUR CHARGEDESK API Secret key ";
baseEncoded = zoho.encryption.base64Encode(username + ":" + password);
//header encapsulation
headersMap = Map();
headersMap.put("Accept-Language","en_US");
headersMap.put("Accept","application/json");
headersMap.put("Authorization","Basic " + baseEncoded);
//data_map init
requestMap = Map();
responseMap = "";
noMap = "";
desc = if(reason == "p_dep","DEPOSIT",if(reason == "r_dep","REFUND",if(reason == "extend","EXTEND","VOID")));
//fetch ID_REF su iddg
idchk = ChargeDeskZoho[ID == iddg];
idchk.ID_RIF=idchk.ID;
thisapp.Mie_funzioni.debug("1a","custom-func","payment_gw",idchk.Order_P,"init:paramam -> " + iddg + space + mode + space + reason + space + preso + space + reso);
//switch case mode
if(mode == 0)
{
//0=charge
thisapp.Mie_funzioni.debug("2a","custom-func","payment_gw",idchk.Order_P,"mode:charge");
}
if(mode == 1)
{
//1=request of charge
requestMap.put("contact",idchk.EMAIL_PAY);
requestMap.put("amount",preso);
requestMap.put("currency","EUR");
requestMap.put("description",desc + " for " + idchk.Order_P);
code = responseMap.get("responseCode");
//get values
if(code == 200)
{
responseText = responseMap.getJSON("responseText").toJSONList();
charge = responseText.getJSON("charge").toJSONList();
idchk.STATO_CHK=charge.getJSON("status_text");
idchk.CHDK_ID=charge.getJSON("charge_id");
idchk.Valore_Preso=preso;
idchk.Valore_Reso=reso;
thisapp.Mie_funzioni.debug("2b","custom-func","payment_gw",idchk.Order_P,"mode:request " + code + space + idchk.CHDK_ID);
idchk.REASON=desc;
}
else
{
responseText = responseMap.getJSON("responseText").toJSONList();
error = responseText.getJSON("error");
type = error.getJSON("type");
message = error.getJSON("message");
thisapp.Mie_funzioni.debug("2b_err","custom-func","payment_gw",idchk.Order_P,"mode:request " + idchk.CHDK_ID + space + code + space + type + space + message);
}
}
if(mode == 2)
{
//2=retreive
responseMap = getUrl(queryUrl,headersMap,false);
code = responseMap.getJSON("responseCode");
//get values
if(code == 200)
{
responseText = responseMap.getJSON("responseText").toJSONList();
idchk.STATO_CHK=responseText.getJSON("status_text");
idchk.PAY_WITH=responseText.getJSON("payment_method");
thisapp.Mie_funzioni.debug("3b","custom-func","payment_gw",idchk.Order_P,"mode:retreive " + code + space + idchk.CHDK_ID + space + idchk.STATO_CHK);
}
else
{
responseText = responseMap.getJSON("responseText").toJSONList();
error = responseText.getJSON("error");
type = error.getJSON("type");
message = error.getJSON("message");
thisapp.Mie_funzioni.debug("3b_err","custom-func","payment_gw",idchk.Order_P,"mode:retreive " + idchk.CHDK_ID + space + code + space + type + space + message);
}
}
if(mode == 3)
{
//3=refund
requestMap.put("amount",reso);
requestMap.put("log_reason",desc + " for " + idchk.Order_P);
responseMap = postUrl(refundUrl,requestMap,headersMap,false);
code = responseMap.get("responseCode");
//get values
if(code == 200)
{
responseText = responseMap.getJSON("responseText").toJSONList();
idchk.STATO_CHK=responseText.getJSON("status_text");
idchk.Valore_Reso=responseText.getJSON("amount_refunded");
thisapp.Mie_funzioni.debug("4b","custom-func","payment_gw",idchk.Order_P,"mode:refund " + code + space + idchk.CHDK_ID + space + idchk.STATO_CHK + space + idchk.Valore_Reso);
}
else
{
responseText = responseMap.getJSON("responseText").toJSONList();
error = responseText.getJSON("error");
type = error.getJSON("type");
message = error.getJSON("message");
thisapp.Mie_funzioni.debug("4b_err","custom-func","payment_gw",idchk.Order_P,"mode:refund " + idchk.CHDK_ID + space + code + space + type + space + message);
}
}
if(mode == 4)
{
//4=cancel
responseMap = postUrl(cancellUrl,noMap,headersMap,false);
code = responseMap.getJSON("responseCode");
//get values
if(code == 200)
{
responseText = responseMap.getJSON("responseText").toJSONList();
idchk.STATO_CHK=responseText.getJSON("status_text");
thisapp.Mie_funzioni.debug("5b","custom-func","payment_gw",idchk.Order_P,"mode:cancel " + code + space + idchk.CHDK_ID + space + idchk.STATO_CHK);
}
else
{
responseText = responseMap.getJSON("responseText").toJSONList();
error = responseText.getJSON("error");
type = error.getJSON("type");
message = error.getJSON("message");
thisapp.Mie_funzioni.debug("5b_err","custom-func","payment_gw",idchk.Order_P,"mode:cancel " + idchk.CHDK_ID + space + code + space + type + space + message);
}
}
if(mode == 5)
{
//5=capture
}
if(mode == 6)
{
//6=notify
requestMap.put("email",idchk.EMAIL_PAY);
responseMap = postUrl(notifyUrl,requestMap,headersMap,false);
code = responseMap.getJSON("responseCode");
//get values
if(code == 200)
{
responseText = responseMap.getJSON("responseText").toJSONList();
idchk.notification_sent=idchk.notification_sent + 1;
thisapp.Mie_funzioni.debug("7b","custom-func","payment_gw",idchk.Order_P,"mode:notify " + code + space + idchk.CHDK_ID + space + idchk.notification_sent + space + idchk.EMAIL_PAY);
}
else
{
responseText = responseMap.getJSON("responseText").toJSONList();
error = responseText.getJSON("error");
type = error.getJSON("type");
message = error.getJSON("message");
thisapp.Mie_funzioni.debug("7b_err","custom-func","payment_gw",idchk.Order_P,"mode:notify" + idchk.CHDK_ID + space + code + space + type + space + message);
}
}
return code;
}