/** * Retrieve a Stripe account for this mercant * * @return Stripe_Customer|boolean * @throws Stripe_Error */ public function getMerchantAccount() { if ($this->account->id) { return $this->account; } if (!$this->entity) { return false; } if (!($access_token = $this->getAccessToken())) { return false; } try { $stripe = new StripeClient(); $stripe->setAccessToken($access_token); $account = $stripe->getAccount(); if (!$account->id || isset($account->deleted)) { throw new Stripe_Error('Account does not exist or has been deleted'); } $this->account = $account; return $this->account; } catch (Stripe_Error $e) { $this->entity->setPrivateSetting('stripe_secret_key', null); $this->entity->setPrivateSetting('stripe_publishable_key', null); return false; } }
/** * Create a Stripe object menu * * @param string $hook Equals 'register' * @param string $type Equals 'stripe-actions' * @param array $return Current menu * @param array $params Additional params * @return array */ function stripe_actions_menu($hook, $type, $return, $params) { $object = elgg_extract('object', $params); switch ($object->object) { case 'card': $user = stripe_get_user_from_customer_id($object->customer); if (!elgg_instanceof($user) || !$user->canEdit()) { return $return; } $stripe = new StripeClient(); $default = $stripe->getDefaultCard($user->guid); if ($default->id == $object->id) { $return[] = ElggMenuItem::factory(array('name' => 'default', 'text' => elgg_echo('stripe:cards:default'), 'href' => false)); } else { $return[] = ElggMenuItem::factory(array('name' => 'default', 'text' => elgg_echo('stripe:cards:make_default'), 'href' => "action/stripe/cards/set_default?card_id={$object->id}&customer_id={$object->customer}", 'is_action' => 800, 'class' => 'elgg-requires-confirmation', 'rel' => elgg_echo('question:areyousure'))); $return[] = ElggMenuItem::factory(array('name' => 'remove', 'text' => elgg_echo('stripe:cards:remove'), 'href' => "action/stripe/cards/set_default?card_id={$object->id}&customer_id={$object->customer}", 'is_action' => true, 'priority' => 900, 'class' => 'elgg-requires-confirmation', 'rel' => elgg_echo('question:areyousure'))); } break; case 'charge': $user = stripe_get_user_from_customer_id($object->customer); if (!elgg_instanceof($user) || !$user->canEdit()) { return $return; } $full = elgg_normalize_url("billing/{$user->username}/charges/view/{$object->id}"); if (current_page_url() !== $full) { $return[] = ElggMenuItem::factory(array('name' => 'details', 'text' => elgg_echo('stripe:charges:view'), 'href' => $full)); } break; case 'invoice': $user = stripe_get_user_from_customer_id($object->customer); if (!elgg_instanceof($user) || !$user->canEdit()) { return $return; } if (isset($object->id)) { $full = elgg_normalize_url("billing/{$user->username}/invoices/view/{$object->id}"); if (current_page_url() !== $full) { $return[] = ElggMenuItem::factory(array('name' => 'details', 'text' => elgg_echo('stripe:invoices:view'), 'href' => $full)); } } break; case 'subscription': $user = stripe_get_user_from_customer_id($object->customer); if (!elgg_instanceof($user) || !$user->canEdit()) { return $return; } $upcoming = elgg_normalize_url("billing/{$user->username}/invoices/upcoming/{$object->id}"); if (current_page_url() !== $upcoming) { $return[] = ElggMenuItem::factory(array('name' => 'details', 'text' => elgg_echo('stripe:invoices:upcoming'), 'href' => $upcoming)); } if (!$object->cancel_at_period_end) { $return[] = ElggMenuItem::factory(array('name' => 'cancel', 'text' => elgg_echo('subscriptions:cancel'), 'href' => "action/stripe/subscriptions/cancel?subscription_id={$object->id}&customer_id={$object->customer}", 'is_action' => 800, 'class' => 'elgg-requires-confirmation', 'rel' => elgg_echo('question:areyousure'))); } break; } return $return; }
/** * Create a new customer card from a Stripe token * @param integer $user_guid * @param string $token * @return Stripe_Card|false */ function stripe_create_card($user_guid = 0, $token = '') { if (!$user_guid) { $user_guid = elgg_get_logged_in_user_guid(); } if (!$token) { return false; } $stripe = new StripeClient(); $card = $stripe->createCard($user_guid, $token); return $card; }
/** * Handle Stripe webhooks */ function stripe_webhook_handler($environment) { $body = get_post_data(); $event_json = json_decode($body); $event_id = $event_json->id; $gateway = new StripeClient($environment); $event = $gateway->getEvent($event_id); if (!$event) { return array('success' => false, 'message' => 'Stripe Event for this webhook was not found'); } $ia = elgg_set_ignore_access(true); $ha = access_get_show_hidden_status(); access_show_hidden_entities(true); $result = elgg_trigger_plugin_hook_handler($event->type, 'stripe.events', array('environment' => $environment, 'event' => $event), array('success' => true)); access_show_hidden_entities($ha); elgg_set_ignore_access($ia); return $result; }
<?php $user = elgg_get_page_owner_entity(); $starting_after = get_input('starting_after', null); $ending_before = get_input('ending_before', null); $limit = get_input('limit', 10); $stripe = new StripeClient(); $cards = $stripe->getCards($user->guid, $limit, $ending_before, $starting_after); $list = elgg_view('stripe/objects/list', array('objects' => $cards, 'starting_after' => $starting_after, 'ending_before' => $ending_before, 'limit' => $limit)); echo elgg_view_module('aside', elgg_echo('stripe:cards:list'), $list); $form = elgg_view_form('stripe/cards/add', array('class' => 'stripe-form'), array('entity' => $user)); echo elgg_view_module('aside', elgg_echo('stripe:cards:add'), $form);
<?php $user = elgg_get_page_owner_entity(); $subscription_id = elgg_extract('id', $vars, null); $stripe = new StripeClient(); $invoice = $stripe->getUpcomingInvoice($user, $subscription_id); echo elgg_view_module('info', elgg_echo('stripe:invoices:upcoming'), elgg_view('stripe/objects/invoice', array('object' => $invoice))); $list = elgg_view('stripe/objects/list', array('objects' => $invoice->lines)); echo elgg_view_module('info', elgg_echo('stripe:invoices:items'), $list); if ($invoice->subscription) { $subscription = $stripe->getSubscription($invoice->customer, $invoice->subscription); if ($subscription) { echo elgg_view_module('info', elgg_echo('stripe:subscriptions:title', array($subscription->id)), elgg_view('stripe/objects/subscription', array('object' => $subscription))); } }
<?php $id = elgg_extract('id', $vars); $user = elgg_get_page_owner_entity(); $stripe = new StripeClient(); $charge = $stripe->getSubscription($user, $id); $title = elgg_echo('stripe:charges:title', array($charge->id)); echo elgg_view_module('info', elgg_echo('stripe:charges:title', array($charge->id)), elgg_view('stripe/objects/invoice', array('object' => $charge))); if ($charge->invoice) { $invoice = $stripe->getInvoice($charge->invoice); echo elgg_view_module('info', elgg_echo('stripe:invoices:title', array($invoice->id)), elgg_view('stripe/objects/charge', array('object' => $charge))); } if (elgg_get_logged_in_user_guid() !== elgg_get_page_owner_guid()) { $customer = $stripe->getCustomer($charge->customer); echo elgg_view_module('info', elgg_echo('stripe:customers:title', array($customer->id)), elgg_view('stripe/objects/customer', array('object' => $customer))); }
<?php $id = elgg_extract('id', $vars); $user = elgg_get_page_owner_entity(); $stripe = new StripeClient(); $card = $stripe->getCard($user, $id); $vars['object'] = $card; echo elgg_view('stripe/objects/card', $vars);
<?php $token = get_input('stripe-token'); $guid = get_input('guid'); $email = get_input('email'); $customer_id = get_input('customer_id'); if ($guid) { $attr = $guid; } else { if ($email) { $attr = $email; } else { if ($customer_id) { $attr = $customer_id; } else { $attr = elgg_get_logged_in_user_guid(); } } } $stripe = new StripeClient(); $card = $stripe->createCard($attr, $token); if ($card) { system_message(elgg_echo('stripe:cards:add:success')); if (elgg_is_xhr()) { echo json_encode(array('label' => "{$card->type}-{$card->last4} ({$card->exp_month} / {$card->exp_year})", 'id' => $card->id, 'view' => elgg_view('stripe/objects/card', array('object' => $card)))); } } else { register_error(elgg_echo('stripe:cards:add:error')); $stripe->showErrors(); } forward(REFERER);
<?php $card_id = get_input('card_id'); $customer_id = get_input('customer_id'); $user = stripe_get_user_from_customer_id($customer_id); if (!elgg_instanceof($user) || !$user->canEdit()) { register_error(elgg_echo('stripe:access_error')); forward(REFERER); } $stripe = new StripeClient(); if ($stripe->deleteCard($user->guid, $card_id)) { system_message(elgg_echo('stripe:cards:remove:success')); } else { register_error(elgg_echo('stripe:cards:remove:error')); $stripe->showErrors(); } forward(REFERER);
<?php $id = elgg_extract('id', $vars); $user = elgg_get_page_owner_entity(); $stripe = new StripeClient(); $invoice = $stripe->getInvoice($id); $title = elgg_echo('stripe:invoices:title', array($invoice->id)); echo elgg_view_module('info', elgg_echo('stripe:invoices:title', array($invoice->id)), elgg_view('stripe/objects/invoice', array('object' => $invoice))); if ($invoice->charge) { $charge = $stripe->getCharge($invoice->charge); if ($charge) { echo elgg_view_module('info', elgg_echo('stripe:charges:title', array($charge->id)), elgg_view('stripe/objects/charge', array('object' => $charge))); } } echo elgg_view_module('info', elgg_echo('stripe:invoices:items:title', array($id)), elgg_view('stripe/pages/invoices/items', array('id' => $invoice->id))); if ($invoice->subscription) { $subscription = $stripe->getSubscription($invoice->customer, $invoice->subscription); if ($subscription) { echo elgg_view_module('info', elgg_echo('stripe:subscriptions:title', array($subscription->id)), elgg_view('stripe/objects/subscription', array('object' => $subscription))); } } if (elgg_get_logged_in_user_guid() !== elgg_get_page_owner_guid()) { $customer = $stripe->getCustomer($invoice->customer); if ($customer) { echo elgg_view_module('info', elgg_echo('stripe:customers:title', array($customer->id)), elgg_view('stripe/objects/customer', array('object' => $customer))); } } //echo $stripe->viewErrors();
/** * Retrieve a Stripe customer account * @return Stripe_Customer|boolean * @throws Stripe_Error */ public function getCustomerAccount() { if ($this->account->id) { return $this->account; } try { $customer_id = $this->getCustomerId(); if (!$customer_id) { throw new Stripe_Error('No customer id'); } $stripe = new StripeClient(); $account = $stripe->getCustomer($customer_id); if (!$account->id || isset($account->deleted)) { throw new Stripe_Error('Customer does not exist or has been deleted'); } return $account; } catch (Stripe_Error $e) { $this->user->removePrivateSetting('stripe_customer_id'); error_log($e->getMessage()); return $this->getCustomerAccount(); } }
<?php $user = elgg_get_page_owner_entity(); $starting_after = get_input('starting_after', null); $ending_before = get_input('ending_before', null); $limit = get_input('limit', 10); $stripe = new StripeClient(); $cards = $stripe->getSubscriptions($user->guid, $limit, $ending_before, $starting_after); echo elgg_view('stripe/objects/list', array('objects' => $cards, 'starting_after' => $starting_after, 'ending_before' => $ending_before, 'limit' => $limit));
<?php $subscription_id = get_input('subscription_id'); $customer_id = get_input('customer_id'); $user = stripe_get_user_from_customer_id($customer_id); if (!elgg_instanceof($user) || !$user->canEdit()) { register_error(elgg_echo('stripe:access_error')); forward(REFERER); } $stripe = new StripeClient(); $subscription = $stripe->getSubscription($user->guid, $subscription_id); if ($subscription) { $subscription = $stripe->cancelSubscription($user->guid, $subscription->id); } if ($subscription->status == 'canceled' || $subscription->cancel_at_period_end) { system_message(elgg_echo('subscriptions:cancel:success')); } else { register_error(elgg_echo('subscriptions:cancel:error')); } forward(REFERER);
<?php access_show_hidden_entities(true); $stripe = new StripeClient(); $has_more = true; $starting_after = null; while ($has_more) { $customers = $stripe->getCustomers(100, null, $starting_after, null); if ($customers->data && sizeof($customers->data)) { foreach ($customers->data as $customer) { // Check if the user with this customer id already exists $user = stripe_get_user_from_customer_id($customer->id); if (!$user) { // Check if user guid is supplied with customer metadata if (isset($customer->metadata->guid)) { $guid = $customer->metadata->guid; $user = get_entity($guid); } } if (!$user) { // Try mapping by email $users = get_user_by_email($customer->email); $user = $users ? $users[0] : false; } if ($user) { // Store any customer IDs this user might have for reference $stripe_ids = $user->stripe_customer_id; if (!$stripe_ids) { $stripe_ids = array(); } else { if (!is_array($stripe_ids)) {
<?php $card_id = get_input('card_id'); $customer_id = get_input('customer_id'); $user = stripe_get_user_from_customer_id($customer_id); if (!elgg_instanceof($user) || !$user->canEdit()) { register_error(elgg_echo('stripe:access_error')); forward(REFERER); } $stripe = new StripeClient(); if ($stripe->setDefaultCard($user->guid, $card_id)) { system_message(elgg_echo('stripe:cards:make_default:success')); } else { register_error(elgg_echo('stripe:cards:make_default:error')); $stripe->showErrors(); } forward(REFERER);
<?php $limit = get_input('limit', 10); $ending_before = get_input('ending_before'); $starting_after = get_input('starting_after'); $stripe = new StripeClient(); $customers = $stripe->getCustomers($limit, $ending_before, $starting_after); $mod = elgg_view('output/url', array('text' => elgg_echo('stripe:customers:sync'), 'href' => 'action/stripe/customers/sync', 'is_action' => true, 'class' => 'elgg-button elgg-button-action mam pam')); echo elgg_view_module('main', elgg_echo('stripe:customers:sync'), $mod); $mod2 = elgg_view('stripe/objects/list', array('objects' => $customers, 'starting_after' => $starting_after, 'ending_before' => $ending_before, 'limit' => $limit)); echo elgg_view_module('main', elgg_echo('stripe:customers:list'), $mod2);
<?php $user = elgg_extract('entity', $vars, elgg_get_logged_in_user_entity()); $stripe = new StripeClient(); $cards = $stripe->getCards($user->guid); $options_values = array('' => elgg_echo('stripe:cards:select')); if ($cards->data) { foreach ($cards->data as $card) { $options_values[$card->id] = "{$card->type}-{$card->last4} ({$card->exp_month} / {$card->exp_year})"; } } $options_values['__new__'] = elgg_echo('stripe:cards:add'); $vars['options_values'] = $options_values; $name = elgg_extract('name', $vars, 'stripe-token'); $value = elgg_extract('value', $vars, ''); if (!sizeof($cards->data)) { $hidden = ' hidden'; echo elgg_view('output/url', array('text' => elgg_echo('stripe:cards:add'), 'href' => 'billing/add_card/' . $user->username, 'class' => 'elgg-button elgg-button-action stripe-cards-no-picker')); } echo elgg_view('input/dropdown', array('name' => $name, 'value' => $value, 'options_values' => $options_values, 'class' => 'stripe-cards-picker' . $hidden, 'data-endpoint' => 'billing/add_card/' . $user->username));
<?php $id = elgg_extract('id', $vars); if (!$id) { return; } $starting_after = get_input('starting_after', null); $ending_before = get_input('ending_before', null); $limit = get_input('limit', 100); $stripe = new StripeClient(); $items = $stripe->getInvoiceItems($user->guid, $id, $limit, $ending_before, $starting_after); echo elgg_view('stripe/objects/list', array('objects' => $items, 'starting_after' => $starting_after, 'ending_before' => $ending_before, 'limit' => $limit));
<?php $user = elgg_get_page_owner_entity(); $starting_after = get_input('starting_after', null); $ending_before = get_input('ending_before', null); $limit = get_input('limit', 10); $stripe = new StripeClient(); $charges = $stripe->getCharges($user->guid, $limit, $ending_before, $starting_after); echo elgg_view('stripe/objects/list', array('objects' => $charges, 'starting_after' => $starting_after, 'ending_before' => $ending_before, 'limit' => $limit));
<?php $id = elgg_extract('id', $vars); $user = elgg_get_page_owner_entity(); $stripe = new StripeClient(); $charge = $stripe->getCharge($id); $title = elgg_echo('stripe:charges:title', array($charge->id)); echo elgg_view_module('info', elgg_echo('stripe:charges:title', array($charge->id)), elgg_view('stripe/objects/charge', array('object' => $charge))); if ($charge->invoice) { $invoice = $stripe->getInvoice($charge->invoice); echo elgg_view_module('info', elgg_echo('stripe:invoices:title', array($invoice->id)), elgg_view('stripe/objects/invoice', array('object' => $invoice))); } if (elgg_get_logged_in_user_guid() !== elgg_get_page_owner_guid()) { $customer = $stripe->getCustomer($charge->customer); echo elgg_view_module('info', elgg_echo('stripe:customers:title', array($customer->id)), elgg_view('stripe/objects/customer', array('object' => $customer))); }