Icetech Payment v0.0.3

@dependency App\Models\Orders
@dependency App\Providers\AppServiceProvider
-



https://medium.com/laravelapps/laravel-package-development-step-by-step-guide-743d9e5e076e


FIRST set up dev domain, pointing to same /public_html

1. (one time) go to package folder, composer init blalba

2. go to main composer.json

3. add to psr-4 composer.json the package namespace -> path.

    "autoload": {
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "psr-4": {
            "App\\": "app/",
            "Icetech\\Payment\\": "packages/icetech/payment/src"
        }
    },


AFTER THAT, composer dump-autoload

3.b go to config\app.php
add to providers:
        Icetech\Payment\PaymentServiceProvider::class,


'Payment' => Icetech\Payment\Payment::class,


4. in App\Http\Middleware\VerifyCsrfToken (disable csrf)

  protected $except = [
        'admin/*',
        'payment/*',
    ];

5. in App\Providers\AppServiceProvider
   // set env based of domain
        if (starts_with(request()->getHost(), ['www.dev.', 'dev.'])) {
            config(['app.env' => 'dev']);
        }

6. go to App\Models\Order   

use App\Mail\OrderAppointed;


    event:
  
        
       static::created(function($model) {
           $model->code = $model->generateCode();
           $model->payment_mode = config('app.env') === 'production' ? 'live' : 'test';
           $model->save();
        });


        public function transactions()
        {
            return $this->hasMany('Icetech\Payment\Models\Transaction');
        }

        public function transaction()
        {
            return $this->hasOne('Icetech\Payment\Models\Transaction')->orderByDesc('id');
        }

          public function isCC() {
                return $this->payment_type === 'cc' || $this->payment_type === 'cc_rate';
            }



    public function isNew() {
        return $this->status === 'new';
    }

    public function canPayWithCC()
    {
        return $this->isNew() && $this->payment_type == 'cc' && !$this->is_paid && now() >= $this->created_at->addHours(2) && !in_array($this->payment_status, ['paid', 'refunded', 'preauthorized']); // 'canceled',
    }

  public function hasValidPayment()
    {
        return in_array($this->payment_status, ['paid', 'preauthorized', 'pending']);
    }

    public function getPaymentStatusClass()
        {
            switch ($this->payment_status) {
                case 'paid':
                    return 'success';
                case 'preauthorized':
                case 'pending':
                    return 'warning';
            }
            return 'danger';
        }

    public function getPaymentStatusMessage()
    {
        switch ($this->payment_status) {
            case 'paid':
                return 'platit';
            case 'pending':
                return 'in procesare';
            case 'preauthorized':
                return 'preautorizat';
        }
        return 'neplatit';
    }

      public function isPaid() {
            return $this->payment_status === 'paid';
        }


    public function isPreauthorized() {
        return $this->payment_status === 'preauthorized';
    }

    public function isTest()
    {
        return $this->payment_mode === 'test';
    }

        public function hasInstallment() {
            return $this->payment_type === 'cc_rate';
        }


public function generateCode()
    {
        $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $random_string = '';
        for ($i = 0; $i < 4; $i++) {
            $random_string .= $chars[rand(0, 25)];
        }
       
        return implode('-', [$random_string, $this->id]);
    }

    
     public function sendEmailConfirmed($force = false)
    {
        // if this is already sent..
        if (!$force && $this->mail_sent) {
            return;
        }
        
        Mail::to($this->deliveryContact)
                ->bcc(config('mail.office'))
                ->send(new OrderAppointed($this));
        $this->mail_sent = true;
        $this->save();
    }



7. go to App\Http\Controllers\Admin\OrderController.php

    index() ->  
        $orders = Order::orderByDesc('id')
                ->when(request()->get('q'), function ($query) {
                    return $query->search(request()->get('q'));
                })
                ->where(request()->except(['q', 'page']))
                ->paginate(config('pagination.admin.items_per_page'));

        return view()->first(['payment::admin.order.index','admin.order.index'], compact('orders'));
    
    show() ->
        return view()->first(['payment::admin.order.show', 'admin.order.show'], compact('order'));


8. view: site.checkout.index, se pune un noi radio cu payment_mode = cc

9. go to App\Http\Controllers\Site\Site\CheckoutController.php

   inainte de a se trimite email-ul de confirmare:

    if ($request->get('payment_type') === 'cc') {
            return redirect(route('payment.redirect', ['order_code' => $order->code]));
        }


    replace blocul de code pt trimiterea email cu: 
   $order->sendEmailConfirmed();

composer dump-autoload
