<!DOCTYPE html>
<html lang="ro-RO">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
    <title>mica24.ro - Anunțuri Presa Scrisă - România Liberă</title>

    <script src="js/jquery-3.1.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#payForm').submit();
        });
    </script>
</head>

<body>
<?php
if (!empty($data) && !empty($data['order']) && !empty($data['billing'])) {
    $order = $data['order'];

    $paymentSettings = $data["paymentSettings"];

    require_once '../src/Mobilpay/Payment/Request/Abstract.php';
    require_once '../src/Mobilpay/Payment/Request/Card.php';
    require_once '../src/Mobilpay/Payment/Invoice.php';
    require_once '../src/Mobilpay/Payment/Address.php';
    #for testing purposes, all payment requests will be sent to the sandbox server. Once your account will be active you must switch back to the live server https://secure.mobilpay.ro
    #in order to display the payment form in a different language, simply add the language identifier to the end of the paymentUrl, i.e https://secure.mobilpay.ro/en for English
    $paymentUrl = $paymentSettings["url"];
    //$paymentUrl = 'https://secure.mobilpay.ro';
    // this is the path on your server to the public certificate. You may download this from Admin -> Conturi de comerciant -> Detalii -> Setari securitate
    // $x509FilePath 	= 'i.e: /ome/certificates/public.cer';
    $x509FilePath = $paymentSettings["publicCertificate"];
    // if(file_exists($x509FilePath)){
    //     var_dump($x509FilePath);
    //     die();
    // }
    try {
        srand((double)microtime() * 1000000);
        $objPmReqCard = new Mobilpay_Payment_Request_Card();
        #merchant account signature - generated by mobilpay.ro for every merchant account
        #semnatura contului de comerciant - mergi pe www.mobilpay.ro Admin -> Conturi de comerciant -> Detalii -> Setari securitate
        $objPmReqCard->signature = $paymentSettings["signature"];
        #you should assign here the transaction ID registered by your application for this commercial operation
        #order_id should be unique for a merchant account
        $objPmReqCard->orderId = md5(uniqid(rand()));

        $objPmReqCard->params = [
            "ad" => $order["reference"]
        ];

        if (!empty($order['quote_id'])) {
            $objPmReqCard->params["quote"] = $order["quote_id"];
        }
        #below is where mobilPay will send the payment result. This URL will always be called first; mandatory
        $objPmReqCard->confirmUrl = $paymentSettings["backref"];
        #below is where mobilPay redirects the client once the payment process is finished. Not to be mistaken for a "successURL" nor "cancelURL"; mandatory
        $objPmReqCard->returnUrl = $paymentSettings["redirectUrl"] . '?ad=' . $order["reference"];
        #detalii cu privire la plata: moneda, suma, descrierea
        #payment details: currency, amount, description
        $objPmReqCard->invoice = new Mobilpay_Payment_Invoice();
        #payment currency in ISO Code format; permitted values are RON, EUR, USD, MDL; please note that unless you have mobilPay permission to
        #process a currency different from RON, a currency exchange will occur from your currency to RON, using the official BNR exchange rate from that moment
        #and the customer will be presented with the payment amount in a dual currency in the payment page, i.e N.NN RON (e.ee EUR)
        $objPmReqCard->invoice->currency = $paymentSettings["currency"];
        $objPmReqCard->invoice->amount = $order["amount"];
        #available installments number; if this parameter is present, only its value(s) will be available
        //$objPmReqCard->invoice->installments= '2,3';
        #selected installments number; its value should be within the available installments defined above
        //$objPmReqCard->invoice->selectedInstallments= '3';
        //platile ulterioare vor contine in request si informatiile despre token. Prima plata nu va contine linia de mai jos.
        $objPmReqCard->invoice->tokenId = 'token_id';
        $objPmReqCard->invoice->details = $order["name"];

        #detalii cu privire la adresa posesorului cardului
        #details on the cardholder address (optional)
        $billingAddress = new Mobilpay_Payment_Address();
        $billingAddress->type = "person"; //should be "person"
        // $billingAddress->type			= $_POST['billing_type']; //should be "person"
        $billingAddress->firstName = $data["billing"]["billFName"];
        $billingAddress->lastName = $data["billing"]["billLName"];
        $billingAddress->address = $data["billing"]["billAddress1"];
        $billingAddress->email = $data["billing"]["billEmail"];
        $billingAddress->mobilePhone = $data["billing"]["billPhone"];
        $objPmReqCard->invoice->setBillingAddress($billingAddress);

        #detalii cu privire la adresa de livrare
        #details on the shipping address
        // $shippingAddress 				= new Mobilpay_Payment_Address();
        // $shippingAddress->type			= "ziar";
        // $shippingAddress->firstName		= "";
        // $shippingAddress->lastName		= "";
        // $shippingAddress->address		= "";
        // $shippingAddress->email			= "";
        // $shippingAddress->mobilePhone		= "";
        // $objPmReqCard->invoice->setShippingAddress($shippingAddress);
        #uncomment the line below in order to see the content of the request
        // echo "<pre>";print_r($objPmReqCard);echo "</pre>";
        $objPmReqCard->encrypt($x509FilePath);
    } catch (Exception $e) {
    }

    // die();

    ?>

    <form name="payForm" id="payForm" action="<?php echo $paymentUrl; ?>" method="post">
        <input type="hidden" name="env_key" value="<?php echo $objPmReqCard->getEnvKey(); ?>"/>
        <input type="hidden" name="data" value="<?php echo $objPmReqCard->getEncData(); ?>"/>
        <input type="submit" style="display: none;">
    </form>

    <?php
}
?>
</body>
