📜 ⬆️ ⬇️

The system of timely replenishment of the mobile account


I think many people had the idea to automate one or another sequence of actions in order to do something really important and ambitious, for example, to go to the shower or eat. This story is about the implementation of one such idea.

CONTENT


SYMBOLS AND ABBREVIATIONS
INTRODUCTION
1. DESCRIPTION OF SERVICES AND SYSTEMS
2. PROJECT TASK
3. DESCRIPTION OF THE PROJECT
3.1. Funds for receiving personal account status
3.2. Means for working with poison
3.3. Twitter Tools
4. WORK AND LOGIC
CONCLUSION
LIST OF USED SOURCES

SYMBOLS AND ABBREVIATIONS


LISA - subscriber’s personal Internet service;
POISON - Yandex.Money;
Drink poison - to receive money on Yandex-wallet;
DB - database.

INTRODUCTION


Like many people, every now and then comes the thought that there are many things in our life that are overly complex and confusing, they require simplification. About this thought came to me when, once again, I received a text from my mobile operator that the balance was approaching zero and, once again, I went to the POISON, went through authorization, clicked to repeat the payment, confirmed, indicated payment password, wait for the execution, out of the poison. Somehow a lot of action, do not you think? It seemed to me that way, and indeed, this is a very tedious process, I need to find a simpler way that would suit me.
')
I would like to note that the process took me so much that I am not sure now - did I save time on all this or was it easier to leave everything as it is? However, I am such a person, I take a great interest in all sorts of interesting activities quickly, so the implementation process itself brought more pleasure than the final result.

1. DESCRIPTION OF SERVICES AND SYSTEMS


LISA is a service that allows subscribers of Motive Company to manage the current state of their personal account and use services via the Internet.

POISON - electronic payment system. Many stores and companies implement support for the Poison, which makes it easy and simple to pay for goods and services. “Motive” is just one of those companies where the possibility of recharge is realized through nuclear weapons.

Twitter is a system that allows users to send short text notes (up to 140 characters) using a web interface, SMS, instant messaging tools, or third-party client programs. A distinctive feature of Twitter is the public availability of posting, which makes it related to blogs.

Back to content →

2. PROJECT TASK


The implementation of the mobile phone balance replenishment system from the Poison Wallet is the main task of the project, the balance replenishment should occur without any predetermined frequency. Mobile account should be replenished only when necessary.

The system should include:
  1. Funds for receiving personal account status;
  2. Means for working with nuclear weapons;
  3. Tools for working with Twitter.

Back to content →

3. DESCRIPTION OF THE PROJECT


The project is located on the Masterhost site, hosting is received free of charge, as part of the student support program. It was necessary to contact technical support to turn on curl and obtain information about the scheduler, since this service was not provided on windows tariffs. It was proposed to add an entry to the crontab on a unix server, to which I agreed. I gave a link to the script and indicated a frequency of 10 minutes, after which I was warned that if the script would create a greater load, it would have to be disabled.

Back to content →

3.1. Funds for receiving personal account status

In the first version, the state of the mobile account had to be received directly through the FOX site, which took quite a long time and did not always give a result, sometimes the duration of the operation reached 10 seconds, which was simply unacceptable.

Figure 3.1 - LISA website.

After a long and merciless communication with the technical support of the Motiv Company, I learned that in the near future the Balance Gadget will be introduced, which is intended to display the current balance of the personal account. In addition, I received information that the service is not planned to have any external interface, which, by the way, I was no longer needed, I was sure that I could get all the information from the gadget.

Figure 3.2 - Information about the "Balance Gadget" and the button for its formation.

Previously, I did not have to work with gadgets for Windows 7 or Vista, after installing / reinstalling the operating system, I immediately disconnected the whole thing, because I didn’t see this as necessary, and on my home computer I’ve got the old Windows XP.

Figure 3.3 - The internal structure of the "Balance Gadget".

In the directory “motiv_balance.gadget \ js” there are files:
I set about studying the balance.js file and immediately came across the desired getInfo () function, in which the request is being generated and sent:
_req = new ActiveXObject("Microsoft.XMLHTTP"); /*don't change this sequence! it invokes non-ordering requests: xz why*/ _req.open("GET", url, true); 
a response is expected, then it is passed to the showResponse () function, which is responsible for processing and displaying the result.

As you have already noticed, accessing the operator’s site is nothing more than a regular GET request, which consists of several parameters:
Here's what it looks like:
  b = System.Gadget.docked ? 'a' : 'b'; a = Math.round(Math.random() * 10000); /*it should be global: no ideas why*/ var url = "https://gadget.motivtelecom.ru/?uid=UID&b="+b+"&a=" + a; 
In response, we receive either information about the current state of the account, for example:
  3 = RUB: 80p.45k. 
where 3 is the version number of the gadget, at the time of writing the article (var VERSION = 3;), or an error message with a detailed description :
  3 = ERR 

Armed with this knowledge, I wrote the simplest function that cURL sends the request.
  $req_lisa = array( CURLOPT_HEADER => 0, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_TIMEOUT => 10, CURLOPT_SSL_VERIFYPEER => 0, CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_URL => LISA_URL ); $ci = curl_init(); curl_setopt_array ($ci, $req_lisa); $result_lisa = curl_exec($ci); 
The time required to execute a query and process the result was reduced from 10 seconds to 0.26 seconds.

Back to content →

3.2. Means for working with nuclear weapons

To work with poison you need:
  1. Examine the documentation;
  2. Register the application (get client_id);
  3. Get a temporary token (less than 1 minute);
  4. Get an authorization token (for a period of 1 year).

Figure 3.4 - Information about the registered application.

The received authorization token is limited to 1 store (Company “Motive”) and the limit “1 thousand rubles per week” with the ability to view information about the current account:
 scope=payment.to-pattern("1122").limit(7,1000) account-info 
Now we have methods at our disposal:To make a payment, we create a payment template:
pattern_id = 1122 & PROPERTY1 = 908 & PROPERTY2 = 9089XXX & sum =% s
and send a request-payment request for nuclear weapons, if everything is correct, then we get a json-string with the status value “success” and the desired request_id, everything else for us, other than “success”, is equivalent to failure and may depend on various factors such such as: technical problems on the side of Yandex, technical problems on the side of the store, or something else.
If everything is in order, then we are sending a process-payment request containing the request_id, thereby confirming the payment:
 $yad = json_decode($result, 1); if ( $yad['status'] == 'success' ) { $pay_fields = sprintf($process_fields, $yad['request_id']); curl_setopt($ci, CURLOPT_URL, YM_PROC_URL); curl_setopt($ci, CURLOPT_POSTFIELDS, $pay_fields); $result_pay = curl_exec($ci); curl_close($ci); return json_decode($result_pay, 1); } 
In case of successful or unsuccessful payment, we also get a json string with the status parameter. If the status parameter has the “success” value, the payment number and the account status information are returned.

Back to content →

3.3. Twitter Tools

A ready library was chosen for work, all that was needed was to register your application on the website for developers and get all the necessary parameters for work:
It seemed to me that it would be very sad and not cool if the same tweets were posted on Twitter, so I sketched a set of phrases that my bot application now operates on:
 function GetRandomTwit() { $music_bands = array ( 'Queen', 'Coldplay', 'Muse', 'Justice', 'Duft Punk', 'Radiohead', 'Adele', 'Beatles', 'Red Hot Chili Peppers', 'Foo Fighters', 'Killers', 'Gorillaz' ); $rand_music_band = $music_bands[rand(0, sizeof($music_bands)-1)]; $twi_mask = array ( '  ,  ,  %1$s #motiv #motivtelecom #wtf', '   : %1$s #motiv', ' ,   %1$s #motiv', '%1$s    #motiv', '#motiv   !    %1$s', '  .,   %3$s. : %1$s #motivtelecom', '%1$s   ,  ? #motiv', ' - ,   %1$s #motiv', '  '.$rand_music_band.'  ...     %1$s #motiv', '!     %1$s #motiv #motivtelecom #whyyyyyy', ', #motiv    , ,     ;)  %2$s .', ' ,  ,    !  ,   %1$s', '     ,  ,     %1$s #motivtelecom', 'YU SO GREEDY? %2$s rub #motiv #motivtelecom', '#motiv    !  %1$s #motivtelecom #yusogreedy', '  ,      ,       !  %2$s .', '   GPRS- : %3$s.   : %1$s #motiv', ',      :( %2$s . #motiv #motivtelecom', ' -,   '.$rand_music_band.',    %2$s . #motiv #'.str_replace(' ','', strtolower($rand_music_band)), '      ..    ! :(    %2$s .', '#motiv     '.$rand_music_band.'  ? , %2$s   .', '  !   %1$s #motivtelecom', ',   ,   - - %)    %1$s #motiv', '   ,   ,   %3$s :)   %2$s .', '  ,     ? , ,    %2$s . #motiv', ' -  ,       . #motiv', 'BREAKING NEWS! Balance: %2$s rub.', '    ,    ! ,    %2$s .' ); return ($twi_mask[rand(0, sizeof($twi_mask)-1)]); } 
The library is extremely simple:
 $twitter = new Twitter(TWT_CON_KEY, TWT_CON_SEC, TWT_ACC_TOK, TWT_ACC_TOK_SEC); $twitter->send(sprintf(GetRandomTwit(), GetSumWithPost($current_bal_value), $current_bal_value, YM_WALLET)); 

Back to content →

4. WORK AND LOGIC


The main idea of ​​the application is to make the payment timely, I do not need the balance to be replenished every day with 50 or 100 rubles, because I can simply not use the phone on that day and it could be implemented at the POISON. It was up to the logic of the work, because all the components already exist and work.

Lowering the extra checks, it all comes down to a clear view:
1. I check the current balance in order to know when to top up, you should always be aware of how much money is in the account now;
2. I determine the difference between the reference value of the balance (100 rubles) and the current one;
2.1. The difference is greater than the specified value (20 rubles):
2.1.1. make payment;
2.1.2. I am writing a new value in the database;
2.1.3. writing on twitter.
2.2. The difference is less than the specified value (20 rubles):
2.2.1. I compare the current value with the last recorded in the database, if the difference is positive, then the balance has increased:
2.2.1.1. I am writing a new value in the database;
2.2.1.2. I am writing to Twitter that someone from outside has filled up the balance.
2.2.2. I compare the current value with the last recorded in the database, if the difference is negative and less than the specified value (-5 rubles):
2.2.2.1. I am writing a new value in the database;
2.2.2.2. I am writing to Twitter that the balance has changed.

There are some reservations that had to be considered:
- Motive does not accept payments of less than 10 rubles - this means that the difference between the current account balance and the reference setpoint must be at least 10 rubles in order to replenish;
- the sum of 20 rubles is taken in order not to tweet about the replenishment of the account;
- the difference of 5 rubles, which was mentioned above, was taken for a reason, there was no limitation before, and the application reported, via Twitter, every 10 minutes that the balance changed by 7 kopecks, or even less - it made me put a certain limiter, at first it was equal to 1 ruble, but even this was not enough, now the limiter is equal to 5 rubles and nothing bothers me anymore;
- if the difference between the current value and the reference leaves more than 100 rubles, then we begin to suspect something is wrong and replenish the balance in parts, first 100 rubles, then the remaining money, on the same principle.
 $pay_bot = (floor(BAL_LEVEL-$current_bal_value)>BAL_PAY_LEVEL_MAX)?BAL_PAY_LEVEL_MAX:(floor(BAL_LEVEL-$current_bal_value)); 
There will be enough time to answer the questions: “Where did the money go? Why such a big minus? Who is to blame? ”, Drink a cup of tea and, finally, turn off the bot.

  $pay_bot = (floor(BAL_LEVEL-$current_bal_value)>BAL_PAY_LEVEL_MAX)?BAL_PAY_LEVEL_MAX:(floor(BAL_LEVEL-$current_bal_value)); if ( $pay_bot >= BAL_PAY_LEVEL ) { $pay_bot += BAL_UP_SUM; $ym_pay_state = YandexMoneyPay($pay_bot); if ( $ym_pay_state['status'] == YM_PAY_STAT ) { $twitter = new Twitter(TWT_CON_KEY, TWT_CON_SEC, TWT_ACC_TOK, TWT_ACC_TOK_SEC); $twitter->send(sprintf($plus_pay, GetSumWithPost($pay_bot), GetSumWithPost($current_bal_value+$pay_bot), GetSumWithPost($ym_pay_state['balance']))); } } else { $pay_user = floor($current_bal_value - $last_bal_value); $bal_diff = $last_bal_value - $current_bal_value; if ( $pay_user > 0 or $bal_diff >= BAL_CHANGE_LEVEL ) { $twitter = new Twitter(TWT_CON_KEY, TWT_CON_SEC, TWT_ACC_TOK, TWT_ACC_TOK_SEC); if ( $pay_user > 0 ) $twitter->send(sprintf($user_pay, GetSumWithPost($pay_user), GetSumWithPost($current_bal_value))); elseif ( $bal_diff >= BAL_CHANGE_LEVEL ) $twitter->send(sprintf(GetRandomTwit(), GetSumWithPost($current_bal_value), $current_bal_value, YM_WALLET)); } } 
I note that the value of BAL_UP_SUM increases any payment. Now the value of BAL_UP_SUM is 1, you see that the above all calculations are applied with rounding down, to go beyond 99 rubles with kopecks, you should add 1 ruble. Also, this value can be increased in order for the application not to bother its constant writing to Twitter.

Back to content →

CONCLUSION


At the time of writing, I ran into some questions that I could not find the answer to.
For a long time I could not understand why I could not make a transfer to a mobile account through nuclear weapons, although in the response to the request-payment-request the value “success” comes. It turned out that the example given in the documentation:
pattern_id = 2904 & phone-prefix = 921 & phone-number = 9538 & sum = 300.00
with the content of the phone-prefix and phone-number fields, it is not necessary for each store, at Motiv these parameters are named PROPERTY1 and PROPERTY2, for MTS or Megaphone these fields may also have other names. Fields for Motive were suggested to me by the moderators of the API Club of the Poison, which is strange. Once I know the values ​​of these fields in the poison, why is it simple, during the payment, they cannot be reassigned from their parameter names to the parameters of the store?

And one more question, it is directly related to the first. When I sent a request with incorrect field names to me, for some reason, the value “success” came in, also the request_id parameter came, for which you need to confirm the payment. Such payments, of course, did not go through, but why did I receive success in status? The developer’s manual clearly states that “success” is a successful implementation.

I would be glad if someone answers these questions.

I would like to thank:The result of the work: http://twitter.com/001011010

Back to content →

LIST OF USED SOURCES


1. Yandex.Money API. Developer's Guide // materials site api.yandex.ru/money/doc/dg/ .
2. Club for developers using the Yandex.Money API // materials on the site clubs.ya.ru/moneyapi/ .
3. Twitter. Site for developers // materials of the site dev.twitter.com .
4. FOX. Gadget balance // materials site lisa.motivtelecom.ru .
5. Materials site en.wikipedia.org .

Source: https://habr.com/ru/post/131962/


All Articles