/**
  * Buy a product
  *
  * @param User     $user
  * @param IPayment $paymentService
  * @param array    $paymentDetails
  *
  * @throws Exception
  */
 public function buy(User $user, IPayment $paymentService, $paymentDetails)
 {
     if (!$this->_isSalable()) {
         throw new Exception('Not for sale');
     }
     if ($this->_isPremium() && !$user->isSubscribed()) {
         throw new Exception('Need to continue subscription to buy this product');
     }
     //...
     if ($paymentService->processPayment($orderData)) {
         $paymentService->savePayment();
         $order = new Order($orderData, $paymentService->getData());
         $order->save();
         return $order;
     }
     throw new Exception('An error occurred while processing the payment');
 }
 /**
  * Pay for subscription and save it to DB
  *
  * @param User      $user
  * @param int       $units
  * @param IPayment  $paymentService
  * @param array     $paymentDetails
  *
  * @return bool
  * @throws Exception
  */
 public static function buy(User $user, $units, IPayment $paymentService, $paymentDetails)
 {
     $userData = $user->getData();
     if ($user->getUserGroup() == User::ADMIN_USER_GROUP) {
         throw new Exception('Administrators can\'t buy subscription');
     }
     if ($user->isSubscribed()) {
         $units += ($userData['subscribed_to'] - strtotime(date('Y-m-d', time()) . ' 00:00:00')) / 86400;
     }
     //...
     if ($paymentService->processPayment($paymentData)) {
         $userData['subscribed_to'] = strtotime(date('Y-m-d', strtotime("+{$units} days")) . ' 00:00:00');
         $paymentService->savePayment();
         return true;
     }
     throw new Exception('An error occurred while processing the payment');
     //...
 }
Example #3
0
 /**
  * Extra profile info for atom entries
  *
  * Clients use some extra profile info in the atom stream.
  * This gives it to them.
  *
  * @param User $cur Current user
  *
  * @return array representation of <statusnet:profile_info> element or null
  */
 function profileInfo($cur)
 {
     $profileInfoAttr = array('local_id' => $this->id);
     if ($cur != null) {
         // Whether the current user is a subscribed to this profile
         $profileInfoAttr['following'] = $cur->isSubscribed($this) ? 'true' : 'false';
         // Whether the current user is has blocked this profile
         $profileInfoAttr['blocking'] = $cur->hasBlocked($this) ? 'true' : 'false';
     }
     return array('statusnet:profile_info', $profileInfoAttr, null);
 }
<?php

// Create and load client
$user = UserFactory::registerUser('*****@*****.**', 'katesmith', 'Kate Smith');
// Create from invite and load client
$user = UserFactory::registerUser('*****@*****.**', 'hannabrown', 'Hanna Brown', 'Default', 'katesmith');
// Create and load administrator
$user = UserFactory::registerUser('*****@*****.**', 'johnsmith', 'John Smith', 'Administrator');
// Load user by email
$user = new User('*****@*****.**');
// Load user by login
$user = new User('johndoe', 'login');
// Check if subscription is valid
$user->isSubscribed();
// Buy subscription via PayPal for 4 days
Subscription::buy($user, 4, new PayPal(), $paymentDetails);
// Buy subscription via PayPal for 10 days
Subscription::buy($user, 10, new WebMoney(), $paymentDetails);
// Load a product by title
$product = new Product('TV');
// Load a product by ID
$product = new Product(736);
// Get product info
$product->getProductInfo();
// Buy the product
$order = $product->buy($user, new PayPal(), $paymentDetails);
// Get order by ID
$order = new Order();
$order->load(123);
// Set different statuses of order
$order->changeStatus(Order::ORDER_STATUS_CANCELLED);