The DS.M homepage and stuff. https://dontsell.me/site
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
8.2 KiB

  1. <?php
  2. require_once __DIR__ . "/settings.php";
  3. require_once __DIR__ . "/database.php";
  4. session_start();
  5. $plan = $_POST['plan'];
  6. if (!isset(PLANS[$plan])) {
  7. header("Location: /#pricing");
  8. die("Invalid plan chosen.");
  9. }
  10. if (!isset($_SESSION['username']) || !isset($_SESSION['password']) || !isset($_SESSION['renewing'])) {
  11. header("Location: /");
  12. die();
  13. }
  14. \Stripe\Stripe::setApiKey(STRIPE_SK);
  15. $token = $_POST['stripeToken'];
  16. $email = $_POST['email'];
  17. try {
  18. // Free plan
  19. if (PLANS[$plan]["monthly"] == 0) {
  20. $free = TRUE;
  21. }
  22. if (!$free && $email !== $_POST['stripeEmail']) {
  23. throw new Exception("You need to use the same email for payment and for your account.");
  24. }
  25. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  26. throw new Exception("The email you supplied is not valid. You need a valid email address to use the service.");
  27. }
  28. if (!$free) {
  29. $customer = \Stripe\Customer::create(array(
  30. 'email' => $database->select('accounts', 'email', ['accountid' => $_SESSION['accountid']])[0],
  31. 'source' => $token
  32. ));
  33. $charge = \Stripe\Charge::create(array(
  34. 'customer' => $customer->id,
  35. 'amount' => PLANS[$plan]["monthly"] * 100,
  36. 'currency' => 'usd'
  37. ));
  38. if (!$charge['paid']) {
  39. throw new Exception("The charge did not complete successfully. Reason: " . $charge["failure_message"]);
  40. }
  41. }
  42. $error = false;
  43. $message = ($free ? "Your account is now active!" : "Your purchase was successful!");
  44. // Setup cURL
  45. $curl = curl_init();
  46. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  47. curl_setopt($curl, CURLOPT_USERPWD, NEXTCLOUD_USER . ":" . NEXTCLOUD_PASS);
  48. curl_setopt($curl, CURLOPT_HTTPHEADER, ['OCS-APIRequest: true', "Content-Type: application/x-www-form-urlencoded"]);
  49. if ($_SESSION['renewing'] == TRUE) {
  50. // Enable account
  51. curl_setopt($curl, CURLOPT_URL, "https://dontsell.me/ocs/v1.php/cloud/users/" . urlencode($_SESSION['username']) . "/enable");
  52. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
  53. $out = curl_exec($curl);
  54. if (strpos($out, "<status>ok</status>") === FALSE) {
  55. preg_match('/\<message\>(.*?)\<\/message\>/', $out, $match);
  56. throw new Exception("Your payment was successful, but we could not setup your account. <a href=\"https://support.netsyms.com\">Click here to contact support.</a> Reason: " . $match[1]);
  57. }
  58. // Set quota
  59. curl_setopt($curl, CURLOPT_URL, "https://dontsell.me/ocs/v1.php/cloud/users/" . urlencode($_SESSION['username']));
  60. curl_setopt($curl, CURLOPT_POSTFIELDS, "key=quota&value=" . PLANS[$plan]["mb"] . "MB");
  61. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
  62. $out = curl_exec($curl);
  63. if (strpos($out, "<status>ok</status>") === FALSE) {
  64. preg_match('/\<message\>(.*?)\<\/message\>/', $out, $match);
  65. throw new Exception("Your payment was successful, but we could not setup your account. <a href=\"https://support.netsyms.com\">Click here to contact support.</a> Reason: " . $match[1]);
  66. }
  67. // Check if account is active on the same plan
  68. if ($database->has("billing", ["uid" => $_SESSION['username'], "validuntil[>=]" => date("Y-m-d"), "quota" => PLANS[$plan]["mb"]])) {
  69. $rows = $database->select("billing", "validuntil", ["uid" => $_SESSION['username'], "validuntil[>=]" => date("Y-m-d"), "quota" => PLANS[$plan]["mb"]]);
  70. $latest = time();
  71. foreach ($rows as $r) {
  72. if (strtotime($r) > $latest) {
  73. $latest = strtotime($r);
  74. }
  75. }
  76. // Add billing data
  77. $database->insert("billing", [
  78. "uid" => $_SESSION['username'],
  79. "validfrom" => date("Y-m-d"),
  80. "validuntil" => date("Y-m-d", strtotime("+30 days", $latest)),
  81. "quota" => PLANS[$plan]["mb"]]
  82. );
  83. } else {
  84. // Add billing data
  85. $database->insert("billing", [
  86. "uid" => $_SESSION['username'],
  87. "validfrom" => date("Y-m-d"),
  88. "validuntil" => date("Y-m-d", strtotime("+30 days")),
  89. "quota" => PLANS[$plan]["mb"]]
  90. );
  91. }
  92. } else {
  93. // Add user
  94. curl_setopt($curl, CURLOPT_URL, "https://dontsell.me/ocs/v1.php/cloud/users");
  95. $postfields = "userid=" . urlencode($_SESSION['username']) . "&password=" . urlencode($_SESSION['password']) . "";
  96. curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
  97. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
  98. $out = curl_exec($curl);
  99. if (strpos($out, "<status>ok</status>") === FALSE) {
  100. preg_match('/\<message\>(.*?)\<\/message\>/', $out, $match);
  101. throw new Exception("Your payment was successful, but we could not setup your account. <a href=\"https://support.netsyms.com\">Click here to contact support.</a> Reason: " . $match[1]);
  102. }
  103. // Set quota
  104. curl_setopt($curl, CURLOPT_URL, "https://dontsell.me/ocs/v1.php/cloud/users/" . urlencode($_SESSION['username']));
  105. curl_setopt($curl, CURLOPT_POSTFIELDS, "key=quota&value=" . PLANS[$plan]["mb"] . "MB");
  106. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
  107. $out = curl_exec($curl);
  108. if (strpos($out, "<status>ok</status>") === FALSE) {
  109. preg_match('/\<message\>(.*?)\<\/message\>/', $out, $match);
  110. throw new Exception("Your payment was successful, but we could not setup your account. <a href=\"https://support.netsyms.com\">Click here to contact support.</a> Reason: " . $match[1]);
  111. }
  112. // Set email
  113. curl_setopt($curl, CURLOPT_URL, "https://dontsell.me/ocs/v1.php/cloud/users/" . urlencode($_SESSION['username']));
  114. curl_setopt($curl, CURLOPT_POSTFIELDS, "key=email&value=$email");
  115. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
  116. $out = curl_exec($curl);
  117. if (strpos($out, "<status>ok</status>") === FALSE) {
  118. preg_match('/\<message\>(.*?)\<\/message\>/', $out, $match);
  119. throw new Exception("Your payment was successful, but we could not setup your account. <a href=\"https://support.netsyms.com\">Click here to contact support.</a> Reason: " . $match[1]);
  120. }
  121. // Add billing data
  122. $database->insert("billing", [
  123. "uid" => $_SESSION['username'],
  124. "validfrom" => date("Y-m-d"),
  125. "validuntil" => date("Y-m-d", strtotime("+30 days")),
  126. "quota" => PLANS[$plan]["mb"]]
  127. );
  128. }
  129. } catch (Exception $e) {
  130. $error = true;
  131. $message = "Something went wrong. Details:<br />" . $e->getMessage();
  132. file_put_contents("../../billingerrors.log", "[" . date("Y-m-d H:i:s") . "]\t" . $_SESSION['username'] . "\t" . $e->getMessage() . "\n", FILE_APPEND);
  133. }
  134. unset($_SESSION['username']);
  135. unset($_SESSION['password']);
  136. unset($_SESSION['renewing']);
  137. ?>
  138. <!DOCTYPE HTML>
  139. <title><?php echo SITE_TITLE; ?></title>
  140. <?php
  141. include __DIR__ . '/inc/meta.php';
  142. include __DIR__ . '/inc/piwik.php';
  143. ?>
  144. <!-- Wrapper -->
  145. <div id="wrapper">
  146. <?php include __DIR__ . "/inc/header.php"; ?>
  147. <!-- Section -->
  148. <section id="one" class="main alt">
  149. <header class="accent1">
  150. <?php
  151. if ($error === TRUE) {
  152. echo "<h1>Oops...</h1>";
  153. } else {
  154. echo "<h1>Thanks!</h1>";
  155. }
  156. ?>
  157. </header>
  158. <div class="inner alt">
  159. <div class="content">
  160. <?php
  161. if ($error !== TRUE) {
  162. ?>
  163. <div style="text-align: center;">
  164. <img src="assets/large-checkmark.svg" style="max-width: 50%; max-height: 200px;"/><br />
  165. <?php
  166. echo $message;
  167. ?>
  168. </div>
  169. <?php
  170. } else {
  171. echo $message;
  172. }
  173. ?>
  174. <p>
  175. </div>
  176. </div>
  177. </section>
  178. <?php include __DIR__ . "/inc/footer.php"; ?>
  179. </div>
  180. <?php include __DIR__ . "/inc/scripts.php"; ?>