/** * Public function that creates a single instance */ public static function getInstance() { if (!isset(self::$_instance)) { self::$_instance = new self(); } return self::$_instance; }
/** * Show page for printing all attached cards with selected text. */ private function print_card() { $id = fix_id($_REQUEST['transaction']); $manager = ShopTransactionsManager::getInstance(); $item_manager = ShopItemManager::getInstance(); $transaction_item_manager = ShopTransactionItemsManager::getInstance(); // get transaction with specified id $transaction = $manager->getSingleItem(array('id'), array('id' => $id)); // ensure transaction is a valid one if (!is_object($transaction)) { return; } // get items associated with transaction $transaction_items = $transaction_item_manager->getItems(array('item', 'description'), array('transaction' => $transaction->id)); if (count($transaction_items) == 0) { return; } $id_list = array(); $description_list = array(); foreach ($transaction_items as $item) { $id_list[] = $item->item; $description_list[$item->item] = $item->description; } // get unique id and gallery $shop_items = $item_manager->getItems(array('id', 'uid', 'gallery'), array('id' => $id_list)); if (count($shop_items) == 0) { return; } // prepare final list and only include items that are actually known cards $items = array(); foreach ($shop_items as $item) { if (!array_key_exists($item->uid, $this->text_position)) { continue; } $position = $this->text_position[$item->uid]; $description = unserialize($description_list[$item->id]); $data = array('text' => $description['text'], 'top' => $position[0] . '%', 'left' => $position[1] . '%', 'bottom' => $position[2] . '%', 'right' => $position[3] . '%', 'image' => gallery::getGroupImageById($item->gallery)); $items[] = $data; } // prepare template $template = new TemplateHandler('print_card.xml', $this->path . 'templates/'); if (count($items) > 0) { foreach ($items as $item) { $template->setLocalParams($item); $template->restoreXML(); $template->parse(); } } }
/** * Handle updating transaction status through AJAX request */ public function json_UpdateTransactionStatus() { $manager = ShopTransactionsManager::getInstance(); $id = fix_id($_REQUEST['id']); $status = fix_id($_REQUEST['status']); $result = false; $transaction = null; if ($_SESSION['logged']) { // get transaction $transaction = $manager->getSingleItem(array('id'), array('id' => $id)); // update status if (is_object($transaction)) { $manager->updateData(array('status' => $status), array('id' => $id)); $result = true; } } print json_encode($result); }
/** * Update buyer information for specified transaction. This function is * called by the payment methods that provide buyer information. Return * value denotes whether information update is successful and if method * should complete the billing process. * * @param string $transaction_uid * @param array $buyer_data * @return boolean */ public function updateBuyerInformation($transaction_uid, $buyer_data) { $result = false; $transaction_manager = ShopTransactionsManager::getInstance(); $buyer_manager = ShopBuyersManager::getInstance(); // make sure buyer is marked as guest if password is not specified if (!isset($buyer_data['password'])) { $buyer_data['guest'] = 1; } // get transaction from database $transaction = $transaction_manager->getSingleItem(array('id', 'buyer'), array('uid' => $transaction_uid)); // try to get buyer from the system based on uid if (isset($buyer_data['uid'])) { $buyer = $buyer_manager->getSingleItem($buyer_manager->getFieldNames(), array('uid' => $buyer_data['uid'])); } // update buyer information if (is_object($transaction)) { // get buyer id if (is_object($buyer)) { $buyer_id = $buyer->id; // update buyer information $buyer_manager->updateData($buyer_data, array('id' => $buyer->id)); } else { // create new buyer $buyer_manager->insertData($buyer_data); $buyer_id = $buyer_manager->getInsertedID(); } // update transaction buyer $transaction_manager->updateData(array('buyer' => $buyer_id), array('id' => $transaction->id)); $result = true; } else { trigger_error("No transaction with specified id: {$transaction_uid}"); } return $result; }
/** * 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); } } }
/** * Handle IPN. */ private function handleIPN() { if (!PayPal_Helper::validate_notification()) { trigger_error('PayPal: Invalid notification received. ' . json_encode($_POST), E_USER_WARNING); return; } // get objects $transaction_manager = ShopTransactionsManager::getInstance(); // get data $handled = false; $type = escape_chars($_POST['txn_type']); $amount = escape_chars($_POST['amount']); // handle different notification types switch ($type) { case 'recurring_payment': case 'recurring_payment_expired': case 'recurring_payment_failed': case 'recurring_payment_profile_created': case 'recurring_payment_profile_cancel': case 'recurring_payment_skipped': case 'recurring_payment_suspended': case 'recurring_payment_suspended_due_to_max_failed_payment': $profile_id = escape_chars($_REQUEST['recurring_payment_id']); $transaction = $transaction_manager->getSingleItem($transaction_manager->getFieldNames(), array('token' => $profile_id)); if (is_object($transaction)) { $handled = $this->handleRecurringIPN($transaction, $type, $amount); } else { trigger_error("PayPal: Unable to handle IPN, unknown transaction {$profile_id}.", E_USER_WARNING); } break; } // record unhandled notifications if (!$handled) { trigger_error("PayPal: Unhandled notification '{$type}'.", E_USER_NOTICE); } }