<?php


namespace App\Http\Controllers\Pay;
use Request;
use App\Models\UserShop;
use App\Models\PromotionUser;
use App\Models\PaymentTransaction;
use App\Http\Controllers\Controller;
use Srmklive\PayPal\Services\PayPal as PayPalClient;


class PayPalController extends Controller
{

    public function payment($shopping_payment, $payt, $identifier, $payment_for, $payment_for_user_id)
    {
        $provider = new PayPalClient;
        // Through facade. No need to import namespaces

        $provider = \PayPal::setProvider();
        $provider->setApiCredentials(config('paypal'));
        $provider->setAccessToken($provider->getAccessToken());
        $provider->setCurrency('EUR');

        $order = $provider->createOrder([
            "intent"=> "CAPTURE",
            "purchase_units"=> [
                 [
                    "reference_id" => $shopping_payment->reference,
                    "shopping_order_id" =>  $shopping_payment->shopping_order_id,
                    "amount"=> [
                        "currency_code"=> "EUR",
                        "value"=> ($shopping_payment->amount / 100)
                    ],
                     'description' => 'test'
                ]
            ],
            'application_context' => [
                'cancel_url' => route('cancel.paypal_payment', [$payment_for, $payment_for_user_id, $payt->id, $shopping_payment->reference, $identifier]),
                'return_url' => route('success.paypal_payment', [$payment_for, $payment_for_user_id, $payt->id, $shopping_payment->reference, $identifier])
            ]  
                 
        ]);
        $payt->key = $order['id'];
        $payt->save();
        return redirect($order['links'][1]['href'])->send();
        
    }

    public function paymentSuccess($payment_for, $id, $transactionId=false, $reference=false, $identifier=false)
    {
          $payt = PaymentTransaction::findOrFail($transactionId);
          if($payt->shopping_payment->reference != $reference){
                abort(404);
          }
          // Init PayPal 
          $provider = \PayPal::setProvider();
          $provider->setApiCredentials(config('paypal'));
          $provider->setAccessToken($provider->getAccessToken());

          // Get PaymentOrder using our transaction ID
          $order = $provider->capturePaymentOrder($payt->key);
          if(isset($order['type'])){
            abort(403, 'PayPal Type: '.$order['type']);
          } 

          if(!isset($order['status']) || $order['status'] !== "COMPLETED"){
                abort(403, 'Error: Order Status ');
          } 

          if(Request::get('token') !== $payt->key){
             abort(403, 'Error: PayPal token');
          }
          $payt->request = $order['status'];
          $payt->save();

          switch ($payment_for) {
            case 7: //promotion
                $PromotionUser = PromotionUser::findOrFail($id);
                return redirect(route('web_promotion_goto', ['thanksorder', $id, $payt->id, $reference, $identifier]));
            case 8: //shop
                $userShop = UserShop::findOrFail($id);
                return redirect(route('web_shop_goto', ['thanksorder', $id, $payt->id, $reference, $identifier]));
                dd('Your payment has been declend. The payment cancelation page goes here!');
        }

 
    }
   
    public function paymentCancel($payment_for, $id, $transactionId=false, $reference=false, $identifier=false)
    {
        switch ($payment_for) {
            case 7: //promotion
                $PromotionUser = PromotionUser::findOrFail($id);
                return redirect(url($PromotionUser->url));
                dd('Your payment has been declend. The payment cancelation page goes here!');
            case 8: //shop
                $userShop = UserShop::findOrFail($id);
                return redirect(url($userShop->url));
                dd('Your payment has been declend. The payment cancelation page goes here!');
        }

     
    }
  
   

}




