/** * Cancel Subscription * * @param int $sub_id * @param boolean $make_api_call (default: FALSE) Shall we tell OG? * @param boolean $expired (default: FALSE) Is this expiring? * * @return boolean */ function CancelSubscription($sub_id, $make_api_call = TRUE, $expired = FALSE, $renewed = FALSE) { if (!($subscription = $this->GetSubscription($sub_id))) { return FALSE; } // calculate end_date $end_date = $this->_calculate_end_date($subscription['end_date'], $subscription['next_charge_date'], $subscription['date_created']); // nullify next charge $next_charge_date = '0000-00-00'; // cancel subscription $update_array = array('active' => '0', 'end_date' => $end_date, 'next_charge_date' => $next_charge_date, 'date_cancelled' => date('Y-m-d H:i:s')); // are we cancelling or expiring? if ($expired == TRUE) { $update_array['expired'] = '1'; } elseif ($expired == FALSE and $renewed == FALSE) { $update_array['cancelled'] = '1'; } $this->EE->db->update('exp_membrr_subscriptions', $update_array, array('recurring_id' => $subscription['id'])); // call "membrr_cancel" hook with: member_id, subscription_id, plan_id, end_date if ($this->EE->extensions->active_hook('membrr_before_cancel') == TRUE) { $this->EE->extensions->call('membrr_before_cancel', $subscription['member_id'], $subscription['id'], $subscription['plan_id'], $subscription['end_date']); if ($this->EE->extensions->end_script === TRUE) { return; } } if ($make_api_call == true) { $config = $this->GetConfig(); if (!class_exists('OpenGateway')) { require dirname(__FILE__) . '/opengateway.php'; } $connect_url = $config['api_url'] . '/api'; $server = new OpenGateway(); $server->Authenticate($config['api_id'], $config['secret_key'], $connect_url); $server->SetMethod('CancelRecurring'); $server->Param('recurring_id', $subscription['id']); $response = $server->Process(); if (isset($response['error'])) { return FALSE; } } // call "membrr_cancel" hook with: member_id, subscription_id, plan_id, end_date if ($this->EE->extensions->active_hook('membrr_cancel') == TRUE) { $this->EE->extensions->call('membrr_cancel', $subscription['member_id'], $subscription['id'], $subscription['plan_id'], $subscription['end_date']); if ($this->EE->extensions->end_script === TRUE) { return; } } return TRUE; }
function post_notify() { // get Membrr config $config = $this->membrr->GetConfig(); // connect to API require_once dirname(__FILE__) . '/opengateway.php'; $connect_url = $config['api_url'] . '/api'; $server = new OpenGateway(); $server->Authenticate($config['api_id'], $config['secret_key'], $connect_url); // first, we'll check for external payment API redirects if ($this->EE->input->get('member') and is_numeric($this->EE->input->get('member')) and is_numeric($this->EE->input->get('plan_id'))) { if ($plan = $this->membrr->GetPlan($this->EE->input->get('plan_id'))) { // get customer ID $server->SetMethod('GetCustomers'); $server->Param('internal_id', $this->EE->input->get('member')); $response = $server->Process(); $customer = !isset($response['customers']['customer'][0]) ? $response['customers']['customer'] : $response['customers']['customer'][0]; if (empty($customer)) { die('Invalid customer record.'); } $server->SetMethod('GetRecurrings'); $server->Param('customer_id', $customer['id']); $server->Param('plan_id', $plan['api_id']); $response = $server->Process(); if (isset($response['recurrings']['recurring'][0])) { $recurrings = $response['recurrings']['recurring']; } elseif (isset($response['recurrings'])) { $recurrings = array(); $recurrings[] = $response['recurrings']['recurring']; } else { $recurrings = array(); } // is there a new recurring charge for this client? foreach ($recurrings as $recurring) { if (!$this->membrr->GetSubscription($recurring['id'])) { // we have a new charge! $end_date = date('Y-m-d H:i:s', strtotime($recurring['end_date'])); $next_charge_date = date('Y-m-d H:i:s', strtotime($recurring['next_charge_date'])); // do we need to perform some maintenance for renewals? if ($this->EE->input->get('renew_recurring_id')) { // validate old subscription $result = $this->EE->db->where('member_id', $this->EE->input->get('member'))->where('recurring_id', $this->EE->input->get('renew_recurring_id'))->get('exp_membrr_subscriptions'); if ($result->num_rows() > 0) { $this->membrr->RenewalMaintenance($this->EE->input->get('renew_recurring_id'), $recurring['id']); } } $coupon = $this->EE->input->cookie('membrr_coupon'); $this->membrr->RecordSubscription($recurring['id'], $this->EE->input->get('member'), $plan['id'], $next_charge_date, $end_date, $recurring['amount'], $coupon); // get the first charge $server->SetMethod('GetCharges'); $server->Param('recurring_id', $recurring['id']); $charge = $server->Process(); // if there was an initial payment, charge should be an array, but there shouldn't be multiple charges! if (!empty($charge) and isset($charge['charges']) and is_array($charge['charges']) and !isset($charge['charges']['charge'][0])) { $charge = $charge['charges']['charge']; $payment = $charge['amount']; $this->membrr->RecordPayment($recurring['id'], $charge['id'], $payment); } // redirect $cookie = $this->EE->input->cookie('membrr_redirect_url'); if (!empty($cookie)) { $redirect_url = $cookie; } else { $redirect_url = $plan['redirect_url']; } header('Location: ' . $redirect_url); die; } } } } // is the secret key OK? ie. is this a legitimate call? if ($this->EE->input->get_post('secret_key') != $config['secret_key']) { die('Invalid secret key.'); } if (!$this->EE->input->get_post('customer_id') or !$this->EE->input->get_post('recurring_id')) { die('Insufficient data.'); } // get customer data from server $server->SetMethod('GetCustomer'); $server->Param('customer_id', $this->EE->input->get_post('customer_id')); $response = $server->Process(); if (!is_array($response) or !isset($response['customer'])) { die('Error retrieving customer data.'); } else { $customer = $response['customer']; } // get subscription data locally $subscription = $this->membrr->GetSubscription($this->EE->input->get_post('recurring_id')); if (!$subscription) { die('Error retrieving subscription data locally.'); } if ($this->EE->input->get_post('action') == 'recurring_charge') { if (!$this->EE->input->get_post('charge_id')) { die('No charge ID.'); } if (is_array($this->membrr->GetPayments(0, 1, array('id' => $this->EE->input->get_post('charge_id'))))) { die('Charge already recorded.'); } $server->SetMethod('GetCharge'); $server->Param('charge_id', $this->EE->input->get_post('charge_id')); $charge = $server->Process(); $charge = $charge['charge']; // record charge $this->membrr->RecordPayment($this->EE->input->get_post('recurring_id'), $this->EE->input->get_post('charge_id'), $charge['amount']); // update next charge date $plan = $this->membrr->GetPlan($subscription['plan_id']); if ($this->membrr->same_day_every_month == TRUE and $plan['interval'] % 30 === 0) { $months = $plan['interval'] / 30; $plural = $months > 1 ? 's' : ''; $next_charge_date = strtotime('today + ' . $months . ' month' . $plural); } else { $next_charge_date = strtotime('now + ' . $plan['interval'] . ' days'); } if (!empty($subscription['end_date']) and strtotime($subscription['end_date']) < $next_charge_date) { // there won't be a next charge // subscription will expire beforehand $next_charge_date = '0000-00-00'; } else { $next_charge_date = date('Y-m-d', $next_charge_date); } $this->membrr->SetNextCharge($this->EE->input->get_post('recurring_id'), $next_charge_date); } elseif ($this->EE->input->get_post('action') == 'recurring_cancel') { $this->membrr->CancelSubscription($subscription['id'], FALSE); } elseif ($this->EE->input->get_post('action') == 'recurring_expire' or $this->EE->input->get_post('action') == 'recurring_fail') { $this->membrr->CancelSubscription($subscription['id'], FALSE, TRUE); } }
/** * Hook: update_email * * Update a customer's record at OG when they edit their profile * with Solspace's User module. */ function update_email($member_id, $member_data, $custom_data) { $config = $this->membrr->GetConfig(); if ($config['update_email'] == FALSE or !isset($member_data['email'])) { return FALSE; } if (!class_exists('OpenGateway')) { require dirname(__FILE__) . '/opengateway.php'; } $connect_url = $config['api_url'] . '/api'; $server = new OpenGateway(); $server->Authenticate($config['api_id'], $config['secret_key'], $connect_url); $server->SetMethod('GetCustomers'); $server->Param('internal_id', $member_id); $response = $server->Process(); if ($response['total_results'] > 0) { // there is already a customer record here $customer = !isset($response['customers']['customer'][0]) ? $response['customers']['customer'] : $response['customers']['customer'][0]; $server->SetMethod('UpdateCustomer'); $server->Param('customer_id', $customer['id']); $server->Param('email', $member_data['email']); $response = $server->Process(); } return TRUE; }
function validate_api($api_url, $api_id, $secret_key) { // does URL exist? $headers = @get_headers($api_url); $file = @file_get_contents($api_url); if (ini_get('allow_url_fopen') and $file == FALSE or !empty($headers) and (!isset($headers[0]) or strstr($headers[0], '404') or strstr($headers[0], '403'))) { return FALSE; } else { include_once dirname(__FILE__) . '/opengateway.php'; $server = new OpenGateway(); $server->Authenticate($api_id, $secret_key, $api_url . '/api'); $server->SetMethod('GetCharges'); $response = $server->Process(); if (!isset($response['error'])) { return TRUE; } else { return FALSE; } } return FALSE; }