<?php

/*
 * clearingtype
elv Debit payment
cc Credit card
rec Invoice
cod Cash on delivery
vor Prepayment
sb Online Bank Transfer
wlt e-wallet
fnc Financing
  */
/*private $payment_methods= [
        'wlt#PPE' => 'PayPal',
        'cc' => 'CreditCard',
        'sb#GPY' => 'giropay',
        'sb#PNT' => 'Sofort',
        'wlt#PDT' => 'paydirekt',
        'fnc' => 'Rechnungskauf',
        'pref' => 'Vorauskasse',
    ];*/


namespace App\Http\Controllers\Pay;

use App\Http\Controllers\Controller;
use App\Models\PaymentTransaction;
use App\Models\ShoppingPayment;
use App\Services\Payone;
use Illuminate\Http\Request;
use Util;
use Yard;


class PayController extends Controller
{



    private $default = [];

    private $personalData = [];

    private $method = [];
    private $prepayment = [];

  /*  private $onlineTransfer = [];
    private $creditCard = []; */
    private $deliveryData = [];


   // private $payment_method;
    private $urls = [];

    private $shopping_user;
    private $shopping_order;
    private $shopping_payment;

    private $reference;

    public function __construct() {

    }

    public function init($shopping_user, $shopping_order){
        $this->shopping_user = $shopping_user;
        $this->shopping_order = $shopping_order;
        $this->default['mode'] = $this->shopping_order->mode;
    }

    public function getShoppingPayment(){
        return $this->shopping_payment;
    }

    //make Payone payment
    public function setPrePayment($payment_method, $amount, $currency, $ret = []){

        $this->reference = substr(uniqid('m', false), 0, 16);
        $this->setMethod($payment_method, $ret);

        $this->prepayment = [
             "reference" => $this->reference, // a unique reference, e.g. order number
             "amount" => $amount, // amount in smallest currency unit, i.e. cents
             "currency" => $currency,
             "param" => $this->shopping_order->id,

         ];

        $this->shopping_payment = ShoppingPayment::create([
            'shopping_order_id' => $this->shopping_order->id,
            'clearingtype' => $this->method["clearingtype"],
            'wallettype' => $this->method["wallettype"],
            'onlinebanktransfertype' => $this->method["onlinebanktransfertype"],
            'reference' => $this->reference,
            'amount' => $amount,
            'currency' => $currency,
            'mode' => $this->shopping_order->mode,
        ]);

        $this->default['mode'] = $this->shopping_order->mode;
        return $this->reference;
    }

    public function setPersonalData(){
        $this->personalData = [
            "firstname" => $this->shopping_user->billing_firstname,
            "lastname" => $this->shopping_user->billing_lastname, // mandatory
            "street" => $this->shopping_user->billing_address,
            "zip" => $this->shopping_user->billing_zipcode,
            "city" => $this->shopping_user->billing_city,
            "country" => ($this->shopping_user->billing_country) ? $this->shopping_user->billing_country->code : "DE", // mandatory
            "email" => $this->shopping_user->billing_email,
           // "language" => ($this->shopping_user->billing_country) ? strtoupper($this->shopping_user->billing_country->code) : "DE", // mandatory
            "language" => "DE",
        ];
    }

    private function setMethod($payment_method, $ret = []){

        //vorkasse
        if($payment_method === 'vor'){
            $this->method = [
                "clearingtype" => "vor",
                "wallettype" => "",
                'onlinebanktransfertype' => "",
                "request" => "authorization",
            ];
        }

        //Rechnungskauf
        if($payment_method === 'fnc'){
            $this->method = [
                "clearingtype" => "fnc",
                "wallettype" => "",
                'onlinebanktransfertype' => "",
                "request" => "authorization",
            ];
        }

    }

    public function ResponseData($identifier){

        $request = array_merge($this->default, $this->personalData, $this->deliveryData, $this->method, $this->prepayment, $this->urls);
        //RECHNUNG MIV

        $payt = PaymentTransaction::create([
            'shopping_payment_id' => $this->shopping_payment->id,
            'request' =>  $this->method['request'],
            'txid' => 0,
            'userid' => 0,
            'status' => $this->shopping_payment->clearingtype,
            'transmitted_data' => $request,
            'txaction' => 'open',
            'mode' => $this->shopping_payment->mode,
        ]);


        Util::setUserHistoryValue(['status'=>5], $identifier);
        return redirect(route('user_checkout_final', [$payt->id, $this->reference, $identifier]));

    }
}




