Advertisement
Guest User

PayStream

a guest
Jun 26th, 2014
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.52 KB | None | 0 0
  1. package com.l2jfrozen.gameserver.powerpak.Servers.Services;
  2.  
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import java.net.URLDecoder;
  6. import java.sql.Connection;
  7. import java.sql.PreparedStatement;
  8. import java.sql.Timestamp;
  9. import java.util.Map;
  10. import java.util.StringTokenizer;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13.  
  14. import javolution.util.FastMap;
  15.  
  16. import com.l2jfrozen.Config;
  17. import com.l2jfrozen.L2Properties;
  18. import com.l2jfrozen.gameserver.datatables.sql.ItemTable;
  19. import com.l2jfrozen.gameserver.model.L2World;
  20. import com.l2jfrozen.gameserver.model.actor.instance.L2PcInstance;
  21. import com.l2jfrozen.gameserver.powerpak.L2Utils;
  22. import com.l2jfrozen.gameserver.util.sql.SQLQuery;
  23. import com.l2jfrozen.gameserver.util.sql.SQLQueue;
  24. import com.sun.net.httpserver.HttpExchange;
  25. import com.sun.net.httpserver.HttpHandler;
  26.  
  27. public class PayStream implements HttpHandler
  28. {
  29.     protected static final Logger _log = Logger.getLogger(PayStream.class.getName());
  30.     // An indication that all options are read, and the handler can be created
  31.  
  32.     // Security Key - defined in the study Lisnoe paystream
  33.     private static String _KEY;
  34.     // Prefix SMS
  35.     private static String _SMSPrefix;
  36.     // ID object, given by a reward
  37.     protected static int _item;
  38.     // The list of numbers and quantities awards for a number. Format: count [; number: the number ...]
  39.     private static Map<Integer, Integer> _bonuses = new FastMap<Integer, Integer>();
  40.  
  41.     private static String _MessageOK;
  42.     private static String _MessageFail;
  43.     protected static String _UserMessage;
  44.  
  45.     public PayStream() throws Exception
  46.     {
  47.         // Parse the config, which lies in the config / powerpak / webservices / paystream.properties
  48.         L2Properties p = new L2Properties("./config/powerpak/webservices/paystream.properties");
  49.         _KEY = p.getProperty("SecurityKey", "");
  50.         _item = Integer.parseInt(p.getProperty("RewardItem", "0"));
  51.         _MessageFail = L2Utils.loadMessage(p.getProperty("MessageFail", "Character %s does not exists"));
  52.         _MessageOK = L2Utils.loadMessage(p.getProperty("MessageOK", "%d CoL added for %s"));
  53.         _SMSPrefix = p.getProperty("SMSPrefix", "");
  54.         _UserMessage = L2Utils.loadMessage(p.getProperty("MessageForUser", ""));
  55.        
  56.         if(ItemTable.getInstance().getTemplate(_item) == null)
  57.             throw new Exception("Reward item (" + _item + ") does not exits");
  58.        
  59.         StringTokenizer st = new StringTokenizer(p.getProperty("AmountForNumbers", ""), ";");
  60.         while(st.hasMoreTokens())
  61.         {
  62.             String token = st.nextToken();
  63.             try
  64.             {
  65.                 int iPos = token.indexOf(":");
  66.                 if(iPos != 0)
  67.                 {
  68.                     int number = Integer.parseInt(token.substring(0, iPos));
  69.                     int reward = Integer.parseInt(token.substring(iPos + 1));
  70.                     _bonuses.put(number, reward);
  71.                 }
  72.             }
  73.             catch(NumberFormatException e)
  74.             {
  75.                 if(Config.ENABLE_ALL_EXCEPTIONS)
  76.                     e.printStackTrace();
  77.  
  78.                 continue;
  79.             }
  80.         }
  81.     }
  82.  
  83.     private class PaymentUpdate implements SQLQuery
  84.     {
  85.  
  86.         private String _charName;
  87.         private L2PcInstance _char;
  88.         private int _count;
  89.         private Map<String, String> _params;
  90.  
  91.         public PaymentUpdate(String charName, int count, Map<String, String> params, L2PcInstance player)
  92.         {
  93.             _charName = charName;
  94.             _count = count;
  95.             _params = params;
  96.             _char = player;
  97.         }
  98.  
  99.         @Override
  100.         public void execute(Connection con)
  101.         {
  102.             try
  103.             {
  104.                 // Stores the result in the database, at the same time check to see whether we re pulling peystrim.
  105.                 PreparedStatement stm = con.prepareStatement("insert into paystream select ?,?,?,?,?,?,? from " + " characters where not exists (select * from  paystream where msgid=?) limit 1");
  106.                 stm.setString(1, _params.get("smsid"));
  107.                 stm.setString(8, _params.get("smsid"));
  108.                 stm.setTimestamp(2, Timestamp.valueOf(_params.get("date")));
  109.                 stm.setString(3, _params.get("user_id"));
  110.                 stm.setString(4, _params.get("num"));
  111.                 stm.setString(5, _charName);
  112.                 stm.setFloat(6, Float.parseFloat(_params.get("cost")));
  113.                 stm.setString(7, _params.get("currency"));
  114.                 boolean isOk = stm.executeUpdate() > 0; // Yes, I must add a user to aytemov.
  115.                 if(isOk && _char != null)
  116.                 {
  117.                     _char.addItem("donate", _item, _count, null, true);
  118.                     if(_char.isOnline() != 0 && _UserMessage.length() > 0)
  119.                         _char.sendMessage(_UserMessage);
  120.  
  121.                     _char.store();
  122.                 }
  123.                 stm.close();
  124.  
  125.             }
  126.             catch(Exception e)
  127.             {
  128.                 if(Config.ENABLE_ALL_EXCEPTIONS)
  129.                     e.printStackTrace();
  130.  
  131.                 _log.log(Level.WARNING, "WebServer: Paystream can't update data : " + e);
  132.             }
  133.  
  134.         }
  135.  
  136.     }
  137.  
  138.     @Override
  139.     public void handle(HttpExchange params) throws IOException
  140.     {
  141.         /* !! WARNING!
  142.         * Paystream supports prefixes beginning with a +
  143.         * HttpServer already decodes from UrlEncode getQuery (), however,
  144.         * It does not decode + as space. The result is a "curve" line
  145.         + + Perfiks imya_chara
  146.         * This is not true, because Line dodzhna be + prefix imya_chara
  147.         *
  148.         */
  149.         if(params.getRequestMethod().equalsIgnoreCase("GET"))
  150.         {
  151.             FastMap<String, String> query = new FastMap<String, String>();
  152.             StringBuffer response = new StringBuffer();
  153.             // Parse the parameters passed in the GET request
  154.             StringTokenizer st = new StringTokenizer(params.getRequestURI().getQuery(), "&");
  155.             while(st.hasMoreTokens())
  156.             {
  157.                 String token = st.nextToken();
  158.                 int iPos = token.indexOf("=");
  159.                 if(iPos != -1)
  160.                 {
  161.                     String param = token.substring(0, iPos).toLowerCase();
  162.                     String value = token.substring(iPos + 1);
  163.  
  164.                     // That is the replacement of which is written above..
  165.                     if(value != null && value.length() > 0)
  166.                     {
  167.                         if(value.charAt(0) == '+')
  168.                             value = "+" + URLDecoder.decode(value.substring(1), "UTF-8");
  169.                         else
  170.                             value = URLDecoder.decode(value, "UTF-8");
  171.                     }
  172.                     query.put(param, value);
  173.                 }
  174.             }
  175.  
  176.             // Check whether there is a secret key and our Do it as well as other required parameters.
  177.             if(query.get("skey") != null && query.get("skey").equals(_KEY) && query.get("num") != null)
  178.             {
  179.                 String SMSText = query.get("msg"); // Take the text of the SMS
  180.                 if(SMSText != null)
  181.                 {
  182.                     int iPos = SMSText.indexOf(" ");
  183.                     if(iPos != -1)
  184.                     {
  185.                         String prefix = SMSText.substring(0, iPos).trim();
  186.                         String charName = SMSText.substring(iPos + 1).trim();
  187.                         boolean prefixOk = _SMSPrefix.length() > 0 ? prefix.equals(_SMSPrefix) : true;
  188.                         if(prefixOk)
  189.                         { // Checked for a match prefix (just in case)
  190.                             try
  191.                             {
  192.                                 Integer amount = _bonuses.get(Integer.parseInt(query.get("num")));
  193.                                 if(amount == null) // Indicate whether our number is on the list?
  194.                                     amount = 1; // No, the issue one aytemov
  195.                                 response.append("status: reply\n\n"); // Header that the request should be returned to PayStream
  196.                                 L2PcInstance pc = L2World.getInstance().getPlayer(charName);
  197.                                 if(pc == null)
  198.                                     pc = L2Utils.loadPlayer(charName);
  199.                                 SQLQueue.getInstance().add(new PaymentUpdate(charName, amount, query, pc));
  200.                                 if(pc != null)
  201.                                     response.append(String.format(_MessageOK, amount, charName));
  202.                                 else
  203.                                     response.append(String.format(_MessageFail, charName));
  204.                                 _log.info("WebServer: Paystream accepted payment from " + query.get("user_id") + " for character " + charName);
  205.                             }
  206.                             catch(NumberFormatException e)
  207.                             {
  208.                                 e.printStackTrace();
  209.                             }
  210.                         }
  211.                     }
  212.                 }
  213.             }
  214.             params.sendResponseHeaders(200, response.length());
  215.             OutputStream os = params.getResponseBody();
  216.             os.write(response.toString().getBytes());
  217.             os.close();
  218.         }
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement