laravel mollie如何接近网络挂钩

时间:2018-09-28 10:28:49

标签: php laravel webshop mollie

我在哪里可以更新Webhook。当我创建付款时,它可以在mollie网站上使用,但是它们具有由mollie生成的ID。我分配给产品的ID在元数据中,但是似乎无法通过元数据ID来获取付款状态。这是我当前的代码,应该会为客户的电子邮件创建发票。

我使用< >更改了一些数据,因此它不显示敏感数据。

public function webhook(Request $request)
{

    $mollie = new Mollie_API_Client;
    $mollie->setApiKey('<API_TESTKEY>');
    $id = $request->id;

    /*
     * Retrieve the payment's current state.
     */
    $payment    = $mollie->payments->get($id);
    $order_id   = $payment->metadata->order_id;
    $user_id    = $payment->metadata->user_id;

    $user       = User::findOrFail($user_id);
    $shipping   = $user->addresses()->where('type', 'shipping')->first();
    $billing    = $user->addresses()->where('type', 'billing')->first();
    $order      = Order::findOrFail($order_id);

    /*
     * Update the order in the database.
     */
    if ($payment->isPaid() == TRUE)
    {

        $order->update([
            'status' => 'betaald'
        ]);

        foreach ($order->products()->get() as $product) {
             //update stock amount
            $qty = $product->pivot->quantity;
            $stock = $product->stock - $qty;
            $product->update([
                'stock' => $stock
            ]);
        }

        // load the pdf view and send request data
        $pdf = PDF::loadView('pages.frontend.orders.invoice.pdf', 
                compact('user', 'order', 'billing'));
        // Setup the paper size and orientation
        $pdf->setPaper('A4', 'portrait');
        $output = $pdf->output();


        //send order confirmation mail to client.
        Mail::send('pages.frontend.orders.emails.order-confirmation',[
            'user' => $user,
            'order' => $order,
            'shipping' => $shipping,
            'billing' => $billing

        ], function ($m) use ($user,$order,$output) {
            $m->from('<MAIL>', '<MAIL_USER>');
            $m->to($user->email, $user->first_name . ' ' . $user->surname)
                ->subject('Bevestiging Bestelling');
            $m->attachData($output, 'invoice.pdf');

        });

        //send confirmation mail to shopowner.
        Mail::send('pages.frontend.orders.emails.client-order',[
            'user' => $user,
            'order' => $order,
            'shipping' => $shipping,
            'billing' => $billing

        ], function ($m) use ($user,$order,$output) {
            $m->from('<MAIL>', '<MAIL_USER>');
            // $m->to('<MAIL>' , '<MAIL_USER>')->subject('Bestelling Klant');
            $m->to('<MAIL>', '<MAIL_USER>')->subject('Bestelling Klant');
            $m->attachData($output, 'invoice.pdf');

        });

        Flash::success('Bedankt voor uw bestelling! 
                U ontvangt direct een mail met uw besteloverzicht.');

    }
    elseif ($payment->isOpen() == FALSE)
    {
        $order->update([
            'status' => 'betaling mislukt'
        ]);

        Flash::error('Oeps! Het lijkt er op dat uw betaling is mislukt of geannuleerd');
    }
}

0 个答案:

没有答案