<?php

$garanti = new GARANTI\GARANTI();
$garanti->setPayMode(GARANTIPAY_PAY_MOD); // STANDART, 3D
$garanti->setSecure3dsecuritylevel(GARANTIPAY_SECURITY_LEVEL);
$garanti->setMode(GARANTIPAY_MOD);
$garanti->setTerminalId(GARANTIPAY_TERMINAL_ID);
$garanti->setProvisionPassword(GARANTIPAY_PROVISION_PASS);
$garanti->setUserId(GARANTIPAY_USER_ID);
$garanti->setMerchantID(GARANTIPAY_MERCHAND_ID);
$garanti->setStrStoreKey(GARANTIPAY_STORE_KEY);

$garanti->setEmailAddress($func->post('email'));
$garanti->setOrderID($order_id);
$garanti->setAmount($total);
$garanti->setInstallmentCnt($func->post('installment')?$func->post('installment'):0);
$garanti->setNumber('');//4282209004348015
$garanti->setExpireMonth('');//07
$garanti->setExpireYear('');//19
$garanti->setCVV2('');//123

$garanti->setSuccessUrl(GARANTIPAY_SUCCESS_URL);
$garanti->setFailedUrl(GARANTIPAY_FAILURE_URL);
$return = $garanti->pay();
//echo '<pre>';print_r($return);die();
//$func->prnt($func->post(),true);
$subject = html_entity_decode(sprintf('Sayın %s, bilet isteğiniz tarafımıza ulaştı.', $func->post('name'), ENT_QUOTES, 'UTF-8'));
$body    = 'Bilet isteğiniz tarafımıza ulaştı. <br> Ödeme tamamlandığı an bilet numaralarınız tarafınıza mail ve sms olarak bildirilecektir.';

if($return) {
    if ($return->Response->ReasonCode == '00') {
//echo 'başarılı';die();
        $transaction_id = (string)$return->RetrefNum;
        $order_info = $db->query("SELECT * FROM " . DBPREFIX . "order WHERE id = '" . $id . "'");
        if ($order_info->num_rows) {
            $db->query("UPDATE " . DBPREFIX . "order SET order_status='1', status='1',transaction_id='".$transaction_id."' WHERE id = '" . $id . "'");
            $location->redirect('success');
        } else {
            $error->set('Sipariş bulunamadı!');
            $location->redirect('payment');
        }
    } else {

        $error->set((string)$return->Response->ErrorMsg);
        $location->redirect('payment');
    }
}else{
    $error->set('Bankayla bağlantı kurulamadı!');
    $location->redirect('payment');
}