npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

alipay-cloud-sdk

v0.9.16

Published

Ant OpenAPI JavaScript API Baas

Downloads

87

Readme

Serverless 云端调用 - 支付宝开放平台

一、基本使用

npm 安装 alipay-cloud-sdk

npm install --save alipay-cloud-sdk

alipay-cloud-sdk 依赖于 @alicloud/mpserverless-sdk

// 1. 引入必要的 sdk
import MiniProgram from '@alicloud/mpserverless-sdk';
import cloud from 'alipay-cloud-sdk';

// 2. 在 app.js 中对 sdk 进行初始化
// 2.1 初始化 mpserverless
const mpserverless = new MiniProgram(my, {
  // 参数能在小程序云服务空间详情中获取
  appId: '',
  spaceId: '',
  endpoint: '',
  clientSecret: '',
});
// 2.3 初始化 alipay-cloud-sdk
cloud.init(mpserverless);

// 3. 在想要使用的页面导入 alipay-cloud-sdk 即可使用
// 比如在 page.js
import cloud from 'alipay-cloud-sdk';

cloud.base.uploadImage({
  filePath: 'https://images/test.jpg',
  fileName: 'test.jpg',
});

二、SDK 支持的配置

在 SDK 初始化的时候,支持一些配置,目前支持如下:

1. 交易消息回调
cloud.init(mpserverless, {
  // 配置此参数后,拥有回调消息的 openapi 将回调该地址
  notify: {
    url: 'https://callback',
  },
});

三、支持的能力

基础的常量能在 sdk 的 constants 目录导入

import MediaType from 'alipay-cloud-sdk/dist/umd/constants/MediaType';
  • 通用基础能力
cloud.base.oauth.getToken({
  grantType: GrantType.AUTH_CODE,
  code: 'authCode', // 通过 JSAPI my.getAuthCode 获取
});
cloud.base.uploadImage({
  fileName: 'test.jpg',
  filePath: 'https://images/test.jpg',
  fileType: MediaType.JPG, // 可选,默认 JPG
});
cloud.base.uploadVideo({
  fileName: 'test.mp4',
  filePath: 'https://videos/test.mp4',
});
// 获取用户数据,如果是老云函数版本需要传入 { authToken: '你的 accessToken' }
// 如果是接入了新云函数,即不需要参数
cloud.base.getUserInfo();
// 提供的通用调用方法,与 openapi 调用模式很像,调用虽然不是很友好,但是可以很快支持未封装的API需求
// 这个通用调用方法仅适用于有 bizContent 包裹对象的接口 
cloud.base.exec('alipay.xxx.yyy', {})
  • 支付能力
import { BillType } from 'alipay-cloud-sdk/dist/umd/contants/BillType';
// 创建支付宝交易号
cloud.payment.create({
  outTradeNo: String(+new Date()),
  totalAmount: '0.1',
  subject: '购买物品',
  buyerId: '支付用户的 userId', // 通过 cloud.base.oauth.getToken 可以获取
});
// 条形码支付
cloud.payment.pay({
  outTradeNo: String(+new Date()),
  authCode: '条形码',
  subject: '购买物品',
  totalAmount: '0.1',
});
// 通过商家交易号查询交易
cloud.payment.query({
  outTradeNo: String(+new Date()),
});
// 关闭交易,只改变交易的状态
cloud.payment.close({
  outTradeNo: String(+new Date()),
});
// 撤销交易,有事务的回滚
cloud.payment.cancel({
  outTradeNo: String(+new Date()),
});
// 退款
cloud.payment.refund({
  outTradeNo: String(+new Date()),
  refundAmount: '0.1',
});
// 查询对账单地址,T+1 规则
cloud.payment.getBillUrl({
  billType: BillType.TRADE,
  billDate: '2019-11-11', // yyyy-MM-dd
});
  • 小程序能力
import { FaceType } from 'alipay-cloud-sdk/dist/umd/contants/FaceType';
// 刷脸认证
cloud.mini.queryFaceCertifyId({
  bizId: String(+new Date()),
  zimId: '', // 通过 JSAPI my.ap.faceVerify 获取
  faceType: FaceType.REAL_AUTH,
  needImg: false,
});
// 人脸采集
cloud.mini.queryFaceVerified({
  bizId: String(+new Date()),
  zimId: '',
  externParam: {}, // 可选
});
// 创建小程序推广二维码
cloud.mini.createQrcode({
  urlParam: 'pages/index/index',
  queryParam: {
    key: 'value'
  },
  describe: '二维码描述',
});
// 发送模板消息
cloud.mini.sendTemplateMessage({
  toUserId: '当前用户 userId',
  formId: '表单组件的 formId',
  userTemplateId: '模板ID', // 管理后台可取
  page: '消息唤起的小程序页面',
  data: {
    // 模板关键词
    keyword1: {
      value: 'value',
    },
  },
});
// 文本风险识别
cloud.mini.detectRiskContent({
  content: '文本',
});
  • 营销能力
export interface TriggerDrawCampRequest {
  userId?: string;
  campId: string;
  loginId?: string; // 这里冗余,对外只要求必传 userId
  bindMobile?: string;
  campSource?: CampSource;
  jsonUa?: string;
  channelInfo?: string;
  clientIp?: string;
  outBizNo?: string;
}
cloud.ap.marketing.triggerDrawCamp({
  campId: '活动id'
});

四、异常

1. 统一异常结构
const error = {
  errCode: ErrorCode.INVALID_PARAM,
  errMsg: 'message',
  message: '具体的描述', // `errCode: ${errCode} | errMsg: ${errMsg}`;
}
2. 错误码

| 错误码 | 错误码描述 | 备注 | | --- | --- | --- | | SYS_INIT | SDK 初始化错误 | 未调用 .init(mpserverless) 来初始化 | | FEATURE_NOT_SUPPORT | 暂不支持的 API | -- | | INVALID_JSON_OBJECT | JSON 解析失败 | 不合法 JSON 解析的时候会报错,主要是入参 | | NOT_FOUND_PARAM | 参数缺省 | 参数不全,错误提示中有具体参数名 | | INVALID_PARAM | 参数不合法 | 参数类型或者值不对 |

五、常量

提供一些常量,可用于 API 参数使用。

1. 导入常量
// 授权类型
import { GrantType } from 'alipay-cloud-sdk/dist/umd/contants/GrantType';
2. 具体的常量
  • 查询账单地址类型
const BillType = {
  TRADE: 'trade',
  SIGN_CUSTOMER: 'signcustomer',
}
  • 用户类型
const BuyerUserType = {
  // 企业
  CORPORATE: 'CORPORATE',
  // 个人
  PRIVATE: 'PRIVATE',
}
  • 人脸认证类型
const FaceType = {
  // 匿名注册
  ANONYMOUS_REGISTER: 1,
  // 匿名认证
  ANONYMOUS_AUTH: 2,
  // 实名认证
  REAL_AUTH: 3,
}
  • 授权类型
const GrantType = {
  AUTH_CODE: 'authorization_code',
  REFRESH_TOKEN: 'refresh_token',
}
  • 上传文件类型
const MediaType = {
  JPG: 'jpg',
  BMP: 'bmp',
  PNG: 'png',
  JPEG: 'jpeg',
  GIF: 'gif',
  // 视频目前只支持 mp4
  MP4: 'mp4',
}
  • 支付状态
const TradeStatus = {
  WAIT_BUYER_PAY: 'WAIT_BUYER_PAY',
  TRADE_CLOSED: 'TRADE_CLOSED',
  TRADE_SUCCESS: 'TRADE_SUCCESS',
  TRADE_FINISHED: 'TRADE_FINISHED',
}