Sending multidimensional array as key with Laravel http client request - laravel

I want to use the HelloSign API to let users sign contracts through our Laravel application. To make the API calls I use Laravel's Http Client which is build around the Guzzle Http Client.
In order to assign a signer to a HelloSign transaction the API expects the following parameter:
signers[Agent][name]
Now when I run this in postman. It works like a charm. But when I use the parameters like this in Laravel's HTTP client I receive the following error message: Invalid parameter: signers[Agent][name]. But when I use the same exact structure using cURL it workins like a charm.
HTTP Client:
Http::withBasicAuth($key, '')->post($url, [
'test_mode' => '1',
'template_id' => $template_id,
'signers[Agent][name]' => 'John Doe',
'signers[Agent][email_address]' => 'dummy#test.com'
]);
cURL:
CURLOPT_POSTFIELDS => array(
'test_mode' => '1',
'template_id' => $template_id,
'signers[Agent][name]' => 'John Doe',
'signers[Agent][email_address]' => 'dummy#test.com'
),
How can I make the HTTP client work?

Try to use multidimensional array:
Http::withBasicAuth($key, '')->post($url, [
'test_mode' => '1',
'template_id' => $template_id,
'signers' => [
'Agent' => [
'name' => 'John Doe',
'email_address' => 'dummy#test.com'
]
]
]);

Related

Send nested array via Laravel Http Client

I want to send request to an API website containing nested params via Laravel Http Client (Laravel 7.x)
$params = [
'type' => 'books',
'variables' => [
'color' => 'red',
'size' => 'large'
]
]
I want my url to like this:
http://example.com/?type=books&variables={"color":"red","size":"large"}
encoded above url:
http://example.com/?type=books&variables=%7B%22color%22%3A%22red%22%2C%22size%22%3A%22large%22%7D
But when I use:
Http::get('http://example.com', $params);
The API server returns error.
But when I use:
Http::get('http://example.com/?type=books&variables={"color":"red","size":"large"}');
It works well.
So how can I convert my params array into url ?
(I don't have access to API server)
try json_encode()
$params = [
'type' => 'books',
'variables' => json_encode([
'color' => 'red',
'size' => 'large'
])
]
$url = "http://example.com?".http_build_query($params);
Http::get($url);
and http_build_query()
ref link https://www.php.net/manual/en/function.http-build-query.php

Stripe webhook test returns empty table on execution, but works on stripe's webhook tester

We're having a problem with our test suite.
When we run this using the test suite we get a 'The table is empty...' response from PHPUnit.
We know it works as we've also tested using Stripe's 'Send a web hook' test function which works, and the response is stored as expected.
Our code is here:
public function test_webhook_received()
{
$this->expectsJobs([StoreStripeWebHookJob::class]);
$this->postJson('/stripeHook', [
'created' => 1326853478,
'livemode' => false,
'id' => 'evt_00000000000000',
'type' => 'account.external_account.created',
'object' => 'event',
'request' => NULL,
'pending_webhooks' => 1,
'api_version' => '2019-12-03',
'data' => [
'object' => [
'id' => 'ba_00000000000000',
'object' => 'bank_account',
'account' => 'acct_00000000000000',
'account_holder_name' => 'Jane Austin',
'account_holder_type' => 'individual',
'bank_name' => 'STRIPE TEST BANK',
'country' => 'US',
'currency' => 'gbp',
'fingerprint' => '8JXtPxqbdX5GnmYz',
'last4' => '6789',
'metadata' => [],
'routing_number' => '110000000',
'status' => 'new',
],
],
]);
$this->assertDatabaseHas('stripe_webhooks', [
'stripe_created_at' => 1326853478,
'type' => 'account.external_account.created',
]);
}
The response received is:
Failed asserting that a row in the table [stripe_webhooks] matches the
attributes {
"stripe_created_at": 1326853478,
"type": "account.external_account.created" }.
The table is empty..
If we remove the
$this->expectsJobs([StoreStripeWebHookJob::class]);
tests succeed. Obviously the expectsJob() call should be where it is though.
ExpectsJob also intercepts the job. Much like expectsException. Judging from your clean naming convention "StoreStripe..." - I'd say it's really not storing under these test circumstances.
You'll need to test separately that your endpoint/controller is queuing a job... and that the job is storing the data. 2 tests.

Sending automated email with dynamic content template

Well, I want to send an automated email every Friday using MailChimp/Mandrill.
Email Template :
Hi {{firstName}},
You weekly weekend pass is {{code}}.
Here, My email content is dynamically generated. So, before sending automated email, there should be some mechanism where I call the rest api from where I get the firstName and code. Then email is sent to different user.
{
[{
firstName: 'test',
code: 'Xewetwerx'
},
{
firstName: 'test2',
code: 'Xewetwrtd'
}]
}
I know we can write code in our backend server and call the API every Friday using cronjob(to send an email every Friday) but I don't want to use any cronjob.
Is it possible to make API call and send automated mail using MailChimp or mandrill ???
Outcome:
2 email should be sent with different code.
Mandrill implements scheduled emails through the sent_at parameter.
It's possible to send an email using a template and specify date and time when the message should be sent as a UTC timestamp in YYYY-MM-DD HH:MM:SS format. If you specify a time in the past, the message will be sent immediately. Note that for the sent_at feature you will need a paid (non free) account
If you choose PHP as the programming language for your backend,
you can use the official PHP API library.
example: (the code is not tested and could contain errors)
require_once '/path/to/MailchimpTransactional/vendor/autoload.php';
$mailchimp = new MailchimpTransactional\ApiClient();
$mailchimp->setApiKey('YOUR_API_KEY');
$msg = array(
'subject' => 'My subject',
'from_email' => 'ellery#example.com',
'to' => array(array('email' => 'recipient1#example.com', 'name' => 'Recipient 1'),array('email' => 'recipient2#example.com', 'name' => 'Recipient 2')),
'merge_vars' => array(array(
'rcpt' => 'recipient1#example.com',
'vars' =>
array(
array(
'name' => 'firstName',
'content' => 'test'),
array(
'name' => 'code',
'content' => 'Xewetwerx')
)),
array(
'rcpt' => 'recipient2#example.com',
'vars' =>
array(
array(
'name' => 'firstName',
'content' => 'test2'),
array(
'name' => 'code',
'content' => 'Xewetwrtd')
))));
$response = $mailchimp->messages->sendTemplate([
"template_name" => "template_name",
"template_content" => [[]],
"message" => $msg,
"send_at" => "2021-06-23 09:05:00" ,
]);
print_r($response);

Guzzle Post Null/Empty Values in Laravel

I've been trying to work with Guzzle and learn my way around it, but I'm a bit confused about using a request in conjunction with empty or null values.
For example:
$response = $client->request('POST',
'https://www.testsite.com/coep/public/api/donations', [
'form_params' => [
'client' => [
'web_id' => NULL,
'name' => 'Test Name',
'address1' => '123 E 45th Avenue',
'address2' => 'Ste. 1',
'city' => 'Nowhere',
'state' => 'CO',
'zip' => '80002'
],
'contact' => [],
'donation' => [
'status_id' => 1,
'amount' => $donation->amount,
'balance' => $donation->balance,
'date' => $donation->date,
'group_id' => $group->id,
],
]
]);
After running a test, I found out that 'web_id' completely disappears from my request if set to NULL. My question is how do I ensure that it is kept around on the request to work with my conditionals?
At this point, if I dd the $request->client, all I get back is everything but the web_id. Thanks!
I ran into this issue yesterday and your question is very well ranked on Google. Shame that it has no answer.
The problem here is that form_params uses http_build_query() under the hood and as stated in this user contributed note, null params are not present in the function's output.
I suggest that you pass this information via a JSON body (by using json as key instead of form_params) or via multipart (by using multipart instead of form_params).
Note: those 2 keys are available as constants, respectively GuzzleHttp\RequestOptions::JSON and GuzzleHttp\RequestOptions::MULTIPART
Try to define anything like form_params, headers or base_uri before creating a client, so:
// set options, data, params BEFORE...
$settings = [
'base_uri' => 'api.test',
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'ajustneedatokenheredontworry'
],
'form_params' => [
'cash' => $request->cash,
'reason' => 'I have a good soul'
]
];
// ...THEN create your client,
$client = new GuzzleClient($settings);
// ...and finally check your response.
$response = $client->request('POST', 'donations');
If you check $request->all() at the controller function that you are calling, you should see that were sent successfully.
For those using laravel, use this:
Http::asJson()

Nexmo Laravel connect to PSTN endpoint not working

My code is shown below. Everything works for me, besides connecting a call to a PSTN endpoint. I'm thinking possibly that maybe the "connect" functions aren't included in the Laravel Nexmo packages that I'm using. I'm using these:
https://github.com/Nexmo/nexmo-laravel
Which is built on another nexmo package:
https://github.com/Nexmo/nexmo-php
My code:
public function getNexmoAnswer(Request $request)
{
return
[
[
'action' => 'talk',
'voiceName' => 'Justin',
'text' => 'Welcome to our great site’
],
[
'action' => 'talk',
'voiceName' => 'Justin',
'text' => "Press # to search now the e.t.a status of your latest order.",
'bargeIn' => true
],
[
'action' => 'connect',
'from' => '17181234567’,
'endpoint' => [
'type' => 'phone',
'number' => '18451234567’
]
]
];
}
The connect action is part of Nexmo's NCCO system and is unrelated to Nexmo-Laravel and Nexmo-PHP. Your NCCO looks to be correct to me.
To connect to another number, you'll need to have activated your account by adding credit. In addition, the from number you're using (17181234567) must be a number purchased from Nexmo

Resources