【问题标题】:How do I do a callback function in Laravel after a transaction processes事务处理后如何在 Laravel 中执行回调函数
【发布时间】:2019-12-07 07:12:20
【问题描述】:

我在这里要做的是在 Laravel 5.4 控制器中实现回调函数。这使用 Authorize.net 处理信用卡交易,然后将一堆东西插入数据库,发送一些消息,开具发票和空运单,等等。

我想要发生的是:

  1. 点击“提交”按钮,发送 AJAX 请求
  2. 处理 Authorize.net 交易
  3. 如果好,则调用回调函数来完成所有繁琐的工作,但返回事务响应。 4) 通知用户

我想这样做的原因是我希望用户等待最少的时间来查看他们的付款处理结果,而不必再等待 5 秒左右盯着等待去的纺车订单完成页面。

回调函数可以帮助我做到这一点吗?

谢谢

我当前的实现导致 500 错误,我不太确定我应该从这里做什么...

[web.config 中的路由]

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

[OrderCheckoutController.php 中的函数 processTransaction]

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

[OrderCheckoutController.php中的函数processPaymentAndOrderInsertion]

    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);
    }

[我的回调函数]


    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'] = [];        
    }

[我之前的非异步实现是这样的……]


    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);
    }

当我以这种方式尝试时,这是返回的错误...

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

【问题讨论】:

    标签: php laravel laravel-5 callback closures


    【解决方案1】:

    您需要传递callable 类型,但仅传递方法的字符串名称将不起作用,因为 PHP 只会检查它是否是全局函数。

    你需要传递一个数组,第一个参数是要调用方法的对象,第二个参数是函数名,如下所示:

    return self::processPaymentAndOrderInsertion($request, [$this, 'createOrder']);
    

    文档:https://www.php.net/manual/en/language.types.callable.php

    【讨论】:

    • 所以当我这样做时,我得到这个错误: {message: "Function name must be a string",...} 异常:"Symfony\Component\Debug\Exception\FatalThrowableError" 文件: "F:\wamp64\www\uspassports\public_html\app\Http\Controllers\OrderCheckoutController.php" 行:112 消息:"函数名必须是字符串"
    • 您确定没有任何语法错误或类似错误吗?它绝对有效,请参阅:3v4l.org/D7rCp
    猜你喜欢
    • 2013-09-24
    • 2015-11-08
    • 2018-03-21
    • 1970-01-01
    • 2013-02-02
    • 2022-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多