Skip to content

Returning data from within a Transaction #1680

Closed
@abishekrsrikaanth

Description

@abishekrsrikaanth

I have the following code snippet which I can't seem to get it working.

    $paymentGatewayTransaction = $this->processTransaction($data);
    DB::connection('new_db')->transaction(function() use($paymentGatewayTransaction){
              //Performing all DB transactions here to insert Order Information.
              //$orderObj is created here
              if($paymentGatewayTransaction['Status'] == "APPROVED")
                      return Response::json(array('Status'=>'Success','Order'=>$orderObj->id),'200')
              else
                      return Response::json(array('Status'=>'Error'),'200')
    });

    function processTransaction($data){
               //Process Gateway Transaction
               if($gatewayWasSuccessfull)
                    return array('Status'=>'APPROVED');
             else
                   return array('Status'=>'DECLINED');
    }

The return Response::json() on lines 5 and 7 don't seem to have any effect at all. The response never reaches the browser.

If I move the if condition from lines 4 thru 8 outside of the closure function, it works fine.

I had to end up doing the following as a workaround.

    $paymentGatewayTransaction = $this->processTransaction($data);
    $orderObj = "";
    DB::connection('new_db')->transaction(function() use($paymentGatewayTransaction, &$orderObj){
              //Performing all DB transactions here to insert Order Information.
              **//$orderObj is set here**
    });

   if($paymentGatewayTransaction['Status'] == "APPROVED")
             return Response::json(array('Status'=>'Success','Order'=>$orderObj->id),'200');
   else
             return Response::json(array('Status'=>'Error'),'200');

    function processTransaction($data){
               //Process Gateway Transaction
               if($gatewayWasSuccessfull)
                    return array('Status'=>'APPROVED');
             else
                   return array('Status'=>'DECLINED');
    }

Created the object $orderObj and made it as a variable by reference to the transaction to use it outside the closure function.

Am not sure where the issue is. Is this a bug?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions