Mailgun laravel 4.2 change version "v2" to "v3" - laravel

I'm working with Laravel 4.2 and Mailgun.
The base url of the api of mailgun changed, now it's https://api.mailgun.net/v3/..........
When I perform a request with Laravel it looks like : https://api.mailgun.net/v2/.........
I checked the Facade who handle that :
// vendor/laravel/framework/src/Illuminate/Mail/Transport/MailgunTransport.php
/**
* Set the domain being used by the transport.
*
* #param string $domain
* #return void
*/
public function setDomain($domain)
{
$this->url = 'https://api.mailgun.net/v2/'.$domain.'/messages.mime';
return $this->domain = $domain;
}
The version is hardcoded ... How I can change that properly ?

Laravel 4.2 was build with mailgun v2. You could probably change v2 to v3 in the code and it would probably work since v3 of mailgun is backwards compatible. (http://blog.mailgun.com/default-api-version-now-v3/) But its not the nicest solution.
You could use an external dependancy like https://github.com/Bogardo/Mailgun that uses mailgun v3 api.

I'd suggest making a code change to the code for the class with the problem, and submitting it back to Laravel's Github as a pull request.
I agree with you that it looks totally hard coded and can't be changed other than doing this, so that seems to be your best bet.
One thing to beware of though: I don't know the mailgun API, but if it's gone from v2 to v3, that implies there have been some breaking changes to the API, so don't think you can just change the number '2' into a '3' and expect it to work; you'll probably also need to change the code that makes the relevant API calls.

Related

channel vs channel_name - Laravel Echo and Laravel variable name for private channel

I came across a weird thing while using Laravel Echo Server in NuxtJs, Redis, and Laravel 8.
Laravel Echo in NuxJs sends a WS message that looks like this:
42["subscribe",{"channel":"container-details-3","auth":{"headers":{"Authorization":"Bearer ****"}}}]
I am using API auth like this:
Broadcast::routes(['middleware' => ['auth:api']]);
This is failing because the request that comes through to my private channel auth in Laravel has a channel variable in it so $request->channel but Laravel is expecting $request->channel_name
By the looks of it I probably can't change it in Laravel since it is just hardcoded so my options are:
Change it in the middleware (seems like a nasty idea)
Force Laravel echo to change the payload of WS message so it will say channel_name instead of channel
Did anyone come across this issue before and what do you think would be the best thing to do here? Or I am completely lost here and just dead wrong?
All of that works fine when I hit auth endpoint via postman with the channel_name variable.
I will be surprised if you are not able to edit the variable name in Laravel. I never did used this framework but this answer looks decent: https://stackoverflow.com/a/47518820/8816585
$channel->slug = str_slug($request->channel_name);
But yeah, rather change it on Laravel's side.

Upgrade to Laravel 5.3 - Mail::send now returns null from Mandrill

I'm upgrading from Laravel 5.2 to 5.3 but since doing so the Mail::send function no returns null when sending via Mandrill.
I had the same issue in Laravel 4.2, but it seems this functionality was then added when upgrading to version 5.
$response = Mail::send($template, $email, function($message) use($subject)
{
$message->to('test#example.com')->subject($subject);
});
dd($response)
Message sends fine, but the response is null where is gave the message ID/content in 5.1 & 5.2 before the upgrade
I can't understand why they would add the feature then remove it again?
This is because the syntax has fundamentally changed. You need to use the Mailable Class rather than a callback.
Your code should look something like this:
Mail::to('test#example.com')->send(new EmailExample($data));
Where EmailExample is a class that extends Mailable and handles your email body and any applicable logic.
See the docs here for further information

Session return null anyway

My project is using three different services and now I never can get sessions values, I've tried the laravel site tutorial and fallowing link question :
Laravel - Session returns null
But none of them worked!
In the first, i used this library:
use Session;
In a controller class :
$result = SocketHelper::sendRequest($req);
Session::put('uuid', $result->uuid);
Session::save();
Session::put('userId', $result->userID);
return Redirect::route('login_step_two');
In an other method :
$uuid = Session::get('uuid');
$userId = Session::get('userId');
But these are null! does I have to use cookie?
I recently upgrade to laravel 5.4
Please help me! It's made me confused!
Thanks.
Try saving Session explicitly like this, give it a try it worked for me, hope same for you.
Session::put('test_session', 'test message');
Session::save();
And retrieve it like this
echo Session::get('test_session');
And forget it like this:
Session::forget('test_session');
Session::save();
I understood the null result was becouse I was posting the value of $request to an other template and it was changed it the way :))) !
so easy to know !
Have you properly upgraded to laravel 5.4?
Laravel Docs
Sessions
Symfony Compatibility
Laravel's session handlers no longer implements Symfony's SessionInterface. Implementing this interface required us to implement extraneous features that were not needed by the framework. Instead, a new Illuminate\Contracts\Session\Session interface has been defined and may be used instead. The following code changes should also be applied:
All calls to the ->set() method should be changed to ->put(). Typically, Laravel applications would never call the set method since it has never been documented within the Laravel documentation. However, it is included here out of caution.
All calls to the ->getToken() method should be changed to ->token().
All calls to the $request->setSession() method should be changed to setLaravelSession().
Do you still have the rights to write session files in php session directory ?
Check the path returned by session_save_path() and check if your php user has the rights to write in it, check also if there is files in the directory and what are their creation date.

Changing The Laravel Passport/OAuth-Server Responses

I'm building an API in Laravel 5.4, using Laravel Passport 3 for authentication. All of my API methods return a set of values that are always returned, success, errors (if there are any errors) etc.
I've changed the response of a \Illuminate\Auth\AuthenticationException throws, to fit with the rest of my app, however I'm not sure how to change the response of various token grant responses, without doing something horrible like editing the vendor files.
I think you can use middleware to change your response.
From laravel documentation:
Before & After Middleware
Whether a middleware runs before or after a
request depends on the middleware itself.
You can capture the response and re-format the response.
You can use laravel's setContent method to set the content in response. Check here.
What you are trying to do here is not supported by the library, so whatever you do will be hacky and will probably break the compatibility with future versions of laravel/passport.
In my opinion, you can only choose between those 2 options:
Instead of declaring passport routes (Passport::routes()) you can declare equivalent routes to your custom methods. Those method internally calls Passport classes and methods, handling passport returning values before returning them to the user. It requires a lot of digging into passport code but, at the same time, if you only add some fields (success or error) you should be able to update your code without too much effort when updating the library.
Fork laravel/passport and modify it to suit you needs. This solution in not as messy as the first, but a merge with new versions of passport in the future will probably be hard.
Of course, both are not great solutions. Keeping the standard passport responses or use a more suitable library are better options: I assume they are not feasible if you are asking.
Another way - create proxy routes for your purposes.
Route::post('custom-auth/token', function (Request $request) {
$proxy = Request::create('oauth/token', 'POST', $request->request->input());
$response = app()->handle($proxy);
return responseCallback($response);
});
Where responseCallback your custom response modificator function.

Bitbucket - Webhook keep returning error 500

Would like to check, I am fairly new to Bitbucket's new introduced webhook where previously i was using services where Bitbucket will execute a link to my site thus triggering a deployment script.
So since the old service is going to be depreciated soon, we all migrated to webhook instead. With the same implementation, I keep getting an error 500 upon commit/push/merge and there is no way for us to see the details for the error given. At first I thought it was my server giving problem but when i call the link manually via browsers and everything was fine. The deployment script can be executed successfully so then why bitbucket's webhook keeps telling me error 500?
Subsequently I find the guide given by Bitbucket was not helpful. There is no specified call method to the url stated so is the webhook initiates a GET or POST request? previously using services initiates a POST request. Then, are there any necessary payloads i need to include into the webhook URL? None is stated. Then, if there is an error at least let me see the error so I can fix it instead of telling me error 500.
I hope someone here can help me with this issue. Below are some specification of the site.
Server : Ubuntu LEMP 14.04 x64 Laravel framework 5.0
Webhook Url: bot.example.com/bitbucket/deploy/{Site API}
Method : GET
And when the abode link is call, it reaches a controller that does
public function attemptDeploy($site_api)
{
$script = 'nohup setsid php ~/scripts/deploy.php ' . $site_api. ' > /dev/null 2>&1 &';
exec($script);
return response('Deploy running.', 200);
}
Note that when i call this link manually either form browser or console everything works perfectly except from bitbucket's webhook. How can i solve this issue?
I was in the same situation. Bitbucket trims the body of the response and I couldn't see the error given by my server.
I've looked into the logs storage/logs/laravel.log and saw TokenMismatchException. Webhooks being API calls they don't store cookies or sessions so CSRF from Laravel breaks.
You need to add an exception from CSRF for the bitbucket deploy route. You can add this exception in app/Http/Middleware/VerifyCsrfToken.php. For example if your link is www.your_site.com/bit_deploy you will have:
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;
class VerifyCsrfToken extends BaseVerifier
{
/**
* The URIs that should be excluded from CSRF verification.
*
* #var array
*/
protected $except = [
'bit_deploy'
];
}
Hope that this helps you ... as I've lost 3 hours on this.
PS: at the time of writing this answer, bitbucket webhooks performs POST calls (not GET)

Resources