/** * Create or retrieve a Stripe customer account * @param mixed $user_attr ElggUser or guid or email * @throws IOException */ function __construct($user_attr = null) { if ($user_attr instanceof ElggUser) { $this->user = $user_attr; } else { if (is_email_address($user_attr)) { $users = get_user_by_email($user_attr); if (!$users) { $customer_ref = elgg_get_plugin_setting($user_attr, 'stripe'); if ($customer_ref) { $customer_ref = unserialize($customer_ref); } else { $customer_ref = array(); } $customer_id = $customer_ref[0]; } else { $this->user = $users[0]; } $email = $user_attr; } else { if (is_string($user_attr) && substr($user_attr, 0, 4) == 'cus_') { $customer_id = $user_attr; } else { if (is_numeric($user_attr)) { $this->user = get_entity($user_attr); } } } } if (!$this->user && $customer_id) { if ($user = stripe_get_user_from_customer_id($customer_id)) { $this->user = $user; } } if (!$this->user) { $this->user = new ElggUser(); $this->user->email = $email; if ($customer_id) { $this->user->setPrivateSetting('stripe_customer_id', $customer_id); } } $this->account = $this->getCustomerAccount(); if (!$this->account) { throw new IOException("Stripe customer account can not be retrieved or created"); } }
<?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);
/** * Webhook on refunded charge * Sends out a notification to the user with a corresponding customer id * * @param string $hook Equals 'charge.refunded' * @param string $type Equals 'stripe.event' * @param array $return * @param array $params * @return array */ function stripe_charge_refunded_event($hook, $type, $return, $params) { $event = elgg_extract('event', $params); if (!$event instanceof Stripe_Event) { return $return; } $charge = $event->data->object; $customer_id = $charge->customer; $customer = stripe_get_user_from_customer_id($customer_id); if ($customer) { if ($merchant_guid = $charge->metadata->merchant_guid) { $merchant = get_entity($merchant_guid); } else { $merchant = elgg_get_site_entity(); } $amount = new StripePricing($charge->refunds[0]->amount / 100, 0, 0, $charge->refunds[0]->currency); $subject = elgg_echo('stripe:notification:charge:refunded:subject', array($merchant->name)); $body = elgg_echo('stripe:notification:charge:refunded:body', array($customer->name, $amount, $merchant->name, $charge->card->type, $charge->card->last4, elgg_view('output/url', array('href' => elgg_normalize_url("billing/{$customer->username}/charges/all"))))); if (notify_user($customer->guid, $merchant->guid, $subject, $body)) { return array('success' => true, 'message' => 'User has been notified about the refund'); } } return array('success' => false, 'message' => 'Notification failed or user does not exist'); }
/** * Delete a customer * @param string $customer_id * @return boolean */ public function deleteCustomer($customer_id = '') { try { $customer = $this->getCustomer($customer_id); if (!$customer) { return true; } $response = $customer->delete(); if ($response->deleted) { $user = stripe_get_user_from_customer_id($response->id); if ($user) { if ($user->getPrivateSetting('stripe_customer_id') == $response->id) { $user->removePrivateSetting('stripe_customer_id'); } } return true; } } catch (Exception $ex) { $this->log($ex); return false; } }
<?php $customer = elgg_extract('object', $vars); if (!$customer instanceof Stripe_Customer) { return; } ?> <div class="stripe-row stripe-object stripe-customer"> <div class="stripe-col-6of12 stripe-customer-user"> <?php if ($user = stripe_get_user_from_customer_id($customer->id)) { $icon = elgg_view_entity_icon($user, 'tiny'); $link = elgg_view('output/url', array('text' => $user->name, 'href' => $user->getURL())); $alt = ''; if (is_array($user->stripe_customer_id)) { $count = sizeof($user->stripe_customer_id); if ($user->getPrivateSetting('stripe_customer_id') == $customer->id) { $alt = "Primary ({$count} total)"; } else { $alt = "Alternative ({$count} total)"; } } echo elgg_view_image_block($icon, $link, array('image_alt' => $alt)); } ?> </div> <div class="stripe-col-6of12 stripe-customer-details"> <div class="stripe-info"> <?php echo $customer->id;