/** * Public function that creates a single instance */ public static function getInstance() { if (!isset(self::$_instance)) { self::$_instance = new self(); } return self::$_instance; }
/** * Handle displaying list of stored currencies * * @param array $tag_params * @param array $children */ public function tag_CurrencyList($tag_params, $children) { $manager = ShopCurrenciesManager::getInstance(); $conditions = array(); $items = $manager->getItems($manager->getFieldNames(), $conditions); // create template $template = $this->_parent->loadTemplate($tag_params, 'currency_list_item.xml'); $template->setMappedModule($this->name); $selected = isset($tag_params['selected']) ? fix_id($tag_params['selected']) : -1; // parse template if (count($items) > 0) { foreach ($items as $item) { $params = $this->getCurrencyForCode($item->currency); $params['selected'] = $selected; // add delete link to params $params['item_delete'] = url_MakeHyperlink($this->_parent->getLanguageConstant('delete'), window_Open('shop_currencies_delete', 270, $this->_parent->getLanguageConstant('title_currencies_delete'), false, false, url_Make('transfer_control', 'backend_module', array('module', $this->name), array('backend_action', 'currencies'), array('sub_action', 'delete'), array('id', $item->id)))); $template->restoreXML(); $template->setLocalParams($params); $template->parse(); } } }
/** * Handle drawing checkout form * * @param array $tag_params * @param array $children */ public function tag_CheckoutForm($tag_params, $children) { $account_information = array(); $shipping_information = array(); $billing_information = array(); $payment_method = null; $stage = isset($_REQUEST['stage']) ? fix_chars($_REQUEST['stage']) : null; $recurring = isset($_SESSION['recurring_plan']) && !empty($_SESSION['recurring_plan']); // decide whether to include shipping and account information if (isset($tag_params['include_shipping'])) { $include_shipping = fix_id($tag_params['include_shipping']) == 1; } else { $include_shipping = true; } $bad_fields = array(); $info_available = false; // grab user information if (!is_null($stage)) { // get payment method $payment_method = $this->getPaymentMethod($tag_params); if (is_null($payment_method)) { throw new PaymentMethodError('No payment method selected!'); } // get billing information $billing_information = $this->getBillingInformation($payment_method); $billing_required = array('billing_full_name', 'billing_card_type', 'billing_credit_card', 'billing_expire_month', 'billing_expire_year', 'billing_cvv'); $bad_fields = $this->checkFields($billing_information, $billing_required, $bad_fields); // get shipping information if ($include_shipping && $stage == 'set_info') { $shipping_information = $this->getShippingInformation(); $shipping_required = array('name', 'email', 'street', 'city', 'zip', 'country'); $bad_fields = $this->checkFields($shipping_information, $shipping_required, $bad_fields); } } $info_available = count($bad_fields) == 0 && !is_null($payment_method); if ($info_available) { $address_manager = ShopDeliveryAddressManager::getInstance(); $currency_manager = ShopCurrenciesManager::getInstance(); // get fields for payment method $return_url = url_Make('checkout_completed', 'shop', array('payment_method', $payment_method->get_name())); $cancel_url = url_Make('checkout_canceled', 'shop', array('payment_method', $payment_method->get_name())); // get currency info $currency = $this->settings['default_currency']; $currency_item = $currency_manager->getSingleItem(array('id'), array('currency' => $currency)); if (is_object($currency_item)) { $transaction_data['currency'] = $currency_item->id; } // get buyer $buyer = $this->getUserAccount(); if ($include_shipping) { $address = $this->getAddress($buyer, $shipping_information); } else { $address = null; } // update transaction $transaction_type = $recurring ? TransactionType::SUBSCRIPTION : TransactionType::SHOPPING_CART; $summary = $this->updateTransaction($transaction_type, $payment_method, '', $buyer, $address); // emit signal and return if handled if ($stage == 'set_info') { Events::trigger('shop', 'before-checkout', $payment_method->get_name(), $return_url, $cancel_url); foreach ($result_list as $result) { if ($result) { $this->showCheckoutRedirect(); return; } } } // create new payment if ($recurring) { // recurring payment $checkout_fields = $payment_method->new_recurring_payment($_SESSION['recurring_plan'], $billing_information, $return_url, $cancel_url); } else { // regular payment $checkout_fields = $payment_method->new_payment($transaction_data, $billing_information, $summary['items_for_checkout'], $return_url, $cancel_url); } // load template $template = $this->loadTemplate($tag_params, 'checkout_form.xml'); $template->registerTagHandler('cms:checkout_items', $this, 'tag_CheckoutItems'); $template->registerTagHandler('cms:delivery_methods', $this, 'tag_DeliveryMethodsList'); // parse template $params = array('checkout_url' => $payment_method->get_url(), 'checkout_fields' => $checkout_fields, 'checkout_name' => $payment_method->get_title(), 'currency' => $this->getDefaultCurrency(), 'recurring' => $recurring, 'include_shipping' => $include_shipping); // for recurring plans add additional params if ($recurring) { $plans = $payment_method->get_recurring_plans(); $plan_name = $_SESSION['recurring_plan']; $plan = $plans[$plan_name]; $params['plan_name'] = $plan['name']; $params['plan_description'] = $this->formatRecurring(array('price' => $plan['price'], 'period' => $plan['interval_count'], 'period' => $plan['interval_count'], 'unit' => $plan['interval'], 'setup' => $plan['setup_price'], 'trial_period' => $plan['trial_count'], 'trial_unit' => $plan['trial'])); } else { $params['sub-total'] = number_format($summary['total'], 2); $params['shipping'] = number_format($summary['shipping'], 2); $params['handling'] = number_format($summary['handling'], 2); $params['total_weight'] = number_format($summary['weight'], 2); $params['total'] = number_format($summary['total'] + $summary['shipping'] + $summary['handling'], 2); } $template->restoreXML(); $template->setLocalParams($params); $template->parse(); } else { // no information available, show form $template = new TemplateHandler('buyer_information.xml', $this->path . 'templates/'); $template->setMappedModule($this->name); $template->registerTagHandler('cms:card_type', $this, 'tag_CardType'); // get fixed country if set $fixed_country = ''; if (isset($this->settings['fixed_country'])) { $fixed_country = $this->settings['fixed_country']; } $params = array('include_shipping' => $include_shipping, 'fixed_country' => $fixed_country, 'bad_fields' => $bad_fields, 'recurring' => $recurring); $template->restoreXML(); $template->setLocalParams($params); $template->parse(); } }
/** * Handle drawing list of items in transaction */ public function tag_TransactionItemList($tag_params, $children) { $manager = ShopTransactionItemsManager::getInstance(); $item_manager = ShopItemManager::getInstance(); $transaction_manager = ShopTransactionsManager::getInstance(); $currency_manager = ShopCurrenciesManager::getInstance(); $id = null; if (isset($tag_params['id'])) { // get id from tag params $id = fix_id($tag_params['id']); } else { if (isset($_REQUEST['id'])) { // get id from request params $id = fix_id($_REQUEST['id']); } } // if we don't have transaction Id, get out if (is_null($id)) { return; } $currency_id = $transaction_manager->getItemValue('currency', array('id' => $id)); $currency = $currency_manager->getItemValue('currency', array('id' => $currency_id)); // get items from database $items = array(); $raw_items = $manager->getItems($manager->getFieldNames(), array('transaction' => $id)); if (count($raw_items) > 0) { foreach ($raw_items as $item) { $items[$item->item] = array('id' => $item->id, 'price' => $item->price, 'tax' => $item->tax, 'amount' => $item->amount, 'description' => $item->description, 'uid' => '', 'name' => '', 'gallery' => '', 'manufacturer' => '', 'author' => '', 'views' => '', 'weight' => '', 'votes_up' => '', 'votes_down' => '', 'timestamp' => '', 'priority' => '', 'visible' => '', 'deleted' => '', 'total' => ($item->price + $item->price * ($item->tax / 100)) * $item->amount, 'currency' => $currency); } } // get the rest of item details from database $id_list = array_keys($items); $raw_items = $item_manager->getItems($item_manager->getFieldNames(), array('id' => $id_list)); if (count($raw_items) > 0) { foreach ($raw_items as $item) { $id = $item->id; $items[$id]['uid'] = $item->uid; $items[$id]['name'] = $item->name; $items[$id]['gallery'] = $item->gallery; $items[$id]['manufacturer'] = $item->manufacturer; $items[$id]['author'] = $item->author; $items[$id]['views'] = $item->views; $items[$id]['weight'] = $item->weight; $items[$id]['votes_up'] = $item->votes_up; $items[$id]['votes_down'] = $item->votes_down; $items[$id]['timestamp'] = $item->timestamp; $items[$id]['priority'] = $item->priority; $items[$id]['visible'] = $item->visible; $items[$id]['deleted'] = $item->deleted; } } if (count($items) > 0) { $template = $this->_parent->loadTemplate($tag_params, 'transaction_details_item.xml'); foreach ($items as $id => $params) { $template->setLocalParams($params); $template->restoreXML(); $template->parse(); } } }
/** * Charge specified amount with specified token and transaction. */ public function chargeToken() { $transaction_uid = fix_chars($_REQUEST['transaction_uid']); $stripe_token = fix_chars($_REQUEST['stripe_token']); $manager = ShopTransactionsManager::getInstance(); $currency_manager = ShopCurrenciesManager::getInstance(); $transaction = null; // make sure we are working on same transaction for current user if (isset($_SESSION['transaction']) && $_SESSION['transaction']['uid'] == $transaction_uid) { $transaction = $manager->getSingleItem($manager->getFieldNames(), array('uid' => $transaction_uid)); } if (is_object($transaction)) { $currency = $currency_manager->getSingleItem(array('currency'), array('id' => $transaction->currency)); try { // create charge Stripe::setApiKey($this->getPrivateKey()); $charge = Stripe_Charge::create(array('amount' => $transaction->total * 100, 'currency' => $currency->currency, 'card' => $stripe_token, 'description' => null)); } catch (Stripe_CardError $error) { } // update transaction status if (is_object($charge) && $charge->paid) { $shop = shop::getInstance(); $shop->setTransactionToken($transaction_uid, $charge->id); $shop->setTransactionStatus($transaction_uid, TransactionStatus::COMPLETED); } } }