Skip to content
Snippets Groups Projects

PaymentController

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Jørgen Birkhaug

    index = handlekurv handleCheckout laster seamless view handleExistingCheckout er paymentUrl handleCompleted er completedUrl

    • Det gjøres en capture via charge() i handleCompleted()
    • Det gjøres evt cancel via cancelPayment() i handleCompleted()

    charge() og cancelPayment() finnes i Payment.php

    Edited
    PaymentController.php 3.75 KiB
    <?php
    
    namespace App\Http\Controllers;
    
    use App\Events\PaymentCompleted;
    use App\Http\Requests\PaymentRequest;
    use App\Payment;
    use Illuminate\Support\Facades\Cache;
    use Illuminate\Support\MessageBag;
    
    class PaymentController extends Controller
    {
        protected $errors = [];
    
        /**
         * Create a new controller instance.
         *
         * @return void
         */
        public function __construct()
        {
            $this->middleware('auth');
        }
    
        /**
         * Show the payment dashboard.
         *
         * @return \Illuminate\Contracts\Support\Renderable
         */
        public function index()
        {
            try {
                auth()->user()->fsCreateInvoice();
            } catch (\Exception $e) {
                return view('home', [
                    'latestInvoice' => null,
                ])->withErrors(['fs' => $e->getMessage()]);
            }
    
            try {
                $invoice = auth()->user()->fsInvoice();
            } catch (\Exception $e) {
                return view('home', [
                    'latestInvoice' => null,
                ])->withErrors(['fs' => $e->getMessage()]);
            }
    
            session(['invoice' => $invoice]);
    
            return view('home', [
                'latestInvoice' => $invoice,
            ]);
        }
    
        /**
         * Show the PayEx Card Payment Page
         *
         * @return \Illuminate\Contracts\Support\Renderable
         */
        public function handleCheckout(PaymentRequest $payment)
        {
            try {
                $data = $payment->checkout();
            } catch (\Exception $e) {
                return redirect()->back()->withInput()->withErrors(new MessageBag(['payex' => $e->getMessage()]));
            }
    
            session(['payment_id' => $data['payment_id']]);
            session(['href' => $data['href']]);
    
            return view('payex_checkout', [
                'payment_id' => $data['payment_id'],
                'href' => $data['href'],
                'type' => $data['type']
            ]);
        }
    
        public function handleExistingCheckout(Payment $payment)
        {
            abort_unless($payment->user_id == auth()->user()->id, 403);
            abort_unless(session()->has('href'), 422);
    
            if ($payment->transaction_number) {
                return redirect()->route('confirmation');
            }
    
            return view('payex_checkout', [
                'payment_id' => $payment->id,
                'href' => session('href'),
                'type' => $payment->paymentType()
            ]);
        }
    
    
        /**
         * Show the confirmation page
         * Perform charge
         * Update FS with payment information
         *
         * @param  \Illuminate\Session\Store $payment_id
         *
         * @return \Illuminate\Contracts\Support\Renderable
         */
        public function handleCompleted()
        {
            $payment = Payment::find(session('payment_id', null));
    
            if (!$payment) {
                return redirect()->route('home');
            }
    
            $payment->event('Load confirmation page');
    
    
            if (!$payment->transaction_number) {
    
                try {
    
                    // throw new \Exception('e');
    
                    $transaction = $payment->charge();
                    $payment->complete($transaction);
    
                } catch (\Exception $e) {
    
                    try {
                        $transaction = $payment->cancelPayment();
                        $payment->cancel($transaction);
                    } catch (\Exception $e) {
                        throw $e;
                    }
    
                    session()->forget('href');
                    $payment->event('Error: No transaction number', $message = __('An error occurred. You have not been charged.'));
                    return view('summary')->withErrors(['payex' => $message]);
                }
    
            }
    
    
            session()->flash('status', __('A receipt has been sent to ') .  auth()->user()->email);
            session()->forget('payment_id');
            session()->forget('href');
            // auth('web')->logout();
            return view('summary');
        }
    }
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment