Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once('libraries/stripe/init.php');
- require_once('libraries/PasswordHash.php');
- require_once('libraries/ses/SimpleEmailService.php');
- require_once('libraries/ses/SimpleEmailServiceMessage.php');
- require_once('libraries/ses/SimpleEmailServiceRequest.php');
- require_once('libraries/jwt/JWT.php');
- header('Content-Type: application/json');
- session_start();
- $_SESSION['stripe_error'] = "";
- function send_email($plan, $link, $email)
- {
- $from = 'All Your Leads <info@allyourleads.com>';
- $subject = 'All Your Leads - Direct Download';
- $text_message = 'Hello,
- Thank you for your purchase of the '.$plan.' plan, the following is a direct download link to your data, the link will be available for the next 24 hours. If you need the link to be available for an extended period please contact support at help@allyourleads.com
- '.$link.'
- All Your Leads
- Sales leads for Ecommerce, Social Media, Alibaba, and much more';
- $html_message = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Welcome!</title>
-
- </head>
- <body bgcolor="#f6f8f1" style="min-width: 100% !important; background-color: #FFFFFF; margin: 0; padding: 0;">
- <table class="main_table" width="100%" bgcolor="#FFFFFF" border="0" cellpadding="0" cellspacing="0">
- <tr>
- <td>
- <table class="content" align="center" cellpadding="0" cellspacing="0" border="0" style="width: 100%; max-width: 600px; min-height: 700px; background-color: #000000;" bgcolor="#000000">
- <tr>
- <td class="td_header" style="height: 100px; background-color: #2185d5; padding-left: 30px;" bgcolor="#2185d5">
- <img src="https://allyourleads.com/images/logo_large.png" height="40" />
- </td>
- </tr>
- <tr>
- <td class="td_body" style="background-color: #eef7f8; vertical-align: top; padding: 30px;" bgcolor="#eef7f8" valign="top">
-
- <p class="main_text" style="font-size: 12x; font-family: Arial; color: #262626;">
-
- Hello,<br /><br />
- Thank you for your purchase of the '.$plan.' plan, the following is a direct download link to your data, the link will be available for the next 24 hours. If you need the link to be available for an extended period please contact support at <a href="mailto:help@allyourleads.com">help@allyourleads.com</a><br /><br /><br />
- <a href="'.$link.'">'.$link.'</a>
- <br /><br /><br /><br />
- All Your Leads<br />
- Sales leads for Ecommerce, Social Media, Alibaba, and much more.
- </p>
- </td>
- </tr>
- <tr>
- <td class="td_footer" style="height: 50px; background-color: #2185d5; text-align: center; font-family: Arial;" align="center" bgcolor="#2185d5">
- <a class="footer_link" href="https://www.allyourleads.com" style="color: #FFFFFF; text-decoration: none;">www.allyourleads.com</a>
- </td>
- </tr>
- </table>
- </td>
- </tr>
- </table>
- </body>
- </html>';
- $m = new SimpleEmailServiceMessage();
- $m->addTo($email);
- $m->setFrom($from);
- $m->setSubject($subject);
- $m->setSubjectCharset('ISO-8859-1');
- $m->setMessageCharset('ISO-8859-1');
- $m->setMessageFromString($text_message,$html_message);
- try
- {
- $ses = new SimpleEmailService('XXXXXXXXXXXXX',
- 'XXXXXXXXXXXXXXXXXXX');
- $ses->sendEmail($m);
- }
- catch(Exception $e)
- {
- error_log("Error trying to send download email to: " . $email .
- ", ERROR MSG: " . $e->getMessage(), 0);
- }
- }
- function get_client_ip() {
- $ipaddress = '';
- if (getenv('HTTP_CLIENT_IP'))
- $ipaddress = getenv('HTTP_CLIENT_IP');
- else if(getenv('HTTP_X_FORWARDED_FOR'))
- $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
- else if(getenv('HTTP_X_FORWARDED'))
- $ipaddress = getenv('HTTP_X_FORWARDED');
- else if(getenv('HTTP_FORWARDED_FOR'))
- $ipaddress = getenv('HTTP_FORWARDED_FOR');
- else if(getenv('HTTP_FORWARDED'))
- $ipaddress = getenv('HTTP_FORWARDED');
- else if(getenv('REMOTE_ADDR'))
- $ipaddress = getenv('REMOTE_ADDR');
- else
- $ipaddress = '127.0.0.1';
- return $ipaddress;
- }
- if (isset($_POST['token']) &&
- isset($_POST['email']) &&
- isset($_POST['plan']) &&
- isset($_POST['password']) &&
- isset($_POST['name']) &&
- isset($_POST['address']) &&
- isset($_POST['zip']))
- {
- $token = $_POST['token'];
- $email = strtolower(trim($_POST['email']));
- $plan = $_POST['plan'];
- $password = $_POST['password'];
- $name = trim($_POST['name']);
- $address = trim($_POST['address']);
- $zip = trim($_POST['zip']);
- $_SESSION['email'] = $email;
- $_SESSION['plan'] = $plan;
- $_SESSION['name'] = $name;
- $_SESSION['address'] = $address;
- $_SESSION['zip'] = $zip;
- $customer_check = false;
- $user_id;
- $customer;
- $customer_id;
- // Get client's IP address
- $ip_address = get_client_ip();
- // Connect to database
- $db = pg_connect('host=allyourleads.com port=9999 dbname=XXXXX user=root password=XXXXXXXXXXXXXXXX') or die('Could not connect: ' . pg_last_error());
- // TEST
- //$secret_key = "XXXXXXXXXXXXX";
- //$publishable_key = "XXXXXXXXX";
- // LIVE
- $secret_key = "XXXXXXXXX";
- $publishable_key = "XXXXXXXXXXXXXXXX";
- \Stripe\Stripe::setApiKey($secret_key);
- // Get existing customer id if they exist with stripe
- $res = pg_query_params($db, "SELECT user_id, stripe_customer_id FROM users " .
- "WHERE stripe_customer_id IS NOT NULL AND email = $1",
- array($email));
- if ($res && pg_num_rows($res) > 0)
- {
- $row = pg_fetch_object($res);
- $user_id = $row->user_id;
- $customer_id = $row->stripe_customer_id;
- // Check if customer exists with stripe
- $customer = \Stripe\Customer::retrieve($customer_id);
- if(!isset($customer['deleted']) && $customer['delinquent'] == FALSE)
- {
- $customer_check = TRUE;
- // Update the last login and IP address
- pg_query_params($db, "INSERT INTO ip_addresses(user_id, ip_address) VALUES($1, $2)",
- array($user_id, $ip_address));
- }
- pg_free_result($res);
- }
- // Create a new customer
- if(!$customer_check)
- {
- try
- {
- $customer = \Stripe\Customer::create(array(
- 'email' => $email,
- 'source' => $token
- ));
- }
- catch (\Stripe\Error\Card $e)
- {
- $_SESSION['stripe_error'] = $e->getMessage();
- header("Location: /download");
- die();
- }
- if (isset($customer))
- {
- $customer_id = $customer->id;
- $hasher = new PasswordHash(8, FALSE);
- $hash_password = $hasher->HashPassword($password);
- $res = pg_query_params($db, "INSERT INTO users(email, password, name, " .
- "address, zip, stripe_customer_id) " .
- "VALUES($1, $2, $3, $4, $5, $6) " .
- "RETURNING user_id",
- array($email, $hash_password, $name, $address,
- $zip, $customer_id));
- if ($res && pg_num_rows($res) > 0)
- {
- // Update the last login and IP address
- $row = pg_fetch_object($res);
- $user_id = $row->user_id;
- pg_query_params($db, "INSERT INTO ip_addresses(user_id, ip_address) VALUES($1, $2)",
- array($user_id, $ip_address));
- pg_free_result($res);
- }
- }
- }
- // Charge the customer based on their selected plan
- $res = pg_query_params($db, "SELECT plan_id, plan, category, plan_type, " .
- "price FROM plans WHERE plan = $1",
- array($plan));
- if ($res && pg_num_rows($res) > 0)
- {
- $row = pg_fetch_object($res);
- $plan_id = $row->plan_id;
- $plan = $row->plan;
- $category = $row->category;
- $plan_type = $row->plan_type;
- $price = $row->price;
- $plan_formatted = ucfirst($category) . ' ' . ucfirst($plan_type);
- try
- {
- $charge = \Stripe\Charge::create(array(
- 'customer' => $customer_id,
- 'amount' => (int) ($price * 100),
- 'currency' => 'usd'
- ));
- if($charge['status'] == "succeeded")
- {
- $stripe_trans_id = $charge['id'];
- $amount = $charge['amount'];
- $currency = $charge['currency'];
- $transaction_id = null;
- $res2 = pg_query_params($db, "INSERT INTO transactions(user_id, plan_id, amount, " .
- "currency, stripe_trans_id) VALUES($1, $2, $3, $4, $5) " .
- "RETURNING transaction_id",
- array($user_id, $plan_id, $amount, $currency, $stripe_trans_id));
- if ($res2 && pg_num_rows($res2) > 0)
- {
- // Get the transaction id
- $row = pg_fetch_object($res2);
- $transaction_id = $row->transaction_id;
- pg_free_result($res2);
- }
- // Update the user account stripe customer id
- pg_query_params($db, "UPDATE users SET stripe_customer_id = $1 WHERE user_id = $2",
- array($customer_id, $user_id));
- // If the user purchased because of referral, give affiliate commission
- if (isset($_COOKIE['referral']))
- {
- // Private cryptographic key used for JWT
- $key = "sepmgz4LEnii4PzVCb3REgEVvkHpagY4";
- try
- {
- $decoded = JWT::decode($_COOKIE['referral'], $key, array('HS256'));
- $code = $decoded->code;
- pg_query_params($db, "INSERT INTO referrals(code_id, commission_amount, " .
- "transaction_id) VALUES(" .
- "(SELECT code_id FROM codes WHERE code = $1), " .
- "((SELECT commission_percent FROM codes " .
- "NATURAL JOIN affiliates WHERE code = $1) * $2), $3)",
- array($code, $amount, $transaction_id));
- }
- catch (Exception $e) {
- error_log("Error decoding cookie, ERROR: " . $e->getMessage(), 0);
- }
- }
- // Get the download URL
- $res3 = pg_query_params($db, "SELECT download_host || '/' || download_unique " .
- "|| '/' || download_file AS download_url " .
- "FROM downloads WHERE plan_id = $1",
- array($plan_id));
- if ($res3 && pg_num_rows($res3) > 0)
- {
- $row = pg_fetch_object($res3);
- $_SESSION['download_url'] = $row->download_url;
- send_email($plan_formatted, $row->download_url, $email);
- header("Location: /thankyou");
- pg_free_result($res);
- pg_free_result($res3);
- }
- else
- {
- // This should never happen!
- error_log("Unable to find download URL for USER: " . $user_id . ", PLAN ID: " . $plan_id, 0);
- }
- }
- else
- {
- // Unable to charge their credit card
- error_log("Unable to charge credit card for USER: " . $user_id . ", STRIPE USER ID: " .
- $customer_id . ", PLAN ID: " . $plan_id . ", AMOUNT: " . (int) ($price * 100), 0);
- $_SESSION['stripe_error'] = 'Unknown error, unable to charge credit card';
- header("Location: /download");
- }
- }
- catch(Exception $e)
- {
- error_log("Stripe error occurred trying to charge credit card for USER: " . $user_id .
- ", STRIPE USER ID: " . $customer_id . ", PLAN ID: " . $plan_id .
- ", AMOUNT: " . (int) ($price * 100) . "\nERROR MSG: " . $e->getMessage(), 0);
- $_SESSION['stripe_error'] = $e->getMessage();
- header("Location: /download");
- }
- }
- pg_close($db);
- }
- else {
- die("Missing parameters");
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement