0

我正在尝试找出一种方法来完成这项工作。最初,我拥有的是一个使用 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');       
}
}

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

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

4

0 回答 0