How to commit just part of transaction PHP? - php

I need ideas to solve the problem where there's an order to be updated, and payments to be created to this order. But if the order charging fails. The updates made to the order should be undone, but the charges created to it should persist.
Example controller code:
$order = Order::find(1);
DB::beginTransaction();
try {
$order->update(['status' => 1]);
chargeOrder ($order);
} catch (PaymentErrorException $e) {
DB::rollback();
throw $e;
}
catch (\Exception $e) {
DB::rollback();
throw $e;
}
DB::commit();
Example function that make the order charging:
function chargeOrder( $order ) {
$payments_service->charge($order);
$order->payments()->create( new Payment() );
}
What i need is, when a PaymentErrorException occurs, only the $order->update() should be undone, but the changes made inside chargeOrder function, should persist.

If I understand the question correctly:
$order = Order::find(1);
$previousStatus = $order->status;
DB::beginTransaction();
try {
$order->update(['status' => 1]);
chargeOrder ($order);
} catch (PaymentErrorException $e) {
$order->update(['status' => $previousStatus]);
}
catch (\Exception $e) {
DB::rollback();
throw $e;
}
DB::commit();

Related

Catch Guzzle Exception and return string

So I need some help on building out one of my methods for retrieving twitter lists using IDs. Below, I will describe and go into detail on what it's returning.
Code:
public static function get_list($list_id)
{
$lists = self::get_lists();
$params = [
'list.fields' => 'created_at,follower_count,member_count,private,description,owner_id',
'user.fields' => 'created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified,withheld'
];
try {
$list = $lists->get($list_id, $params);
} catch (\GuzzleHttp\Exception\ClientException $e) {
return $e;
}
return $list;
}
When $lists->get() has an issue, it throws the following items object(GuzzleHttp\Exception\ClientException)#1640 (10) { ["request":"GuzzleHttp\Exception\RequestException":private]=> error.
What I'd like to achieve:
Return $e so that I can read the error (Unable to get this to work).
If I switch out return $e for return 'Hello', I still see the object and not the string.
The IDE suggests that it #throws GuzzleException.
Does anyone see anything wrong in how I'm handling my exception and why I'm unable to properly return the exception error?
Try to use exception hierarchy to catch any exception. ClientException only catches status code between 400x-499. To catch other exception or catch within the same Exception you can use RequestException.
public static function get_list($list_id)
{
$lists = self::get_lists();
$params = [
'list.fields' => 'created_at,follower_count,member_count,private,description,owner_id',
'user.fields' => 'created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified,withheld'
];
try {
$list = $lists->get($list_id, $params);
if($list->getStatusCode() == 200)){
$return_list = json_decode($list->getBody(),true);
}
} catch (\GuzzleHttp\Exception\ClientException $e) {
$error['error'] = $e->getMessage();
$error['request'] = $e->getRequest();
if($e->hasResponse()){
// you can pass a specific status code to catch a particular error here I have catched 400 Bad Request.
if ($e->getResponse()->getStatusCode() == '400'){
$error['response'] = $e->getResponse();
}
}
return $error;
} catch(\GuzzleHttp\Exception\RequestException $se){
$error['error'] = $e->getMessage();
$error['request'] = $e->getRequest();
return $error;
} catch(Exception $e){
//other errors
}
return $list;
}

multiple methods try without skipping others php

I have this code:
try{
firstMethod()
secondMethod()
}
catch(Exception $e){
....
}
What I want is execute all try/catch block functions but capturing if one throws exception, without skipping the following methods
a possible but not pretty code would be:
try{
firstMethod();
}
catch(Exception $e){
....
}
try{
secondMethod();
}
catch(Exception $e){
....
}
I am assuming you might have a lot of those if you are looking for more convenient way than "not pretty" take?
I would say just loop through them:
foreach ( [ 'firstMethod', 'secondMethod' ] as $callable ) {
try {
$callable();
}
catch ( Exception $e ) {
}
}
Why not write try catch in each function and log the exceptions somewhere.
function firstMethod() {
try {
//code
}
catch (Exception $e) {
logException($e);
}
}
function secondMethod() {
try {
//code
}
catch (Exception $e) {
logException($e);
}
}
function mainMethod() {
firstMethod();
secondMethod();
}
This will help in doing something like this:
function someOtherMethod() {
secondMethod();
}

If condition on save laravel

I had 2 tables. driver and part_time_available in the same form, when I select driver type = parttime, it'll show part_time_available field(day, start_time, end_time).
How to make condition if user choose fulltime. it didn't store part_time_available field to database.
here's my savehandler code so far :
public function saveHandler(Request $request, $obj)
{
try {
DB::beginTransaction();
$obj->fill($request->all());
if (!$obj->save()) {
throw new ValidationException($obj->errors());
}
foreach($request->parttimeAvailabilities as $pta) {
\Log::info($pta);
if (empty($pta['id'])) {
$parttimeAvailability = new PartTimeAvailability();
}
else {
$parttimeAvailability = PartTimeAvailability::find($pta['id']);
}
$parttimeAvailability->driver()->associate($obj);
$pta['driver_id'] = isset($pta['driver_id']);
$parttimeAvailability->day = $pta['day'];
$parttimeAvailability->start_time = isset($pta['start_time']) ? $pta['start_time'] : '00:00:00';
$parttimeAvailability->end_time = isset($pta['end_time']) ? $pta['end_time'] : '00:00:00';
$parttimeAvailability->available = isset($pta['available']);
$parttimeAvailability->save();
};
$obj->save();
if (!$parttimeAvailability->save()) {
throw new ValidationException($parttimeAvailability->errors());
}
DB::commit();
return $this->sendSuccessResponse($request);
} catch (ValidationException $e) {
DB::rollback();
\Log::error($e->errors);
return $this->sendErrorResponse($request, $e->errors);
} catch (Exception $e) {
DB::rollback();
\Log::error($e->getMessage());
return $this->sendErrorResponse($request,'Unable to process. Please contact system Administrator');
}
}
I mean before running foreach, it needs to check it's parttime or not.
any idea ?
You can give a condition before the whole foreach loop. such as:
if($request->get('driver_type') != 'full_time'){
foreach loop
}

execute statement only if an exception occurs but when multiple catch clauses in place

I need to rollback transaction and send correct response to the client if anything in the try block fails so I do it like that:
try {
$wf = $this->createWordForm($requestParams);
$wfl = $this->createWordFormLink($requestParams, $wf);
$wordList = $this->bindWordFormOrLinkToTextWord($requestParams, $wf, $wfl);
$db->commit();
} catch (Kohana_ORM_Validation_Exception $e) {
$exceptionHasOccured = TRUE;
return JsonResponse::ValidationFail($this->response, $e->errors());
} catch (Exception $e) {
$exceptionHasOccured = TRUE;
return JsonResponse::Error($this->response, $e->getMessage());
} finally {
if ($exceptionHasOccured) {
$db->rollback();
}
}
As you can see I'm using finally construction to rollback transaction. Is this correct approach?
You could catch all exceptions in one catch block, as long as your not specifically using the exception type for any purpose.
try {
$wf = $this->createWordForm($requestParams);
$wfl = $this->createWordFormLink($requestParams, $wf);
$wordList = $this->bindWordFormOrLinkToTextWord($requestParams, $wf, $wfl);
$db->commit();
} catch (Exception $e) {
$db->rollback();
if($e instanceof Kohana_ORM_Validation_Exception ) {
return JsonResponse::ValidationFail($this->response, $e->errors());
} else {
return JsonResponse::Error($this->response, $e->getMessage())
}
}

Unable to getParam()

I wanted to add an action on Sales>Order in Magento admin.
Screenshot-
I followed the second method from this blog- www.blog.magepsycho.com/adding-new-mass-action-to-admin-grid-in-magento/
My problem-
I am not able to get the order id (for performing the action on it) in the action controller.
My code in class MyPackage_MyModule_IndexController extends Mage_Adminhtml_Controller_Action
protected function _initOrder()
{
$id = $this->getRequest()->getParam('order_id'); ///TROUBLE HERE
$order = Mage::getModel('sales/order')->load($id);
if (!$order->getId()) {
$this->_getSession()->addError($this->__('This order no longer exists.'));
$this->_redirect('dash/sales_order');
$this->setFlag('', self::FLAG_NO_DISPATCH, true);
return false;
}
Mage::register('sales_order', $order);
Mage::register('current_order', $order);
return $order;
}
public function approvecodAction() {
if ($order = $this->_initOrder()) {
try {
$order->setStatus('codapproved')
->save();
$this->_getSession()->addSuccess(
$this->__('The order has been approved for COD.')
);
}catch (Mage_Core_Exception $e) {
$this->_getSession()->addError($e->getMessage());
}catch (Exception $e) {
$this->_getSession()->addError($this->__('The order has not been approved for COD.'));
Mage::logException($e);
}
$this->_redirect('*/sales_order/view', array('order_id' => $order->getId()));
}
}
Note I copied the above two functions from app/code/core/Mage/Adminhtml/controllers/Sales/OrderController.php and modified for my purpose.
Please tell me how and where to set the parameter order id? Or if they are getting set, then how to get them?
Thanks!
You're dealing with a mass action callback on the controller, so you will be getting an array of values in the parameter instead of a single value. You're going to need to do something more like this in your action method:
public function approvecodAction() {
$orderIds = $this->getRequest()->getPost('order_ids', array());
foreach ($orderIds as $orderId) {
$order = Mage::getModel('sales/order')->load($orderId);
try {
$order->setStatus('codapproved')
->save();
$this->_getSession()->addSuccess(
$this->__('The order has been approved for COD.')
);
}catch (Mage_Core_Exception $e) {
$this->_getSession()->addError($e->getMessage());
}catch (Exception $e) {
$this->_getSession()->addError($this->__('The order has not been approved for COD.'));
Mage::logException($e);
}
}
$this->_redirect('*/sales_order/view', array('order_id' => $order->getId()));
}
Hope that helps!

Categories