shopify Unprocessable Entity - php

while running this code with the app instalation,i am getting the Unprocessable Entity status code 422 error.
Here is the code
$sc = new ShopifyClient($_SESSION['shop'], $_SESSION['token'], $api_key, $secret);
$charge = array
(
"webhooks"=> array
(
"topic"=>"orders/create",
"address"=>"http://www.abc123no.com/nomi/s.php?key=123456789",
"format"=>"json"
)
);
try
{
$webhooks = $sc->call('POST','/admin/webhooks.json',$charge);
}
catch (ShopifyApiException $e)
{
var_dump($e->getResponseHeaders());
}

Error code 422 is for validation errors. The body of the response will describe the error to help you debug your application.
e.g. The response might be: {"errors":{"address":["for this topic has already been taken"]}}
Your error appears to be from using the wrong format for the request. The create endpoint doesn't take an array of webhooks, and you should use the singular "webhook" for the top-level key.
The Webhook API documentation has the correct format for the request body.

Change webhooks to webhook , it might create bad request error
"**webhooks**"=> array
(
"topic"=>"orders/create",
"address"=>"http://www.abc123no.com/nomi/s.php?key=123456789",
"format"=>"json"
)

Related

GraphQL query connect to Dutchie API using PHP

I have a store in Dutchie.com. I want to access it's products using API key.
This has to do via Dutchie API using GraphQL integrated with PHP.
This is the Sample API Key:
public-eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJBUEktQ0xJRU5UIiwiZXhwIjozMzE4NjM5Mjc0NSwiaWF0IjoxNjI5NDgzOTQ1LCJpc3MiOiJodHRwczovL2R1dGNoY29tIiwianRpIjoiNGMtOTMyOC00MjhkLWEyYTMtOWQzMTc2ZTUwODY0IiwiZW50ZXJwcmlzZV9pZChLWExYTctNDM3OC05NWY4LTNlYzVzBiNSIsInV1aWQiOiI0M2ZlMjdkNy1iZWU2LTQxOTgtYWNhMi03N2Y5Y2I3MjI5MGIifQ.hCQWpcQ5uhKnZOSVQDA5SCMkx5kopC7H3upeU-1jMpg
This is the GraphQL Ping mutation.
mutation Ping {
ping {
id,
time
}
}
Dutchie End Point: https://plus.dutchie.com/plus/2021-07/graphql
http header parameter
{
"Authorization":"Bearer API KEY HERE"
}
Ping output
Basically I want to run GraphQL query in my PHP page. I'll add into my WordPress page later.
I have tried php-graphql-client php library.
Can someone help me to do this using above library or another one really appreciate. I wasted too much time for this as I have only few knowledge of GraphQL.
This is the code what I have tried.
$client = new Client(
'https://plus.dutchie.com/plus/2021-07/graphql',
['Authorization => Bearer API Key here']
);
// Create the GraphQL mutation
$gql = (new Mutation('Ping'))
->setSelectionSet(
[
'id',
'time',
]
);
// Run query to get results
try {
$results = $client->runQuery($gql);
}
catch (QueryError $exception) {
// Catch query error and desplay error details
print_r($exception->getErrorDetails());
exit;
}
// Display original response from endpoint
var_dump($results->getResponseObject());
// Display part of the returned results of the object
var_dump($results->getData()->pokemon);
// Reformat the results to an array and get the results of part of the array
$results->reformatResults(true);
print_r($results->getData()['data']);
Error what I got.
https://github.com/guzzle/psr7/blob/master/src/MessageTrait.php
Instead of:
$client = new Client(
'https://plus.dutchie.com/plus/2021-07/graphql',
['Authorization => Bearer API Key here']
);
Try
$client = new Client(
'https://plus.dutchie.com/plus/2021-07/graphql',
['Authorization' => 'Bearer API Key here']
);

DocuSign - Can't set "sent" on createEnvelope

I am using Docusign PHP Client and trying to create and envelope and send it as email. With the current SDK, I was getting an error:
INVALID_REQUEST_BODY The request body is missing or improperly formatted. Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'API_REST.Models.v2.document[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.\r\n ◀
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive t ▶
Path 'documents.documentBase64', line 1, position 31.
So I had to edit EnvelopeApi.php (line 2876) json_encode($httpBody) to make it work.
Now that it's working, I receive a response like this, however I can't change status created to sent is my problem.
EnvelopeSummary {#460 ▼
#container: array:4 [▼
"envelope_id" => "6b9ef863-2ee0-4ea6-9f7e-20b7d4f59b22"
"status" => "created"
"status_date_time" => "2018-10-03T12:30:22.8600000Z"
"uri" => "/envelopes/6b9ef863-2ee0-4ea6-9f7e-20b7d4f59b22"
]
}
My full code:
First, I authenticated and fetched my $accountId
And then creating Envelope:
$path = public_path('test.pdf');
$b64Doc = base64_encode(file_get_contents($path));
$document = new Document();
$document->setName("TEST.pdf");
$document->setFileExtension("pdf");
$document->setDocumentId(1);
$document->setDocumentBase64($b64Doc);
$sign_here = new SignHere();
$sign_here->setXPosition(25);
$sign_here->setYPosition(50);
$sign_here->setDocumentId(1);
$sign_here->setPageNumber(1);
$sign_here->setRecipientId(1);
$tabs = new Tabs();
$tabs->setSignHereTabs($sign_here);
$signers = new Signer();
$signers->setName('Test User');
$signers->setEmail('test#mailinator.com');
$signers->setRoleName('Signer');
$signers->setRecipientId(1);
$signers->setRoutingOrder(1);
$signers->setTabs($tabs);
$recipients = new Recipients();
$recipients->setSigners($signers);
$envelope_definition = new EnvelopeDefinition();
$envelope_definition->setEmailSubject('Signature Request');
$envelope_definition->setStatus("sent"); // ***
$envelope_definition->setDocuments($document);
$envelope_definition->setRecipients($recipients);
$options = new CreateEnvelopeOptions();
$options->setCdseMode(null);
$options->setMergeRolesOnDraft(null);
try {
$envelopeSummary = $envelopeApi->createEnvelope($accountId, $envelope_definition, $options);
dd($envelopeSummary);
// Also tried this:
// $envelopeApi->update($accountId, $envelopeSummary->getEnvelopeId(), json_encode(['status' => 'sent']);
} catch (ApiException $e){
dd($e->getResponseBody()->errorCode . " " . $e->getResponseBody()->message);
}
$envelope_definition->setStatus("sent"); this should trigger the email, right? But it doesn't for some reason. Also I can't see this created envelope in my Sandbox either.
You are not setting signers correctly. It must be an array of signer objects.
Here is some untested code:
# This code creates a signer, not signers
$signer = new Signer();
$signer->setName('Test User');
$signer->setEmail('test#mailinator.com');
$signer->setRoleName('Signer');
$signer->setRecipientId(1);
$signer->setRoutingOrder(1);
$signer->setTabs($tabs);
$recipients = new Recipients();
# setSigners wants an array of signer objects.
# in this case, we make an array with one element
$recipients->setSigners(array($signer));
Also, you are not creating the tabs right either. Again, it needs to be an array of the tab type.
See this example for additional ideas.
Yes, setting status to sent should make DocuSign send the envelope upon creation. The fact that the response contains "status" => "created" seems to indicate that your setting of the property ($envelope_definition->setStatus("sent");) is not actually being included as part of the request that's being issued to DocuSign.
I've compared your code with the code examples provided in GitHub for the PHP SDK, specifically, with the signatureRequestOnDocument function on that page. The only obvious difference I can see between your code and that example code is in the syntax for creating objects. For example, creating the envelope:
Your code: $envelope_definition = new EnvelopeDefinition();
PHP SDK example code: $envelop_definition = new DocuSign\eSign\Model\EnvelopeDefinition();
I don't know much about PHP, let alone about the DocuSign PHP SDK, but I'd suggest that you try to closely mimic the code examples that are part of the SDK repo on GitHub, to see if you get a different result that way.
My code work like this :
$signersArray = array();
$signer = new Signer();
$signer->set...
$signersArray[] = $signer;
$recipients->setSigners($signersArray);
If it's not working try to dump the data send from the SDK to the API and double check that the status is correct :
Go to Docusign/esign-client/src/ApiClient.php and var_dump($postData) at line 159

PHP: Error in creating Server Instance in Google Cloud Compute

While using Google Cloud Compute's API in PHP, I am able to start, stop, delete instances as well as create and delete disks.
However, when trying to create an Instance, I keep getting this error
Invalid value for field 'resource.disks'
PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/compute/v1/projects/project/zones/zone/instances: (400) Invalid value for field 'resource.disks': ''. Boot disk must be specified.' in /var/www/html/google/google-api-php-client/src/Google/Http/REST
Here is the request I am making
self::connectClient();
$computeService = new Google_Service_Compute($this->client);
if ($this->client->getAccessToken())
{
$googleNetworkInterfaceObj = new Google_Service_Compute_NetworkInterface();
$network = self::DEFAULT_NETWORK;
$googleNetworkInterfaceObj->setNetwork($network);
$diskObj = self::getDisk($instance_name);
$new_instance = new Google_Service_Compute_Instance();
$new_instance->setName($instance_name);
$new_instance->setMachineType(self::DEFAULT_MACHINE_TYPE);
$new_instance->setNetworkInterfaces(array($googleNetworkInterfaceObj));
$new_instance->setDisks(array(
"source"=>$diskObj->selfLink,
"boot"=>true,
"type"=>"PERSISTENT",
"deviceName"=>$diskObj->name,
));
$insertInstance = $computeService->instances->insert(self::DEFAULT_PROJECT,self::DEFAULT_ZONE_NAME, $new_instance);
Any help will be highly appreciated, thank you.
Ok the solution was really simple (and silly)
Instead of
$new_instance->setDisks(array(
"source"=>$diskObj->selfLink,
"boot"=>true,
"type"=>"PERSISTENT",
"deviceName"=>$diskObj->name,
));
It's supposed to be
$new_instance->setDisks(array(
array(
'source'=>self::getDisk($instance_name)->selfLink,
'boot'=>true,
'type' => "PERSISTENT",
'deviceName'=>self::getDisk($instance_name)->name,
)
));

Symfony2 functional test redirect

I am working with Symfony2 and the FOSRestBundle and I now try to write a functional test for my rest api. I want to POST a user name and display it, similar to http://npmasters.com/2012/11/25/Symfony2-Rest-FOSRestBundle.html. I.e., my controller returns a View::createRedirect([...],Codes::HTTP_CREATED) instance.
Now, my test looks like:
[...]
$client = static::createClient(array('debug'=>true));
$request = $client->request('POST', '/names',
array(),
array(),
array('CONTENT_TYPE' => 'application/json'),
$expression);
$response = $client->getResponse();
\print_r($response);
$this->assertEquals($response->getStatusCode(), 201);
$response = $client->followRedirect();
\print_r($response);
When I run it, I get this output:
Symfony\Component\HttpFoundation\Response Object
(
[...]
[location] => Array
(
[0] => /names/90
)
[...]
There was 1 error:
1) MYNAME\MyBundle\Tests\Controller\RestControllerTest::testAddUser
LogicException: The request was not redirected.
Why wasn't it redirected?
Where is my mistake?
Thank you for any help!
If you want to generate a redirect, your backend should generate a 301 (“permanent”) or 302 (“temporary”) HTTP status. A status code of 201 is not really a redirect, even if you set a Location header. I think it depends on the client implementation what happens on a 201 + Location.

How to handle Doctrine 2's errors nicely?

I have a bit of code that sometimes throw errors for different reasons. And I would like to get more informations about it. The problem is either I have nothing at all and my app break or I use try catch and get a whole bunch of stuff from xdebug_message that I can't really show the user.
Let's say I need to do a collection of object AND make sure that some argument is filled (For this case we have nullable=false in the entity). If I miss one of those two points Here is what I get with the following code :
// We create the event
$event = new Entity\Event;
$event->setType( $this->input->post( 'type' ) );
$event->setDescription( $this->input->post( 'description' ) );
$event->setPlace( $place );
$event->setUser( $user );
// We can now persist this entity:
try
{
$em->persist( $event );
$em->flush();
}
catch( \Doctrine\DBAL\DBALException $e )
{
// Error When Persisting the Entity !!
// 500 Internal Server Error
// A generic error message, given when no more specific message is suitable
$this->response( array( 'error' => $e ), 500 );
}
$message = array(
"success" => TRUE
);
// Everything is fine
$this->response( $message, 200 ); // 200 being the HTTP response code
In this case, it returns:
{"error":{"xdebug_message":"
What I would like to do is that from this error message, automatically execute some function or send an explicit message to the front side for any configuration possible. I can't really use xdebug here, it is not very helpful for this purpose.
How can I get more explicit details from PHP or Doctrine itself ?
I'm working with Doctrine2 and Codeigniter 2.1 or 2 dont know.
Thanks
there some function give you nice msg. like $e->getMessage() $e->getCode()

Categories