I need to create a SKU via stripe API.
The problem is in inventory field.
Stripe api response is:
'error' => [
'message' => 'Invalid hash',
'param' => 'inventory',
'type' => 'invalid_request_error'
]
My php code is:
$endPoint = 'https://api.stripe.com/v1/skus';
$APIKEY_TEST = 'my_api_key';
$headers = array('Authorization: Bearer '.$APIKEY_TEST);
$sku = [
'active' => 'true',
'inventory' => ['quantity' => 10000000 ,'type' => 'infinite', 'value' => null],
"currency" => "eur",
"price" => $price,
"product" => $stripe_product_id
];
$array_string ='';
foreach($sku as $key => $value) {
$array_string .= $key.'='.$value.'&';
}
rtrim($array_string, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $endPoint);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $array_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
curl_close($ch);
In stripe api docs inventory is hash type field.
I have tried json_encode() without luck.
Maybe the problem is in sending an array instead of a hash.
In $sku array, inventory field is also an nested associative array.
Maybe the problem resides there as well.
Is there a way to send CURLOPT_POSTFIELDS containing inventory so that stripe accepts it?
EDIT:
In Stripe dashboard i can see my request:
{
"active": "true",
"inventory": "Array",
"currency": "eur",
"price": "3",
"product": "prod_F6ipvfYFvOxxQq"
}
Inventory field has no data, but instead "Array".
After trying a lot of possible solutions, i found the answer:
$post_array = http_build_query($sku);
And know stripe accepts the $sku array with nested inventory array.
It worth notice that stripe does not accept JSON in requests.
The request has to be url encoded.
Related
I'm trying to send a post request with this payload:
$request_content = [
"data" => [
[
"sku" => "0987",
"price" => $price,
"category" => "moveis",
"brand" => "bartira",
"zip_code" => "07400000",
"affiliate" => "google-shopping"
]
]
];
Since it's a post i set the CURLOPT_POST to true;
$encoded_request = json_encode($request_content);
$ch = curl_init("https://my-service/endpoint/");
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Token my-token"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded_request);
The $encoded_request content shown in print_r is:
{
"data": [
{
"sku": "0987",
"price": "5.99",
"category": "moveis",
"brand": "bartira",
"zip_code": "07400000",
"affiliate": "google-shopping"
}
]
}
If i use this content on the Postman i get the right response from the service that i'm requesting, but on my code i got the error;
{"data":["This field is required."]}
Which configuration i'm missing on curl_ to format the payload correctly?
You can try to set CURLOPT_HTTPHEADER and change your variable $request_content, something like this:
//set your data
$request_content = [
"data" => [
"sku" => "0987",
"price" => $price,
"category" => "moveis",
"brand" => "bartira",
"zip_code" => "07400000",
"affiliate" => "google-shopping"
]
];
$encoded_request = json_encode($request_content);
$ch = curl_init("https://my-service/endpoint/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded_request);
// Set HTTP Header for POST request
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Token my-token',
'Content-Type: application/json',
'Content-Length: ' . strlen($encoded_request)]
);
I am trying to create a Sales Invoice through Sage Accounting API calls (its documentation can be found here: https://developer.sage.com/api/accounting/api/)
To make my code clearer I have created a class that helps me make those calls accordingly.
Here is the method I use to make those calls:
public function postRequest()
{
$url = $this->baseEndpoint . $this->request;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (isset($this->params)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->params);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer $this->token",
"Host: api.accounting.sage.com",
"Content-Type: application/json"
));
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
return $response;
}
How I call this method:
$params = array(
"sales_invoice" => array(
"contact_id" => "485fdfe0be154f9c9af44351de16e5be",
"date" => "2019-06-13",
"invoice_lines" => array(
array(
"description" => "description",
"ledger_account_id" => "f04157c90ff0496ab3a22f2558e46010",
"unit_price" => 10 ,
"quantity" => 1,
"tax_rate_id" => "ES_RE_STANDARD",
"tax_rate" => 0.1
)
)
)
);
$params = json_encode($params);
$request = "v3.1/sales_invoices";
$sageRequest = new SageRequest($token, $request, $params);
$sageRequest->postRequest();
According to the API documentation, that should work, but still I get this error:
[$severity] => error
[$dataCode] => UnexpectedError
[$message] => An unexpected error occurred.
[$source] =>
If there is anyone who has some experience with the Sage Accounting API, I would be more than grateful to know what I have done wrong.
This example works for me on a Spanish business:
{
"sales_invoice": {
"contact_id": "22b609fba11642238f2ecd0f5fe3e0b5",
"date": "2019-06-12",
"invoice_lines": [
{
"description": "Description",
"ledger_account_id": "829739738de811e996c90122ae3d08ca",
"quantity": 1,
"unit_price": 100,
"tax_rate_id": "ES_STANDARD"
}
],
"main_address": {
"city": "Madrid"
}
}
}
Make sure your contact is listed in the contact endpoint. Use GET https://api.accounting.sage.com/v3.1/ledger_accounts?visible_in=sales to get a list of all valid ledger accounts for sales objects.
I see your question uses ES_RE_STANDARD as tax rate. I will update this answer soon with an example for the "recargo de equivalencia" tax rate.
I'm using the Magento ver. 2.1.2 Rest Api to create users, following this :
http://devdocs.magento.com/guides/m1x/api/rest/Resources/resource_customers.html#RESTAPI-Resource-Customers-HTTPMethod-POST-customers
$data = [
"customer" => [
"firstname" => 'Earl',
"lastname" => 'Hickey',
"email" => 'earl-2#example.com',
"password" => 'password',
"website_id" => 1,
'store_id' => 1,
"group_id" => 1
]
];
$token = $this->get('lp_api')->getToken();
$ch = curl_init( $this->endpoint . 'customers');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json", "Authorization: Bearer " . json_decode( $token ),
)
);
// var_dump(curl_getinfo($c));
$result = curl_exec($ch);
If i send a password (as in the example above), i've got the following error :
Next Exception: Report ID: webapi-583357a3bf02f; Message: Property "Password" does not have corresponding setter in class "Magento\Customer\Api\Data\CustomerInterface". in /var/www/html/www.magento.dev/vendor/magento/framework/Webapi/ErrorProcessor.php:195
I noticed that if i remove the "password" => 'password' from the $data array, a user is created without password (seems odd to me).
I can't find any help on this error.
Any idea anyone ?
Refer below link for Magento 2.x version.
http://devdocs.magento.com/swagger/index_20.html#/
I have used below body for creating customers through Rest Api and it worked properly.
{
"customer": {
"email": "xyz#abc.com",
"firstname": "x",
"lastname": "y",
"website_id":1,
"group_id":1,
"custom_attributes": [
{
"attribute_code": "mobile_no",
"value": "1234567890"
}
]
},
"password": "123456"
}
Hi i am trying to send a post request to an API, using PHP.
I have the following code. The API expects products to be a list of dictionaries like
[{"id":1, "name": "nyan"}, {"id": 2, "name": "cat"}]
I have the following code, which seems to be sending it correctly, but the API doesn't accept it, i am supposing that it is to do with the way PHP arrays are encodede, but can't really figure it out, any help is appreciated.
<?php
$data = array(
key => "API_KEY",
private_key => "PRIVATE_API_KEY",
products => array(array(id => 73,
name => "A nice t-shirt",
description => "A nice t-shirt with a picture of a cat",
price => 9.95,
brand => "Cat t-shirts",
category => 11,
rating => 5))
);
$data_string = json_encode($data);
$ch = curl_init('http://api.clerk.io/v2/product/add');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, 'Content-Type: application/json');
$result = curl_exec($ch);
echo $result;
You don't need to convert it to JSON String,
So remove following line,
$data_string = json_encode($data);
And update following line,
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
http_build_query() generates URL-encoded query string from Array.
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data_json)));
Add Content length in your header
$data = array(
key => "API_KEY",
private_key => "PRIVATE_API_KEY",
products => array(
array(id => 73,
name => "A nice t-shirt",
description => "A nice t-shirt with a picture of a cat",
price => 9.95,
brand => "Cat t-shirts",
category => 11,
rating => 5
)
)
);
$data_string = json_encode($data);
echo '<br>'.$data_string;
you will get the products dictionaries from 'products' key. so you just have to pass 'products' key data to API.
I'm trying to use QPX Express API for my website to search for flights.
https://developers.google.com/qpx-express/v1/requests#Examples
I have no idea how to run
curl -d #request.json --header "Content-Type: application/json" https://www.googleapis.com/qpxExpress/v1/trips/search?key=xxxxxxxxxxxxxx
from my php file. And how should I manage the json file. I quess I should make a php file and set the header type, am I correct?
I could not find anything from anywhere
You do not need to create and save an actual JSON file for each request. You can simply create a JSON string and send that as the POST payload. As far as executing the curl, you should see the native functions available in the PHP Manual. Specifically, curl_init(), curl_setopt(), and curl_exec(). Here's an example...
$url = "https://www.googleapis.com/qpxExpress/v1/trips/search?key=YOUR_API_KEY_HERE";
$postData = '{
"request": {
"passengers": {
"adultCount": 1
},
"slice": [
{
"origin": "BOS",
"destination": "LAX",
"date": "2016-05-10"
},
{
"origin": "LAX",
"destination": "BOS",
"date": "2016-05-15"
}
]
}
}';
$curlConnection = curl_init();
curl_setopt($curlConnection, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($curlConnection, CURLOPT_URL, $url);
curl_setopt($curlConnection, CURLOPT_POST, TRUE);
curl_setopt($curlConnection, CURLOPT_POSTFIELDS, $postData);
curl_setopt($curlConnection, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curlConnection, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlConnection, CURLOPT_SSL_VERIFYPEER, FALSE);
$results = curl_exec($curlConnection);
You could also use an array to create the payload, and then use json_encode() to convert it into a JSON string.
$postData = array(
"request" => array(
"passengers" => array(
"adultCount" => 1
),
"slice" => array(
array(
"origin" => "BOS",
"destination" => "LAX",
"date" => "2016-05-10"
),
array(
"origin" => "LAX",
"destination" => "BOS",
"date" => "2016-05-15"
)
)
)
);
And then use
curl_setopt($curlConnection, CURLOPT_POSTFIELDS, json_encode($postData));