Esempio n. 1
0
 /**
  * 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;
     }
 }
Esempio n. 2
0
 /**
  * Get Stripe customer ID for the user
  * @return string|boolean
  */
 public function getCustomerId()
 {
     $customer_id = $this->user->getPrivateSetting('stripe_customer_id');
     if (!$customer_id) {
         $stripe = new StripeClient();
         // Try other customer IDs stored on this user
         $customer_ids = $this->user->stripe_customer_id;
         if ($customer_ids) {
             if (!is_array($customer_ids)) {
                 $customer_ids = array($customer_ids);
             }
             foreach ($customer_ids as $customer_id) {
                 $account = $stripe->getCustomer($customer_id);
                 if ($account) {
                     break;
                 }
             }
         }
         if (!$account) {
             $account = $stripe->createCustomer($this->user);
         }
         $customer_id = $account->id;
         $this->user->setPrivateSetting('stripe_customer_id', $customer_id);
     }
     return $customer_id;
 }
Esempio n. 3
0
/**
 * Check if the email of the user has already been associated with a Stripe customer
 * If so, map them
 *
 * @param string $event
 * @param string $type
 * @param ElggUser $user
 * @param true
 */
function stripe_register_user($event, $type, $user)
{
    $customer_ref = elgg_get_plugin_setting($user->email, 'stripe');
    if (!$customer_ref) {
        return;
    }
    $customer_ref = unserialize($customer_ref);
    if (is_array($customer_ref) && sizeof($customer_ref)) {
        $user->setPrivateSetting('stripe_customer_id', $customer_ref[0]);
        $customer_ref = array_reverse($customer_ref);
        foreach ($customer_ref as $c_ref) {
            create_metadata($user->guid, 'stripe_customer_id', $c_ref, '', $user->guid, ACCESS_PUBLIC, true);
        }
    }
    elgg_unset_plugin_setting($user->email, 'stripe');
    return true;
}
Esempio n. 4
0
 /**
  * Make sure entity is loaded from cache during save operations
  * See #10612
  */
 public function testNewUserLoadedFromCacheDuringSaveOperations()
 {
     $user = new \ElggUser();
     // Add temporary metadata, annotation and private settings
     // to extend the scope of tests and catch issues with save operations
     $user->test_metadata = 'bar';
     $user->annotate('test_annotation', 'baz');
     $user->setPrivateSetting('test_setting', 'foo');
     $metadata_called = false;
     $metadata_event_handler = function ($event, $type, $metadata) use(&$metadata_called) {
         /* @var $metadata \ElggMetadata */
         $entity = get_entity($metadata->entity_guid);
         $this->assertEqual($metadata->entity_guid, $entity->guid);
         $metadata_called = true;
     };
     $annotation_called = false;
     $annotation_event_handler = function ($event, $type, $annotation) use(&$annotation_called) {
         /* @var $metadata \ElggAnnotation */
         $entity = get_entity($annotation->entity_guid);
         $this->assertEqual($annotation->entity_guid, $entity->guid);
         $annotation_called = true;
     };
     elgg_register_event_handler('create', 'metadata', $metadata_event_handler);
     elgg_register_event_handler('create', 'annotation', $annotation_event_handler);
     $user->save();
     elgg_unregister_event_handler('create', 'metadata', $metadata_event_handler);
     elgg_unregister_event_handler('create', 'annotation', $annotation_event_handler);
     $user->delete();
     $this->assertTrue($metadata_called);
     $this->assertTrue($annotation_called);
 }
Esempio n. 5
0
 /**
  * Create a new customer
  * @param ElggUser $user
  * @param array $data
  * @return Stripe_Customer|false
  */
 public function createCustomer($user = null, $data = array())
 {
     $fields = array('account_balance', 'card', 'coupon', 'plan', 'quantity', 'trial_end', 'metadata', 'description', 'email');
     try {
         foreach ($data as $key => $value) {
             if (!in_array($key, $fields)) {
                 $data[$key] = '';
             }
         }
         $data = array_filter($data);
         if ($user) {
             if (!$data['email']) {
                 $data['email'] = $user->email;
             }
             if (!$data['description']) {
                 $data['description'] = $user->name;
             }
             if (!is_array($data['metadata'])) {
                 $data['metadata'] = array();
             }
             $data['metadata']['guid'] = $user->guid;
             $data['metadata']['username'] = $user->username;
         }
         $customer = Stripe_Customer::create($data);
         if ($user && $user->guid) {
             // 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)) {
                     $stripe_ids = array($stripe_ids);
                 }
             }
             if (!in_array($customer->id, $stripe_ids)) {
                 create_metadata($user->guid, 'stripe_customer_id', $customer->id, '', $user->guid, ACCESS_PUBLIC, true);
             }
             // Store current Customer ID
             $user->setPrivateSetting('stripe_customer_id', $customer->id);
         } else {
             // Store customer IDs with their email reference locally
             // so that users can be assigned their existing customer ID upon registration
             $customer_ref = elgg_get_plugin_setting($customer->email, 'stripe');
             if ($customer_ref) {
                 $customer_ref = unserialize($customer_ref);
             } else {
                 $customer_ref = array();
             }
             array_unshift($customer_ref, $customer->id);
             elgg_set_plugin_setting($customer->email, serialize($customer_ref), 'stripe');
         }
         return $customer;
     } catch (Exception $ex) {
         $this->log($ex);
         return false;
     }
 }