الدفع
منطق دفع اللاعب داخل اللعبة هو أن اللاعبين يستخدمون عملة تطبيق Lobah لشراء العناصر داخل اللعبة.
1. إعداد المنتجات
اذهب إلى مركز مطوري Lobah في صفحة Operations -> Products
، ثم اضغط على Add New Product,
واملأ المعلومات التالية:
- Product name: اسم المنتج
- Product identification: معرف فريد للمنتج. يُستخدم هذا المعرف لتمييز المنتجات داخل اللعبة؛ يرجى التأكد من تميزه.
- Product price: سعر المنتج بعملة Lobah Coins. نسبة التحويل بين Coins والدولار الأمريكي هي
100:1
.
2. الحصول على App ID و App Key
اذهب إلى مركز مطوري Lobah في صفحة Deployment -> Configuration
للحصول على App ID و App Key.
3. التحقق من Server API والدفع
نقطة نهاية Server API الخاصة بـ Lobah هي: https://game-gateway.lobah.net
أول خطوة لاستخدام Server API هي تنفيذ خوارزمية التوقيع، ثم استدعاء واجهة الشراء لإجراء الدفع. لمزيد من التفاصيل، راجع قسم Authentication
في وثيقة API 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();
}
}