I'm trying to set an get payment status API with a payment processor. Below is the code they provided me. There are some information in the $result variable that I want, what I don't understand is what type of variable is '$result' and how can I take certain data from it. printing the $result shows "Transaction ID is : xxxx status is ACCEPTED". What I basically want is to take only the transaction ID and store it in a variable.
function payFawry($ID)
{
$ticket = get_row("select * FROM `tickets` WHERE ID = '$ID' and canceled = 0");
$user = get_row("select * from users where ID = '$ticket[clientID]'");
$amount = $ticket[cost].".00";
$customerMobile = substr($user[phone],-11);
$description = "Ticket ".$ticket[ID];
$merchantCode = "CsdsadasdasdasdasdasdaJasdasd";
$merchantRefNum = $ticket[ID];
$customerProfileId = $ticket[clientID];
$paymentMethod = "PAYATFAWRY";
$cardToken = "";
$customerEmail = substr($user[email]);
$secureKey = "sadasdsadafdsfasfasdasdasdasd";
$signature = $merchantCode.$merchantRefNum.$customerProfileId.$paymentMethod.$amount.$cardToken.$secureKey;
$signature = hash('sha256', $signature);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://atfawry.com/ECommerceWeb/Fawry/payments/charge',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{ \"merchantCode\":\"$merchantCode\", \"merchantRefNum\":\"$merchantRefNum\", \"customerProfileId\":\"$customerProfileId\", \"customerMobile\":\"$customerMobile\", \"customerEmail\":\"$customerEmail\", \"paymentMethod\":\"$paymentMethod\", \"cardToken\":\"$cardToken\", \"amount\":$amount, \"currencyCode\":\"EGP\", \"description\":\"$description\", \"paymentExpiry\":".strtotime('+3 hour')."077, \"chargeItems\":[ { \"itemId\":\"897fa8e81be26df25db592e81c31c\", \"description\":\"asdasd\", \"price\":$amount, \"quantity\":1 } ], \"signature\":\"$signature\" }",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$status = 1;
$response = json_decode ( $response , true ) ;
$referenceNumber = '<div style="font-size: 18px;line-height: 30px;margin-bottom: 9px;">Please proceed to any Fawry Retail (Kiosk, Supermarket) <br>Ask for Fawry Pay Service and enter you Reference Number: </div><div class="head-span" >'.$response[referenceNumber].'</div>';
$message = "برجاء استخدام رقم السداد $response[referenceNumber] لكود الخدمة 788 لدى فوري لتأكيد الحجز قم $ticket[ID]";
$new_password = encode_password($new_password) ;
send_sms($customerMobile ,$message);
$r = array("Status"=>$status,"Result"=>$referenceNumber,"referenceNumber"=>$response[referenceNumber],"Errors"=>"","NextStep"=>$NextStep);
return $r;
}
Returns 1 on success or 0 for non success
https://developer.fawrystaging.com/docs/server-apis/payment-notifications/get-payment-status-v2
Related
I understand how classes are useful, but don't know what's the proper way to create ones that have prepared statements. I've started making a class and I want a method called isOnline that returns if the url is online.
// This is the code I'm trying to make a class.
global $db;
$stmt = $db->prepare("SELECT url FROM urls WHERE rel=? ORDER BY url");
$stmt->bind_param("i", $_SESSION['admin_id']);
$stmt->execute();
$result = $stmt->get_result();
?>
<?php
while($row = $result->fetch_array())
{
$url = $row['url'];
$site = strtolower($url);
// check if is online
$curl = curl_init();
curl_setopt_array( $curl, array(
CURLOPT_HEADER => true,
CURLOPT_NOBODY => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => 'https://'.$site ) );
$headers = explode( "\n", curl_exec( $curl ) );
$statushttp = $headers[0];
if(!$statushttp == 0){
}else{
$curl = curl_init();
curl_setopt_array( $curl, array(
CURLOPT_HEADER => true,
CURLOPT_NOBODY => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => 'http://'.$site ) );
$headers = explode( "\n", curl_exec( $curl ) );
$statushttp = $headers[0];
}
echo "$url with $statushttp <br>";
// email person here.
}
$stmt->free_result();
Not sure what you are doing to determine if a site is online or not but as far as creating a class to handle this. You should use something like this. If you can explain what you are doing to determine if a site is up or not I can update the class.
class SomeClassNameHere{
//this is the constructor of the class. This is ran when you create it
public SomeClassNameHere(){
}
//this is a public function that you can call
public function isOnline(){
//this is the first step to breaking up this into smaller peaces
//I moved your section of code that gets the urls from the database
//into a function that returns the rows from the database below.
$rows = getUrls();
while($row = $result->fetch_array()){
$site = strtolower($row['url']);
}
// check if is online
$curl = curl_init();
curl_setopt_array( $curl, array(
CURLOPT_HEADER => true,
CURLOPT_NOBODY => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => 'https://'.$site ) );
$headers = explode( "\n", curl_exec( $curl ) );
$statushttp = $headers[0];
if(!$statushttp == 0){
}else{
$curl = curl_init();
curl_setopt_array( $curl, array(
CURLOPT_HEADER => true,
CURLOPT_NOBODY => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => 'http://'.$site ) );
$headers = explode( "\n", curl_exec( $curl ) );
$statushttp = $headers[0];
}
//I wouldn't echo out the results here,
//I would have this function return a true or false and do the display
//somewhere else
//like this
if(statushttp == whateverIsTrue){ //remember to edit this
return true;
}else{
return false;
}
}
//this has the possibility to return more than one row. You will need to
//change your function to handle this.
public function getUrls(){
$stmt = $db->prepare("SELECT url FROM urls WHERE rel=? ORDER BY url");
$stmt->bind_param("i", $_SESSION['admin_id']);
$stmt->execute();
return $stmt->get_result();
}
}
To call this class you would want to do something like this.
//this is creating an instance of the class
$foo= new SomeClassNameHere();
//this will call the isOnline function of that class and return true or false
$siteOnline = $foo->isOnline();
if($siteOnline){
echo "Yeah!";
}else{
echo "Sad:-(";
}
So, I'm trying to make a promotion bot for Roblox using Webservices.
Here is what code I have:
<?php
$group_id = $_GET['groupId'];
$new_role_set_id = $_GET['newRoleSetId'];
$target_user_id = $_GET['targetUserId'];
$login_user = 'username=RPIRankBot&password=NoYou';
$file_path_rs = 'rs.txt';
$file_path_token = 'token.txt';
$current_rs = file_get_contents($file_path_rs);
$current_token = file_get_contents($file_path_token);
function getRS()
{
global $login_user, $file_path_rs;
$get_cookies = curl_init('https://www.roblox.com/newlogin');
curl_setopt_array($get_cookies,
array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => array("Content-Length: " . strlen($login_user)),
//CURLOPT_POSTFIELDS => $login_user
)
);
$rs = (preg_match('/(\.ROBLOSECURITY=.*?);/', curl_exec($get_cookies), $matches) ? $matches[1] : '');
file_put_contents($file_path_rs, $rs, true);
curl_close($get_cookies);
return $rs;
}
function changeRank($rs, $token)
{
global $group_id, $new_role_set_id, $target_user_id, $file_path_token;
$promote_user = curl_init("http://www.roblox.com/groups/api/change-member-rank?groupId=$group_id&newRoleSetId=$new_role_set_id&targetUserId=$target_user_id");
curl_setopt_array($promote_user,
array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => array("Cookie: $rs", "X-CSRF-TOKEN: $token")
)
);
$resp = curl_exec($promote_user);
$resp_header_size = curl_getinfo($promote_user, CURLINFO_HEADER_SIZE);
$resp_header = substr($resp, 0, $resp_header_size);
$resp_body = substr($resp, $resp_header_size);
if (preg_match('/GuestData/', $resp_header)) {
$resp_body = changeRank( getRS(), $token );
} else if (preg_match('/Token Validation Failed/', $resp_header)) {
$new_token = (preg_match('/X-CSRF-TOKEN: (\S+)/', $resp_header, $matches) ? $matches[1] : '');
file_put_contents($file_path_token, $new_token, true);
$resp_body = changeRank( $rs, $new_token );
}
curl_close($promote_user);
return $resp_body;
}
echo changeRank($current_rs, $current_token);
This is giving me the HTTP Error 411. The request must be chunked or have a content length.
I've looked all around about this error, all of the scripts had the stuff to do with MySQL which wasn't what I was looking for. I know you have to supply a content header to the server, but I am confused as to how to do it.
Help would be greatly appreciated.
So problem is I am using inline keyboard in my bot. And when I am trying to push this keyboard it gives me 3-5 callbacks. I don't know where is my mistake.
EDIT
I don't know why but it causes this error when i working with mysqli->fetch_assoc();
There is not full code just peace where I use inline keyboard
if ($callback_data!=Null){
checkJSON(3124,$order_id);
$message_id = $update['callback_query']['message']['message_id'];
$callback_data = json_decode($callback_data,true);
checkJSON(3125,$callback_data["order_id"]);
$order_id = $callback_data["order_id"];
checkJSON(3126,$order_id);
$rs = $mysqli->query("SELECT manager_id FROM orders WHERE id=".$order_id);
$row = $rs->fetch_assoc();
$manager = $row['manager_id'];
if ($manager!=Null){
$rs = $mysqli->query("SELECT telegram_id FROM managers WHERE id=".$manager);
$row = $rs->fetch_assoc();
$manager_telegram_id = $row['telegram_id'];
if ($chatID==$manager_telegram_id){
$callback_data = json_decode($callback_data);
$order_id = $callback_data["order_id"];
$status = $callback_data["status"];
checkJSON(1231234,$callback_data);
if($status == '3'){
editMessage($chatID,$message_id,"Заказ N".$order_id." подтвержден");
}
else{
editMessage($chatID,$message_id,"Заказ N".$order_id." отклонен");
}
$mysqli->query("UPDATE orders SET status=".$status." WHERE id=".$order_id);
}
sendMessage($chatID,$update['callback_query']['message']['message_id']);
editMessage($chatID,$message_id,
"Данный заказ уже в оброботке");
}
else{
$get_manager_query = $mysqli->query("SELECT id FROM managers WHERE telegram_id=".$chatID);
$row = $get_manager_query->fetch_assoc();
$manager = $row['id'];
$data1 = json_encode(array("order_id"=>$order_id,"status"=>3));
$data2 = json_encode(array("order_id"=>$order_id,"status"=>4));
$inline_button1 = array("text"=>"Принять","callback_data"=>$data1);
$inline_button2 = array("text"=>"Отказать","callback_data"=>$data2);
$inline_keyboard = [[$inline_button1,$inline_button2]];
$keyboard=json_encode(array("inline_keyboard"=>$inline_keyboard));
editMessage($chatID,$message_id,
"Вы приняли данный заказ",$keyboard);
$rs = $mysqli->query("UPDATE orders SET status=1, manager_id=".$manager." WHERE id=".$order_id);
}
}
function sendMessage($chatID,$text){
$sendto =API_URL."sendmessage?chat_id=".$chatID."&text=".urlencode($text);
file_get_contents($sendto);
}
function editMessage($chatId, $messageId, $message,$replyMarkup=Null) {
$url = API_URL."editMessageText?chat_id=".$chatId."&message_id=".$messageId.
"&text=".urlencode($message)."&reply_markup=".$replyMarkup;
file_get_contents($url);
}
It's Not your mistake!
Telegram Server is send callbacks for every changes in inline query.
You can setting that to off in BotFatherwith /setinlinefeedback command.
For more information visit this
Set your data by some separator After, get that data by $update['callback_query']['data'] and split by seted separator.
$botToken = "(your token)";
$website = "https://(site_url)/bot" . $botToken;
$order_id = '44423'; //my additional data
$keyboard = ["inline_keyboard" => [
[
[
"text" => "Доставлено",
"callback_data" => "delivered_".$order_id, // set few data by '_' separator Like ("delivered_443423_phone12345667_...)
],
[
"text" => "Затримуюсь",
"callback_data" => "delaying_".$order_id,
]
],
]
];
$params = [
'chat_id' => $chat_id,
'text' => $msg,
'parse_mode' => 'html',
'reply_markup' => json_encode($keyboard),
];
$ch = curl_init($website . '/sendMessage');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
Update processing. For get update you need set webhook on file where bot is placed. https://api.telegram.org/bot(your token)/setWebhook?url=https://example.com/bot_directory/bot.php Site must have SSL (https).
$update = json_decode(file_get_contents('php://input'), TRUE);
$botToken = "(your token)";
$botAPI = "https://api.telegram.org/bot" . $botToken;
$msg = $update['message']['text'];
$user_id = $update['message']['from']['id'];
if (isset($update['callback_query'])) {
$update_multiple = explode('_', $update['callback_query']['data']); //split data
if ($update_multiple[0] == 'delivered') {
$data1 = $update_multiple[1]; // get some data
// ......
}
}
This is an auto promotion system (written in php) for a website, however it seems to give an error that I'm not familiar with. I replaced the website name with 'example' for privacy reasons.
<?php
$group_id = $_GET['groupId'];
$new_role_set_id = $_GET['newRoleSetId'];
$target_user_id = $_GET['targetUserId'];
$login_user = 'username=user&password=pass';
$file_path_rs = 'rs.txt';
$file_path_token = 'token.txt';
$current_rs = file_get_contents($file_path_rs);
$current_token = file_get_contents($file_path_token);
function getRS()
{
global $login_user, $file_path_rs;
$get_cookies = curl_init('https://www.example.com/newlogin');
curl_setopt_array($get_cookies,
array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_POST => true,
// CURLOPT_HTTPHEADER => array("Content-Length: " . strlen($login_user)),
CURLOPT_POSTFIELDS => $login_user
)
);
$rs = (preg_match('/(\.SECURITY=.*?);/', curl_exec($get_cookies), $matches) ? $matches[1] : '');
file_put_contents($file_path_rs, $rs, true);
curl_close($get_cookies);
return $rs;
}
function changeRank($rs, $token)
{
global $group_id, $new_role_set_id, $target_user_id, $file_path_token;
$promote_user = curl_init("http://www.example.com/groups/api/change-member- rank? groupId=$group_id&newRoleSetId=$new_role_set_id&targetUserId=$target_user_id");
curl_setopt_array($promote_user,
array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => array("Cookie: $rs", "X-CSRF-TOKEN: $token")
)
);
$resp = curl_exec($promote_user);
$resp_header_size = curl_getinfo($promote_user, CURLINFO_HEADER_SIZE);
$resp_header = substr($resp, 0, $resp_header_size);
$resp_body = substr($resp, $resp_header_size);
if (preg_match('/GuestData/', $resp_header)) {
$resp_body = changeRank( getRS(), $token );
} else if (preg_match('/Token Validation Failed/', $resp_header)) {
$new_token = (preg_match('/X-CSRF-TOKEN: (\S+)/', $resp_header, $matches) ? $matches[1] : '');
file_put_contents($file_path_token, $new_token, true);
$resp_body = changeRank( $rs, $new_token );
}
curl_close($promote_user);
return $resp_body;
}
echo changeRank($current_rs, $current_token);
When accessing the page I get the page error:
Bad Request - Invalid Content Length
HTTP Error 400. There is an invalid content length or chunk length in the request.
I'm not sure why this is happening, and I've tried just about everything, even switching hosts, but the issue still arises. What can I do to fix this?
I'm currently using a text file which contains a bunch of usernames, instead of the text file I want my script to run through a mysql column instead.
Is the line I'm having trouble with, I'm wondering if I could use the result of mysql query as the variable?
$name = file("/var/www/html/memberlist/memberlist.txt");
Here's my full code if needed
<?php
include 'dbopen.php';
function get_web_page($url) {
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_USERAGENT => "Highscores grabber v1.0",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 10
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
return $content;
}
function getStats($name)
{
$skills = array('Overall', 'Attack', 'Defence', 'Strength', 'Hitpoints', 'Ranged', 'Prayer', 'Magic', 'Cooking', 'Woodcutting', 'Fletching', 'Fishing', 'Firemaking', 'Crafting', 'Smithing', 'Mining', 'Herblore', 'Agility', 'Thieving', 'Slayer', 'Farming', 'Runecrafting', 'Hunter', 'Construction');
$hs = get_web_page("http://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player=$name");
$out = Array();
$stats = explode("\n", $hs);
for($i = 0; $i<count($skills);$i++) {
$stat = explode(',', $stats[$i]);
$out[$skills[$i]] = Array();
$out[$skills[$i]]['rank'] = $stat[0];
$out[$skills[$i]]['level'] = $stat[1];
$out[$skills[$i]]['xp'] = $stat[2];
}
return $out;
}
$name = file("/var/www/html/memberlist/memberlist.txt");
for($j=0;isset($name[$j]);$j++)
{
$out[$j] = getStats($name[$j]);
}
if (!empty($out))
{
for($k=0;isset($out[$k]);$k++)
{
$hitpointsexp = $out[$k]['Hitpoints']['xp'];
$rangedexp = $out[$k]['Ranged']['xp'];
$magicexp = $out[$k]['Magic']['xp'];
$rsn = trim($name[$k]);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
$query= ("
INSERT INTO
gains (runescape_name, hitpoints_starting_exp, magic_starting_exp, range_starting_exp)
VALUES
('" . $rsn . "', '" . $hitpointsexp . "', '" . $magicexp . "', '" . $rangedexp . "')
ON DUPLICATE KEY UPDATE
hitpoints_starting_exp='$hitpointsexp', magic_starting_exp='$magicexp', range_starting_exp='$rangedexp'
");
$result = mysql_query($query);
}
}
?>