How o setup call backurl in laravel? - php

I want to setup callback url for blockchain.info receive api.
Below is a php example but i don't know to to setup this in laravel
$my_callback_url = 'https://mystore.com?invoice_id=058921123&secret='.$secret;
my route is following
Route::get('btc_ipn/{invoice_id}/{secret}',['as'=>'btc_ipn','uses'=>'HomeController#btcIPN']);
I tried but ipn doesn't work.

If you want to build URL for the route you've shown, do something like this:
$url = route('btc_ipn', ['invoice_id' => $invoice->id, 'secret' => $secret]);
If you need to create a route for this URL:
https://mystore.com?invoice_id=058921123&secret=' . $secret;
Create a get or post route:
Route::get('/', 'HomeController#btcIPN');
Then in the controller:
public function btcIPN()
{
$invoiceId = request('invoice_id');
$secret = request('secret');
}

To meet routes you created as
Route::get('btc_ipn/{invoice_id}/{secret}',['as'=>'btc_ipn','uses'=>'HomeController#btcIPN']);
Your url should be like
$invoice_id = $request->invoice_id;
$secret = $request->secret;
$my_callback_url = "/btc_ipn/$invoice_id/$secret";
If you want to return with some data like error use
return redirect()->back()->withErrors(['msg', 'The Message']);

Related

How to get query params and set to the url in codeigniter-3?

i am using codeigniter-3, i have some API's i want to call those API's in the codeigniter-3 for that i use Guzzle class i want to get the id from the view file and i want to set according to the API URL, can you please help me to acheive this thing ..?
public function deletecurl($url, Array $params){
$client = new GuzzleHttp\Client();
$response = $client->request('DELETE',$url,$params);
$body = $response->getBody();
$arr_body = json_decode($body);
return $arr_body;
}
controller.php
public function deletestation(){
$this->load->library('Customcurls');
$url = "http://test.com/test-api/api/stations/remove_station";
$datas = ['station_id' => '1']; //hardcoded but it's vary for each record
$this->customcurls->deletecurl($url,['query' =>$datas]);
}
view file
<a class="dropdown-item" href="<?php echo base_url();?>product/dletestation/<?php echo $station->station_id;?>">Delete</a>
my expectation is the URL should be like this http://armycalling.com/baligaz-api/api/stations/remove_station?station_id=baligaz_454383

Finding User with Auth - Laravel

I am trying to find the logged in user in my application using Auth but i get trying to get property of non-object which i understand clearly that it is returning null.
In my code below, an event triggers my webhook and post is sent to the address below. The function orderCreateWebhook triggers but that is where the error comes from..
The line $get_template = Order::where('id', Auth::user()->id);. Why is Auth returning null please? I am logged as well because i use auth in this same controller for another function which works fine.
Is it because it a webhook ?
Controller
public function registerOrderCreateWebhook(Request $request)
{
$shop = "feas.myshopify.com";
$token = "8f43d89a64e922d7d343c1173f6d";
$shopify = Shopify::setShopUrl($shop)->setAccessToken($token);
Shopify::setShopUrl($shop)->setAccessToken($token)->post("admin/webhooks.json", ['webhook' =>
['topic' => 'orders/create',
'address' => 'https://larashop.domain.com/order-create-webhook',
'format' => 'json'
]
]);
}
public function orderCreateWebhook(Request $request)
{
$get_template = Order::where('id', Auth::user()->id);
$baseurl = "https://apps.domain.net/smsapi";
$query = "?key=7e3e4d4a6cfebc08eadc&to=number&msg=message&sender_id=Shopify";
$final_uri = $baseurl.$query;
$response = file_get_contents($final_uri);
header ("Content-Type:text/xml");
}
In your function registerOrderCreateWebhook you appear to be making a request to shopify api and providing your webhook as the address which shopify will redirect the user to upon success. If this is correct, that request does not know about the user who generated the original request that made the api request since the request is coming from a completely different origin.
You would need to pass some key along with the url and then obtain the user within orderCreateWebhook. Something like:
Shopify::setShopUrl($shop)->setAccessToken($token)->post("admin/webhooks.json",
['webhook' =>
['topic' => 'orders/create',
'address' => 'https://larashop.domain.com/order-create-webhook/some-unique-key',
'format' => 'json'
]
]);
My suggestion would be to have a unique hash stored somewhere that relates back to the user in your system, perhaps a column in your users table. I wouldn't use the user_id for security reasons. So you would end up with something like:
//route
Route::get('/order-create-webhook/{uniqueKey}', 'YourController#orderCreateWebhook');
//or
Route::post('/order-create-webhook/{uniqueKey}', 'YourController#orderCreateWebhook');
// depending on the request type used by api which calls this endpoint
// controller function
public function orderCreateWebhook($uniqueKey, Request $request)
{
$user = User::where('unique_key', $uniqueKey)->first();
$get_template = Order::where('id', Auth::user()->id);
$baseurl = "https://apps.domain.net/smsapi";
$query = "?key=7e3e4d4a6cfebc08eadc&to=number&msg=message&sender_id=Shopify";
$final_uri = $baseurl.$query;
$response = file_get_contents($final_uri);
header ("Content-Type:text/xml");
}
Is it because it a webhook ?
Yes, you can't use sessions in a webhook. It's the shopify server which is making the call. You should read the doc, it may exist a way to give an unique identifier in your call to shopify api and get it back in the webhook to find your user associated.
just use this to get authenticated user
use the facade in your class/Controller
use Illuminate\Support\Facades\Auth
public function getAuthUser(){
$user = Auth::user()
if(!is_null($user)
{
//user is authenticated
}
else
{
// no user
}
}

Get html content from auth protected route inside laravel app?

I want to get with (curl) guzzle html content of a other page inside my laravel app.
The classic way would be:
$client = new Client();
$client = $client->request('GET', route('print.page'))->getBody();
The problem is, all this routes are auth protected and I get there only html from my login page.
I tried to send login trough guzzle again but I think this is not a good idea with double login.
Is there any better way to get html from this protected route?
In case you calling this inside a controller and you have a current authenticated user, you have to get the session name and the real session id:
public function FooController()
{
$name = Session::getName();
$sessionId = $_COOKIE[$name];
$cookieJar = CookieJar::fromArray([
$name => $sessionId,
], 'example.com');
$client = new Client();
$body = $client->request( // changed the variable from $client to $body here
'GET',
route('print.page'),
['cookies' => $cookieJar]
)->getBody();
}

Laravel 5 - redirect not working

I'm not able to redirect in my application using:
return redirect('/);
The problem comes from when the user do a login using facebook.
Facebook returns a url that contains #= and seems to block my site execution:
public function createUserFromFacebookInfos( $info )
{
$user = User::firstOrCreate([
'email' => $info->email
]);
$user->name = $info->name;
$user->provider_token = $info->token;
$user->provider_id = 1;
$user->save();
$this->auth->login( $user );
return redirect('/');
}
How can I remove that piece of url?
I have read that that code is added when redirect_uri is missing, but where should I set this redirect_uri?
Make sure that you are propagating the redirect object returned from sub-function out from your main controller function.

How to get additional $_GET Parameters in Yii2 when prettyurl is enabled?

how can i get additional $_GET Parameters in Yii2 even when prettyurl is enabled?
I need to read some feedback from the redirect Paypal-Link but i cant change the Link-Format on Paypal-Side to fit my Yii2 implementation:
http://example.com/controller/action?success=boolean&token=xyz
Thanks for your help!
you can use it
http://www.yiiframework.com/doc-2.0/guide-runtime-requests.html
for e.g
if you need to use $_GET['success'] or $_GET['token']
you must use it:
$request = Yii::$app->request;
$get = $request->get();
$success = $request->get('success');
$token= $request->get('token');
I figured out a way:
$url = parse_url(Yii::$app->request->url);
parse_str($url['query'], $array);
$success = $array['success'];
$token = $array['token'];
but it still doesnt seem like the right Yii2-ish way to solve it.
http://www.yiiframework.com/doc-2.0/yii-web-urlmanager.html this will help You. you can specify GET POST method for any controller,
[
'dashboard' => 'site/index',
'POST <controller:\w+>s' => '<controller>/create',
'<controller:\w+>s' => '<controller>/index',
'PUT <controller:\w+>/<id:\d+>' => '<controller>/update',
'DELETE <controller:\w+>/<id:\d+>' => '<controller>/delete',
'<controller:\w+>/<id:\d+>' => '<controller>/view',];
for example
'POST <controller:\w+>/<success:\w+>/<token:\w+>' => '<controller>/update',
Use the Request class.
http://www.yiiframework.com/doc-2.0/yii-web-request.html
print_r(Yii::$app->request->get()); returns all get variables in an array. It's like doing print_r($_GET); in straight php.
If you want a specific $_GET variable you access it as follows:
Yii::$app->request->get('varName');
In your case it would be:
$success = Yii::$app->request->get('success');
$token = Yii::$app->request->get('token');
Here is my successful returnUrl from paypal which yii2 handles beautifully with prettyurl enabled in the UrlManager.
http://multi2.myhost/subscription/subscription/success?token=EC-8GE539098H175763M
I have created a subscription module and a controller class called SubscriptionController and actions called actionSuccess and actionCancel.
The Paypal redirect only passes one parameter. The token. No need for two parameters. Your success and cancel returnurl's should be something like:
controller/action or subscription/success/
controller/action or subscription/cancel/
Your success returnUrl:
SubscriptionController/actionSuccess($token)
public actionSuccess($token)
{
}
and your cancel returnUrl:
SubscriptionController/actionCancel($token)
public actionCancel($token)
{
}
Using this method there is no need for a second parameter to handle the success and cancel variable since the separate Controller Actions solve this problem. Incorporate the 'success' in the name of the action which satisfies PrettyUrl Management.
You will have to modify both merchant preference returnUrls in the following code.
$merchantPreferences = new MerchantPreferences();
$merchantPreferences->setReturnUrl($model->merchant_preference_returnurl)
->setCancelUrl($model->merchant_preference_cancelurl)
There is therefore no need for:
$request = Yii::$app->request;
$get = $request->get();
$token = $request->get('token');
at the beginning of the action.

Categories