Created
March 16, 2018 02:32
-
-
Save camilamoreiradev/f146049a27506e89d729ce73a7c39919 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
date_default_timezone_set('America/Sao_Paulo'); | |
require_once 'payment/cmlpmt.php'; | |
function getPurchase($id, $payment, $user) { | |
include 'includes/db.php'; | |
$_id = explode("-", $id); | |
$sql = "SELECT valor_real, valor_dolar, duration, meses FROM cm_planos WHERE id = ".$_id[1]; | |
$result = $conn->query($sql); | |
if ($result->num_rows > 0) { | |
while($row = $result->fetch_assoc()) { | |
$_valor = ($payment == 'pg')?$row['valor_real']:$row['valor_dolar']; | |
$_duration = $row['duration']; | |
$_dtinicial = date('Y-m-d'); | |
$_hr = date('H:i:s'); | |
$_dtfinal = calcDays(date('Y-m-d'), $_duration); | |
$_meses = $row['meses']; | |
} | |
} | |
$busca = "SELECT COUNT(*) AS qtd, id FROM cm_planos_users_new WHERE usuario = '".$user."' AND status = 1"; | |
$result = $conn->query($busca); | |
if ($result->num_rows > 0) { | |
while($row = $result->fetch_assoc()) { | |
if ($row['qtd'] > 0) { | |
return $row['id']; | |
} else { | |
$sum_valor_final = $_valor*$_meses; | |
$insert = "INSERT INTO cm_planos_users_new (plano, usuario, dtinicio, dtfinal, valor, status) | |
VALUES (".$_id[1].", '".$user."', '".$_dtinicial.' '.$_hr."', '".$_dtfinal.' '.$_hr."', '".$sum_valor_final."', 1)"; | |
if ($conn->query($insert) === TRUE) { | |
$sql_last_id = "SELECT LAST_INSERT_ID() AS new_id FROM cm_planos_users_new WHERE usuario = '".$user."'"; | |
$result = $conn->query($sql_last_id); | |
if ($result->num_rows > 0) { | |
while($row = $result->fetch_assoc()) { | |
return $row['new_id']; | |
} | |
} | |
} else { | |
echo "Error: " . $insert . "<br>" . $conn->error; | |
} | |
} | |
} | |
} | |
} | |
function calcDays($data, $days) { | |
$data = DateTime::createFromFormat('Y-m-d', $data); | |
$data->add(new DateInterval('P30D')); | |
return $data->format('Y-m-d'); | |
} | |
$_plano = $_POST['plano']; | |
$_fpagto = $_POST['paym']; | |
$_user = $_POST['login']; | |
$_idcompra = getPurchase($_plano, $_fpagto, $_user); | |
$purchase = (new Purchase())->find($_idcompra); | |
if($_fpagto === 'py') { | |
checkout($purchase, (new Paypal())); | |
} else { | |
checkout($purchase, (new Pagseguro())); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment