mardi 27 mars 2018

Laravel Mollie - Get payment info in view

I made a system where the user is able to buy a certain product. I'm using the Mollie API and when the user presses the button to pay he/she gets redirected and this method will be executed:

    $payment = Mollie::api()->payments()->create([
        "amount" => $order->price_sum,
        "description" => "Webshop samenstellen AndCode",
        "redirectUrl" => "http://stage.io/order/" . $order->id,
        "webhookUrl"  => "http://e6895061.ngrok.io/webhook",
    ]);

    return redirect($payment->links->paymentUrl);

In here the Mollie payment is made with the info. Below you find the routes that are associated with the whole payment setup:

Route::post('/order/{id}/betalen', 'OrderController@payment')->name('payOrder');
Route::get('/order/{id}', 'OrderController@show')->name('showConfirm');
Route::post('/webhook', 'OrderController@returnPayment')->name('webhook');

I'm using ngrok and in between the routes with the names payorder and showConfirm the route webhook is being executed. (The webhook is defined in the payment method) Which is

 public function returnPayment(Request $request)
{
    $id = $request->input('id');

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

    return response()->json(['id', $payment->id, 'amount', $payment->amount, 'method', $payment->method, 'status', $payment->status]);


}

Mollie stated in their documentation the following:

A webhook is a URL Mollie will call when a payment's status changes, for example from open to paid. At this URL you should place a script that – when it's called – fetches the payment status and processes it, if it has changed. In case the status changed to paid, you should mark the order as paid.

In the returnPayment method I can fetch the payment details as suggested above such as ID, Status, etc. Now when the user paid he/she gets redirected to this route

Route::get('/order/{id}', 'OrderController@show')->name('showConfirm');

This route fetches the Order using the ID in the URL and displays all its info in the view. Now I need to pass the data of the mollie payment to that specific view in order to do something like $payment->id to show the payment's id. How can I achieve that? The show method for the view looks like this:

public function show($id)
{
    $order = Order::find($id);

    return view('orders.show', compact('order'));
}

Since the returnPayment method is only to fetch the data and do something with it. I can't put a redirect inside the method since its already defined in the payment method. How can I pass the payment data to the showConfirm view?

Thanks in advance!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire