跳到主要内容

内购

一、后端对接

请完成后端内购支付的接入,在后端实现对应接口后,再执行下一步操作。

未实现后端接口就进行页面对接,可能会触发平台服务器的告警。

二、客户端对接

用户支付在一个网页完成,因此客户端需要使用 SDK 在 WebView 打开指定支付页面。

SDK 方法名称

XL_OpenPayUrlXL_OpenPayUrlW(宽字符接口)

调用示例

// 以下代码依赖 Windows API
#include <windows.h>

// 打开支付页面的接口导出为 XL_OpenPayUrl
if (HMODULE hModule = GetModuleHandle(_T("XLGameLauncher.dll")))
{
void(WINAPI * XL_OpenPayUrl)(LPCSTR, LPCSTR);

(PVOID &)XL_OpenPayUrl = GetProcAddress(hModule, "XL_OpenPayUrl");

if (XL_OpenPayUrl)
{
// 第一个参数是支付链接,链接格式见下方文档解释
XL_OpenPayUrl("https://youxi.xunlei.com/gamepayexe?paramExt=xxxxxxxx", userId);
}
}

// 打开支付页面的宽字符接口导出为 XL_OpenPayUrl
if (HMODULE hModule = GetModuleHandle(_T("XLGameLauncher.dll")))
{
void(WINAPI * XL_OpenPayUrl)(LPCWSTR, LPCWSTR);

(PVOID &)XL_OpenPayUrl = GetProcAddress(hModule, "XL_OpenPayUrlW");

if (XL_OpenPayUrl)
{
// 第一个参数是支付链接,链接格式见下方文档解释
XL_OpenPayUrl(L"https://youxi.xunlei.com/gamepayexe?paramExt=xxxxxxxx", userId);
}
}

调用参数说明

参数类型说明
支付链接string详见支付链接章节
userIdstring平台的用户 ID

支付链接

支付链接格式为:https://youxi.xunlei.com/gamepayexe?paramExt=xxxxxxxx ,链接中的 paramExt 参数请根据下方说明替换为实际值。

链接参数说明

支付链接有且仅有一个参数 paramExt,是一个由 JSON 经过 Base64 编码得到的字符串,JSON 需要包含的参数如下所示:

参数类型必选说明
cpOrderIdstring游戏方的订单号
userIdstring平台的用户 ID(由平台提供)
gameIdstring平台的游戏 ID(由平台提供)
role_idString角色 ID
role_nameString角色名称
serverIdstring区服 ID
gameNamestring平台的游戏名称(由平台提供)
productNamestring商品名称
其他字段string / number其它自定义字段,回调游戏方接口时数据会写到 ext2 参数里面,参数不宜过多过长。

paramExt 参数 Base64 的加解密参考逻辑

此处使用业界通用的RFC 4648 第 4 章节描述的 Base64 版本。

URL 安全的 Base64

Base64 编码后的字符串在 URL 不能安全使用,因为这些字符串可能包含 / + = 字符,在 URL 使用可能出问题。

因此,编码后的字符串需要执行一次字符替换操作,将 / 替换为 _a+ 替换为 _b= 替换为 _c。Base64 解码前,按照同样的规则进行反向替换。

// 以下用 php 代码表示大致逻辑=
// 适用于 paramExt 的 Base64 编码过程
static function base_encode($str) {
// 将 JSON 字符串编码为 Base64 字符串
// base64_encode是 php 内置的 Base64 编码方法,其他编程语言请查找相关实现
$old = base64_encode($str);

// 将 Base64 编码后的字符串中的 / + = 替换成 _a _b _c ,以便作为 URL 参数传输
$src = array("/","+","=");
$dist = array("_a","_b","_c");
$new = str_replace($src,$dist,$old);

return $new;
}

// 适用于 paramExt 的 Base64 解码过程
static function base_decode($str) {
// 将字符串中的 _a _b _c 替换成 / + = ,以便 Base64 解码
$src = array("_a","_b","_c");
$dist = array("/","+","=");
$old = str_replace($src,$dist,$str);

// 将 Base64 字符串解码为 JSON 字符串
// base64_decode 是 php 内置的 Base64 编码方法,其他编程语言请查找相关实现
$new = base64_decode($old);

return $new;
}

// 举个例子
$payinfo = '{ "gameId": 6, "productName": "红宝石", "gameName": "王者荣耀", "extid": 123 }' // 标准 JSON 字符串

$paramExt = base_encode($payinfo) = 'eyAiZ2FtZUlkIjogNiwgInByb2R1Y3ROYW1lIjogIue6ouWuneefsyIsICJnYW1lTmFtZSI6ICLnjovogIXojaPogIAiLCAiZXh0aWQiOiAxMjMgfQ=='