📜 ⬆️ ⬇️

Java library for working with the Yandex.Money API

Yandex.Money API We continue the topic of working with the Yandex.Money API. If you have a website / portal / application written in Java, then I want to show you some potentially useful features. Although even if you do not have an application, you can quickly write something useful for yourself - 4fun, so to speak. Under the cat, you will see how easy and fast is OAuth authentication, how to make requests to the Yandex.Money API, and how to pay for a mobile phone using the Java SDK.

As a bonus, I will tell you about simple and useful ways to work without using an API (for example, creating a one-time payment link to transfer money to someone else's account) and ready-made tools for donanes and crowdfunding.


Java Yandex.Money API SDK


I have written a library to simplify working with the Yandex.Money API. She is able to pass OAuth-authorization in the API, call the main functions (request for account information, history of operations and detailed information on them, money transfer to other users, payment to stores). The SDK uses the Gson libraries to work with json and the Apache HTTP client version 4, but I’ll still mention them. The library was developed and launched in Java 6.
')
As examples, I will consider the possibility of authorization and payment for the mobile communication operator Megafon, and you can collect the rest yourself or with the help of maven from the sources on github 'e, save yourself and consider it in more detail. If you understand and something will be unclear, I recommend to look in the manual on the API or look for the answer to the question in the club .

Getting a token

First of all, let's take a look at OAuth authentication. It was invented by Blaine Cook during the development of Twitter. Well, that is not David Blaine, otherwise it would be a special street magic spec. So, this authorization method was created to use any service in your applications, but without entering and storing the login and password for this service. The application sends the user a request for rights to the service, the user logs in there, approves the use of the service by our application, and then the service issues an access token for its functions. Everything is simple, but for beginner developers such authorization usually becomes an obstacle. With the library is a matter of several lines:

YandexMoney ym = new YandexMoneyImpl(Consts.CLIENT_ID); //       //      Collection<Permission> scope = new LinkedList<Permission>(); scope.add(new AccountInfo()); scope.add(new OperationHistory()); codeReqUri = ym.authorizeUri(scope, Consts.REDIRECT_URI); response.sendRedirect(codeReqUri); 


When creating a ym object, it is passed an application identifier (it is usually written in application constants). Then we put down the list of rights for the scope and make a call to the received URI. Next on the redirect page, we exchange the time code for a permanent access token:

  String code = request.getParameter("code"); ReceiveOAuthTokenResponse resp = ym.receiveOAuthToken(code, Consts.REDIRECT_URI); if (resp.isSuccess()) { out.println(": " + resp.getAccessToken()); } 


Mobile payment

I will show you how to pay for mobile communication operator Megaphone. Pre-user with OAuth authorization, you must specify the required rights to pay to the store. Then in the same way we create an object and call the method, passing it the user token:

  YandexMoney ym = new YandexMoneyImpl(Consts.CLIENT_ID); try { Map<String, String> map = new HashMap<String, String>(); map.put("PROPERTY1", "921"); map.put("PROPERTY2", "302"); map.put("sum", "2.50"); YandexMoneyImpl ym = new YandexMoneyImpl(Consts.CLIENT_ID); String token = (String) session.getAttribute("token"); try { RequestPaymentResponse resp = ym.requestPaymentShop(token, "337", map); if (resp.isSuccess()) { out.println(" : " + resp.getPaymentId()); //       //(processPaymentByWallet  processPaymentByCard) ProcessPaymentResponse paymentResp = ym.processPaymentByWallet(token, resp.getPaymentId()); ... } } catch (Exception e) { out.println("   : " + e.getMessage()); } 


When requesting a payment, we transfer the token of the paying user, the payment template ( pattern_id ) and the Map parameters defined for this store. In this way, you can organize, for example, periodic top-up or something else useful. Approximately the same is with other challenges. A more complete example with the big four operators can be viewed on github 'e in the file web / mobile / index.jsp , as well as other calls.

Used libraries


The SDK uses Gson libraries to work with json and Apache HTTP client version 4.

The apache HTTP client is needed to send requests to the Yandex.Money server and receive responses. It also guarantees the verification of the certificate of the remote host. By default, the client runs in BrowserCompatHostnameVerifier mode, which allows you to check whether the host of the domain to which we are accessing is trusted. The root certificate of the Yandex.Money certificate chain (GTE CyberTrust Global Root) is built into the java platform, so there is no need to prescribe trusted certificates separately.

I used the Gson library to parse json responses. It has several tasty features that can be useful to you. It allows you to easily and quickly create equivalent java objects from json strings. And you can use the nesting of objects into each other. It is also conveniently done that the fields described in the java-style can be loaded into the json-style fields and vice versa. For example, the sampleFieldNameInJava field can be converted to json's sample_field_name_in_java or SampleFieldNameInJava , depending on the chosen name policy when creating the object. It loads everything through reflection. Easy to use and convenient library.

Bonus


For those who do not touch the programming of the soul, I will show some simple and useful tools without using the API.

Direct payment link

If, for the purposes of your project, the use of the API is unnecessary, then you can make a direct payment link - after clicking on it, the user will be on a page with a pre-filled money transfer form. Many people ask about it in the Yandex.Money club on a toy, maybe this feature will be useful to you too. But keep in mind that this is more like a live hacking than a documented feature and should be used at your own risk. The example shows (see the web / simple / index.jsp file ) how to form such a link: there are 5 main parameters responsible for the title, comments, and sum. The rest of the service, you can not delve into them. As a result, after clicking on the link, we find ourselves on the Yandex.Money website and, in order to make the transfer, you just need to enter the payment password.

You can also create a translation request that you can still edit. For this you can simply link to
 https://money.yandex.ru/direct-payment.xml 
add the desired parameters from the specified 5 (sum, receiver, etc.). For example,
 https://money.yandex.ru/direct-payment.xml?sum=10&receiver=mdv00&destination=my%20comment 
As a recipient, you can specify both the account number and the account on Yandex.

Crowdfunding and donations

To collect donations, there are special tools that are not yet known by all. They can be found in the Services tab of the Yandex.Money main page in the Collection of Money section. In addition, there is a lot more useful, but we will focus on our topic.

That's all for today, thank you all for your attention. I hope the information will be useful to you. Questions, constructive suggestions and pull requests, as always, are welcome.

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


All Articles