@extends('OmniPayViews::layouts.redirection-layout')

@php
$TAP_PUBLISHABLE_API_KEY = config('omnipay.'.$provider.'.data')['publishable_api_key'];
$currencies = config('omnipay.'.$provider.'.currencies');

$purchase_attempt = \twa\ecomcart\models\EcomPurchasesAttemptModel::find($id);
if(!$purchase_attempt){ return redirect('/'); }
$user = \twa\ecomauth\models\EcomUsersModel::find($purchase_attempt->ecom_users_id);
if(!$user){ return redirect('/'); }

if(!isset($user->first_name) || empty($user->first_name)){
$address = $purchase_attempt->billing_address_details;
$first_name = $address['first_name'];
}else{
$first_name = $user->first_name;
}

try {
    $country = \twa\ecomprofile\models\EcomCountriesModel::where('code' , $user->phone_country_code)->first();
    $phone_code = $country ? "+".$country->phone_code : null;
    $phone_number = (new \Propaganistas\LaravelPhone\PhoneNumber($user->phone,$user->phone_country_code))->formatNational();
} catch (\Throwable $th) {
    $phone_code = "+961";
    $phone_number = "70444309";
}

@endphp


@section('title')
<title>goSell</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<link rel="shortcut icon" href="https://goSellJSLib.b-cdn.net/v1.6.0/imgs/tap-favicon.ico" />
<link href="https://goSellJSLib.b-cdn.net/v1.6.0/css/gosell.css" rel="stylesheet" />
@endsection

@section('content')
<script type="text/javascript" src="https://goSellJSLib.b-cdn.net/v1.6.0/js/gosell.js"></script>

<div id="root"></div>

@endsection


@section('scripts')

<script>
  goSell.config({
  containerID: "root",
  gateway: {
    publicKey: "{{$TAP_PUBLISHABLE_API_KEY}}",
    merchantId: null,
    language: "en",
    contactInfo: true,
    supportedCurrencies: {!! json_encode($currencies) !!},
    supportedPaymentMethods: "all",
    saveCardOption: false,
    customerCards: true,
    notifications: "standard",
    callback: (response) => {
      console.log("response", response);
    },
    onClose: () => {
      console.log("onClose Event");
    },

    labels: {
      cardNumber: "Card Number",
      expirationDate: "MM/YY",
      cvv: "CVV",
      cardHolder: "Name on Card",
      actionButton: "Pay",
    },
    style: {
      base: {
        color: "#535353",
        lineHeight: "18px",
        fontFamily: "sans-serif",
        fontSmoothing: "antialiased",
        fontSize: "16px",
        "::placeholder": {
          color: "rgba(0, 0, 0, 0.26)",
          fontSize: "15px",
        },
      },
      invalid: {
        color: "red",
        iconColor: "#fa755a ",
      },
    },
  },
  customer: {
    id: null,
    first_name: "{{$first_name}}",
    email: "{{$user->email}}",
    phone: {
      country_code: "{{$phone_code}}",
      number: "{{$phone_number}}",
    },
  },
  order: {
    amount: "{{$amount}}",
    currency: "SAR",
    items : null,
    shipping: null,
    taxes: null,
  },
  transaction: {
    mode: "charge",
    charge: {
      saveCard: false,
      threeDSecure: true,
      description: "Test Description",
      statement_descriptor: "Sample",
      reference: {
        transaction: "{{$id}}",
        order: "{{$id}}",
      },
      hashstring:"",
      metadata: {},
      receipt: {
        email: false,
        sms: true,
      },
      redirect: "{{request()->input('url_success')}}",
      post: null,
    },
  },
});
window.addEventListener('load', (event) => {
  goSell.openPaymentPage()
});
window.setTimeout(function(){
  goSell.openPaymentPage();
},5000);


</script>

@endsection
