等待在函数内调用的函数的响应

问题描述

我正在尝试找出一种方法来完成这项工作。最初我拥有的是一个使用 Mollie 的综合支付服务提供商。每当有人购买东西时,都会调用多个函数

这是开始付款的地方:

axios.post('/api/molliepayment',{ products,totalPrice,paymentMethod,isSingleProduct }).then(response => {
    window.open.location.href = response.data.data._links.checkout.href;
}).catch(() => {});

我一直使用 Mollie 的方式是在我的控制器中创建付款并将付款作为 json 响应返回到 Vue 视图:

$payment = Mollie::api()->payments()->create([
    'amount' => [
        'currency' => 'EUR',// Type of currency you want to send
        'value' => $totalPrice,// You must send the correct number of decimals,thus we enforce the use of strings
    ],'method' => $method,'description' => "Beschrijving",'webhookUrl' => route('webhooks.mollie'),'redirectUrl' => route('payment.success',['is_singular' => $request->get('isSingleProduct') ? $request->get('isSingleProduct') : 0]),"Metadata" => [
        "products" => $products,"user_id" => Auth::id(),"user" => Auth::user(),"totalPrice" => $totalPrice,],]);

$payment = Mollie::api()->payments()->get($payment->id);

现在当窗口打开时 webhook 被调用

public function webhook(Request $request) {
    if (! $request->has('id')) {
        return;
    }
    $payment = Mollie::api()->payments()->get($request->id);

//Log::info('Test '.$payment->Metadata->order_id);

$statusOfPayment='';

if ($payment->isPaid() && !$payment->hasrefunds() && !$payment->hasChargebacks()) {
    /*
     * The payment is paid and isn't refunded or charged back.
     * At this point you'd probably want to start the process of delivering the product to the customer.
     */
       $statusOfPayment='paid';

} elseif ($payment->isopen()) {
    /*
     * The payment is open.
     */
     $statusOfPayment='open';
} elseif ($payment->isPending()) {
    /*
     * The payment is pending.
     */
     $statusOfPayment='pending';

} elseif ($payment->isFailed()) {
    /*
     * The payment has Failed.
     */
    $statusOfPayment='Failed';

} elseif ($payment->isExpired()) {
    /*
     * The payment is expired.
     */
} elseif ($payment->isCanceled()) {
    /*
     * The payment has been canceled.
     */

      $statusOfPayment='expired';

} elseif ($payment->hasrefunds()) {
    /*
     * The payment has been (partially) refunded.
     * The status of the payment is still "paid"
     */

        $statusOfPayment='partially refunded';
} elseif ($payment->hasChargebacks()) {
    /*
     * The payment has been (partially) charged back.
     * The status of the payment is still "paid"
     */
      $statusOfPayment='partially charged back';
}

$order = Order::create([
    'user_id' => $payment->Metadata->user_id,'address_id' => 1,'order_status' => $statusOfPayment,'total_price' => $payment->Metadata->totalPrice,'is_delivered' => 0,]);
// Log::info($payment->Metadata->products);
// $order_details = $order->order_details()->create([
//     'quantity' => '1',//     'totalPrice' => '8000',// ]);
foreach($payment->Metadata->products as $product) {
    // Log::info($product->id);
    
    $order_details = $order->order_details()->create([
        'product_id' => $product->id,'quantity' => $product->quantity,'total_price' => $product->price * $product->quantity,]);

    $productUnits = Product::find($product->id);

    $productUnits->decrement('units',$product->quantity);

    $productUnits->save();
    
}
Mail::to($payment->Metadata->user->email)->send(new OrderCreatedMail($order));

}

最后,当支付成功时,这个函数调用

public function paymentSuccess(Request $request,$id) {
    //Hier de bestelling afronden en de status op betaald zetten.
    //$payment = Mollie::api()->payments->get($request->id);
    // if (! $request->has('id')) {
    //     return;
    // }
    // $payment = Mollie::api()->payments()->get($request->id);

// if ( $payment->Metadata->isSingleProduct == true ) {
//     return redirect()->to('/dashboard/orders');           
// } else {
//     return redirect()->to('/dashboard/orders?success=1');
// }
Log::info($id);
if ( $id == 0 ) {
    return redirect()->to('/dashboard/orders?clear_cart=1');
} else {
    return redirect()->to('/dashboard/orders');       
}
}

因为我正在创建一个 SPA 应用程序,前端使用 Vue,后端使用 Laravel,我想尝试等待 paymentSuccess 函数的响应并将其返回到 Vue 视图,而不是将用户重定向到视图使用路由器助手。现在,我不太确定如何做到这一点。

有没有办法让我做到这一点?提前致谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)