private function charge_succeeded($event) { $this->load->model('Subscription'); $this->load->helper('notification'); $customer_id = $event->data->object->customer; $card_last_four = $event->data->object->card->last4; $amount = round($event->data->object->amount / 100, 2); $subscription = $this->Subscription->load_by_field('stripe_customer_id', $customer_id); if ($subscription) { /* Their subscription has failed to charge */ $this->Subscription->update($subscription->id, array('updated' => timestamp_to_mysqldatetime(now()))); $text = sprintf('[Stripe Event] charge.succeeded for subscription [%d]', $subscription->id); loggly(array('text' => $text, 'method' => 'stripe.charge_succeeded', 'event' => $event)); $user = $this->User->load($subscription->user_id); notify_successful_charge($user, $card_last_four, $amount); } }
function notify_successful_charge($user, $card_last_four = '', $amount = '') { $CI =& get_instance(); include APPPATH . '/views/emails/successful_charge.php'; $subject = sprintf('[%s] Subscription Receipt', $CI->config->item('site_title')); $mg = new Mailgun($CI->config->item('mailgun_key')); $response = $mg->sendMessage($CI->config->item('mailgun_domain'), array('from' => $CI->config->item('notifications_email_from') . ' <' . $CI->config->item('notifications_email') . '>', 'to' => $user->email, 'subject' => $subject, 'html' => $msg, 'text' => $msg_text, 'o:tracking' => 'yes', 'o:tracking-clicks' => 'yes', 'o:tracking-opens' => 'yes')); $log_text = sprintf('[Subscription Payment Successful] Sending notice: [%s] to %s', $subject, $user->email); log_message('info', $log_text); loggly(array('text' => $log_text, 'method' => 'notification_helper.notify_failed_charge', 'user' => $user, 'response' => $response)); }
/** * * @SWG\Api( * path="/subscription_card", * description="API for user actions", * @SWG\Operation( * method="POST", * type="Subscription", * summary="Adds/Updates the card on a subscription for the current user", * @SWG\Parameter( * name="token", * description="The token received from stripe for this transaction", * paramType="form", * required=false, * type="string" * ) * ) * ) */ public function subscription_card_post() { $this->validate_user(); $this->load->library('form_validation'); $this->form_validation->set_rules('token', 'Token', 'trim|xss_clean'); if ($this->form_validation->run() == FALSE) { json_error('There was a problem with your submission: ' . validation_errors(' ', ' ')); exit; } else { $user = get_user(); $this->load->model(array('Plan', 'Subscription')); $this->load->helper('notification'); \Stripe\Stripe::setApiKey($this->config->item('stripe_private_key')); $subscription = $this->Subscription->load_by_user_id(get_user_id()); if (!$subscription) { log_message('error', sprintf('[subscription_card_post] User [%s] does not have a valid subscription', $user->username)); json_error("User does not have a valid subscription"); } else { try { $stripe_customer = \Stripe\Customer::retrieve($subscription->stripe_customer_id); $stripe_cards = $stripe_customer->sources->all(array("object" => "card", "limit" => 1)); /* Create the new card */ $stripe_customer->sources->create(array("source" => $this->post('token', TRUE))); /* Delete their current card */ if (sizeof($stripe_cards->data) > 0) { $stripe_card = $stripe_cards->data[0]; $stripe_card->delete(); } $this->subscription_get(); } catch (Exception $e) { $error = '[subscription_card_post] Exception when deleting and adding a new card: ' . $e->getMessage(); log_message('info', $error); loggly(array('error' => $e->getMessage(), 'text' => $error, 'method' => 'rest.users.subscription_card_post', 'actor_id' => get_user_id())); json_error($e->getMessage()); exit; } } } }