This topic is locked
[SOLVED]

 External API in a custom buttom

5/8/2020 9:22:36 PM
PHPRunner General questions
aadham author

Hi

I'm using PHPrunner 10.4 for a customers loyalty project that uses APIs to communicate with another app.
The problem is that I have a case where once a custom button on a products list page is clicked, it should send three fields to an external API to obtain the necessary approval to redeem a customer's loyalty points.

Once the authorization is received, the transaction will be added to a table in the my local database.
Here's the code I'm using to communicate successfully with the API:

require_once("../include/dbcommon.php");

$Points = $_SESSION['Points'];

$ProductID = $_SESSION['Product_ID'];
$Login_Token = "XXXXXX";
/*****************************************************************************************/
$Body_Parameters = array(

"RequestInfo" => array("ChannelCode" => "web", "Points" => "$Points", "ProductID" => "$ProductID")

);



require_once('lib/APIConnection.php');

$RedeemVoucher_Result = APIConnection("RedeemVoucher", $Login_Token, $Body_Parameters);


However, due to my somewhat limited knowledge in programming I'd appreciate all the help I can get on how to achieve that with a custom button.
Thank you.

Admin 5/9/2020

So, what is happening? Any error messages? Does the same code work if you place it to a standalone PHP file?

aadham author 5/9/2020



So, what is happening? Any error messages? Does the same code work if you place it to a standalone PHP file?


Thank you Sergey for responding.
The code works as it should in a standalone PHP file.
I've used the following code in the custom button's "Server" section to call the API, but it didn't seem to work:

global $dal;
$record=$button->getCurrentRecord();

if ($record["Product_ID"])

{

$token= DBLookup ("select token from sometable where User_Name = '".$_SESSION["UserName"]."' ");

$_SESSION["Token"] = $token;

$Body_Parameters = array(

"RequestInfo" => array("ChannelCode" => "web", "Points" => "$Price", "ProductID" => "$ProductID")

);



require_once('lib/APIConnection.php');

$RedeemVoucher_Result = APIConnection("RedeemVoucher", $_SESSION["Token"], $Body_Parameters);
if($RedeemVoucher_Result['status'] == true)

{

$result["txt"] = "Points redeemed successfully";

}

else

{

$result["txt"] = "Operation failed";

}

}


I left the default code in Client Before and Client After unchanged.
Thanks

Admin 5/10/2020

Check this article that explains how to troubleshoot custom buttons:

https://xlinesoft.com/phprunner/docs/troubleshooting_custom_buttons.htm
I'm pretty sure that there is some PHP error happens in the Server part of the code and you need to find it.

aadham author 5/10/2020



Check this article that explains how to troubleshoot custom buttons:

https://xlinesoft.com/phprunner/docs/troubleshooting_custom_buttons.htm
I'm pretty sure that there is some PHP error happens in the Server part of the code and you need to find it.


Thank you for the link Sergey.

I've revised my code to the following:

$token = $_SESSION['Token'];

$record=$button->getCurrentRecord();

$_SESSION['Product_ID'] = $record["Product_ID"];

$_SESSION['Price'] = $record["Price"]*$_SESSION['Points'];

if ($_SESSION["Balance"]<$record["Price"]*$_SESSION['Points'])

{

$message = "Total points to be redeemed is more than your available points.";

}

else

{

$Body_Parameters = array(

'RequestInfo' => array('ChannelCode' => 'web', 'Points' => $record['Price']*$_SESSION['Points'], 'ProductID' => $record['Product_ID'])

);



require_once('APIs/lib/APIConnection.php');

$RedeemVoucher_Result = APIConnection("RedeemVoucher", $token, $Body_Parameters);

echo "Product ID: ".$record['Product_ID']. "
";

echo "Price: ".$record['Price']*$_SESSION['Points']. "
";

echo "Token: ".$token;

if($RedeemVoucher_Result['status'] == true)

{



echo "<pre>";

print_r($RedeemVoucher_Result);

$result["txt"] = "Points redeemed successfully";

echo "</pre>";

}

else

{

echo "<pre>";

print_r($RedeemVoucher_Result);

$result["txt"] = "Operation failed";

echo "</pre>";

}
}


Connection to the API now seems to be successful, but I'm getting the following error message that I'm not getting with the standalone PHP file:



[msg] => The JSON value could not be converted to System.Collections.Generic.Dictionary`2[System.String,System.String]. Path: $.Points | LineNumber: 2 | BytePositionInLine: 14.Cannot get the value of a token type |Number| as a string.


The Client After code is the default one:

var message = result["txt"] + " !!!";

ajax.setMessage(message);


Could it be that the token value is being sent in manner different from what the API is expecting, or is it something else I'm missing?

Thank you

Admin 5/11/2020

It looks like the variable $_SESSION['Token'] is not populated.

aadham author 5/11/2020



It looks like the variable $_SESSION['Token'] is not populated.



Echoing $_SESSION['Token'], among others, is displaying the correct value on the page.

Admin 5/11/2020

But here is what I can see in your error message:

[msg] => The JSON value could not be converted to System.Collections.Generic.Dictionary`2[System.String,System.String]. Path: $.Points | LineNumber: 2 | BytePositionInLine: 14.Cannot get the value of a token type |Number| as a string.


  1. The error message looks a .NET error message meaning that the call to API is being made
  2. The error message also talks about the token meaning that token value is either missing or token's format is incorrect (it says something about conversion from Number to String or vice versa).
    My suggestion is to print $token and $Body_Parameters and then run the same API call manually to obtain more info.

aadham author 5/12/2020



But here is what I can see in your error message:

[msg] => The JSON value could not be converted to System.Collections.Generic.Dictionary`2[System.String,System.String]. Path: $.Points | LineNumber: 2 | BytePositionInLine: 14.Cannot get the value of a token type |Number| as a string.


  1. The error message looks a .NET error message meaning that the call to API is being made
  2. The error message also talks about the token meaning that token value is either missing or token's format is incorrect (it says something about conversion from Number to String or vice versa).
    My suggestion is to print $token and $Body_Parameters and then run the same API call manually to obtain more info.


Thank you very much Sergey.
I've printed the $token and $Body_Parameters the API is receiving and they're identical to the ones my app is sending, so I sent the developers of the other app asking for their diagnosis.
I'll update the post once I hear back from them.
Thank you.

aadham author 5/24/2020



Thank you very much Sergey.
I've printed the $token and $Body_Parameters the API is receiving and they're identical to the ones my app is sending, so I sent the developers of the other app asking for their diagnosis.
I'll update the post once I hear back from them.
Thank you.


Hi

As connection was made successfully to the API and after getting in touch with the developers of the other app, it turned out that the API expected one of the variables as a string, so I changed the code to the following:

$record=$button->getCurrentRecord();

$_SESSION['Product_ID'] = $record["Product_ID"];

$Points = $record["Price"]*$_SESSION['Points'];

$token = $_SESSION['Token'];

if ($_SESSION["Balance"]<$record["Price"]*$_SESSION['Points'])

{

$result["txt"]= "Total points to be redeemed is more than your available points.";

}

else

{

$Body_Parameters = array

(

"RequestInfo" => array("ChannelCode" => "web", "Points" => (string)$Points, "ProductID" => $record['Product_ID'])

);



require_once('APIs/lib/APIConnection.php');

$RedeemVoucher_Result = APIConnection("RedeemVoucher", $token, $Body_Parameters);



And it worked as it should, so thank you very much Sergey for your kind assistance.