@atlaspay/atlas-pay-connect v0.1.8
Atlas.js
A library for connecting your application to Atlas Pay.
Initializing Atlas
Before you can use the features provided by Atlas.js, you need to initialize it with your account ID from your dashboard
. Here's how you can do it:
import Atlas from '@atlaspay/atlas-pay-connect'
// Initialize Atlas
Atlas.initializeAtlas({
accountId: 'your-account-id',
})
Accessing Features
Once Atlas.js is initialized, you can access its features using the following methods:
// Access the Checkout feature
const checkout = Atlas.Checkout
// Access the Products feature
const products = Atlas.Products
// Access the Customer feature
const customer = Atlas.Customer
Features
Checkout
openIframe
openIframe(productId: string): void
This method opens a checkout iframe for the specified product. It appends an iframe to the document body, allowing customers to complete the payment process within the iframe.
Parameters:
productId
(string): The ID of the product for which the checkout iframe needs to be opened.
const checkout = Atlas.Checkout
// Open a checkout iframe for a product
checkout.openIframe('product123')
open
open(params: OpenParamsType): void
This method opens the payment URL in the current window to initiate the checkout process. It redirects the user to the payment interface where they can complete the payment.
Parameters:
params
(OpenParamsType): An object containing the following properties:productId
(string): The ID of the product to be checked out.customerId
(string): The ID of the customer initiating the checkout.callbackUrl
(string): The URL to which the user will be redirected after completing the payment.
Example:
const checkout = Atlas.Checkout
// Open the payment interface for a product
checkout.open({
productId: 'product123',
customerId: 'customer456',
callbackUrl: 'https://your-app.com/checkout-complete',
})
payInvoice
Parameters:
invoiceId
(string): The ID of the invoice to be paid.
Example:
const checkout = Atlas.Checkout
// Pay an invoice and get the order details
checkout.payInvoice({invoiceId: 'invoice789'}).then(order => {
console.log('Paid invoice:', order)
})
createOrder
Parameters:
subscriptionId
(string): The ID of the subscription associated with the order.customerId
(string): The ID of the customer placing the order.
Example:
const checkout = Atlas.Checkout
// Create an order and get the order details
checkout
.createOrder({
subscriptionId: 'sub123',
customerId: 'customer456',
})
.then(order => {
console.log('Created order:', order)
})
Products
The Products
module provides methods for retrieving customer-related product information configured in the dashboard
.
getCustomerProducts
getCustomerProducts(email: string): Promise<Product[]>
This method retrieves products associated with a customer based on their email address.
Parameters:
email
(string): The email address of the customer.
Returns:
A promise that resolves to an array of Product
objects.
Example:
const products = Atlas.Products
// Retrieve products associated with a customer
products.getCustomerProducts('customer@example.com').then(productList => {
console.log('Customer products:', productList)
})
getCustomerProduct
getCustomerProduct(productId: string): Promise<ProductWithSubscriptions>
This method retrieves detailed information about a specific product, including its associated subscriptions.
Parameters:
productId
(string): The ID of the product.
Returns:
A promise that resolves to a ProductWithSubscriptions
object.
Example:
const products = Atlas.Products
// Retrieve detailed information about a product
products.getCustomerProduct('product123').then(productDetails => {
console.log('Product details:', productDetails)
})
Customer
createCustomer
createCustomer(email: string): Promise<CreateCustomerResponse>
This method allows you to create a new customer by providing their email address. It returns a promise that resolves to a CreateCustomerResponse
object containing customer details.
Parameters:
email
(string): The email address of the customer.
Returns:
A promise that resolves to a CreateCustomerResponse
object.
Example:
const customer = Atlas.Customer
// Create a new customer
customer.createCustomer('customer@example.com').then(response => {
console.log('Created customer:', response.data)
})
getBalance
getBalance(id: string): Promise<BalanceType>
This method retrieves the balance information for a specific customer based on their ID. It returns a promise that resolves to a BalanceType
object containing balance details.
Parameters:
id
(string): The ID of the customer.
Returns:
A promise that resolves to a BalanceType
object.
Example:
const customer = Atlas.Customer
// Get balance information for a customer
customer.getBalance('customer123').then(balanceInfo => {
console.log('Customer balance:', balanceInfo)
})
createTopUpInvoice
createTopUpInvoice({ customerId, amount }: {
customerId: string, amount: number
}): Promise<{data: TopUpInvoice}>
The createTopUpInvoice
method allows you to create a balance top-up invoice for a specific customer. This invoice is used to add funds to the customer's balance.
Parameters:
customerId
(string): The ID of the customer for whom you want to create the invoice.amount
(number): The amount you want to add to the customer's balance.
Returns:
A promise that resolves to a followin object:
type ReturnedType = {data: TopUpInvoice}
// where TopUpInvoice is:
type TopUpInvoice = {
id: string
status: string
date: string
type: string | null
preimage: string | null
r_hash: string | null
payment_request: string | null
orderId: string | null
customerId: string | null
createdAt: string
updatedAt: string
}
Example:
const customer = Atlas.Customer
// Create a balance top-up invoice
customer
.createTopUpInvoice({
customerId: 'customer123',
amount: 100.0, // Specify the amount to top up
})
.then(response => {
console.log('Created top-up invoice:', response.data)
})
markTopUpInvoiceAsCompleted
markTopUpInvoiceAsCompleted({invoiceId}: {invoiceId: string}): Promise<void>
The markTopUpInvoiceAsCompleted method allows you to mark a balance top-up invoice as completed. Once an invoice is marked as completed, the backend is notified that the invoice has been paid
Parameters:
invoiceId
(string): The ID of the invoice you want to mark as completed.
Returns:
A void promise
Example:
const customer = Atlas.Customer
// Mark a balance top-up invoice as completed
customer
.markTopUpInvoiceAsCompleted({
invoiceId: 'invoice123', // Specify the invoice ID to mark as completed
})
.then(() => {
console.log('Top-up invoice marked as completed')
})