I am trying to add tracking number and order status in my sandbox account through tracking API but getting "Authorization failed due to insufficient permissions" error. Anyone have any idea why this error happens? Below are my codes and response from paypal -
API request
$clientId = 'my client id';
$secretKey = 'my secret key';
$token = $this->getAccessToken($clientId, $secretKey);
$transaction = $this->_objectManager->create('\Magento\Sales\Api\Data\TransactionSearchResultInterfaceFactory')->create()->addOrderIdFilter($currentOrder->getId())->getFirstItem();
$transactionId = $transaction->getData('txn_id');
$postfields = [
'trackers' =>
[
0 =>
[
'transaction_id' => $transactionId,
'tracking_number' => $currentOrderTrackingNumber,
'status' => 'SHIPPED',
'carrier' => 'OTHER',
'carrier_name_other' => 'SCS Logistics'
]
]
];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api-m.sandbox.paypal.com/v1/shipping/trackers-batch",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($postfields),
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Authorization: Bearer " . $token
),
));
$response = curl_exec($curl);
curl_close($curl);
$response = json_decode($response, true);
Response from paypal
Array
(
[tracker_identifiers] => Array
(
)
[errors] => Array
(
[0] => Array
(
[name] => NOT_AUTHORIZED
[message] => Authorization failed due to insufficient permissions
[details] => Array
(
[0] => Array
(
[field] => /trackers/1/transaction_id
[value] => 42L93730906985049
[location] => body
[issue] => USER_NOT_AUTHORIZED
[description] => You are not authorized to add or modify tracking number for this transaction
)
)
[links] => Array
(
)
)
)
[links] => Array
(
[0] => Array
(
[href] => https://api-m.sandbox.paypal.com/v1/shipping/trackers-batch
[rel] => self
[method] => POST
[encType] => application/json
)
)
)
I already contacted support and they added required permission but it's not working. Please if anyone have any idea about it help me. Thanks in advance.
Related
I use this library:
https://github.com/php-curl-class/php-curl-class
Base on documentation I write function for post order:
public function create_order()
{
$curl = new Curl();
$curl->setHeader('CustomerId', 'xxxxxxx');
$curl->setHeader('UserName', 'xxxxxxxxxx');
$curl->setHeader('ActionApiKey', 'xxxxxxxxxxxxxxx');
$order = array(
"createEmpty" => false,
"header" => array(
"comment" => "string",
"country" => "Polska",
"currency" => "PLN",
"isFileRequired" => true,
"actionCustomerId" => "81790",
"payer" => "EndCustomer",
"paymentType" => "CashOnDelivery",
"partnerOrderId" => "81790",
"deliveryAddresType" => "EndCustomer",
"cashOnDeliveryType" => "FullRate",
"cashOnDelivery" => 155,
"deliveryCompanyName" => "TEST API ORDER DO NOT SHIP",
"deliveryCity" => "Poznań",
"deliveryPhone" => "xxxxxxx",
"deliveryStreet" => "xxxxxxxxxx",
"deliveryZipCode" => "xxxxxxx"
),
"items" => array(
array(
"actionProductId" => "MULLOGKAM0087",
"quantity" => 1,
"price" => 122,
"backOrderType" => "BackOrder"
)
)
);
$curl->setHeader('Content-Type', 'application/json');
$curl->post('xxxxxxxxxxxxxxxxxxxxx/v2/Order', json_encode($order));
if ($curl->error) {
echo 'Error: ' . $curl->error_code . ': ' . $curl->error_message;
} else {
echo 'Respone: ' . $curl->response;
}
}
After run controller, I get white page (I not get any error) but order not created. I test in Insomia API client and with this headers and data order created sucess.
Im not sure I correct post headers also with order data ?
I created App in Shopware and able to get apiKey, secretKey, shopUrl and shopId. I want to get all my products using these credentials.
This is my code
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "http://shopware.example.com/store-api/product",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => [
"Accept: application/json",
"Content-Type: application/json",
"sw-access-key: SW234C1LVA010CWHDD34WK1TMW"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$data = json_decode($response, true);
print_r($data);
}
and I am getting error like this
Array
(
[errors] => Array
(
[0] => Array
(
[status] => 412
[code] => FRAMEWORK__ROUTING_SALES_CHANNEL_NOT_FOUND
[title] => Precondition Failed
[detail] => No matching sales channel found.
[meta] => Array
(
[parameters] => Array
(
)
)
)
)
)
When I using API key from settings page API I am able to get products.
But how to use App credentials (App key, secret key, shop URL) and get products
You should have gotten the API key during App registration:
https://developer.shopware.com/docs/guides/plugins/apps/app-base-guide#confirmation-request
Did you try that one? If it does not work for the store API you might want to use the admin API.
i'm new, and i've a problem with webservice.
when i try to retrieve customers filtering by date_add i get this error:
<message><![CDATA[This filter does not exist. Did you mean: "deleted"? The full list is: "id", "id_default_group", "id_lang", "newsletter_date_add", "ip_registration_newsletter", "last_passwd_gen", "secure_key", "deleted", "passwd", "lastname", "firstname", "email", "id_gender", "birthday", "newsletter", "optin", "website", "company", "siret", "ape", "outstanding_allow_amount", "show_public_prices", "id_risk", "max_payment_days", "active", "note", "is_guest", "id_shop", "id_shop_group"]]></message>
this is my code:
$yesterday = date("Y-m-d H:i:s", time() - 60 * 60 * 24);
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
// Qui imposto le opzion dell'array per il Webservice: in questo caso i clienti e setto che il formato è json
$opt = array( 'resource' => 'customers',
'filter[date_add]' => '['.$yesterday.']&date=1',
'display' => 'full',
'output_format' => 'JSON'
);
// faccio la chiamata
$xml = $webService->get($opt);
I'm stuck, I do not know what to do !!! any help is appreciated
Looking at the Customer class webservice parameters, there is no date_add:
protected $webserviceParameters = array(
'fields' => array(
'id_default_group' => array('xlink_resource' => 'groups'),
'id_lang' => array('xlink_resource' => 'languages'),
'newsletter_date_add' => array(),
'ip_registration_newsletter' => array(),
'last_passwd_gen' => array('setter' => null),
'secure_key' => array('setter' => null),
'deleted' => array(),
'passwd' => array('setter' => 'setWsPasswd'),
),
'associations' => array(
'groups' => array('resource' => 'group'),
)
);
If you have access to the server installation, you could add the date_add field to the webserviceParameters.
Or the function getWebserviceObjectList to change the behavour.
If you don't have access to the server installation, you can use the sort to get the latest customers until you reach the one you already have. You can use with limit to use in a foreach or while loop.
EDIT After some testing and inspecting what was going on:
First, it's we don't need to add the field date_add to the $webserviceParameters.
The request should be:
$opt = array( 'resource' => 'customers',
'filter[date_add]' => '['.$begin.','.$finish.']',
'date' => 1,
'display' => 'full',
'output_format' => 'JSON'
);
If you only add one date to the filter it will try to match only that date.
Then you need to change the PSWebServiceLibrary.php in line 284 and add the field 'date':
$params = array('filter', 'display', 'sort', 'limit', 'id_shop', 'id_group_shop', 'date');
Now it should work. I tested myself.
EDIT 2:
This is my full test, after changing the the line i mentioned in PSWebserviceLibrary.php, this retrieves my customers with date_add in the interval date.
<?php
require_once( 'PSWebServiceLibrary.php' );
echo 'starting<br />'.PHP_EOL;
try {
// creating web service access
$webService = new PrestaShopWebservice(MY_BASE_URL, WS_KEY, true);
$yesterday = date("Y-m-d", time() - 60 * 60 * 24);
$begin = date("Y-m-d", time() - 60 * 60 * 24 * 30);
$opt = array( 'resource' => 'customers',
'filter[date_add]' => '['.$begin.','.$yesterday.']',
'date' => 1,
'display' => 'full',
'output_format' => 'JSON'
);
//Retrieving the XML data
$xml = $webService->get($opt);
}
catch (PrestaShopWebserviceException $ex) {
// Shows a message related to the error
echo 'Other error: <br />' . $ex->getMessage();
}
?>
first of all: thanks
I modified as your indication, but the result does not change, still gives me the same error.
protected $webserviceParameters = array(
'fields' => array(
'id_default_group' => array('xlink_resource' => 'groups'),
'id_lang' => array('xlink_resource' => 'languages'),
'newsletter_date_add' => array(),
'ip_registration_newsletter' => array(),
'last_passwd_gen' => array('setter' => null),
'secure_key' => array('setter' => null),
'deleted' => array(),
'passwd' => array('setter' => 'setWsPasswd'),
/** inizio modifica aggiunta per il webservice */
'date_add' => array(),
/** fine della modifica */
),
'associations' => array(
'groups' => array('resource' => 'group'),
)
);
I can not understand
I'm trying to query jira for data with folowing jql:
$jql_options = array(
"jql" => $jql,
"startAt" => 0,
"maxResults" => 1,
"validateQuery" => false,
"fields" => array(
"summary",
"status",
"assignee"
),
"expand" => "changelog"
);
And i get this error:
string(282) "{"errorMessages":["Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token\n at [Source: org.apache.catalina.connector.CoyoteInputStream#464d8928; line: 1, column: 2] (through reference chain: com.atlassian.jira.rest.v2.search.SearchRequestBean[\"expand\"])"]}"
I understand it's an error with "expand" => "changelog" but on their site they present an requeste representation like mine.
Can u give me some advice? :)
It seems i was reading the rest api documentation wrong.
"expand" option was available only in get request.
I appended jql data in url + "&expand=changelog" and other options i send them in post!
$jql_options = array(
"startAt" => 0,
"maxResults" => 1,
"validateQuery" => false,
"fields" => array(
"key"
)
);
$data = json_encode($jql_options);
$options = array(
CURLOPT_URL => $this->jiraURL . "/rest/api/latest/search?jql=" .urlencode($jql) . "&expand=changelog",
CURLOPT_HTTPHEADER => array(
'Authorization: Basic ' . base64_encode($this->username.":".$this->password),
'Content-type: application/json') ,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $data
);
I am using a API provided by this website
http://pnrapi.alagu.net/
By using this API, we can get PNR status of our indian railways.
I am using CURL to make a call and get the page content which is something like this, in an array format:
Array ( [url] => http://pnrapi.alagu.net/api/v1.0/pnr/4563869832 [content_type] => application/json;charset=utf-8 [http_code] => 200 [header_size] => 185 [request_size] => 130 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 2.906 [namelookup_time] => 0 [connect_time] => 0.312 [pretransfer_time] => 0.312 [size_upload] => 0 [size_download] => 548 [speed_download] => 188 [speed_upload] => 0 [download_content_length] => 548 [upload_content_length] => 0 [starttransfer_time] => 2.906 [redirect_time] => 0 [certinfo] => Array ( ) [primary_ip] => 50.57.204.234 [primary_port] => 80 [local_ip] => 192.168.1.10 [local_port] => 60105 [redirect_url] => [errno] => 0 [errmsg] => [content] => {"status":"OK","data":{"train_number":"16178","chart_prepared":false,"pnr_number":"4563869832","train_name":"ROCKFORT EXPRES","travel_date":{"timestamp":1369506600,"date":"26-5-2013"},"from":{"code":"TPJ","name":"TIRUCHIRAPPALLI JUNCTION","time":"22:20"},"to":{"code":"MS","name":"CHENNAI EGMORE","time":"05:15"},"alight":{"code":"MS","name":"CHENNAI EGMORE","time":"05:15"},"board":{"code":"TPJ","name":"TIRUCHIRAPPALLI JUNCTION","time":"22:20","timestamp":1369587000},"class":"2A","passenger":[{"seat_number":"W/L 39,RLGN","status":"W/L 27"}]}} )
but when I go to the URL http://pnrapi.alagu.net/api/v1.0/pnr/4563869832 , it gives me output as shown below:
{"status":"OK","data":{"train_number":"16178","chart_prepared":false,"pnr_number":"4563869832","train_name":"ROCKFORT EXPRES","travel_date":{"timestamp":1369506600,"date":"26-5-2013"},"from":{"code":"TPJ","name":"TIRUCHIRAPPALLI JUNCTION","time":"22:20"},"to":{"code":"MS","name":"CHENNAI EGMORE","time":"05:15"},"alight":{"code":"MS","name":"CHENNAI EGMORE","time":"05:15"},"board":{"code":"TPJ","name":"TIRUCHIRAPPALLI JUNCTION","time":"22:20","timestamp":1369587000},"class":"2A","passenger":[{"seat_number":"W/L 39,RLGN","status":"W/L 27"}]}}
Now, it seems that output on my web page with curl have got some extra text which is in the start as you can see both the outputs above.
Well, my question is, how can I get the values from above array.
I am talking about the array output which I'm getting on my page using CURL, which looks like this:
Array (
[url] => http://pnrapi.alagu.net/api/v1.0/pnr/4563869832
[content_type] => application/json;charset=utf-8
[http_code] => 200
[header_size] => 185
[request_size] => 130
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 2.906
[namelookup_time] => 0
[connect_time] => 0.312
[pretransfer_time] => 0.312
[size_upload] => 0
[size_download] => 548
[speed_download] => 188
[speed_upload] => 0
[download_content_length] => 548
[upload_content_length] => 0
[starttransfer_time] => 2.906
[redirect_time] => 0
[certinfo] => Array ( )
[primary_ip] => 50.57.204.234
[primary_port] => 80
[local_ip] => 192.168.1.10
[local_port] => 60105
[redirect_url] =>
[errno] => 0
[errmsg] => [content] => {"status":"OK","data":{"train_number":"16178","chart_prepared":false,"pnr_number":"4563869832","train_name":"ROCKFORT EXPRES","travel_date":{"timestamp":1369506600,"date":"26-5-2013"},"from":{"code":"TPJ","name":"TIRUCHIRAPPALLI JUNCTION","time":"22:20"},"to":{"code":"MS","name":"CHENNAI EGMORE","time":"05:15"},"alight":{"code":"MS","name":"CHENNAI EGMORE","time":"05:15"},"board":{"code":"TPJ","name":"TIRUCHIRAPPALLI JUNCTION","time":"22:20","timestamp":1369587000},"class":"2A","passenger":[{"seat_number":"W/L 39,RLGN","status":"W/L 27"}]}} )
Code in my PHP page is:
<?php
function get_web_page( $url )
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$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 );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
$pnr = get_web_page('http://pnrapi.alagu.net/api/v1.0/pnr/4563869832');
echo "<code>";
print_r($pnr);
echo "</code>";
?>
I only need the values under "content" which are train number, train name, travel date etc.
So, what would be best way to extract this information into each variable?
Like I want it like this:
$train_no = [some code];
$train_name = [some_code];
and so on...
Thanks in advance.
I tried this:
echo $pnr['content'];
and the output I got is:
{"status":"OK",
"data":"train_number":"16178",
"chart_prepared":false,
"pnr_number":"4563869832",
"train_name":"ROCKFORT EXPRES",
"travel_date":{"timestamp":1369506600,"date":"26-5-2013"},
"from":{"code":"TPJ","name":"TIRUCHIRAPPALLI JUNCTION","time":"22:20"},
"to":{"code":"MS","name":"CHENNAI EGMORE","time":"05:15"},
"alight":{"code":"MS","name":"CHENNAI EGMORE","time":"05:15"},
"board":{"code":"TPJ","name":"TIRUCHIRAPPALLI JUNCTION","time":"22:20","timestamp":1369587000},
"class":"2A","passenger":[{"seat_number":"W/L 39,RLGN","status":"W/L 27"}]}}
Now can any one give me an idea about how can I fetch unique values from above array?
I'm not sure where the JSON string is. But let's say it's the $pnr variable.
$json = json_decode($pnr, true);
$train_no = $json["data"]["train_number"];
$train_name = $json["data"]["train_name"];
Updated:
If you don't need all the other things you can do something like the following:
$npr = file_get_contents(url);
and then run the code above.
You're looking through the header, where you should be looking at the content. Return $content instead in your function and then you can parse out the response:
function get_web_page( $url ) {
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = json_decode( curl_exec( $ch ) );
curl_close( $ch );
return array(
'train_no' => $content->data->train_number,
'train_name' => $content->data->train_name,
);
}
$pnr = get_web_page('http://pnrapi.alagu.net/api/v1.0/pnr/4563869832');
echo "<pre>" . print_r($pnr, true) . "</pre>";