Laravel 5.2 - method DELETE not allowed (error 405) - angularjs

I have a database with two tables: table CATEGORIES has an id that is the fk of table PRODUCTS.
When I want to delete a category, and a record in table products has associated that category id, laravel return a 405 error and i cannot access to destroy method. How can I avoid 405 error and access to destroy method?
Here's my route config:
$api = app('Dingo\Api\Routing\Router');
$api->version('v1',['middleware' => ['api']], function($api){
$api->resource('categories', 'App\Http\Controllers\CategoriesController');
$api->resource('products', 'App\Http\Controllers\ProductsController');
$api->get('categories/{id}/products', 'App\Http\Controllers\CategoriesController#products');
});
EDIT
If the category isn't associated with any product, delete method doesn't throw any error so I guess it isn't a route problem

Laravel only returns HTTP 405 if a route exists but not for that method. I'm not entirely familiar with Dingo, but run php artisan route:list to check that the route you're trying to use has been registered for a DELETE request.

Related

react-paypal-button-v2 returning the wrong order id

I was trying to debug a problem related to refunding Paypal orders (in a sandbox environment) using order IDs (which were stored previously). Every time I tried to perform a refund, the Paypal API would return an INVALID_RESOURCE_ID error, meaning that no such order existed. After much debugging, I have made a revelation with the initial process when I stored said order ID. The following method is how I am retrieving and storing said order id:
const onApprove = (data, actions) => {
// Redux method of saving checkout in backend with order ID via using data.orderID
dispatch(saveCheckout(data.orderID);
return actions.order.capture();
}
<PayPalButton
amount={totalPrice}
currency= "AUD"
createOrder={(data, actions) => createOrder(data, actions)}
onApprove={(data, actions) => onApprove(data, actions)}
options={{
clientId: "<placeholder>",
currency: "AUD"
}}
/>
I am using the recommended data.orderID from the docs and yet, upon inspecting the network tab, the following is shown:
{"id":"5RJ421191B663801G","intent":"CAPTURE","status":"COMPLETED","purchase_units":[{"reference_id":"default","amount":{"currency_code":"AUD","value":"24.00"},"payee":{"email_address":"sb-sg4zd7438633#business.example.com","merchant_id":"EJ7NSJGC6SRXQ"},"shipping":{"name":{"full_name":"John Doe"},"address":{"address_line_1":"1 Cheeseman Ave Brighton East","admin_area_2":"Melbourne","admin_area_1":"Victoria","postal_code":"3001","country_code":"AU"}},"payments":{"captures":[{"id":"7A2856455D561633D","status":"COMPLETED","amount":{"currency_code":"AUD","value":"24.00"},"final_capture":true,"seller_protection":{"status":"ELIGIBLE","dispute_categories":["ITEM_NOT_RECEIVED","UNAUTHORIZED_TRANSACTION"]},"create_time":"2021-10-11T00:40:58Z","update_time":"2021-10-11T00:40:58Z"}]}}],"payer":{"name":{"given_name":"John","surname":"Doe"},"email_address":"sb-432azn7439880#personal.example.com","payer_id":"KMEQSKCLCLUZ4","address":{"country_code":"AU"}},"create_time":"2021-10-11T00:40:48Z","update_time":"2021-10-11T00:40:58Z","links":[{"href":"https://api.sandbox.paypal.com/v2/checkout/orders/5RJ421191B663801G","rel":"self","method":"GET"}]}
The id saved by onApprove is 5RJ421191B663801G but there is another ID under captures and id which is 7A2856455D561633D. This is the actual order id I need to save in order to make the refund later on. However, I am struggling as to how I can retrieve this value as that id value seems to be only visible via the network. The objects returned via the onApprove and action.order.get() methods only return the first "false" id. Any advice would be greatly appreciated.
These are two separate types of IDs, the order ID (used only during buyer checkout approval), and the payment/transaction ID (which only exists after an order is captured, and is the one needed for any later refund or accounting purposes)
Since you are capturing on the client side with actions.order.capture(), this is where you would need to add a .then(function(data){ ... }) to do something with the capture data (particularly data.purchase_units[0].payments.captures[0].id). That is the id you would use for a refund.
In actual best practice, if anything important needs to be done with the capture id -- such as storing it in a database for reference -- you should not be creating and capturing orders on the client side, and instead calling a server-side integration where that database write will be performed.
Follow the Set up standard payments guide and make 2 routes on your server, one for 'Create Order' and one for 'Capture Order', documented here. Both routes should return only JSON data (no HTML or text). Inside the 2nd route, when the capture API is successful you should store its resulting payment details in your database (particularly the aforementioned purchase_units[0].payments.captures[0].id, which is the PayPal transaction ID) and perform any necessary business logic (such as sending confirmation emails or reserving product) immediately before forwarding your return JSON to the frontend caller.
Pair those 2 routes with the frontend approval flow: https://developer.paypal.com/demo/checkout/#/pattern/server
Or for react, use the official react-paypal-js

How to update a Azure AD user?

I have successfully created a new user in Azure AD following Create User reference.
Now I want to update that very same user following Update User reference. To keep things simple This is the JSon content I'm sending:
{
"userType": "T"
}
The documentation tells me to send a PATCH request, but I always receive an HTTP Error 400 (Bad request). If I try sending a POST request I receive an HTTP Error 405 (Method not allowed).
The destination URL is https://graph.microsoft.com/v1.0/users/user-id.
The user-id is the one returned with the user details.
What am I missing?
Update 1
Those are the permissions set to the application:
This is exactly how you update user (PATCH).
However the userType property cannot be T, from the docs you refer:
That property can only have one of the two distinct values: Member or Guest.
Of course, the user-id in path should the id property as returned by the get users operation.
I am pretty sure that if you use a normal REST client will be able to see the whole error message, which will be more meaningful.
If you still have issue - paste the exact error message.

Can we update payment method for braintree subscription?

I'm new to braintree. Can we update payment method to an existing subscription. Is their any way to update payment method. So
user can change their cards for subscription ?
From braintree documentation will this work ?
$result = $gateway->subscription()->update('old_subscription_id', [
'id' => 'old_subscription_id',
'paymentMethodToken' => 'new_payment_method_token'
]);
Full disclosure, I work at Braintree. If you have any additional questions, contact Support
You can update an existing subscription to use any other payment method that is stored in your Vault. You can do this with an existing payment method, or by creating a new payment method with a paymentMethod()->create() API request.
With that said, the request you provided may work, but I recommend not including the id parameter, as that is reserved for setting a new subscription ID. The subscription ID you are hoping to edit will be placed in the first argument, as you've included in your code. You can simplify the request to look something more like this:
$result = $gateway->subscription()->update('old_subscription_id', [
'paymentMethodToken' => 'new_payment_method_token'
]);

Persisting a backbone model and the id

Let's assume we create a new model class an instanciate a person using it:
var User = Backbone.Model.extend({
urlRoot: '/user'
});
var nou = new User({
name: "nourdine"
});
Now, of course, we want to persist it. Not having added an id backbone will create a POST request and communicate the server the intention to create an entity under /user containing the data {name: "nourdine"}. Here's how we do it:
nou.save(null, {
success: function (model, response, options) {
// ... what do I do here?
}
})
The server will now create a record in the db containing the JSON data rearranged in some form and assign an ID to it. NOW:
1 - What is the server suppose to return in the HTTP response? A JSON containing the JSON provided by the clinet + the newly created fields, namely the ID of the new record?
2 - who is going to update the model in the client with these data? Me? Matter of fact I would like to tell the model in the client that a new ID has been assign to it by the server so that the next time I do user.save() I will obtain a PUT rather than a POST. But who is supposed to update the model in the client?
Thanks
so this is my work flow for this
client -> create model and populate with data
client -> save model (model.save())
server -> create server side version of model using data, assign an id
server -> respond with success and the id of the newly created model
client -> in the success set the id to the one passed back
now the only potential issue i have with my work flow is if something did not get set in the server successfully but the model was still created, my client model would not reflect that of the server anymore, but i minimize this by returning error if the model could not be created exactly as passed.
And now i am able to call model.save() again this time having the id so initiating a PUT request
From the documentation to a Backbone.Model
After a successful server-side save, the client is (optionally) updated with the server-side state.
So if you return a valid JSON your model will be updated automatically

CakePHP 2.0 - customize database error message

I have a CakePHP 2.0 application with a MySQL database. Two database tables are connected with a 1:n relation and a foreign key constraint.
So if I want to delete an entry which is connected in the other database table, I get the error:
Error: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a forein key constraint fails (...)
SQL Query: DELETE 'Test' FROM 'tests' AS 'Test' WHERE 'Test'.'id' = 10
Notice: If you want to customize this error message, create app/View/Errors/pdo_error.ctp
But what I want to do is to handle the error message! I read something about 'onError' but putting it into the 'AppModel' it seems not to be called (maybe it works only with CakePHP 1.3?):
class Test extends AppModel {
function onError() {
echo "TESTTESTTEST";
$db = ConnectionManager::getDataSource('default');
$err = $db->lastError();
$this->log($err);
$this->log($this->data);
}
}
So what can I do? I want to remain on this page, and I want to show only an error message (not a stack trace and this kind of stuff).
Anyone an idea?
What about using the .ctp?
If you want to customize this error message, create app/View/Errors/pdo_error.ctp
The one that's being used is in the Cake directory, you could just copy that to your app/View/Errors directory and remove the stack trace from that if you like.
There's also the beforeDelete() Model callback function you could use to set a flashMessage.
Data base error normally give 500 error so cakephp handle 500 exception by using
View/Errors/error500.ctp or app/View/Errors/pdo_error.ctp only and customize this page
and add this function bellow in AppController.php
function beforeRender() {
if($this->name == 'CakeError') {
$this->set('title','Internal error occurs');
$this->layout = 'frontend';
}
}

Resources