搭配BaaS授权使用游戏内商店
您可以搭配BaaS授权系统使用游戏内商店来销售游戏内商品。该方案的交互过程如下:
- 用户通过BaaS授权系统登录您的应用程序。
- BaaS服务向艾克索拉服务器请求支付中心访问令牌,并传入用户ID。
- 艾克索拉服务器向BaaS服务返回支付中心访问令牌。
- BaaS服务将支付中心访问令牌传给应用程序。
- 应用程序使用支付中心访问令牌通过使用API与艾克索拉服务器交互。
要获取支付中心访问令牌,请按照以下用于Firebase和PlayFab的说明将现成的函数添加到您的项目。
向Firebase项目添加云函数
- 初始化您的Firebase项目。
- 导入并配置支付中心访问令牌的接收函数,其中:
<MerchantID>
— 在发布商帐户项目设置 > Webhooks部分找到的商户ID。<ProjectID>
— 在项目设置 > 项目ID部分找到的项目ID。<ApiKey>
— 在发布商帐户公司设置 > API密钥部分找到的API密钥。
接收支付中心访问令牌的函数代码:
Copy
- js
const merchantId = "<MerchantID>";
const projectId = "<ProjectID>";
const apiKey = "<ApiKey>";
exports.getXsollaAccessToken = functions.https.onCall((data, context) => {
if (!context.auth) {
throw new functions.https.HttpsError(
"failed-precondition",
"The function must be called while authenticated."
);
}
const userId = context.auth.uid;
const userEmail = context.auth.token.email || null;
const authHeader = Buffer.from(`${merchantId}:${apiKey}`).toString("base64");
const postData = JSON.stringify(
{
"user": {
"id": {
"value": userId,
},
"email": {
"value": userEmail,
},
},
"settings": {
"project_id": Number(projectId),
},
}
);
const options = {
hostname: "api.xsolla.com",
port: 443,
path: `/merchant/v2/merchants/${merchantId}/token`,
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": postData.length,
"Authorization": `Basic ${authHeader}`,
},
};
return new Promise( (resolve, reject) => {
const req = https.request(options, (res) => {
if (res.statusCode != 200) {
reject(
new functions.https.HttpsError("internal", "Token not received")
);
}
let body = [];
res.on("data", (d) => {
body.push(d);
});
res.on("end", () => {
try {
body = JSON.parse(Buffer.concat(body).toString());
} catch (e) {
reject(
new functions.https.HttpsError("internal", "Malformed response")
);
}
resolve({
"token": body.token,
});
});
});
req.on("error", (e) => {
reject(new functions.https.HttpsError("internal", "Internal error"));
});
req.write(postData);
req.end();
});
});
- 参照此示例将该函数部署到生产环境。
- 添加客户端侧逻辑从您的应用程序调用函数。将
getXsollaAccessToken
指定为函数名称。传参为非必需。 - 在应用程序中,自行实现与API交互的方法或使用艾克索拉SDK。
向PlayFab项目添加云脚本
- 创建包含函数代码的JS文件以接收支付中心访问令牌,其中:
<MerchantID>
— 在发布商帐户项目设置 > Webhooks部分找到的商户ID。<ProjectID>
— 在项目设置 > 项目ID部分找到的项目ID。<your_authorization_basic_key>
— 根据Base64标准编码的商户ID:API密钥对。API密钥可在发布商帐户公司设置 > API密钥部分找到。
Note
如果已在项目中使用了云脚本,请将接收支付中心访问令牌的函数添加到该代码的末尾。
接收支付中心访问令牌的函数代码:
Copy
- js
handlers.GetXsollaAccessToken = function (args) {
// TODO replace with production credentials
const merchantId = <MerchantID>; // Merchant ID from the Publisher Account
const projectId = <ProjectID>; // Project ID from the Publisher Account
const authHeader = '<your_authorization_basic_key>' // Base64(merchant_id:api_key)
const headers = {
"Authorization": `Basic ${authHeader}`
};
const body = {
user: {
id: {
value: currentPlayerId
},
playfab_id: {
value: currentPlayerId
}
},
settings: {
project_id: projectId
}
};
const url = `https://api.xsolla.com/merchant/v2/merchants/${merchantId}/token`;
const httpMethod = "post";
const content = JSON.stringify(body);
const contentType = "application/json";
const response = JSON.parse(http.request(url, httpMethod, content, contentType, headers));
if ('token' in response) {
return {
"token" : response.token
}
} else {
return {
"error_message" : response.message
}
}
}
- 前往PlayFab项目设置。
- 上传云脚本文件。
- 在生产环境中运行云脚本。
- 添加客户端侧逻辑从应用程序调用函数。将
GetXsollaAccessToken
指定为函数名。传参为非必需。
调用支付中心访问令牌的接收函数示例如下:
Copy
kotlin
- kotlin
- C#
- C++
val tokenRequest = PlayFabClientModels.ExecuteCloudScriptRequest()
tokenRequest.FunctionName = "GetXsollaAccessToken"
val res = PlayFabClientAPI.ExecuteCloudScript(tokenRequest)
val result = res.Result.FunctionResult as Map<*, *>
val token = result["token"]
val errorMessage = result["error_message"]
var tokenRequest = new ExecuteCloudScriptRequest{
FunctionName = "GetXsollaAccessToken"
};
PlayFabClientAPI.ExecuteCloudScript(
tokenRequest,
scriptResult =>
{
var functionResult = scriptResult.FunctionResult as Dictionary<string, string>;
var token = functionResult["token"];
},
playfabError => { Debug.LogError($"GetXsollaAccessToken error: {playfabError.ErrorMessage}"); });
void UMyClass::GetXsollaToken()
{
FClientExecuteCloudScriptRequest tokenRequest;
tokenRequest.FunctionName = TEXT("GetXsollaAccessToken");
UPlayFabClientAPI::FDelegateOnSuccessExecuteCloudScript onSuccess;
onSuccess.BindUFunction(this, "OnTokenRecieved");
UPlayFabClientAPI::FDelegateOnFailurePlayFabError onFailure;
onSuccess.BindUFunction(this, "OnError");
UPlayFabClientAPI::ExecuteCloudScript(tokenRequest, onSuccess, onFailure, nullptr);
}
void UMyClass::OnTokenRecieved(FClientExecuteCloudScriptResult result, UObject* customData)
{
const FString& token = result.FunctionResult->GetStringField(TEXT("token"));
// do something with a token
}
void UMyClass::OnError(FPlayFabError error, UObject* customData)
{
// handle errors
}
Note
本例中使用PlayFab SDK方法向云脚本发送请求。您也可以不在项目中添加PlayFab SDK,而是自行实现请求和响应过程。
本文对您的有帮助吗?
感谢您的反馈!
我们会查看您的留言并运用它改进用户体验。为此页面评分
为此页面评分
不想回答
感谢您的反馈!
有用链接
上次更新时间: 2021年10月18日发现了错别字或其他内容错误? 请选择文本,然后按Ctrl+Enter。