title: How do I do a callback function in Laravel after a transaction processes
body: <p>What I'm trying to do here is to implement a callback function in a Laravel 5.4 controller.  This uses Authorize.net to process a credit card transaction, then inserts a bunch of stuff into the database, sends some messages, makes an invoice and airbill, and so on.</p>
  
  <p>What I WANT to happen is:</p>
  
  <ol>
  <li>Hit the "Submit" button, sends AJAX request</li>
  <li>Processes the Authorize.net transaction</li>
  <li>If good, then call a callback function to do all the gruntwork but return a transaction response.
  4) Notify the user</li>
  </ol>
  
  <p>The reason I wanna do it this way is that I want the user to wait the minimum amount of time to see the result of their payment processing without having to wait another 5 seconds or so staring at a spinning wheel waiting to go to the order complete page.</p>
  
  <p>Can a callback function help me do this?</p>
  
  <p>Thanks</p>
  
  <p>My current implementation results in a 500 error, and I'm not quite sure what I should do from here...</p>
  
  <p>[ route in web.config ]</p>
  
  <pre class="lang-php prettyprint-override"><code>// AJAX call to process the transaction, insert the new order, inform the user of success/failure
  Route::post('/shop/processtransaction', 'OrderCheckoutController@processTransaction');
  </code></pre>
  
  <p>[ function processTransaction in OrderCheckoutController.php ]</p>
  
  <pre class="lang-php prettyprint-override"><code>    public function processTransaction(Request $request) {
          return self::processPaymentAndOrderInsertion($request, 'createOrder');
      }
  </code></pre>
  
  <p>[ function processPaymentAndOrderInsertion in OrderCheckoutController.php ]</p>
  
  <pre class="lang-php prettyprint-override"><code>    public function processPaymentAndOrderInsertion(Request $request, callable $createOrderCallback = null) {
          $order_proc = new OrderProcessingTools;
          $transaction_response = $order_proc-&gt;processTransaction($request);
  
          if($transaction_response['success'] === true) {
              self::$createOrderCallback($request, $transaction_response);
          }
          return json_encode($transaction_response);
      }
  </code></pre>
  
  <p>[ my callback function ]</p>
  
  <pre class="lang-php prettyprint-override"><code>
      public function createOrder(Request $request, $transaction_response) {
          $order_proc = new OrderProcessingTools;
          $new_order = $order_proc-&gt;insertNewOrder($request);
          $new_order-&gt;payment_status_id = $transaction_response['response_data']['order_payment_status_id'];
          $new_order-&gt;save();
  
          // record the payment transaction
          $order_proc-&gt;insertOrderPaymentData($new_order, $transaction_response);
  
          // insert the travelers for this order
          $travelers = $order_proc-&gt;insertOrderTravelers($new_order);
  
          // insert order inbound shipment record
          $order_proc-&gt;insertInboundOrderShipping($new_order-&gt;id);
  
          // generate inbound shipping airbill
          $order_proc-&gt;generateInboundShippingAirbill($new_order-&gt;id);
  
          /// generate the invoive
          $order_proc-&gt;generateInvoice($new_order);
  
          // send new order notification to the user
          $order_proc-&gt;sendNewOrderNotificationToUser($new_order);
  
          // send new order notification to admin
          $order_proc-&gt;sendNewOrderNotificationToAdmin($new_order);
  
          // finally kill the session variable
          $_SESSION['travelers'] = [];        
      }
  </code></pre>
  
  <p>[ my previous non-asynchronous implementation looks like this...]</p>
  
  <pre class="lang-php prettyprint-override"><code>
      public function processTransaction(Request $request) {
          // :: POST 
          // Process the Authorize.net transaction, insert the order, generate invoices 
          // and airbills, send notifications
  
          $order_proc = new OrderProcessingTools;
          $transaction_response = $order_proc-&gt;processTransaction($request);
  
          if($transaction_response['success'] === true) {
              // insert a new order
              $new_order = $order_proc-&gt;insertNewOrder($request);
              $new_order-&gt;payment_status_id = $transaction_response['response_data']['order_payment_status_id'];
              $new_order-&gt;save();
  
              // record the payment transaction
              $order_proc-&gt;insertOrderPaymentData($new_order, $transaction_response);
  
              // insert the travelers for this order
              $travelers = $order_proc-&gt;insertOrderTravelers($new_order);
  
              // insert order inbound shipment record
              $order_proc-&gt;insertInboundOrderShipping($new_order-&gt;id);
  
              // generate inbound shipping airbill
              $order_proc-&gt;generateInboundShippingAirbill($new_order-&gt;id);
  
              /// generate the invoive
              $order_proc-&gt;generateInvoice($new_order);
  
              // send new order notification to the user
              $order_proc-&gt;sendNewOrderNotificationToUser($new_order);
  
              // send new order notification to admin
              $order_proc-&gt;sendNewOrderNotificationToAdmin($new_order);
  
              // finally kill the session variable
              $_SESSION['travelers'] = [];
          }
  
          // either good news or bad news at this point.. 
          return json_encode($transaction_response);
      }
  </code></pre>
  
  <p>When I try it this way, this is the error that is returned...</p>
  
  <pre><code>xception: "Symfony\Component\Debug\Exception\FatalThrowableError"
  file: "F:\wamp64\www\uspassports\public_html\app\Http\Controllers\OrderCheckoutController.php"
  line: 105
  message: "Argument 2 passed to App\Http\Controllers\OrderCheckoutController::processPaymentAndOrderInsertion() must be callable or null, string given
  </code></pre>