Skip to content

Payment

The logic for player payments within the game involves players using Lobah APP currency to purchase in-game items.

1. Configure Products

Go to the Lobah Developer Center Operations -> Products page, click Add New Product, and fill in the following information:

  • Product name: Name of the product
  • Product identification: Unique identifier for the product. This is used to identify products within the game; please ensure it is distinct.
  • Product price: Price of the product, in Lobah currency Coins. The ratio of Coins to USD is 100:1.

2. Obtain App ID and App Key

Go to the Lobah Developer Center Deployment -> Configuration page to obtain the App ID and App Key.

3. Server API Validation and Payment

The Lobah Lobah Server API endpoint is: https://game-gateway.lobah.net

The first step in using the Server API is to perform a signature algorithm, then call the Purchase interface to make the payment. For more details, refer to the Authentication section in the API Reference documentation.

Below is an example Java code snippet for signing and making a payment, provided for reference:

java
package net.lobah;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;

public class Demo {

    private static final String appKey = "";       // Configuration -> appKey
    private static final String accessToken = "";  // params.token 
    private static final String appId = "";        // params.gameId 
    private static final String sessionId = "";    // params.sessionId 
    private static final String uid = "";          // params.uid
    private static final String productId = "";    // Product Identification
    
    private static final String method = "POST";
    private static final String host = "https://game-gateway.lobah.net";
    
    public static void main(String[] args) throws Exception {
        test();
        getProfile();
        purchase();
    }

    public static void test() throws Exception {
        String path = "/1.0/open-gateway/game/test";
        String postBody = "";
        httpPost(path, postBody);
    }

    public static void purchase() throws Exception {
        String referenceId = UUID.randomUUID().toString().replace("-", "").substring(0, 12);
        String path = "/1.0/open-gateway/game/purchase";
        String postBody = "{ \"app_id\": " + appId + ", " + " \"product_id\": \"" + productId + "\", \"reference_id\": \"" + referenceId + "\", \"session_id\": " + sessionId + ", \"uid\": " + uid + " }";
        httpPost(path, postBody);
    }

    public static void getProfile() throws Exception {
        String path = "/1.0/open-gateway/game/get-profile";
        String postBody = "{\"app_id\": " + appId + ", \"token\": \"" + accessToken + "\", \"uid\": " + uid + " }";
        httpPost(path, postBody);
    }

    public static void httpPost(String requestPath, String postBody) throws IOException, NoSuchAlgorithmException, InvalidKeyException {
        Map<String, String> reqParamsMap = new TreeMap<>();
        reqParamsMap.put("access_token", accessToken);
        reqParamsMap.put("app_id", appId);
        reqParamsMap.put("nonce", UUID.randomUUID().toString().replace("-", "").substring(0, 8));
        reqParamsMap.put("ts", String.valueOf(Instant.now().getEpochSecond()));
        reqParamsMap.put("uid", uid);
        reqParamsMap.put("zone", "SA");

        String reqParam = encodeURL(reqParamsMap);
        String encodeStr = method + requestPath + reqParam + postBody;
        String urlEncodedData = URLEncoder.encode(encodeStr, "UTF-8");
        String digest = genDigest(urlEncodedData, appKey, "HmacMD5");
        System.out.println("Encode String: " + encodeStr);
        System.out.println("Encode Data: " + urlEncodedData);
        System.out.println("Digest: " + digest);
        httpPostWithSig(host + requestPath + "?" +  reqParam + "&sig=" + digest, postBody);
    }

    private static void httpPostWithSig(String requestUrl, String requestBody) throws IOException {
        System.out.println("Request Url: " + requestUrl);
        URL url = new URL(requestUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod(method);
        connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
        connection.setRequestProperty("Content-Type", "application/json; utf-8");
        connection.setRequestProperty("Accept", "application/json");
        connection.setDoOutput(true);

        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = requestBody.getBytes(StandardCharsets.UTF_8);
            os.write(input, 0, input.length);
        }

        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);

        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
            StringBuilder response = new StringBuilder();
            String responseLine;
            while ((responseLine = br.readLine()) != null) {
                response.append(responseLine.trim());
            }
            System.out.println("Response Body: " + response);
        }
        connection.disconnect();
    }

    private static String genDigest(String msg, String keyString, String algo) throws NoSuchAlgorithmException, InvalidKeyException {
        SecretKeySpec key = new SecretKeySpec((keyString).getBytes(StandardCharsets.UTF_8), algo);
        Mac mac = Mac.getInstance(algo);
        mac.init(key);
        byte[] bytes = mac.doFinal(msg.getBytes(StandardCharsets.US_ASCII));
        StringBuilder hash = new StringBuilder();
        for (byte aByte : bytes) {
            String hex = Integer.toHexString(0xFF & aByte);
            if (hex.length() == 1) {
                hash.append('0');
            }
            hash.append(hex);
        }
        return hash.toString();
    }

    private static String encodeURL(Map<String, String> parameters) {
        StringBuilder result = new StringBuilder(2048);
        for (String encodedName : parameters.keySet()) {
            String encodedValue = parameters.get(encodedName);
            if (result.length() > 0) {
                result.append('&');
            }
            result.append(encodedName);
            if (encodedValue != null) {
                result.append("=");
                result.append(encodedValue);
            }
        }
        return result.toString();
    }
}

Swiper and Play