@astropay/astropay-sdk

中文文档
4.2.0发布于 2023/09/11UNLICENSEDTypeScriptESM + CJS近 30 天下载 2.4万

Official AstroPay client for easy frontend integration to One Touch

原始使用文档
文档目录15 节
  1. Table of Contents
  2. Getting Started
  3. Prerequisites
  4. Installation
  5. Usage
  6. Platform Init
  7. Event Listeners
  8. Method Call
  9. Supported languages
  10. Updrading
  11. V4 - Latest
  12. V3
  13. V2
  14. Support
  15. License

AstroPay SDK for JavaScript

Official AstroPay SDK for easy product integrations

NPM version NPM downloads Typing code style: prettier

Table of Contents

Getting Started

Prerequisites

  • Node.js v14 or higher.

Installation

Using your favorite package manager, install @astropay/astropay-sdk package.

NPM:


npm install --save @astropay/astropay-sdk

YARN:


yarn add @astropay/astropay-sdk

Usage

Platform Init

SDK config parameters

To correctly configure the SDK, the init method needs to receive the APP_ID and the configuration object with the properties described below:

PropertyTypeDefault valueDescription
environment'sandbox' | 'production' | undefined'production'Enviroment to use.
languageSupported languages | undefined'en'The language set on the init object has priority over the language of the URL property on the ASTROPAY_RESPONSE
loggerInstance of Logger | undefinedundefinedUse to console log actions inside the SDK

If the language property isn't set on the init, the SDK will try to use the language query parameter on the URL of the ASTROPAY_RESPONSE. If both are not set, the SDK will use the default language: english

Code snippet:

import AstropaySDK, { ClientConfiguration, ConsoleLogger, DepositStatus } from  '@astropay/astropay-sdk';

// SDK Config
const config: ClientConfiguration = {
    environment: 'production',
    language: 'en',
    logger: new ConsoleLogger(),
};

// SDK Init
AstroPaySDK.init(APP_ID, config);

Event Listeners

Set up event listeners that receives a callback funcion as a parameter.

// SDK Event Listeners
const eventListening = AstroPaySDK.getEventListener('deposit');
eventListening.on('close', (data) => {
    console.log('data on close event', data);
    }
);

Method Call

Method Parameters

When calling one of the product methods (Deposit | Cashout | Onboarding) to effectively open the SDK, the espected parameters are the ASTROPAY_RESPONSE and the configUI properties to manipulate the UI according to your needs:

PropertyTypeDefault valueDescription
ASTROPAY_RESPONSEObjectnot applicable: mandatory to send on the callThe init object provided by AstroPay services on your call. Check the examples on the Usage section.
PropertyTypeDefault valueDescription
backdrop'static' | 'active''static'If you want the AstroPay SDK to be closed when the user clicks on the Backdrop (Outside the content) use active
If static, the backdrop element will not close the SDK modal when the user clicks outside content.
zIndexstring'auto'The z-index property specifies the stack order of the popup.
Greater stack order is always in front of an element with a lower stack order.

Notice that the ASTROPAY_RESPONSE should be configured with real values for the product calls (Deposit | Cashout | Onboarding). Be careful not to send an incorrect value on these properties

Deposit

// Non mandatory object
const configUI = {backdrop:'active', zIndex:'auto'}

// Mandatory object
const ASTROPAY_RESPONSE = {  
    "merchant_deposit_id":  "0c435984-0b55-4f16-a15e-36070fbcee19",  		
    "deposit_external_id": "QYPvyLz9fM57EGHtR1DV3TcZOlBng7Uw1m1KWUpe",  
    "status":  "PENDING",  
    "url":  "https://onetouch-sandbox.astropay.com/deposit/QYPvyLz9fM57EGHtR1DV3TcZOlBng7Uw1m1KWUpe",  
    "type":  "default"  
};

// Show the DEPOSIT client
AstropaySDK.deposit(ASTROPAY_RESPONSE);

Cashout

const ASTROPAY_RESPONSE = {  
    "merchant_cashout_id":  "0c435984-0b55-4f16-a15e-36070fbcee19",  		
    "cashout_external_id": "QYPvyLz9fM57EGHtR1DV3TcZOlBng7Uw1m1KWUpe",  
    "status":  "PENDING",  
    "url":  "https://onetouch-sandbox.astropay.com/cashout/QYPvyLz9fM57EGHtR1DV3TcZOlBng7Uw1m1KWUpe"
};

// Show the CASHOUT client
AstropaySDK.cashout(ASTROPAY_RESPONSE);

Onboarding

const ASTROPAY_RESPONSE = {  
    "platform_merchant_id":  "0c435984-0b55-4f16-a15e-36070fbcee19",  
    "merchant_name": "Merchant"
    "onboarding_external_id": "QYPvyLz9fM57EGHtR1DV3TcZOlBng7Uw1m1KWUpe",  
    "status":  "PENDING",  
    "url":  "https://onetouch-sandbox.astropay.com/onboarding/QYPvyLz9fM57EGHtR1DV3TcZOlBng7Uw1m1KWUpe",
};

// Show the ONBOARDING client
AstropaySDK.onboarding(ASTROPAY_RESPONSE);

Payment

This method receives the AstroPay Response and returns a Promise that will be resolve to an object with the payment data to be display in your site as you please.

//Method Response 
AstropaySDK.payment(AstropayResponse): Promise<PaymentData>

Method response interface

interface PaymentData {
  amount: number;
  currency: string;
  status: string;
  payment_type: string;
  data: {
        txt_qr: string;
        image: string;
    } & Record<string, any>
}

The returned object fields:

  • amount: Amount of the payment
  • currency: Currency of the payment
  • status: Status of the payment
  • payment_type: A string define by the method selected for this payment
  • data: Object containing the data of the payment with a key - value format

Notice that this method is only compatible with type: "gateway" deposits


Important

Based on the payment_type field the data object could include specific fields:

Payment TypeDataValueDescription
PIXObjecttxt_qr: string,
image: string
The qr code for the pix payment as a string.
Base64 string containing the qr code image to embbed
// Mandatory object
const ASTROPAY_RESPONSE = {  
    "merchant_deposit_id":  "0c435984-0b55-4f16-a15e-36070fbcee19",  		
    "deposit_external_id": "QYPvyLz9fM57EGHtR1DV3TcZOlBng7Uw1m1KWUpe",  
    "status":  "PENDING",  
    "url":  "https://onetouch-sandbox.astropay.com/deposit/QYPvyLz9fM57EGHtR1DV3TcZOlBng7Uw1m1KWUpe",  
    "type":  "gateway"
};

// Get the payment data response
AstropaySDK.payment(ASTROPAY_RESPONSE).
    .then(result => {
      //This is the payment data response object
      console.log(result)
    })

Example Response:

{
  amount: 5,
  currency: "BRL", 
  status: "PENDING",
  payment_type: "PIX",
  data: {
    txt_qr: "examplePixCode"
    image: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPoAAAD6CAYAAAAY4QDjAAAABmJLR0QA/wD/AP+gvaeTAAADfElEQVRoge2aXW/TQBBFf2BOwAhoh0CaQt0EfQAdAEYAIUARgCFWJED7FhYgFiwKFCAEsEKA"
  }
}

Sign in

This method allows you to render the "Sign in with Astropay" button on your site.

On the site you want to display the option setup an element with the "ap-login" id.

//On your site
 <div id="ap-login"></div>

// Mandatory Parameters
client_id: string
redirect_uri: string

// Show the Sign in with AstroPay button
AstropaySDK.signin(client_id, redirect_uri);

Supported languages

Languagecode
Englishen
Españoles
Indonesiaid
日本人ja
한국의ko
Portuguêspt
ไทยth
Türktr
Tiếng Việtvi
簡中zh

Updrading

V4 - Latest

The last version of the SDK offers a better UI control, event listeners, new configuration parameters and new product integrations usage.

To fully understand this version, check the documentation on the Usage section.

Breaking changes

To correctly configure the event of automatic SDK modal closing and redirection, check the new Event Listeners section on this documentation.

V3

Breaking changes

Notice that for old versions only the _external_id was necessary to initiate the SDK UI. Now the method receives an object with the AstroPay init response provided by your own services.

  • Legacy method call:
const ASTROPAY_EXTERNAL_DEPOSIT_ID =  'External Deposit ID';
AstropaySDK.showDeposit(ASTROPAY_EXTERNAL_DEPOSIT_ID);
  • V3 updated method call:
const ASTROPAY_RESPONSE = {  
    "merchant_deposit_id":  "0c435984-0b55-4f16-a15e-36070fbcee19",  		
    "deposit_external_id": "QYPvyLz9fM57EGHtR1DV3TcZOlBng7Uw1m1KWUpe",  
    "status":  "PENDING",  
    "url":  "https://onetouch-sandbox.astropay.com/deposit/QYPvyLz9fM57EGHtR1DV3TcZOlBng7Uw1m1KWUpe",  
    "type":  "default"  
};

// Show the DEPOSIT client
AstropaySDK.deposit(ASTROPAY_RESPONSE);

V2

Usage example

//Your APP ID SDK Credential
const APP_ID = ""; 

const ASTROPAY_CONFIG = {
  environment: "production",
  onDepositStatusChange: (depositResult) => getDepositStatus(depositResult), //Subscribes to every transaction status update.
  onClose: (depositResult) => iframeClosed(depositResult),
};

//Initiates the SDK with the configuration set above
AstropaySDK.init(APP_ID, ASTROPAY_CONFIG); 

//Sets the Deposit External ID generated for the deposit
const ASTROPAY_EXTERNAL_DEPOSIT_ID = "oehpJvCma8jaoaM7qmK3m3MX1LsmUWk8iulDl3Wx"; 

//Here you can do something according to the transaction current status.
const getDepositStatus = (depositResult) => { console.log(depositResult);};

const iframeClosed = (depositResult) => {
  //Here you can also do a action if the user closes the iFrame, also checking the transaction status.
  console.log(
    "action: iFrame was closed",
    "Deposit Status:",
    depositResult
  ); 
};

Support

We provide detailed documentation on the AstroPay OneTouch API Guide. Please check it for more information that might be necessary for developers.

If you need more technical support, please contact support

License

Copyright © 2023 AstroPay. All rights reserved.

版本与发布记录

@astropay/astropay-sdk 共发布 14 个版本,首次发布于 2022/06/09;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.2.0latest2023/09/113 年前
4.1.32023/09/083 年前
4.1.22023/07/083 年前
4.1.02023/06/143 年前
4.0.22023/05/233 年前
4.0.12023/05/123 年前

关于 @astropay/astropay-sdk 的常见问题

@astropay/astropay-sdk 是什么?

Official AstroPay client for easy frontend integration to One Touch

如何安装 @astropay/astropay-sdk?

在项目目录运行 npm install @astropay/astropay-sdk,或使用 pnpm add / yarn add / bun add。

@astropay/astropay-sdk 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@astropay/astropay-sdk 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@astropay/astropay-sdk 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=14.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@astropay/astropay-sdk 可以商用吗?

@astropay/astropay-sdk 使用 UNLICENSED 许可。作者声明为 UNLICENSED,即不授予公开使用许可(与公共领域的 Unlicense 不同)。使用前需获得作者授权。以上为概括,不构成法律意见,请以许可原文为准。

@astropay/astropay-sdk 的下载量有多少?

根据 npm 官方下载统计,@astropay/astropay-sdk 在 2026/10/07 – 2026/11/05 这 30 天内被下载 24,004 次,日均约 800 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@astropay/astropay-sdk 还在维护吗?

最新版本 4.2.0 发布于 2023/09/11(3 年前);至今共发布 14 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/06/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。