支付宝小程序设备SDK接入

本文档详细介绍了阿里云风险识别产品中设备风控SDK(支付宝小程序版)的接入流程和技术规范,帮助开发者快速集成设备风险识别能力,有效防范设备伪造、批量注册、恶意登录等风险行为。

背景信息

支付宝小程序SDK是专为小程序环境设计的设备风险识别工具,通过嵌入SDK,开发者可以快速集成设备指纹能力,实时获取设备信息并联动风险识别服务。该SDK支持多种场景,如注册、登录、营销活动等,有效防范黑产攻击,提升业务安全性。详情请参见支付宝小程序

SDK集成指南

  1. 下载并引入小程序 SDK

    下载支付宝小程序SDK,建议在小程序的首屏加载index页面引入SDK,并将AliyunFP对象全局挂载,方便别的页面调用。

    import * as AliyunFP from './feilin.alipay.xxx.js';
    const app = getApp();
    app.AliyunFP = AliyunFP;
  2. 添加配置信息

    在调用小程序SDK 接口前,需在对应的 axml 中加入feilin-view节点:

    <view>
      <view> 业务的 UI 布局 </view>
      
      <view id = "feilin-view" style="position:fixed;top:99999rpx;"> 
        <canvas id="feilin-canvas" type="2d" style="width:150px;height:150px;"></canvas> 
        <canvas id="feilin-webgl" type="webgl" style="width:150px;height:150px;"></canvas>
      </view>
    </view>
  3. 初始化SDK

    引入小程序 SDK之后,在页面加载onReady时初始化SDK。

    Page({
      onReady() {
        AliyunFP.init(
          {
            appKey: '这里填写风险识别控制台申请的AppKey',
            appName: '这里填小程序应用名称,客户自定义',
            openId: '支付宝小程序用户ID,非必填,建议传入',
            endpoints: [
              'https://cloudauth-device.aliyuncs.com',
              'https://cn-shanghai.device.saf.aliyuncs.com'
            ],
          },
          (initStatus, deviceToken) => {
            console.log("initStatus:" + initStatus + "  deviceToken:" + deviceToken);
          }
        );
      }
    });
    重要

    如果不指定服务地址,则endpoints会读取默认配置。请务必将endpoints的域名添加至小程序后台白名单,以确保相关功能正常运行。

    endpoints默认服务地址:

    • 中国内地(默认)服务地址:https://cloudauth-device.aliyuncs.com,https://cn-shanghai.device.saf.aliyuncs.com

    • 国际/中国香港服务地址:https://cloudauth-device.ap-southeast-1.aliyuncs.com,https://ap-southeast-1.device.saf.aliyuncs.com

  4. 获取deviceToken

    获取deviceToken,建议用 AliyunFP.init 接口回调返回的值,或者在发起业务行为(如注册、登录、订购等)时调用一次,AliyunFP.getTokenAliyunFP.init之间至少预留1s左右的时间差。

    AliyunFP.getToken()

接入示例

页面结构 (axml)

<view>
  <view class="page-description">
    {{ message }}
  </view>
  
  <button type="primary" onTap="onLogin">
    登录
  </button>

  <view id="feilin-view" style="position:fixed;top:99999rpx;">
    <canvas id="feilin-canvas" type="2d" style="width:150px;height:150px;"></canvas>
    <canvas id="feilin-webgl" type="webgl" style="width:150px;height:150px;"></canvas>
  </view>
</view>

逻辑实现 (js)

import * as AliyunFP from './feilin.alipay.xxx.js';
const app = getApp();
app.AliyunFP = AliyunFP;


Page({
  onReady() {   
    AliyunFP.init({
      appKey: '这里填写风险识别控制台申请的AppKey',
      appName: '这里填小程序应用名称,客户自定义',
      openId: '支付宝小程序用户ID,非必填,建议传入',
      endpoints: ['https://cloudauth-device.aliyuncs.com', 'https://cn-shanghai.device.saf.aliyuncs.com']
    }, (initStatus, deviceToken) => {
      console.log("initStatus:" + initStatus + "  deviceToken:" + deviceToken);
    });
  },
  
  onLogin() {
    // 某行为点击,这里举例为登录。
    console.info("onLogin!");
    const deviceToken = AliyunFP.getToken();
    // 将deviceToken传入客户的服务后台
    const data = {
      "deviceToken": deviceToken,
      "其他业务参数": ""
    };
    // 发送后台请求...后台通过deviceToken查询风险信息
  }
});

注意事项

  1. 安全性:开发者应确保在调用风险识别API接口时,传输的数据是安全的,防止数据泄露或被篡改。

  2. 兼容性:支付宝小程序设备风控SDK可能在不同版本的支付宝小程序中存在兼容性问题,开发者需要关注支付宝小程序的更新动态,并进行相应的适配工作。

  3. 性能:由于设备风控功能需要在设备上采集和计算数据,可能会对应用的性能产生一定影响。开发者需要在实际应用中进行性能测试和优化,确保应用的流畅性和用户体验。

调用风险识别API接口

根据风险识别服务的事件参数文档说明,将deviceToken与其他必要参数组合,调用风险识别API接口进行设备风险检测。详情请参见: