lundi 29 juillet 2019

How do I do a callback function in Laravel after a transaction processes

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.

What I WANT to happen is: 1) Hit the "Submit" button, sends AJAX request 2) Processes the Authorize.net transaction 3) If good, then call a callback function to do all the gruntwork but return a transaction response. 4) Notify the user

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.

Can a callback function help me do this?

Thanks

My current implementation results in a 500 error, and I'm not quite sure what I should do from here...

[ route in web.config ]

// AJAX call to process the transaction, insert the new order, inform the user of success/failure
Route::post('/shop/processtransaction', 'OrderCheckoutController@processTransaction');

[ function processTransaction in OrderCheckoutController.php ]

    public function processTransaction(Request $request) {
        return self::processPaymentAndOrderInsertion($request, 'createOrder');
    }

[ function processPaymentAndOrderInsertion in OrderCheckoutController.php ]

    public function processPaymentAndOrderInsertion(Request $request, callable $createOrderCallback = null) {
        $order_proc = new OrderProcessingTools;
        $transaction_response = $order_proc->processTransaction($request);

        if($transaction_response['success'] === true) {
            self::$createOrderCallback($request, $transaction_response);
        }
        return json_encode($transaction_response);
    }

[ my callback function ]


    public function createOrder(Request $request, $transaction_response) {
        $order_proc = new OrderProcessingTools;
        $new_order = $order_proc->insertNewOrder($request);
        $new_order->payment_status_id = $transaction_response['response_data']['order_payment_status_id'];
        $new_order->save();

        // record the payment transaction
        $order_proc->insertOrderPaymentData($new_order, $transaction_response);

        // insert the travelers for this order
        $travelers = $order_proc->insertOrderTravelers($new_order);

        // insert order inbound shipment record
        $order_proc->insertInboundOrderShipping($new_order->id);

        // generate inbound shipping airbill
        $order_proc->generateInboundShippingAirbill($new_order->id);

        /// generate the invoive
        $order_proc->generateInvoice($new_order);

        // send new order notification to the user
        $order_proc->sendNewOrderNotificationToUser($new_order);

        // send new order notification to admin
        $order_proc->sendNewOrderNotificationToAdmin($new_order);

        // finally kill the session variable
        $_SESSION['travelers'] = [];        
    }

[ my previous non-asynchronous implementation looks like this...]


    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->processTransaction($request);

        if($transaction_response['success'] === true) {
            // insert a new order
            $new_order = $order_proc->insertNewOrder($request);
            $new_order->payment_status_id = $transaction_response['response_data']['order_payment_status_id'];
            $new_order->save();

            // record the payment transaction
            $order_proc->insertOrderPaymentData($new_order, $transaction_response);

            // insert the travelers for this order
            $travelers = $order_proc->insertOrderTravelers($new_order);

            // insert order inbound shipment record
            $order_proc->insertInboundOrderShipping($new_order->id);

            // generate inbound shipping airbill
            $order_proc->generateInboundShippingAirbill($new_order->id);

            /// generate the invoive
            $order_proc->generateInvoice($new_order);

            // send new order notification to the user
            $order_proc->sendNewOrderNotificationToUser($new_order);

            // send new order notification to admin
            $order_proc->sendNewOrderNotificationToAdmin($new_order);

            // finally kill the session variable
            $_SESSION['travelers'] = [];
        }

        // either good news or bad news at this point.. 
        return json_encode($transaction_response);
    }

When I try it this way, this is the error that is returned...

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire