vTiger API Rest - create new lead - api

I downloaded the vTiger classes from here:
https://blog.crm-now.de/2018/02/26/new-api-description-and-new-example-code-for-crm-rest-operations/?lang=en
But when I try to create a new lead it gives me this in response:
"create failed: lastname does not have a value"
Below is my code:
$params= array(
'email' => 'myemail#libe.test',
'firstname' => 'my_name',
'lastname ' => 'my_surname',
'assigned_user_id' => 'my_user_id'
);
$url = "url_my_site.com/webservice.php";
$wsC = new WS_Curl_Class($url, 'my_username', 'secret_key');
if (!$wsC->login()) {
echo "error";
}
$result = $wsC->operation("create", array("elementType" => "Leads", "element" => json_encode($params)), "POST");
if ($wsC->errorMsg) {
// ERROR handling if describe operation was not successful
echo $wsC->errorMsg;
}
I don't understand why that error returns to me. Can someone help me?
Thank you

Probably because there is a space at the end of 'lastname'

Related

Jazzcash Payment Integration V2.0

I'm working on Rest based api version 2.0. It constantly giving me error
Please provide a valid value for pp_SecureHash
It is working fine in case of "Page redirection" so hash key generation method is correct and issue is with parameters (maybe some missing/extra/wrong data). what I'm doing wrong?
{ “uri”: “https://sandbox.jazzcash.com.pk/ApplicationAPI/API/2.0/Purchase/DoMWalletTransaction”, “method”: “POST”, “body”: “pp_Amount=1100&pp_BillReference=billRef&pp_CNIC=345678&pp_Description=jazzcash&pp_Language=EN&pp_MerchantID=xyz&pp_MobileNumber=03123456789&pp_Password=xyz&pp_ReturnURL=https://sandbox.jazzcash.com.pk/ApplicationAPI/API/2.0/Purchase/DoMWalletTransaction&pp_SecureHash=BC3BABD0481A2FA756F2E16CE15FC6F8029D40E23B974065668CCEAC300B80AE&pp_TxnCurrency=PKR&pp_TxnDateTime=20220406132730&pp_TxnExpiryDateTime=20220406142730&pp_TxnRefNo=T20220406142730&ppmpf_1=1&ppmpf_2=2&ppmpf_3=3&ppmpf_4=4&ppmpf_5=5” }
Your pp_SecureHash value needs to be HmacSHA256 encoded.
var sHash = HMACSHA256Encode(hash, jazz.salt);
You can refer this code sample to achieve your requirement.
C#: Generate pp_SecureHash ()
Please check your merchant Id, password and salt key. It's also through the error if your any of info is wrong.
Read the doc carefully. if add version 1.2 and in the doc it's mention 1.1 it's not going to work and through the same error. provide valid secure
If you are sure that other info is correct so you can generate secure ssh by using below method
$post_data = array(
"pp_Version" => Config::get('constants.jazzcash.VERSION'),
"pp_TxnType" => "MWALLET",
"pp_Language" => Config::get('constants.jazzcash.LANGUAGE'),
"pp_MerchantID" => Config::get('constants.jazzcash.MERCHANT_ID'),
"pp_SubMerchantID" => "",
"pp_Password" => Config::get('constants.jazzcash.PASSWORD'),
"pp_BankID" => "TBANK",
"pp_ProductID" => "RETL",
"pp_TxnRefNo" => $pp_TxnRefNo,
"pp_Amount" => $pp_Amount,
"pp_TxnCurrency" => Config::get('constants.jazzcash.CURRENCY_CODE'),
"pp_TxnDateTime" => $pp_TxnDateTime,
"pp_BillReference" => "billRef",
"pp_Description" => "Description of transaction",
"pp_TxnExpiryDateTime" => $pp_TxnExpiryDateTime,
"pp_ReturnURL" => Config::get('constants.jazzcash.RETURN_URL'),
"pp_SecureHash" => "",
"ppmpf_1" => "1",
"ppmpf_2" => "2",
"ppmpf_3" => "3",
"ppmpf_4" => "4",
"ppmpf_5" => "5",
);
private function get_SecureHash($data_array)
{
ksort($data_array);
$str = '';
foreach($data_array as $key => $value){
if(!empty($value)){
$str = $str . '&' . $value;
}
}
$str = Config::get('constants.jazzcash.INTEGERITY_SALT').$str;
$pp_SecureHash = hash_hmac('sha256', $str, Config::get('constants.jazzcash.INTEGERITY_SALT'));
//echo '<pre>';
//print_r($data_array);
//echo '</pre>';
return $pp_SecureHash;
}
And Last by calling this method you can get the secure key
$pp_SecureHash = $this->get_SecureHash($post_data);

Lumen Google reCAPTCHA validation

I already seen some tuts and example about it and I have implemented it somehow.
Method in controller looks like this:
The logic used is just php and I would like to use more a lumen/laravel logic and not just simple vanilla php. Also I have tried and did not worked anhskohbo / no-captcha
public function create(Request $request)
{
try {
$this->validate($request, [
'reference' => 'required|string',
'first_name' => 'required|string|max:50',
'last_name' => 'required|string|max:50',
'birthdate' => 'required|before:today',
'gender' => 'required|string',
'email' => 'required|email|unique:candidates',
'g-recaptcha-response' => 'required',
]);
//Google recaptcha validation
if ($request->has('g-recaptcha-response')) {
$secretAPIkey = env("RECAPTCHA_KEY");
// reCAPTCHA response verification
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secretAPIkey.'&response='.$request->input('captcha-response'));
$response = json_decode($verifyResponse);
if ($response->success) {
//Form submission
//Saving data from request in candidates
$candidate = Candidate::create($request->except('cv_path'));
$response = array(
"status" => "alert-success",
"message" => "Your mail have been sent."
);
} else {
$response = array(
"status" => "alert-danger",
"message" => "Robot verification failed, please try again."
);
}
}
} catch(Exception $e) {
return response()->json($e->getMessage());
}
return response()->json(['id' => $candidate->id, $response]);
}
Okey. Google has an package for this:reCAPTCHA PHP client library
just: composer require google/recaptcha "^1.2"
and in your method inside controller:
$recaptcha = new \ReCaptcha\ReCaptcha(config('app.captcha.secret_key'));
$response = $recaptcha->verify($request->input('g-recaptcha-response'), $_SERVER['REMOTE_ADDR']);
if ($response->isSuccess()) {
//Your logic goes here
} else {
$errors = $response->getErrorCodes();
}
config('app.captcha.site_key') means that I got the key from from config/app.php and there from .env file.
If you have not config folder, you should create it, also create app.php file same as in laravel.

Update API in laravel 5.7

I have create API of update my project details, I test it in POSTMAN app it shows the success message but there no effect in the database.
Here are my code:
ProjectsController.php
public function UpdateProject($id)
{
$data = Input::all();
$q = Project::where('id',$id)->update($data);
return response()->json([
'code' => SUCCESS,
'message' => 'Project data update successfully'
]);
}
api.php
Route::post('UpdateProject/{id}','ProjectsController#UpdateProject');
Postman - see image.
output in postman:
{
"code": "200",
"message": "Project data update successfylly"
}
Can anyone help me out?
Thank you
I think you need to check all input details closely , it also comes with token when you submit the form so you need to save all details except token
Change this
$data = Input::all();
to this
$data = Input::except('_token');
I hope this resolves the issue.
in your model add fillable :
protected $fillable = ['name', 'project_group_id','number','ROM','address','city','state','zip','start_date','end_date','duration','description','timeline_type','project_type_id','project_category_id','office_id'];
You have forgotten to run the ->save() method after updating the data:
public function UpdateProject($id)
{
$data = Input::all();
$q = Project::find($id)
$q = $q->fill($data);
$q->save();
return response()->json([
'code' => SUCCESS,
'message' => 'Project data update successfully'
]);
}
You can use this method it will reduce your code
Route (api)
Route::post('UpdateProject/{project}','ProjectsController#UpdateProject');
ProjectsController.php
public function UpdateProject(Request $request, Project $project)
{
$data = $request->all();
$project->update($data);
return response()->json([
'code' => SUCCESS,
'message' => 'Project data update successfully'
]);
}

SQL query for restoring the latest revision on all posts in wordpress

My wordpress site recently got attacked and all the posts seem to have been updated with a blank version. So I want to restore the most latest revision for all posts. (NB: In wordpress the current published version is also stored in the database as a revision. So I think I would need to restore the 2nd revision (when arranged in the descending order))
What would be the SQL query for that?
Any help would be greatly appreciated!
So, all you need is in your database (luckily). The posts themselves can be retrieved like this
global $wpdb;
$query = "SELECT * FROM wp_posts WHERE post_type = 'revision'";
$query_results = $wpdb->get_results($query, ARRAY_A);
foreach ($query_results as $q_key => $q_value) {
$post = array(
'post_content' => $q_value['post_content']
'post_name' => $q_value['post_name']
'post_title' => $q_value['post_title']
'post_status' => $q_value['post_status']
'post_type' => 'post'
'post_author' => $q_value['post_author']
'ping_status' => $q_value['ping_status']
'post_parent' => $q_value['post_parent']
'menu_order' => $q_value['menu_order']
'to_ping' => $q_value['to_ping']
'pinged' => $q_value['pinged']
'post_password' => $q_value['post_password']
'guid' => $q_value['guid']
'post_content_filtered' => $q_value['post_content_filtered']
'post_excerpt' => $q_value['post_excerpt']
'post_date' => $q_value['post_date']
'post_date_gmt' => $q_value['post_date_gmt']
'comment_status' => $q_value['comment_status']
);
wp_insert_post( $post );
}
Read about wp_insert_post() here.
Now, depending on how much posts you have, this can either take a while, or it can break. If it breaks, then you'll need to wrap this in a function that you'll call with AJAX. Something like this should do the trick:
add_action( 'wp_ajax_insert_posts', 'database_ajax_insert_posts' );
add_action( 'wp_ajax_nopriv_insert_posts', 'database_ajax_insert_posts' );
function database_ajax_insert_posts(){
global $wpdb;
$offset = $_POST['offset'];
$query = "SELECT * FROM wp_posts WHERE post_type = 'revision' LIMIT $offset, 1";
$query_results = $wpdb->get_results($query, ARRAY_A);
foreach ($query_results as $q_key => $q_value) {
$post = array(
'post_content' => $q_value['post_content']
'post_name' => $q_value['post_name']
'post_title' => $q_value['post_title']
'post_status' => $q_value['post_status']
'post_type' => 'post'
'post_author' => $q_value['post_author']
'ping_status' => $q_value['ping_status']
'post_parent' => $q_value['post_parent']
'menu_order' => $q_value['menu_order']
'to_ping' => $q_value['to_ping']
'pinged' => $q_value['pinged']
'post_password' => $q_value['post_password']
'guid' => $q_value['guid']
'post_content_filtered' => $q_value['post_content_filtered']
'post_excerpt' => $q_value['post_excerpt']
'post_date' => $q_value['post_date']
'post_date_gmt' => $q_value['post_date_gmt']
'comment_status' => $q_value['comment_status']
);
wp_insert_post( $post );
}
}
And AJAX
jQuery(document).ready(function($) {
"use strict";
var offset = 1;
function ajax_call(){
$.ajax({
type: "POST",
url: ajaxurl,
data: {
'action': 'insert_posts',
'offset': offset,
},
success: function(response) {
if (offset < 100){ //Number of posts go here
offset += 1;
ajax_call();
} else{
$('#wpbody-content').append('<p>All done!</p>' );
}
},
error : function (jqXHR, textStatus, errorThrown) {
$('#wpbody-content').html(jqXHR + ' :: ' + textStatus + ' :: ' + errorThrown);
}
});
}
ajax_call();
});
Also be sure to localize your AJAX url with the handle of the script in which you'll put your AJAX call.
wp_localize_script('your_handle', 'db_from_WP', array(
'ajaxurl' => admin_url( 'admin-ajax.php' )
));
Taxonomy should be in the database, and you could get them in a similar fashion.
This is not foolproof, but should work. The thing that I can see that could be a bit problematic is getting the right taxonomy for the post, but that is doable, although with a bit fiddling around.
Hope this helps.

Magento API - Get Product Names from specific Category

I'm working on the Magento API and I got some questions...
I try to get all Product Names from a specific category at Magento.
here's my code for that:
<?php
$host = "www.host.tld/index.php";
$client = new SoapClient("http://".$host."/api/soap/?wsdl");
$apiuser= "user"; //webservice user login
$apikey = "pass"; //webservice user pass
try {
// Login
$sess_id= $client->login($apiuser, $apikey);
// Getting all products from category
$filters = array( 'category_ids' => array('107') );
$productList = $client->call($sess_id, 'catalog_category.assignedProducts', $filters);
//iterate and get all the product_id's and put it into one array
foreach($productList as $products => $values){
if (isset($values['product_id']) || array_key_exists('product_id', $values)) {
$product_ids[] = $values['sku'];
}
}
//Get product details from product_id
foreach($product_ids as $key => $values) {
$details = $client->call( $sess_id, 'product.info', array($values));
#echo $details['name'];
}
}
catch (Exception $e) { //while an error has occured
echo "==> Error: ".$e->getMessage();
exit();
}
?>
The main problem I have is, that the performance I get is not the best. For each "product name" I make one api-call for getting all products. Every time for each product. That's probably not so clever.
What can I optimize. Am I missing something?
I can imagine, that if I want to get the details from more than one category, my server will crash ;). We have about 1000 products in our shop.
Thanks for the help.
Try
$proxy = new SoapClient('http://magentohost/api/soap/?wsdl');
$sessionId = $proxy->login('apiUser', 'apiKey');
$filters = array(
'id' => array('in' => array(<product_id_1>, <product_id_2>, ...))
);
$products = $proxy->call($sessionId, 'product.list', array($filters));
If you need some extended info about product - you should alter magento code, see this my answer on the similar questionL Magento API v1- List prices for all products in one call
You can use to catalog_category.assignedProducts
Retrieve the list of products assigned to a required category.
try this if you are using SOAP V1:
$client = new SoapClient('http://magentohost/api/soap/?wsdl');
// If somestuff requires api authentification,
// then get a session token
$session = $client->login('apiUser', 'apiKey');
$result = $client->call($session, 'catalog_category.assignedProducts', '4');
var_dump($result);
// If you don't need the session anymore
//$client->endSession($session);
if you are using SOAP V2:
$proxy = new SoapClient('http://magentohost/api/v2_soap/?wsdl'); // TODO : change url
$sessionId = $proxy->login('apiUser', 'apiKey'); // TODO : change login and pwd if necessary
$result = $proxy->catalogCategoryAssignedProducts($sessionId, '4');
var_dump($result);
and the respond would be like this:
array
0 =>
array
'product_id' => string '1' (length=1)
'type' => string 'simple' (length=6)
'set' => string '4' (length=1)
'sku' => string 'n2610' (length=5)
'position' => string '1' (length=1)
1 =>
array
'product_id' => string '2' (length=1)
'type' => string 'simple' (length=6)
'set' => string '4' (length=1)
'sku' => string 'b8100' (length=5)
'position' => string '1' (length=1)