'Day', 'monthly' => 'Month', 'yearly' => 'Year'); // constructor function payment() { // globals global $base_dir, $db, $domain; // connections $this->db = $db; // stripe api include_once("{$base_dir}/includes/stripe/lib/Stripe.php"); // ipn gateway $this->ipn_url = "https://{$domain}/actions/ipn.action.php"; } function init() { global $users, $template; if( !isset($users) ){ $users = new users(); } // admin test condition exception $user_type = ''; if( isset($_SESSION['login']) ){ $user_type = $users->get_user_type($_SESSION['login']); } /* if($user_type == 'admin'){ $this->IS_ONLINE = true; } //$this->IS_ONLINE = false; ///////////// REMOVE after testing ///////////// */ if( isset($_SERVER['HTTP_HOST']) ){ if($_SERVER['HTTP_HOST'] != 'www.franchiseball.com'){ if($_SERVER['HTTP_HOST'] == 'gamedev'){ $this->IS_ONLINE = false; } } } // testing credentials if( $this->IS_ONLINE == false ){ // stripe staging settings $this->secret_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'; $this->publishable_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // production credentials } else { // stripe live settings $this->secret_key = 'YYYYYYYYYYYYYYYYYYYYYYYYYYYY'; $this->publishable_key = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyy'; } // init stripe api Stripe::setApiKey($this->secret_key); } // cancel_cycle_payment : cancels a subscription based transaction (ex : subscription/membership) function cancel_cycle_payment($customer_id = '') { if($customer_id == ''){ return; } $this->init(); $customer = Stripe_Customer::retrieve($customer_id); if( isset($customer->id) && $customer->id != '' ){ $customer->cancelSubscription(); } } function process_payment() { global $market; if( !isset($market) ){ $market = new market(); } // payment token recieved if( isset($_POST['stripeToken']) && $_POST['stripeToken'] != '' ){ // vars $errors = array(); $charge_token = $_POST['stripeToken']; $charge_value = $market->get_cart_total(); $charge_value = $charge_value + ($charge_value * $this->servicefee_pct) + $this->servicefee_additional; $charge_value = round($charge_value * 100); // create customer $customer = Stripe_Customer::create(array( 'email' => $_SESSION['email'], 'card' => $charge_token )); // seperate payment types : direct & recurring $reoccuring_set = array(); $product_set = array(); $product_data = $market->get_product_data($_SESSION['cart']['products']); foreach($product_data as $index => $product){ if($product['product_reoccuring'] != 'none'){ $reoccuring_set[] = $product; } else { $product_set[] = $product; } } if( !empty($reoccuring_set) ){ $product = $reoccuring_set[0]; $plan_id = $product['product_slug']; if( !empty($product_set) ){ $charge_amt = $product['product_price'] + ($product['product_price'] * $this->servicefee_pct) + $this->servicefee_additional; $charge_amt = round($charge_amt * 100); } $charge_value = ($charge_value - $charge_amt); $subscription = $customer->updateSubscription(array('plan' => $plan_id)); if( isset($subscription->customer) && $subscription->customer != '' ){ $product['customer_id'] = $subscription->customer; $this->on_charge_success(array($product)); } else { $errors[] = 'Error creating subscription'; } } if( !empty($product_set) ){ $products_str = ''; foreach($product_set as $datum){ if($datum['product_slug'] == 'credits-million-gc'){ $gc += $datum['product_entity']; } else { $products_str .= "{$comma}{$datum['product_name']}"; $comma = ', '; } } if($gc > 0){ $products_str .= "{$comma} \$" . number_format($gc) . " GC"; } $charge = Stripe_Charge::create(array( 'customer' => $customer->id, 'amount' => $charge_value, 'currency' => 'usd', 'description' => $products_str )); if( isset($charge->id) && $charge->id != '' ){ foreach($product_set as $index => $datum){ $product_set[$index]['charge_id'] = $charge->id; } $this->on_charge_success($product_set); } else { $errors[] = 'Error creating charge'; } } if( empty($errors) ){ $this->products_purchased = $_SESSION['cart']['products']; // redirect $ref_page = '/home.php'; if( isset($_SESSION['value']['ref_page']) && $_SESSION['value']['ref_page'] != '' ){ $ref_page = $_SESSION['value']['ref_page']; } $purchase_data = $this->get_purchase_data(); if( !empty($purchase_data) ){ $_SESSION['dialog'] = array( 'ui' => 'market', 'method' => 'payment_confirm', 'title' => 'Your payment was successful', 'data' => $purchase_data ); unset($_SESSION['value']['ref_page']); } unset($_SESSION['cart']['products']); header("Location: {$ref_page}"); } } } function on_charge_success($cart_set = array()) { global $team, $market; if( !isset($team) ){ $team = new team(); } if( !isset($market) ){ $market = new market(); } // login team if login condition exists if( !isset($_SESSION['login']) && isset($_SESSION['cond_login_team']) ){ if($_SESSION['cond_login_team'] > 0){ $ui = new ui(); $ui->autologin_team($_SESSION['cond_login_team']); } } if( !empty($cart_set) ){ if( !empty($cart_set) ){ foreach($cart_set as $product){ $this->post_payment($product); if( $team->add_product($product) ){ $team->enable_product($product); } } } } } function get_purchase_data() { global $market; if( !isset($market) ){ $market = new market(); } if( !empty($this->products_purchased) ){ $product_data = $market->get_product_data($this->products_purchased); $product_data = $market->quantity_group_product_data($product_data); } return $product_data; } function post_payment(&$product, $manual_team_id = '') { global $team, $schedule; if( !isset($team) ){ $team = new team(); } if( !isset($schedule) ){ $schedule = new schedule(); } $team_id = $team->get_primary_team(); if($manual_team_id != ''){ $team_id = $manual_team_id; } $time = $schedule->time(); $transaction_id_sql = ''; if( !empty($product) ){ if( isset($product['charge_id']) ){ $transaction_id_sql = " payment_charge_id = '{$product['charge_id']}',"; } if( isset($product['customer_id']) ){ $transaction_id_sql = " payment_customer_id = '{$product['customer_id']}',"; } $sql = "INSERT INTO payments SET team_id = {$team_id}, payment_amount = {$product['product_price']},{$transaction_id_sql} payment_time = {$time}"; $this->db->query($sql); $product['payment_id'] = mysql_insert_id(); } } function payment_form() { global $generate, $market, $domain; if( !isset($generate) ){ $generate = new generate(); } if( !isset($market) ){ $market = new market(); } $this->init(); //$domain = 'gamedev'; // todo : remove $order_amount = $market->get_cart_total(); $charge_total = $order_amount + ($order_amount * $this->servicefee_pct) + $this->servicefee_additional; ?>
Name on card
Card Number
Expiration (MM/YYYY)
 / 
CVC
$ (with service fee)
get_primary_team(); if($manual_team_id != ''){ $team_id = $manual_team_id; } // vars $time = $schedule->time(); $data_arr = array(); $per_page = 20; $start_point = ($page - 1) * $per_page; $type_sql = ''; $range_sql = ''; $active_sql = ''; if($product_type != ''){ $type_sql = " AND products.product_type = '{$product_type}' "; } if( is_array($date_range) && !empty($date_range) ){ if( isset($date_range[0]) && isset($date_range[1]) ){ $range_sql = " AND (payments.payment_time >= {$date_range[0]} AND payments.payment_time <= {$date_range[1]}) "; } } elseif( !is_array($date_range) && $date_range != '' ){ $range_sql = " AND (payments.payment_time >= {$date_range}) "; } // if a recurring type, show only active (time < expire time) by default if($reoccuring_type != 'none'){ $active_sql = " AND (teams_products.teamproduct_expiretime > {$time}) "; } $sql = "SELECT * FROM teams_products LEFT JOIN products ON products.product_id = teams_products.product_id LEFT JOIN payments ON payments.payment_id = teams_products.payment_id WHERE payments.team_id = {$team_id} AND teams_products.teamproduct_enabled = 'yes' AND products.product_reoccuring = '{$reoccuring_type}'{$type_sql}{$range_sql}{$active_sql} ORDER BY payments.payment_time DESC LIMIT {$start_point}, {$per_page}"; $data_arr = $this->db->get_arr($sql); return $data_arr; } // recurring payment update handler function recurring_payment_update($charge_obj) { global $schedule; if( !isset($schedule) ){ $schedule = new schedule(); } $time = $schedule->time(); $one_day_secs = 60 * 60 * 24; $customer_id = $charge_obj->customer; $amount = $charge_obj->amount / 100; //print_r($charge_obj); $sql = "SELECT * FROM teams_products LEFT JOIN payments ON payments.payment_id = teams_products.payment_id WHERE payments.payment_customer_id = '{$customer_id}' AND teams_products.teamproduct_enabled = 'yes' ORDER BY payment_time DESC LIMIT 0,1"; if($res = $this->db->query($sql)){ $prev_product = mysql_fetch_assoc($res); $team_id = $prev_product['team_id']; $last_payment_id = $prev_product['payment_id']; $last_payment_time = $prev_product['payment_time']; // ignore first payment from recurring transaction if( ($time - $last_payment_time) < $one_day_secs ){ return; } // insert new payment $payment_sql = "INSERT INTO payments SET team_id = {$team_id}, payment_amount = {$amount}, payment_customer_id = '{$customer_id}', payment_time = {$time}"; if( $this->db->query($payment_sql) ){ // disable old product $new_payment_id = mysql_insert_id(); $update_sql = "UPDATE teams_products SET teamproduct_enabled = 'no' WHERE teamproduct_id = {$prev_product['teamproduct_id']}"; if( $this->db->query($update_sql) ){ // get current product expire time $duration_arr = $this->db->get_arr("SELECT product_duration FROM products WHERE product_id = {$prev_product['product_id']}"); // insert new team product instantiation $expire_time = $time + $duration_arr[0]['product_duration']; $product_sql = "INSERT INTO teams_products SET team_id = {$team_id}, product_id = {$prev_product['product_id']}, payment_id = {$new_payment_id}, teamproduct_quantity = {$prev_product['teamproduct_quantity']}, teamproduct_expiretime = {$expire_time}, teamproduct_purchasetime = {$time}"; $this->db->query($product_sql); } } } } function ipn_recieve() { global $schedule; if( !isset($schedule) ){ $schedule = new schedule(); } $time = $schedule->time(); $this->init(); $body = @file_get_contents('php://input'); $event_json = json_decode($body); ob_start(); print_r($event_json); $event_str = ob_get_contents(); ob_end_clean(); $sql = "INSERT INTO ipn_messages SET ipn_value = '{$event_str}', ipn_time = {$time}"; $this->db->query($sql); if( !empty($event_json) ){ // checkout ipn if( isset($event_json->type) && $event_json->type == 'charge.succeeded' ){ $charge_obj = $event_json->data->object; if( isset($charge_obj->invoice) ){ $invoice_id = $charge_obj->invoice; //$invoice_id = ''; // todo remove $invoice_obj = Stripe_Invoice::retrieve($invoice_id)->lines->all(array('count' => 1, 'offset' => 0)); if( isset($invoice_obj->data[0]->plan) && !empty($invoice_obj->data[0]->plan) ){ $plan_obj = $invoice_obj->data[0]->plan; if( isset($plan_obj->id) && $plan_obj->id != '' ){ $this->recurring_payment_update($charge_obj); } } } } } } } // end class payment