<?php
session_start();
if(!function_exists("_")){
  function _($text){
    return $text;
  }
}


$isCompany = false;
if($_SESSION["COMPANY-USER-GM"]!=""){
    $isCompany = true;
}

$current = json_decode($_SESSION["order"], true);

if($isCompany){
    ?>
    <input type="hidden" name="CompanyQuote" value="true">
     <div id="totals">
        <h4><?=_("Obtendrás una ")?><strong> <?=($current["type"]=="1"?_("Traducción certificada"):_("Traducción estándar"))?> </strong> <?=_("con los siguientes detalles")?>:  </h4>
        <h4><?=$current["word"]?></h4>
      <?php
      if($current["addon"]!=""){
        ?>
          <p> <strong><?=_("Adicional")?>:</strong> <?=$current["addon"]?></p>
        <?php
      }
      ?>
      <?php
      if($current["delivery"]!=""){
        ?>
          <p><strong><?=_("Forma de entrega")?>: </strong><?=$current["delivery"]?></p>
        <?php
      }
      ?>
        <div class="">
            <h4><?=_("Total estimado")?></h4>
            <h2>$<?=number_format($current["total"],2 )?></h2>
        </div>
    </div>
    <?php
}else{
    
if($current["only-quote"]=="0"){
    ?>
    <br><br>
    
    <div id="totals">
        <h4><?=_("Vas a pagar por una ")?><strong> <?=($current["type"]=="1"?_("Traducción certificada"):_("Traducción estándar"))?> </strong> <?=_("con los siguientes detalles")?>:  </h4>
        <h4><?=$current["word"]?></h4>
      <?php
      if($current["addon"]!=""){
        ?>
          <p> <strong><?=_("Adicional")?>:</strong> <?=$current["addon"]?></p>
        <?php
      }
      ?>
      <?php
      if($current["delivery"]!=""){
        ?>
          <p><strong><?=_("Forma de entrega")?>: </strong><?=$current["delivery"]?></p>
        <?php
      }
      ?>
        <div class="">
            <h4><?=_("Total estimado")?></h4>
            <h2>$<?=number_format($current["total"],2 )?></h2>
        </div>
    </div>
    <input type="hidden" name="PaymentId" id="payment-id">

<script>
    noPay = false;
    var button = $("[href='#finish']").first();

braintree.dropin.create({
  authorization: token,
  selector: '#dropin-container'
}, function (err, instance) {
  $(button).click(function(){
    instance.requestPaymentMethod(function (err, payload) {
      $.post("../braintree/index.php",{payment_method_nonce: payload.nonce, total: totalPayment}).done(function(result){
          
          $("#payment-id").val(result);
          if(result==""){
              alert("<?=_("Tu pago fue declinado, por favor intenta de nuevo o contáctanos para darte una mejor solución")?>")
          }else{
            submitForm1();  
          }
          //
      });
    });  
  })
});
</script>


<div id="dropin-container"></div>

<hr>
<strong>Or pay with paypal</strong>
<input type="hidden" name="PaypalId" id="paypalid">
    <input type="hidden" name="PayerId" id="paypalpayerid">
   <div id="paypal-button-container"></div>
      <!-- Sample PayPal credentials (client-id) are included -->

      <script>
          const fundingSources = [
              paypal.FUNDING.PAYPAL
          ]

          for (const fundingSource of fundingSources) {
              const paypalButtonsComponent = paypal.Buttons({
                  fundingSource: fundingSource,

                  // optional styling for buttons
                  // https://developer.paypal.com/docs/checkout/standard/customize/buttons-style-guide/
                  style: {
                      shape: 'rect',
                      height: 40,
                  },

                  // set up the transaction
                  createOrder: (data, actions) => {
                      // pass in any options from the v2 orders create call:
                      // https://developer.paypal.com/api/orders/v2/#orders-create-request-body
                      const createOrderPayload = {
                          purchase_units: [
                              {
                                  amount: {
                                      value: '<?=$current["total"]?>',
                                  },
                              },
                          ],
                      }

                      return actions.order.create(createOrderPayload)
                  },

                  // finalize the transaction
                  onApprove: (data, actions) => {
                      const captureOrderHandler = (details) => {
                          console.log(details)
                          if(details.id){
                              $("#payment-id").val(details.id);
                              
                              //$("#paypalpayerid").val(details.payer.payer_id);

                              submitForm1(); 
                          }else{
                              alert("Error al procesar pago")
                          }
                      }

                      return actions.order.capture().then(captureOrderHandler)
                  },

                  // handle unrecoverable errors
                  onError: (err) => {
                      console.error(
                          'An error prevented the buyer from checking out with PayPal',
                      )
                  },
              })

              if (paypalButtonsComponent.isEligible()) {
                  paypalButtonsComponent
                      .render('#paypal-button-container')
                      .catch((err) => {
                          console.error('PayPal Buttons failed to render')
                      })
              } else {
                  console.log('The funding source is ineligible')
              }
          }
      </script>



    <!--
    <div id="payment1">
        <div class="col-md-12">
            <label class="pay"><?=_("Número de tarjeta")?></label>
            <input  type="text" name="CardNumber" id="cr_no" placeholder="0000-0000-0000-0000"  maxlength="16">
        </div>
        <div class="form-flex">
            <div class="form-group">
                <label class="pay"><?=_("Mes de expiración")?></label>
                <select  name="ExpirationMonth" class="form-control">
                    <option value="01">01</option>
                    <option value="02">02</option>
                    <option value="03">03</option>
                    <option value="04">04</option>
                    <option value="05">05</option>
                    <option value="06">06</option>
                    <option value="07">07</option>
                    <option value="08">08</option>
                    <option value="09">09</option>
                    <option value="10">10</option>
                    <option value="11">11</option>
                    <option value="12">12</option>
                </select>
            </div>
            <div class="form-group">
                <label class="pay"><?=_("Año de expiración")?></label>
                <select  name="ExpirationYear" class="form-control">
                    <option value="2022">2022</option>
                    <option value="2023">2023</option>
                    <option value="2024">2024</option>
                    <option value="2025">2025</option>
                    <option value="2026">2026</option>
                    <option value="2027">2027</option>
                    <option value="2028">2028</option>
                    <option value="2029">2029</option>
                    <option value="2030">2030</option>
                </select>
            </div>
            <div class="form-group">
                <label class="pay">CVV</label>
                <input  type="password" name="CVC" placeholder="&#9679;&#9679;&#9679;" class="placeicon" minlength="3" maxlength="3">
            </div>
        </div>
    </div>
 -->
<?php
}else{
    ?>
    <input type="hidden" name="Quoted" value="true">
    <div id="totals">
        <h4><?=_("Tu traducción necesita ser cotizada, envíanos tu información con el botón de finalizar y nos comunciaremos contigo a la brevedad")?></h4>

    </div>
<?php
}
}

?>
<script>
    function setPayment(type){
        $("#payment1").hide();
        $("#payment2").hide();
        $("#payment"+type).show();
    }

        
        
        
 </script>

