Skip to main content

6.0.0

With RuStore you can integrate payments in your mobile app.

tip

If you are in doubt read the instruction in the usage scenarios.

Implementation example

Look at the example app to learn how to integrate the SDK.

Prerequisites

  • The user and the app are not banned in RuStore.
  • In-app purchases for the app are enabled in RuStore Console.
  • Defold 1.6.2 and later.
caution

The service has some restrictions to work outside of Russia.

Connecting to project

Connecting to project

  1. Copy the plugin and the sample app projects from the official RuStore repository on GitFlic.
  2. Copy the billing_example/extension_rustore_billing and billing_example/extension_rustore_core folders in the root of your project.

RuStore SDK uses deeplink to handle third-party payment applications. This makes it easier to pay with third-party apps and return to your app.

To configure deeplinks functionality in your app, define deeplinkScheme in your AndroidManifest file and redefine the onNewIntent method of your activity.

AndroidManifest.xml
<activity>
<!-- RUSTORE BILLING INTENT FILTER -->
<activity android:name="ru.rustore.defold.billing.RuStoreIntentFilterActivity" android:exported="true" android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Set your appscheme -->
<data android:scheme="yourappscheme" />
</intent-filter>
</activity>

Replace yourappscheme in the example above with the name of your scheme. For example, ru.package.name.rustore.scheme.

info

The scheme defined in the AndroidManifest file must match the scheme you specify in the create method of the RuStore SDK billing.

App example contains a modified manifest in the billing_example/extension_rustore_billing/manifests/android/AndroidManifest.xml file

Initialization

Initialize the library before calling its methods.

For initialization, call the init() method.
Calling init
local APPLICATION_ID = "123456"
local DEEPLINK_SCHEME = "yourappscheme"
local DEBUG_LOGS = true

rustorebilling.init(APPLICATION_ID, DEEPLINK_SCHEME, DEBUG_LOGS)
  • 123456 — application code from the RuStore console (example: https://console.rustore.ru/apps/123456).

    To get the app ID, copy digits from the URL address of the app page in the RuStore Console that appear after apps/.

  • yourappscheme — deeplink scheme that i sused to return to your app after paying with a third party payment app (for example, SberPay or SBP). SDK generates its own host to this scheme.
  • debugLogs — flag that enables/disables logging. Set it to true, if you want events to be logged. Otherwise, set it to false.
note
Tye deeplink scheme that is passed in yourappscheme must match the scheme specified in AndroidManifest.xml (see Deeplink processing).

Before you start

Payment appRuStore Server RuStore_Billing_ClientYour serverYour appUserPayment appRuStore Server RuStore_Billing_ClientYour serverYour appUserPayments availability check[Optional]Server validation[Mandatory]Purchasing consumable product[Mandatory]Deeplink processing for paying with SBP, SberPay, etc.Product purchaseStarts your app checkPurchasesAvailabilityResult getProductsProducts list of your appDisplaying list of available purchasesPurchasing product purchaseProductRequest payment methodMaking paymentPayment methodPayment resultPayment infoServer validation (public API)Reliable purchase informationDeliver product to the userValidation result confirmPurchaseConsumption result purchaseProductRequest payment methodSpecified SBP/SberPay/T-PayStart payment processPayment scenarioPurchase paymentReturn to app OnNewIntentPayment resultDisplaying payment screen with the result

Payments availability check

  • The current version of RuStore is installed on the user's device.
  • RuStore app supports payments.
  • User is authorized in RuStore.
  • The user and the app are not banned in RuStore.
  • In-app purchases for the app are enabled in RuStore Console.

You must subscribe to events once before using this method:

  • rustore_check_purchases_available_success;
  • rustore_check_purchases_available_failure.
Subscription to events
function init(self)
# rustorebilling initialization

rustorecore.connect("rustore_check_purchases_available_success", _check_purchases_available_success)
rustorecore.connect("rustore_check_purchases_available_failure", _check_purchases_available_failure)
end

function _check_purchases_availability_success(self, channel, value)
local data = json.decode(value)
end

function _check_purchases_availability_failure(self, channel, value)
local data = json.decode(value)
end
Calling check_purchases_availability
rustorebilling.check_purchases_availability()

The rustore_check_purchases_availability_success callback returns a JSON string with the service availability information (see below).

  • isAvailable — compliance with the payments availability conditions (true/false).
  • cause — error information.

The error structure is described in Error Handling.

The rustore_check_purchases_availability_failure callback returns a JSON string with the error information. The error structure is described in Error Handling.

Getting started with SDK

Retrieving products list

You verified that payments are available and the users are able to make purchases. Now you can request products list. Use the get_products() method to request the information about products added to your app in RuStore Console.

You must subscribe to events once before using this method:

  • rustore_on_get_products_success;
  • rustore_on_get_products_failure.
Subscription to events
function init(self)
# rustorebilling initialization

rustorecore.connect("rustore_on_get_products_success", _on_get_products_success)
rustorecore.connect("rustore_on_get_products_failure", _on_get_products_failure)
end

function _on_get_products_success(self, channel, value)
local data = json.decode(value)
end

func _on_get_products_failure(self, channel, value)
local data = json.decode(value)
end
Calling get_products
local PRODUCT_IDS = {
"non_con2",
"non_con1",
"con2",
"con1",
"sub2",
"sub1"}

rustorebilling.get_products(PRODUCT_IDS)

PRODUCT_IDS — the list of product IDs. Must not exceed 100 entries.

To specify the product IDs required for the method:

  1. Open RuStore Console.
  2. Click the "Applications" tab.
  3. Select the necessary app.
  4. In the left side menu select "Monetization".
  5. Select product type: "Subscriptions" or "In-App purchases".
  6. Copy the IDs of the required products. These are product ids.

The rustore_on_get_products_success callback returns a JSON string with the products information (see below).

  • currency — ISO 4217 currency code.
  • description — descriptions in language.
  • imageUrl — image URL.
  • language — language specified with BCP 47 code.
  • price — price in minimum currency units.
  • priceLable — formatted product price, including currency symbol in language.
  • productId — product ID assigned to product in RuStore Console (mandatory).
  • productStatus — product type.
  • productType — product status.
  • promoImageUrl — promo image URL.
  • title — product name in language.
  • subscription — subscription description, returns only for subscription products.

Available subscription fields (see below).

  • subscriptionPeriod — subscription period.
  • freeTrialPeriod — subscription trial period.
  • gracePeriod — subscription grace period.
  • introductoryPrice — formated introductory price with the currency symbol in the product:language language.
  • introductoryPriceAmount — introductory price in minimum currency units.
  • introductoryPricePeriod — introductory price invoice period.

Available fields of the period object (see below).

  • years — amount of years.
  • months — amount of months.
  • days — amount of days.

The rustore_on_get_products_failure callback returns a JSON string with the error information. The error structure is described in Error Handling.

Product purchase

To purchase product, use the purchase_product() method.

You must subscribe to events once before using this method:

  • rustore_on_purchase_product_success;
  • rustore_on_purchase_product_failure.
Subscription to events
function init(self)
# rustorebilling initialization

rustorecore.connect("rustore_on_purchase_product_success", _on_purchase_product_success)
rustorecore.connect("rustore_on_purchase_product_failure", _on_purchase_product_failure)
end

function _on_purchase_product_success(self, channel, value)
local data = json.decode(value)
end

function _on_purchase_product_failure(self, channel, value)
local data = json.decode(value)
end
Calling purchase_product
local PRODUCT_ID = "example_id"
local PARAMS = "{" ..
"\"orderId\":\"example\"," ..
"\"quantity\":1," ..
"\"payload\":\"example\"" ..
"}"

rustorebilling.purchase_product(PRODUCT_ID, PARAMS)
  • PRODUCT_ID — product ID assigned to product in RuStore Console (mandatory);
  • PARAMS — optional parameters:
    • orderId — payment ID generated by the app (optional). If you specify this parameter in your system, you will receive it via our API. Otherwise, it will be generated automatically;
    • quantity — product amount (optional);
    • payload — string with additional order information, that you can specify on purchase initialization.

Therustore_on_purchase_product_success callback returns a JSON string with the purchase information. Available fields:

  • type — request result type

    :

    • Success - successful purchase result;
    • Failure - there was a problem during sending payment request or receiving payment status, purchase status unknown;
    • Cancelled — payment request sent, although, the user closed the payment screen on their app, thus, the payment result is unknown;
    • InvalidPaymentState — SDK payments error. May occur due to an incorrect return deeplink.
  • data — JSON string with optional fields

    .

The Success object is returned if a request is successful. Available fields:

  • orderId — payment ID generated by the app (optional). If you specify this parameter in your system, you will receive it via our API. Otherwise, it will be generated automatically.
  • purchaseId — product ID.
  • productId — product ID assigned to product in RuStore Console (mandatory).
  • invoiceId — invoice ID.
  • sandbox — test payment flag. true — test payment, false — actual payment.
  • subscriptionToken — purchase token for server validation .

The Failure object is returned if there was an error during request processing. Available fields:

  • purchaseId — product ID.
  • invoiceId — invoice ID.
  • orderId — payment ID generated by the app (optional). If you specify this parameter in your system, you will receive it via our API. Otherwise, it will be generated automatically.
  • quantity — product amount (optional).
  • productId — product ID assigned to product in RuStore Console (mandatory).
  • sandbox — test payment flag. true — test payment, false — actual payment.
  • errorCode — error code.

Error codes are described in section Error codes.

The Cancelled object returns if a purchase is canceled by the user. Available fields:

  • purchaseId — product ID.
  • sandbox — test payment flag. true — test payment, false — actual payment.

The InvalidPaymentState object is returned if there is an SDK error. For example, if there is an incorrect return deeplink.

The rustore_on_purchase_product_failure callback returns a JSON string with the error information. The error structure is described in Error Handling.

Getting products list

This method returns purchases with the following statuses. For more informations on possible purchase states see Getting purchase info.

Type/StatusINVOICE_CREATEDCONFIRMEDPAID
CONSUMABLE++
NON-CONSUMABLE++
SUBSCRIPTION++
note

This method returns incomplete purchases that require attention. Also, it shows confirmed subscriptions and consumable products that cannot be purchased more than once.

Go get the user's purchases list, use the get_purchases() method.

You must subscribe to events once before using this method:

  • rustore_on_get_purchases_success;
  • rustore_on_get_purchases_failure.
Subscription to events
function init(self)
# rustorebilling initialization

rustorecore.connect("rustore_on_get_purchases_success", _on_get_purchases_success)
rustorecore.connect("rustore_on_get_purchases_failure", _on_get_purchases_failure)
end

function _on_get_purchases_success(self, channel, value)
local data = json.decode(value)

for key, value in pairs(data) do
-- value.amount
end
end

function _on_get_purchases_failure(self, channel, value)
local data = json.decode(value)
end
Calling get_purchases
rustorebilling.get_purchases()

The rustore_on_get_purchases_success callback returns an object array with the information about purchases. Available fields:

  • amount — price in minimum currency units.
  • amountLable — formatted price, including currency symbol specified in the language language.
  • currency — ISO 4217 currency code.
  • developerPayload — string with additional order information, that you can specify on purchase initialization.
  • invoiceId — invoice ID.
  • language — language specified with BCP 47 code.
  • orderId — payment ID generated by the app (optional). If you specify this parameter in your system, you will receive it via our API. Otherwise, it will be generated automatically.
  • productId — product ID assigned to product in RuStore Console (mandatory).
  • productType — product type.
  • purchaseId — product ID.
  • purchaseState — purchase state:
  • purchaseTime — purchase time.
  • quantity — product amount (optional).
  • subscriptionToken — purchase token for server validation .

The rustore_on_get_purchases_failure callback returns a JSON string with the error information. The error structure is described in Error Handling.

Getting purchase information

Go get purchase information, use the get_purchase_info() method.

You must subscribe to events once before using this method:

  • rustore_on_get_purchase_info_success;
  • rustore_on_get_purchase_info_failure.
Subscription to events
function init(self)
# rustorebilling initialization

rustorecore.connect("rustore_on_get_purchase_info_success", _on_get_purchase_info_success)
rustorecore.connect("rustore_on_get_purchase_info_failure", _on_get_purchase_info_failure)
end

function _on_get_purchase_info_success(self, channel, value)
local data = json.decode(value)
end

function _on_get_purchase_info_failure(self, channel, value)
local data = json.decode(value)
end
Calling get_purchase_info
# Your purchase information request UI implementation
function _on_get_purchase_info_pressed(purchaseId):
rustorebilling.get_purchase_info(purchaseId)
end

purchaseId — product ID

The rustore_on_get_purchase_info_success callbacj returns a JSON string with the purchase information. Available fields:

  • amount — price in minimum currency units.
  • amountLable — formatted price, including currency symbol specified in the language language.
  • currency — ISO 4217 currency code.
  • developerPayload — string with additional order information, that you can specify on purchase initialization.
  • invoiceId — invoice ID.
  • language — language specified with BCP 47 code.
  • orderId — payment ID generated by the app (optional). If you specify this parameter in your system, you will receive it via our API. Otherwise, it will be generated automatically.
  • productId — product ID assigned to product in RuStore Console (mandatory).
  • productType — product type.
  • purchaseId — product ID.
  • purchaseState — purchase state:
  • purchaseTime — purchase time.
  • quantity — product amount (optional).
  • subscriptionToken — purchase token for server validation .

The rustore_on_get_purchase_info_failure callback returns a JSON string with the error information. The error structure is described in Error Handling.

State model (purchaseState)

CONSUMABLES - purchase state model

img

NON-CONSUMABLES - purchase state model

img

(SUBSCRIPTIONS) - purchase state model

img

Consume (confirm) purchase

Products that require confirmation

Please, keep in mind the purchase type. Confirmation is only needed if your product is CONSUMABLE and can be purchased more than once.

To correctly deliver such products, confirm purchases with the confirmPurchase method. On delivering a product, use server validation. Deliver your product only after the payment (invoice) is in the CONFIRMED status. Use the addOnSuccessListener callback of the confirmPurchase method to deliver products.

caution

The PAID status is intermediate and meas the user's money is put on hold on their card, so you need to confirm the purchase.

ConfirmPurchase request

Us the confirm_purchase() method to confirm a purchase. Purchase confirmation request must be accompanied by the delivery of the product. After calling the confirmation method the purchase changes its state to CONSUMED.

You must subscribe to events once before using this method:

  • rustore_on_confirm_purchase_success;
  • rustore_on_confirm_purchase_failure.
Subscription to events
function init(self)
# rustorebilling initialization

rustorecore.connect("rustore_on_confirm_purchase_success", _on_confirm_purchase_success)
rustorecore.connect("rustore_on_confirm_purchase_failure", _on_confirm_purchase_failure)
end

function _on_confirm_purchase_success(self, channel, value)
local data = json.decode(value)
end

function _on_confirm_purchase_failure(self, channel, value)
local data = json.decode(value)
end
Calling confirm_purchase
# Your purchase confirmation UI implementation
function _on_confirm_purchase_pressed(purchaseId):
rustorebilling.confirm_purchase(purchaseId)
end
  • purchaseId — product ID.

Server validation

If you need to validate a successful app using API RuStore methods, you can use the subscriptionToken value, from the json string received upon a successful product purchase.

SubscriptionToken consists of invoiceId and userId of the purchase separated by period: $invoiceId.$userId.

Getting subscriptionToken from the purchase result

function _on_purchase_product_success(self, channel, value)
local data = json.decode(value)

if data.type == "Success" then
local subscriptionToken = data.data.subscriptionToken
yourApi.validate(subscriptionToken)
end
end

You can also get subscriptionToken from the purchases list.

function _on_get_purchases_success(self, channel, value)
local data = json.decode(value)

for key, value in pairs(data) do
yourApi.validate(value.subscriptionToken)
end
end

Purchase cancellation

To cancel a purchase, use the delete_purchase method.

You must subscribe to events once before using this method:

  • rustore_on_delete_purchase_success;
  • rustore_on_delete_purchase_failure.

Calling cancellation method

Subscription to events
function init(self)
# rustorebilling initialization

rustorecore.connect("rustore_on_delete_purchase_success", _on_delete_purchase_success)
rustorecore.connect("rustore_on_delete_purchase_failure", _on_delete_purchase_failure)
end

function _on_delete_purchase_success(self, channel, value)
local data = json.decode(value)
end

function _on_delete_purchase_failure(self, channel, value)
local data = json.decode(value)
end
Calling delete_purchase
# Your purchase cancellation UI implementation
function _on_delete_purchase_pressed(purchaseId):
rustorebilling.delete_purchase(purchaseId)
end
  • purchaseId — product ID.

The rustore_on_delete_purchase_success callback returns purchase ID:

  • purchaseId — product ID.

The rustore_on_delete_purchase_failure callback returns a JSON string with the error information. Available fields:

  • purchaseId — product ID.
  • cause — error information.

The error structure is described in Error Handling.

info

Use this method if your app logic is related to purchase cancellation. The purchase is canceled automatically after a 20-min timeout, or upon a second purchase from the same customer.

Handling pending payments

Handling of unfinished payments is done by the developer.

To confirm a purchase of CONSUMABLE product in the PAID state, call the [purchase confirmation method].(#confirm) (see Retrieve purchase details).

When dealing with purchase cancellation using payment processing methods take into account your business logic. Some developers implement additional checks before purchase confirmation or cancellation. In that case, make a separate purchase state request.

tip

For example, if the user paid for the product that you cannot deliver for some reason, call the payment cancellation method for the purchase in the PAID status to cancel the purchase.

If the get purchases list method returns a purchase in the INVOICE_CREATED state, you can us the purchase cancellation method. For instance, if you don't want to see a purchase in that state in the purchase list. This is optional as RuStore handles cancellation of such purchases on its side.

info

In some cases, after paying with a bank app (SBP, SberPay, T-Pay, etc.) and returning to your app the purchase status may still be INVOICE_CREATED while payment status shows that the purchase failed. This is caused by the purchase processing time by the bank. In this case, you need to adapt your screen lifecycle to the product list retrieval logic.

Alternatively, you can implement cancellation of purchases in the INVOICE_CREATED state only through user action in your app. For example, create a dedicated button for this purpose.

Event logging

If you need to log payment library events, add optional debugLogs in the init call and subscribe to the events:

  • rustore_on_payment_logger_debug;
  • rustore_on_payment_logger_error;
  • rustore_on_payment_logger_info;
  • rustore_on_payment_logger_verbose;
  • rustore_on_payment_logger_warning.
local APPLICATION_ID = "123456"
local DEEPLINK_SCHEME = "yourappscheme"
local DEBUG_LOGS = true
local LOG_TAG = "yourtag"

function init(self)
rustorecore.connect("rustore_on_payment_logger_debug", _on_payment_logger_debug)
rustorecore.connect("rustore_on_payment_logger_error", _on_payment_logger_error)
rustorecore.connect("rustore_on_payment_logger_info", _on_payment_logger_info)
rustorecore.connect("rustore_on_payment_logger_verbose", _on_payment_logger_verbose)
rustorecore.connect("rustore_on_payment_logger_warning", _on_payment_logger_warning)

rustorebilling.init(APPLICATION_ID, DEEPLINK_SCHEME, DEBUG_LOGS)
end

function _on_payment_logger_debug(self, channel, value)
rustorecore.log_debug(LOG_TAG, value)
end

function _on_payment_logger_error(self, channel, value)
rustorecore.log_error(LOG_TAG, value)
end

function _on_payment_logger_info(self, channel, value)
rustorecore.log_info(LOG_TAG, value)
end

function _on_payment_logger_verbose(self, channel, value)
rustorecore.log_verbose(LOG_TAG, value)
end

function _on_payment_logger_warning(self, channel, value)
rustorecore.log_warning(LOG_TAG, value)
end

Logging enabling parameter:

  • DEBUG_LOGS — enable logging (logging will be automatically disabled for Release builds).

All logging callbacks return a JSON string. Available fields:

  • e — error information or "null". The error structure is described in Error Handling.
  • message – error description.

Changing interface theme

To dynamically change the theme, use the set_theme method.

setTheme usabe example

rustorebilling.set_theme(0)
  • 0 - dark theme;
  • 1 - light theme.

Error handling

Error structure

Processing json error
function _on_failure(self, channel, value)
local data = json.decode(value)

local name = data.simpleName
local message = data.detailMessage
end
  • simpleName – error name.
  • detailMessage – error description.

Possible errors

  • RuStoreNotInstalledException — RuStore is not installed on the user's device;
  • RuStoreOutdatedException — RuStore version installed on the user's device does not support this SDK;
  • RuStoreUserUnauthorizedException — user is not authorized in RuStore;
  • RuStoreRequestLimitReached — not enough time has passed since the process was last shown;
  • RuStoreReviewExists — this user has already rated your app;
  • RuStoreInvalidReviewInfo — problems with ReviewInfo;
  • RuStoreException — basic RuStore error from which other errors are inherited.
On calling the purchase_product method, errors are handled automatically.

Use the set_error_handling method to display the error dialog to the user.

function init(self)
rustorebilling.set_error_handling(true)

# rustorebilling initialization
end
  • true — display dialog;
  • false — do not display dialog.

Error codes

Below is the list of possible errors that can be received errorCode field.

HTTP codeError codeDescription
40040001Incorrect request parameters: mandatory parameters are not filled in/incorrect parameters format.
40040003App not found.
40040004App status: inactive.
40040005Product not found.
40040006Product status: inactive.
40040007Invalid product type. Supported types: consumable, non-consumable, subscription.
40040008An order with this order_id already exists.
40040009This client already has an active order for this product in the invoice_created state. Prompt the client to either complete their payment or cancel the purchase.
40040010For consumable products. This customer has and order for this product in the paid state. First, the order needs to be consumed (confirmed) in the app, then, you can send another purchase request.
40040011For non-consumable products. This client already has an order of this product in the pre_confirmed/confirmed state. Such product has already been purchased. This product cannot be sold more than once.
40040012For subscription products. This client already has an order of this product in the pre_confirmed/confirmed state. Such product has already been purchased. This product cannot be sold more than once.
40040013For subscription products. The data was not received in the GET/products (serviceId, user_id) response.
40040014One or more mandatory attribute was not received in the request.
40040015Failed to change the order status during the purchase update (not allowed).
40040016quantity > 1 is specified for a non-consumable product purchase.
40040017Product deleted, new purchases not available.
40040018Cannot consume products with product type.
40140101Invalid token.
40140102Token lifetime has expired.
40340301Access to the requested resource is denied (unauthorized).
40340302This call is not authorized for the current token (method not allowed).
40340303The app ID in the request does not match the token.
40340305Incorrect token type.
40440401Not found.
40840801The notification timeout period specified in the request has expired.
50050***Payment service internal error.