This Java library contains classes that allows you to do payments using Yandex.Money public API.
The library uses:
- OkHttp 1.5.4;
- Google GSON 2.2.4.
To be able to use the library you: the first thing you need to do is to register your application and get your unique client id. To do that please follow the steps described on this page (also available in Russian).
All API methods are represented as classes in package com.yandex.money.model.cps
.
Every instance of your application should have an unique id. To get it use API method instance-id
passing your client id. Once obtained instance id can be used in other API methods.
Do NOT request instance id every time you need to call API method. Obtain it once and reuse it.
In order to call API methods you may want to use YandexMoney
class as a context for all of your requests. You may also want to pass you own HTTP client so there are two constructors in YandexMoney
class.
YandexMoney instance can perform a request (call of API method). For example, if you want to get instance id
using YandexMoney instance you can do it like this:
...
final String clientId = "[your_client_id]";
YandexMoney ym = new YandexMoney(clientId);
InstanceId response = ym.execute(new InstanceId.Request(clientId));
// handling the response
...
All requests are performed synchronously so you may want to call these methods in background thread.
There are two API methods you should call when you performing a payment: request-external-payment
and process-external-payment
. Corresponding classes in the library are RequestExternalPayment
and ProcessExternalPayment
.
When you do RequestExternalPayment
, you create payment's context:
...
String patternId = ... // depends on your implementation
Map<String, String> params = ... // depends on your implementation
RequestExternalPayment rep = ym.execute(new RequestExternalPayment.Request.newInstance(instanceId, patternId, params));
String requestId = rep.getRequestId();
...
Payment considered completed when all required information is entered and ProcessExternalPayment
completed successfully.
There are four statuses of ProcessExternalPayment
(see Status
class and API documentation for more details):
Status | Meaning |
---|---|
Success | Payment processed successfully. |
Refused | Payment refused. |
In Progress | Payment in progress. |
Ext Auth Required | External authentication required (i.e. 3D Secure) |
So to process payment you should do something like this:
public void processPayment(String requestId, ProcessExternalPayment.Request pepRequest)
throws Exception {
ProcessExternalPayment pep = ym.execute(pepRequest);
switch (pep.getStatus()) {
case SUCCESS:
// payment succeeded
break;
case REFUSED:
// payment refused
break;
case IN_PROGRESS:
processPayment(requestId, pepRequest);
break;
case EXT_AUTH_REQUIRED:
// show web page for 3D Secure authentication
break;
}
}