Advertisement
Guest User

Untitled

a guest
May 13th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.07 KB | None | 0 0
  1. //
  2. // MethodBook.cpp
  3. //
  4. // Description:
  5. //
  6. // Handles the requests to the method credit.book in the XMLRPC server.
  7. //
  8. // Copyright (c) 2010, Cyclelogic
  9. //
  10.  
  11.  
  12. #include "MethodBook.h"
  13. #include <map>
  14. #include <string>
  15. #include <utility>
  16. #include <vector>
  17. #include <xmlrpc-c/base.hpp>
  18. #include <xmlrpc-c/client_simple.hpp>
  19. #include <xmlrpc-c/girerr.hpp>
  20. #include <xmlrpc-c/registry.hpp>
  21. #include "Exception.h"
  22. #include "CCO/Common/ChargeNotification.h"
  23. #include "CCO/Util/ChargeNotification.h"
  24. #include "CCO/Util/Exception.h"
  25. #include "OfferingsManager.h"
  26. #include "Poco/Any.h"
  27. #include "Poco/DateTime.h"
  28. #include "Poco/Format.h"
  29. #include "Poco/Logger.h"
  30. #include "Poco/PriorityNotificationQueue.h"
  31. #include "Poco/Util/AbstractConfiguration.h"
  32.  
  33.  
  34. using namespace CCO::Common;
  35. using namespace CCO::Util;
  36. using Poco::Any;
  37. using Poco::format;
  38. using Poco::Logger;
  39. using Poco::PriorityNotificationQueue;
  40. using Poco::Util::AbstractConfiguration;
  41.  
  42.  
  43. namespace CCO {
  44. namespace CCO {
  45.  
  46.  
  47. MethodBook::MethodBook(AbstractConfiguration& config, Logger& logger, PriorityNotificationQueue& outgoingQueue, OfferingsManager& offeringsManager):
  48.   _config(config),
  49.   _logger(logger),
  50.   _outgoingQueue(outgoingQueue),
  51.   _offeringsManager(offeringsManager)
  52. {
  53.   this->_signature = "S:ssiiiss";
  54.   this->_help = "Books the credit at the carrier platform.";
  55. }
  56.  
  57.  
  58. void MethodBook::execute(xmlrpc_c::paramList const& paramList, xmlrpc_c::value* const retvalP)
  59. {
  60.   ChargeNotification* charge = new ChargeNotification();
  61.  
  62.   const std::string username = paramList.getString(0);
  63.   const std::string password = paramList.getString(1);
  64.   const int productId = paramList.getInt(2);
  65.   const int partnerId = paramList.getInt(3);
  66.   const int applicationId = paramList.getInt(4);
  67.   const std::string brand = paramList.getString(5);
  68.   const std::string msisdn = paramList.getString(6);
  69.   const bool isSync = true; // book/confirm operations are always syncronic
  70.  
  71.   try
  72.   {
  73.     Offering offering = _offeringsManager.getOffering(productId, partnerId, applicationId, brand, isSync);
  74.     Login login = _offeringsManager.getLogin(username, password);
  75.  
  76.     if (login.partnerId != offering.partnerId)
  77.     {
  78.       throw PermissionException();
  79.     }
  80.  
  81.     std::string url = _offeringsManager.getUrl(offering.wrapperId);
  82.  
  83.     charge->setCarrierId(offering.carrierId);
  84.     charge->setMsisdn(msisdn);
  85.     charge->setPartnerId(offering.partnerId);
  86.     charge->setProductId(offering.productId);
  87.     charge->setApplicationId(offering.applicationId);
  88.     charge->setIsSync(isSync);
  89.     charge->setCurrencyId(offering.currencyId);
  90.     charge->setPrice(offering.price);
  91.     charge->setStatus(book(url, charge->getId(), brand, msisdn, productId, partnerId));
  92.     DateTime now;
  93.     charge->setChargedAt(now);
  94.     charge->setRespondedAt(now);
  95.     charge->setCreatedAt(now);
  96.  
  97.  
  98.     if (charge->getStatus() == CHARGED)
  99.     {
  100.       // registrarlo en un memcache
  101.     }
  102.   }
  103.   catch (girerr::error& ex)
  104.   {
  105.     // some of the parameters weren't present or weren't in the rigth type.
  106.     throw xmlrpc_c::fault(format("The arguments provided are invalid: %s", ex.what()), xmlrpc_c::fault::CODE_TYPE);
  107.   }
  108.   catch (OfferingNotFoundException& ex)
  109.   {
  110.     _logger.warning(format("[%s] %s.", charge->getId().toString(), ex.message()));
  111.     charge->setStatus(CONFIGURATION_ERROR);
  112.   }
  113.   catch (LoginNotFoundException& ex)
  114.   {
  115.     _logger.warning(format("[%s] %s.", charge->getId().toString(), ex.message()));
  116.     charge->setStatus(CONFIGURATION_ERROR);
  117.   }
  118.   catch (PermissionException& ex)
  119.   {
  120.     _logger.warning(format("[%s] %s.", charge->getId().toString(), ex.message()));
  121.     charge->setStatus(CONFIGURATION_ERROR);
  122.   }
  123.  
  124.   std::map<std::string, xmlrpc_c::value> result;
  125.   result.insert(std::pair<std::string,xmlrpc_c::value>("transaction", xmlrpc_c::value_string(charge->getId().toString())));
  126.   result.insert(std::pair<std::string,xmlrpc_c::value>("response", xmlrpc_c::value_int(IntFromStatus(charge->getStatus()))));
  127.   *retvalP = xmlrpc_c::value_struct(result);
  128.   if (charge->getStatus() != CHARGED)
  129.   {
  130.     _outgoingQueue.enqueueNotification(charge, charge->getPriority());
  131.   }
  132. }
  133.  
  134.  
  135. Status MethodBook::book(const std::string url, const UUID& id, const std::string& brand, const std::string& msisdn, const int& productId, const int& partnerId)
  136. {
  137.   try
  138.   {
  139.     xmlrpc_c::clientSimple client;
  140.     xmlrpc_c::value result;
  141.  
  142.     client.call(url, "credit.book", "sssii", &result, id.toString().c_str(), brand.c_str(), msisdn.c_str(), productId, partnerId);
  143.  
  144.     return StatusFromInt(xmlrpc_c::value_int(result));
  145.   }
  146.   catch (girerr::error& ex)
  147.   {
  148.     _logger.critical(format("[%s] Unexpected error from the wrapper: %s", id.toString(), ex.what()));
  149.     return INTERNAL_ERROR;
  150.   }
  151.   catch (xmlrpc_c::fault& ex)
  152.   {
  153.     _logger.critical(format("[%s] Unexpected fault from the wrapper: %s", id.toString(), ex.getDescription()));
  154.     return INTERNAL_ERROR;
  155.   }
  156.   catch (StatusException& ex)
  157.   {
  158.     _logger.critical(format("[%s] %s", id.toString(), ex.message()));
  159.     return INTERNAL_ERROR;
  160.   }
  161. }
  162.  
  163.  
  164. } } // CCO::CCO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement