Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // MethodBook.cpp
- //
- // Description:
- //
- // Handles the requests to the method credit.book in the XMLRPC server.
- //
- // Copyright (c) 2010, Cyclelogic
- //
- #include "MethodBook.h"
- #include <map>
- #include <string>
- #include <utility>
- #include <vector>
- #include <xmlrpc-c/base.hpp>
- #include <xmlrpc-c/client_simple.hpp>
- #include <xmlrpc-c/girerr.hpp>
- #include <xmlrpc-c/registry.hpp>
- #include "Exception.h"
- #include "CCO/Common/ChargeNotification.h"
- #include "CCO/Util/ChargeNotification.h"
- #include "CCO/Util/Exception.h"
- #include "OfferingsManager.h"
- #include "Poco/Any.h"
- #include "Poco/DateTime.h"
- #include "Poco/Format.h"
- #include "Poco/Logger.h"
- #include "Poco/PriorityNotificationQueue.h"
- #include "Poco/Util/AbstractConfiguration.h"
- using namespace CCO::Common;
- using namespace CCO::Util;
- using Poco::Any;
- using Poco::format;
- using Poco::Logger;
- using Poco::PriorityNotificationQueue;
- using Poco::Util::AbstractConfiguration;
- namespace CCO {
- namespace CCO {
- MethodBook::MethodBook(AbstractConfiguration& config, Logger& logger, PriorityNotificationQueue& outgoingQueue, OfferingsManager& offeringsManager):
- _config(config),
- _logger(logger),
- _outgoingQueue(outgoingQueue),
- _offeringsManager(offeringsManager)
- {
- this->_signature = "S:ssiiiss";
- this->_help = "Books the credit at the carrier platform.";
- }
- void MethodBook::execute(xmlrpc_c::paramList const& paramList, xmlrpc_c::value* const retvalP)
- {
- ChargeNotification* charge = new ChargeNotification();
- const std::string username = paramList.getString(0);
- const std::string password = paramList.getString(1);
- const int productId = paramList.getInt(2);
- const int partnerId = paramList.getInt(3);
- const int applicationId = paramList.getInt(4);
- const std::string brand = paramList.getString(5);
- const std::string msisdn = paramList.getString(6);
- const bool isSync = true; // book/confirm operations are always syncronic
- try
- {
- Offering offering = _offeringsManager.getOffering(productId, partnerId, applicationId, brand, isSync);
- Login login = _offeringsManager.getLogin(username, password);
- if (login.partnerId != offering.partnerId)
- {
- throw PermissionException();
- }
- std::string url = _offeringsManager.getUrl(offering.wrapperId);
- charge->setCarrierId(offering.carrierId);
- charge->setMsisdn(msisdn);
- charge->setPartnerId(offering.partnerId);
- charge->setProductId(offering.productId);
- charge->setApplicationId(offering.applicationId);
- charge->setIsSync(isSync);
- charge->setCurrencyId(offering.currencyId);
- charge->setPrice(offering.price);
- charge->setStatus(book(url, charge->getId(), brand, msisdn, productId, partnerId));
- DateTime now;
- charge->setChargedAt(now);
- charge->setRespondedAt(now);
- charge->setCreatedAt(now);
- if (charge->getStatus() == CHARGED)
- {
- // registrarlo en un memcache
- }
- }
- catch (girerr::error& ex)
- {
- // some of the parameters weren't present or weren't in the rigth type.
- throw xmlrpc_c::fault(format("The arguments provided are invalid: %s", ex.what()), xmlrpc_c::fault::CODE_TYPE);
- }
- catch (OfferingNotFoundException& ex)
- {
- _logger.warning(format("[%s] %s.", charge->getId().toString(), ex.message()));
- charge->setStatus(CONFIGURATION_ERROR);
- }
- catch (LoginNotFoundException& ex)
- {
- _logger.warning(format("[%s] %s.", charge->getId().toString(), ex.message()));
- charge->setStatus(CONFIGURATION_ERROR);
- }
- catch (PermissionException& ex)
- {
- _logger.warning(format("[%s] %s.", charge->getId().toString(), ex.message()));
- charge->setStatus(CONFIGURATION_ERROR);
- }
- std::map<std::string, xmlrpc_c::value> result;
- result.insert(std::pair<std::string,xmlrpc_c::value>("transaction", xmlrpc_c::value_string(charge->getId().toString())));
- result.insert(std::pair<std::string,xmlrpc_c::value>("response", xmlrpc_c::value_int(IntFromStatus(charge->getStatus()))));
- *retvalP = xmlrpc_c::value_struct(result);
- if (charge->getStatus() != CHARGED)
- {
- _outgoingQueue.enqueueNotification(charge, charge->getPriority());
- }
- }
- Status MethodBook::book(const std::string url, const UUID& id, const std::string& brand, const std::string& msisdn, const int& productId, const int& partnerId)
- {
- try
- {
- xmlrpc_c::clientSimple client;
- xmlrpc_c::value result;
- client.call(url, "credit.book", "sssii", &result, id.toString().c_str(), brand.c_str(), msisdn.c_str(), productId, partnerId);
- return StatusFromInt(xmlrpc_c::value_int(result));
- }
- catch (girerr::error& ex)
- {
- _logger.critical(format("[%s] Unexpected error from the wrapper: %s", id.toString(), ex.what()));
- return INTERNAL_ERROR;
- }
- catch (xmlrpc_c::fault& ex)
- {
- _logger.critical(format("[%s] Unexpected fault from the wrapper: %s", id.toString(), ex.getDescription()));
- return INTERNAL_ERROR;
- }
- catch (StatusException& ex)
- {
- _logger.critical(format("[%s] %s", id.toString(), ex.message()));
- return INTERNAL_ERROR;
- }
- }
- } } // CCO::CCO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement